forked from regclient/regclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_test.go
295 lines (285 loc) · 7.37 KB
/
image_test.go
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
package regclient
import (
"archive/tar"
"context"
"errors"
"io"
"testing"
"time"
"github.com/regclient/regclient/internal/rwfs"
"github.com/regclient/regclient/types"
"github.com/regclient/regclient/types/ref"
)
func TestImageCheckBase(t *testing.T) {
ctx := context.Background()
fsOS := rwfs.OSNew("")
fsMem := rwfs.MemNew()
err := rwfs.CopyRecursive(fsOS, "testdata", fsMem, ".")
if err != nil {
t.Errorf("failed to setup memfs copy: %v", err)
return
}
delayInit, _ := time.ParseDuration("0.05s")
delayMax, _ := time.ParseDuration("0.10s")
rc := New(WithFS(fsMem), WithRetryDelay(delayInit, delayMax))
rb1, err := ref.New("ocidir://testrepo:b1")
if err != nil {
t.Errorf("failed to setup ref: %v", err)
return
}
rb2, err := ref.New("ocidir://testrepo:b2")
if err != nil {
t.Errorf("failed to setup ref: %v", err)
return
}
rb3, err := ref.New("ocidir://testrepo:b3")
if err != nil {
t.Errorf("failed to setup ref: %v", err)
return
}
m3, err := rc.ManifestHead(ctx, rb3)
if err != nil {
t.Errorf("failed to get digest for base3: %v", err)
return
}
dig3 := m3.GetDescriptor().Digest
r1, err := ref.New("ocidir://testrepo:v1")
if err != nil {
t.Errorf("failed to setup ref: %v", err)
return
}
r2, err := ref.New("ocidir://testrepo:v2")
if err != nil {
t.Errorf("failed to setup ref: %v", err)
return
}
r3, err := ref.New("ocidir://testrepo:v3")
if err != nil {
t.Errorf("failed to setup ref: %v", err)
return
}
tests := []struct {
name string
opts []ImageOpts
r ref.Ref
expectErr error
}{
{
name: "missing annotation",
r: r1,
expectErr: types.ErrMissingAnnotation,
},
{
name: "annotation v2",
r: r2,
expectErr: types.ErrMismatch,
},
{
name: "annotation v3",
r: r3,
expectErr: types.ErrMismatch,
},
{
name: "manual v2, b1",
r: r2,
opts: []ImageOpts{ImageWithCheckBaseRef(rb1.CommonName())},
},
{
name: "manual v2, b2",
r: r2,
opts: []ImageOpts{ImageWithCheckBaseRef(rb2.CommonName())},
expectErr: types.ErrMismatch,
},
{
name: "manual v2, b3",
r: r2,
opts: []ImageOpts{ImageWithCheckBaseRef(rb3.CommonName())},
expectErr: types.ErrMismatch,
},
{
name: "manual v3, b1",
r: r3,
opts: []ImageOpts{ImageWithCheckBaseRef(rb1.CommonName())},
},
{
name: "manual v3, b3 with digest",
r: r3,
opts: []ImageOpts{ImageWithCheckBaseRef(rb3.CommonName()), ImageWithCheckBaseDigest(dig3.String())},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := rc.ImageCheckBase(ctx, tt.r, tt.opts...)
if tt.expectErr != nil {
if err == nil {
t.Errorf("check base did not fail")
} else if err.Error() != tt.expectErr.Error() && !errors.Is(err, tt.expectErr) {
t.Errorf("error mismatch, expected %v, received %v", tt.expectErr, err)
}
} else {
if err != nil {
t.Errorf("check base failed")
}
}
})
}
}
func TestCopy(t *testing.T) {
ctx := context.Background()
// create regclient
delayInit, _ := time.ParseDuration("0.05s")
delayMax, _ := time.ParseDuration("0.10s")
rc := New(WithRetryDelay(delayInit, delayMax))
tempDir := t.TempDir()
rSrc, err := ref.New("ocidir://./testdata/testrepo:v1")
if err != nil {
t.Errorf("failed to parse src ref: %v", err)
}
rTgt, err := ref.New("ocidir://" + tempDir + ":v1")
if err != nil {
t.Errorf("failed to parse tgt ref: %v", err)
}
err = rc.ImageCopy(ctx, rSrc, rTgt)
if err != nil {
t.Errorf("failed to copy: %v", err)
}
rSrc, err = ref.New("ocidir://./testdata/testrepo:v2")
if err != nil {
t.Errorf("failed to parse src ref: %v", err)
}
rTgt, err = ref.New("ocidir://" + tempDir + ":v2")
if err != nil {
t.Errorf("failed to parse tgt ref: %v", err)
}
err = rc.ImageCopy(ctx, rSrc, rTgt, ImageWithReferrers(), ImageWithDigestTags())
if err != nil {
t.Errorf("failed to copy: %v", err)
}
rSrc, err = ref.New("ocidir://./testdata/testrepo:v3")
if err != nil {
t.Errorf("failed to parse src ref: %v", err)
}
rTgt, err = ref.New("ocidir://" + tempDir + ":v3")
if err != nil {
t.Errorf("failed to parse tgt ref: %v", err)
}
err = rc.ImageCopy(ctx, rSrc, rTgt)
if err != nil {
t.Errorf("failed to copy: %v", err)
}
rSrc, err = ref.New("ocidir://./testdata/testrepo:v3")
if err != nil {
t.Errorf("failed to parse src ref: %v", err)
}
rTgt, err = ref.New("ocidir://" + tempDir + ":v3")
if err != nil {
t.Errorf("failed to parse tgt ref: %v", err)
}
err = rc.ImageCopy(ctx, rSrc, rTgt, ImageWithReferrers(), ImageWithDigestTags(), ImageWithFastCheck())
if err != nil {
t.Errorf("failed to copy: %v", err)
}
rSrc, err = ref.New("ocidir://./testdata/testrepo:child")
if err != nil {
t.Errorf("failed to parse src ref: %v", err)
}
rTgt, err = ref.New("ocidir://" + tempDir + ":child")
if err != nil {
t.Errorf("failed to parse tgt ref: %v", err)
}
err = rc.ImageCopy(ctx, rSrc, rTgt, ImageWithReferrers(), ImageWithDigestTags())
if err != nil {
t.Errorf("failed to copy: %v", err)
}
rSrc, err = ref.New("ocidir://./testdata/testrepo:mirror")
if err != nil {
t.Errorf("failed to parse src ref: %v", err)
}
rTgt, err = ref.New("ocidir://" + tempDir + ":mirror")
if err != nil {
t.Errorf("failed to parse tgt ref: %v", err)
}
err = rc.ImageCopy(ctx, rSrc, rTgt, ImageWithDigestTags())
if err != nil {
t.Errorf("failed to copy: %v", err)
}
}
func TestExportImport(t *testing.T) {
ctx := context.Background()
// copy testdata images into memory
fsOS := rwfs.OSNew("")
fsMem := rwfs.MemNew()
err := rwfs.CopyRecursive(fsOS, "testdata", fsMem, ".")
if err != nil {
t.Errorf("failed to setup memfs copy: %v", err)
return
}
// create regclient
delayInit, _ := time.ParseDuration("0.05s")
delayMax, _ := time.ParseDuration("0.10s")
rc := New(WithFS(fsMem), WithRetryDelay(delayInit, delayMax))
rIn, err := ref.New("ocidir://testrepo:v1")
if err != nil {
t.Errorf("failed to parse ref: %v", err)
}
rOut, err := ref.New("ocidir://testout:v1")
if err != nil {
t.Errorf("failed to parse ref: %v", err)
}
// export repo to tar
fileOut, err := fsMem.Create("test.tar")
if err != nil {
t.Errorf("failed to create output tar: %v", err)
}
err = rc.ImageExport(ctx, rIn, fileOut)
fileOut.Close()
if err != nil {
t.Errorf("failed to export: %v", err)
}
// modify tar for tests
fileR, err := fsMem.Open("test.tar")
if err != nil {
t.Errorf("failed to open tar: %v", err)
}
fileW, err := fsMem.Create("test2.tar")
if err != nil {
t.Errorf("failed to create tar: %v", err)
}
tr := tar.NewReader(fileR)
tw := tar.NewWriter(fileW)
for {
th, err := tr.Next()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
t.Errorf("failed to read tar header: %v", err)
}
th.Name = "./" + th.Name
err = tw.WriteHeader(th)
if err != nil {
t.Errorf("failed to write tar header: %v", err)
}
if th.Size > 0 {
_, err = io.Copy(tw, tr)
if err != nil {
t.Errorf("failed to copy tar file contents %s: %v", th.Name, err)
}
}
}
fileR.Close()
fileW.Close()
// import tar to repo
fileIn, err := fsMem.Open("test2.tar")
if err != nil {
t.Errorf("failed to open tar: %v", err)
}
fileInSeeker, ok := fileIn.(io.ReadSeeker)
if !ok {
t.Fatalf("could not convert fileIn to io.ReadSeeker, type %T", fileIn)
}
err = rc.ImageImport(ctx, rOut, fileInSeeker)
if err != nil {
t.Errorf("failed to export: %v", err)
}
}