forked from MimeLyc/regclient
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage_test.go
More file actions
131 lines (126 loc) · 2.93 KB
/
image_test.go
File metadata and controls
131 lines (126 loc) · 2.93 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
package regclient
import (
"context"
"errors"
"testing"
"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
}
rc := New(WithFS(fsMem))
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 v1, b1",
r: r1,
opts: []ImageOpts{ImageWithCheckBaseRef(rb1.CommonName())},
},
{
name: "manual v1, b2",
r: r1,
opts: []ImageOpts{ImageWithCheckBaseRef(rb2.CommonName())},
expectErr: types.ErrMismatch,
},
{
name: "manual v1, b3",
r: r1,
opts: []ImageOpts{ImageWithCheckBaseRef(rb3.CommonName())},
expectErr: types.ErrMismatch,
},
{
name: "manual v2, b1",
r: r2,
opts: []ImageOpts{ImageWithCheckBaseRef(rb1.CommonName())},
},
{
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")
}
}
})
}
}