forked from open-component-model/open-component-model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_transfer_test.go
More file actions
352 lines (299 loc) · 11.9 KB
/
Copy path08_transfer_test.go
File metadata and controls
352 lines (299 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Step 8: Transferring Component Versions
//
// What you'll learn:
// - Building a transfer graph that describes how to move a component version
// - Executing the graph to transfer a component from one CTF repository to another
// - Using transfer.Mapping with transfer.NewRepositoryResolver to describe a source-to-target transfer
// - Driving recursive transfer of component references via transferv1alpha1.Config
// - Verifying the transferred component version and its resource payload in the target repository
// - Applying custom HTTP timeouts to the repository provider used during transfer
package examples
import (
"bytes"
"io"
"os"
"strings"
"testing"
"github.com/opencontainers/go-digest"
"github.com/stretchr/testify/require"
"ocm.software/open-component-model/bindings/go/blob/filesystem"
"ocm.software/open-component-model/bindings/go/blob/inmemory"
genericv1 "ocm.software/open-component-model/bindings/go/configuration/generic/v1/spec"
"ocm.software/open-component-model/bindings/go/ctf"
descriptor "ocm.software/open-component-model/bindings/go/descriptor/runtime"
v2 "ocm.software/open-component-model/bindings/go/descriptor/v2"
httpv1alpha1 "ocm.software/open-component-model/bindings/go/http/spec/config/v1alpha1"
"ocm.software/open-component-model/bindings/go/oci"
ocictf "ocm.software/open-component-model/bindings/go/oci/ctf"
"ocm.software/open-component-model/bindings/go/oci/repository/provider"
"ocm.software/open-component-model/bindings/go/oci/repository/resource"
ctfrepospec "ocm.software/open-component-model/bindings/go/oci/spec/repository/v1/ctf"
"ocm.software/open-component-model/bindings/go/runtime"
"ocm.software/open-component-model/bindings/go/transfer"
transferv1alpha1 "ocm.software/open-component-model/bindings/go/transfer/v1alpha1/spec"
)
// TestExample_TransferCTFtoCTF demonstrates transferring a component version
// with a local blob resource from one CTF repository to another.
//
// The transfer works in two phases:
// 1. Build a transformation graph that describes the transfer plan
// 2. Execute the graph to perform the actual data movement
func TestExample_TransferCTFtoCTF(t *testing.T) {
r := require.New(t)
ctx := t.Context()
component := "acme.org/transfer-example"
version := "1.0.0"
resourceContent := []byte("payload to transfer")
// --- Set up the source CTF repository with a component version ---
sourcePath := t.TempDir()
sourceSpec := newCTFSpecAt(t, sourcePath)
sourceRepo := ctfSpecToRepo(t, sourceSpec)
res := &descriptor.Resource{
Relation: descriptor.LocalRelation,
ElementMeta: descriptor.ElementMeta{
ObjectMeta: descriptor.ObjectMeta{Name: "my-resource", Version: version},
},
Type: "plainText",
Access: &v2.LocalBlob{
LocalReference: digest.FromBytes(resourceContent).String(),
MediaType: "text/plain",
},
}
desc := &descriptor.Descriptor{
Meta: descriptor.Meta{Version: "v2"},
Component: descriptor.Component{
Provider: descriptor.Provider{Name: "acme.org"},
ComponentMeta: descriptor.ComponentMeta{
ObjectMeta: descriptor.ObjectMeta{Name: component, Version: version},
},
Resources: []descriptor.Resource{*res},
},
}
b := inmemory.New(bytes.NewReader(resourceContent))
newRes, err := sourceRepo.AddLocalResource(ctx, component, version, res, b)
r.NoError(err)
desc.Component.Resources[0] = *newRes
r.NoError(sourceRepo.AddComponentVersion(ctx, desc))
// --- Build the transfer graph ---
// The target is another CTF repository (writable so the transfer can store data).
targetSpec := newCTFSpecAt(t, t.TempDir())
targetSpec.AccessMode = ctfrepospec.AccessModeReadWrite
// transfer.Mapping pairs the source components with a target repository and a resolver.
// transfer.NewRepositoryResolver wraps the source repo directly, so no custom resolver is needed.
// A nil cfg uses the defaults; see TestExample_Transfer_WithTransferConfig
// below for driving transfer settings explicitly.
tgd, err := transfer.BuildGraphDefinition(ctx, nil,
transfer.Mapping{
Components: []transfer.ComponentID{{Component: component, Version: version}},
Target: targetSpec,
Resolver: transfer.NewRepositoryResolver(sourceRepo, sourceSpec),
},
)
r.NoError(err)
r.NotEmpty(tgd.Transformations)
// --- Execute the transfer ---
repoProvider := provider.NewComponentVersionRepositoryProvider(
provider.WithTempDir(t.TempDir()),
)
resourceRepo := resource.NewResourceRepository(nil)
builder := transfer.NewDefaultBuilder(repoProvider, resourceRepo, nil)
graph, err := builder.BuildAndCheck(tgd)
r.NoError(err)
r.NoError(graph.Process(ctx))
// --- Verify the component version arrived in the target ---
targetRepo := ctfSpecToRepo(t, targetSpec)
got, err := targetRepo.GetComponentVersion(ctx, component, version)
r.NoError(err)
r.Equal(component, got.Component.Name)
r.Equal(version, got.Component.Version)
r.Len(got.Component.Resources, 1)
r.Equal("my-resource", got.Component.Resources[0].Name)
// Also verify the resource payload was transferred correctly.
readBlob, _, err := targetRepo.GetLocalResource(ctx, component, version, map[string]string{
"name": "my-resource",
"version": version,
})
r.NoError(err)
rc, err := readBlob.ReadCloser()
r.NoError(err)
defer rc.Close()
data, err := io.ReadAll(rc)
r.NoError(err)
r.Equal(resourceContent, data)
}
// TestExample_Transfer_WithTransferConfig demonstrates driving a transfer with
// a [transferv1alpha1.Config]. Here Recursive: RecursiveInfinite causes the
// parent component's references to be walked and transferred along with it,
// so both parent and child appear in the target repository.
func TestExample_Transfer_WithTransferConfig(t *testing.T) {
r := require.New(t)
ctx := t.Context()
parent := "acme.org/recursive-parent"
child := "acme.org/recursive-child"
version := "1.0.0"
// --- Set up source CTF: a child component and a parent that references it ---
sourceSpec := newCTFSpecAt(t, t.TempDir())
sourceRepo := ctfSpecToRepo(t, sourceSpec)
childDesc := &descriptor.Descriptor{
Meta: descriptor.Meta{Version: "v2"},
Component: descriptor.Component{
Provider: descriptor.Provider{Name: "acme.org"},
ComponentMeta: descriptor.ComponentMeta{
ObjectMeta: descriptor.ObjectMeta{Name: child, Version: version},
},
},
}
r.NoError(sourceRepo.AddComponentVersion(ctx, childDesc))
parentDesc := &descriptor.Descriptor{
Meta: descriptor.Meta{Version: "v2"},
Component: descriptor.Component{
Provider: descriptor.Provider{Name: "acme.org"},
ComponentMeta: descriptor.ComponentMeta{
ObjectMeta: descriptor.ObjectMeta{Name: parent, Version: version},
},
References: []descriptor.Reference{
{
ElementMeta: descriptor.ElementMeta{
ObjectMeta: descriptor.ObjectMeta{Name: "child-ref", Version: version},
},
Component: child,
},
},
},
}
r.NoError(sourceRepo.AddComponentVersion(ctx, parentDesc))
// --- Build the transfer graph with the transfer config ---
targetSpec := newCTFSpecAt(t, t.TempDir())
targetSpec.AccessMode = ctfrepospec.AccessModeReadWrite
cfg := &transferv1alpha1.Config{
Recursive: transferv1alpha1.RecursiveInfinite,
}
tgd, err := transfer.BuildGraphDefinition(ctx, cfg,
transfer.Mapping{
Components: []transfer.ComponentID{{Component: parent, Version: version}},
Target: targetSpec,
Resolver: transfer.NewRepositoryResolver(sourceRepo, sourceSpec),
},
)
r.NoError(err)
r.NotEmpty(tgd.Transformations)
// --- Execute the transfer ---
repoProvider := provider.NewComponentVersionRepositoryProvider(
provider.WithTempDir(t.TempDir()),
)
resourceRepo := resource.NewResourceRepository(nil)
builder := transfer.NewDefaultBuilder(repoProvider, resourceRepo, nil)
graph, err := builder.BuildAndCheck(tgd)
r.NoError(err)
r.NoError(graph.Process(ctx))
// --- Verify both parent and child arrived in the target ---
targetRepo := ctfSpecToRepo(t, targetSpec)
gotParent, err := targetRepo.GetComponentVersion(ctx, parent, version)
r.NoError(err)
r.Equal(parent, gotParent.Component.Name)
r.Len(gotParent.Component.References, 1)
r.Equal(child, gotParent.Component.References[0].Component)
gotChild, err := targetRepo.GetComponentVersion(ctx, child, version)
r.NoError(err)
r.Equal(child, gotChild.Component.Name)
r.Equal(version, gotChild.Component.Version)
}
// TestExample_Transfer_WithHTTPConfig demonstrates performing a CTF-to-CTF
// transfer using a repository provider that applies custom HTTP timeouts.
//
// In production you would set these timeouts to match your network constraints.
// Here we set explicit values to demonstrate the wiring pattern.
func TestExample_Transfer_WithHTTPConfig(t *testing.T) {
r := require.New(t)
ctx := t.Context()
component := "acme.org/transfer-http-example"
version := "1.0.0"
resourceContent := []byte("payload to transfer with http config")
// --- Set up source CTF repository ---
sourceSpec := newCTFSpecAt(t, t.TempDir())
sourceRepo := ctfSpecToRepo(t, sourceSpec)
res := &descriptor.Resource{
Relation: descriptor.LocalRelation,
ElementMeta: descriptor.ElementMeta{
ObjectMeta: descriptor.ObjectMeta{Name: "my-resource", Version: version},
},
Type: "plainText",
Access: &v2.LocalBlob{
LocalReference: digest.FromBytes(resourceContent).String(),
MediaType: "text/plain",
},
}
desc := &descriptor.Descriptor{
Meta: descriptor.Meta{Version: "v2"},
Component: descriptor.Component{
Provider: descriptor.Provider{Name: "acme.org"},
ComponentMeta: descriptor.ComponentMeta{
ObjectMeta: descriptor.ObjectMeta{Name: component, Version: version},
},
Resources: []descriptor.Resource{*res},
},
}
b := inmemory.New(bytes.NewReader(resourceContent))
newRes, err := sourceRepo.AddLocalResource(ctx, component, version, res, b)
r.NoError(err)
desc.Component.Resources[0] = *newRes
r.NoError(sourceRepo.AddComponentVersion(ctx, desc))
// --- Resolve HTTP config from an OCM config with custom timeouts ---
const yamlConfig = `
type: generic.config.ocm.software/v1
configurations:
- type: http.config.ocm.software/v1alpha1
timeout: 60s
tlsHandshakeTimeout: 10s
`
var cfg genericv1.Config
r.NoError(genericv1.Scheme.Decode(strings.NewReader(yamlConfig), &cfg))
httpCfg, err := httpv1alpha1.ResolveHTTPConfig(&cfg)
r.NoError(err)
// --- Build and execute the transfer using a provider with HTTP config ---
targetSpec := newCTFSpecAt(t, t.TempDir())
targetSpec.AccessMode = ctfrepospec.AccessModeReadWrite
tgd, err := transfer.BuildGraphDefinition(ctx, nil,
transfer.Mapping{
Components: []transfer.ComponentID{{Component: component, Version: version}},
Target: targetSpec,
Resolver: transfer.NewRepositoryResolver(sourceRepo, sourceSpec),
},
)
r.NoError(err)
repoProvider := provider.NewComponentVersionRepositoryProvider(
provider.WithHTTPConfig(httpCfg),
provider.WithTempDir(t.TempDir()),
)
resourceRepo := resource.NewResourceRepository(nil)
builder := transfer.NewDefaultBuilder(repoProvider, resourceRepo, nil)
graph, err := builder.BuildAndCheck(tgd)
r.NoError(err)
r.NoError(graph.Process(ctx))
// --- Verify ---
targetRepo := ctfSpecToRepo(t, targetSpec)
got, err := targetRepo.GetComponentVersion(ctx, component, version)
r.NoError(err)
r.Equal(component, got.Component.Name)
r.Equal(version, got.Component.Version)
}
// --- helpers ---
// newCTFSpecAt creates a CTF repository specification for the given directory path.
func newCTFSpecAt(t *testing.T, path string) *ctfrepospec.Repository {
t.Helper()
return &ctfrepospec.Repository{
Type: runtime.Type{Name: ctfrepospec.Type, Version: ctfrepospec.Version},
FilePath: path,
}
}
// ctfSpecToRepo opens an existing CTF repository from a spec.
func ctfSpecToRepo(t *testing.T, spec *ctfrepospec.Repository) *oci.Repository {
t.Helper()
r := require.New(t)
fs, err := filesystem.NewFS(spec.FilePath, os.O_RDWR|os.O_CREATE)
r.NoError(err)
store := ocictf.NewFromCTF(ctf.NewFileSystemCTF(fs))
repo, err := oci.NewRepository(ocictf.WithCTF(store), oci.WithTempDir(t.TempDir()))
r.NoError(err)
return repo
}