-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_test.go
209 lines (185 loc) · 4.66 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
package clade_test
import (
"testing"
"github.com/distribution/distribution/v3/reference"
"github.com/lesomnus/clade"
"github.com/lesomnus/pl"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func must[T any](obj T, err error) T {
if err != nil {
panic(err)
}
return obj
}
func TestBaseImageUnamarshalYaml(t *testing.T) {
ref := &clade.ImageReference{
Named: must(reference.ParseNamed("cr.io/repo/name")),
Tag: (*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "tag")))),
}
tcs := []struct {
desc string
data string
expected clade.BaseImage
}{
{
desc: "string",
data: "cr.io/repo/name:tag",
expected: clade.BaseImage{
Primary: ref,
Secondaries: nil,
},
},
{
desc: "map",
data: "{name: cr.io/repo/name, tags: tag}",
expected: clade.BaseImage{
Primary: ref,
Secondaries: nil,
},
},
{
desc: "field `with`",
data: `
name: cr.io/repo/name
tags: tag
with:
- cr.io/repo/name:tag
- name: cr.io/repo/name
tag: tag
`,
expected: clade.BaseImage{
Primary: ref,
Secondaries: []*clade.ImageReference{ref, ref},
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
var actual clade.BaseImage
err := yaml.Unmarshal([]byte(tc.data), &actual)
require.NoError(err)
require.Equal(tc.expected, actual)
})
}
t.Run("fails if", func(t *testing.T) {
tcs := []struct {
desc string
data string
msgs []string
}{
{
desc: "not a string or a map",
data: "[]",
msgs: []string{"string", "map"},
},
{
desc: "invalid ImageReference type",
data: "{name: {}}",
msgs: []string{"map into string"},
},
{
desc: "invalid ImageReference format",
data: "{name: cr.io/repo/name, tags: (foo bar)}",
msgs: []string{"bar"},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
var pipeline clade.BaseImage
err := yaml.Unmarshal([]byte(tc.data), &pipeline)
for _, msg := range tc.msgs {
require.ErrorContains(err, msg)
}
})
}
})
}
// func TestImageUnmarshalYaml(t *testing.T) {
// t.Run("must be object", func(t *testing.T) {
// var image clade.Image
// err := yaml.Unmarshal([]byte("foo"), &image)
// require.ErrorContains(t, err, "str")
// require.ErrorContains(t, err, "into")
// })
// t.Run(".platform", func(t *testing.T) {
// t.Run("is boolean expression", func(t *testing.T) {
// var image clade.Image
// err := yaml.Unmarshal([]byte("platform: t & f"), &image)
// require.NoError(t, err)
// })
// t.Run("can be empty", func(t *testing.T) {
// var image clade.Image
// err := yaml.Unmarshal([]byte("platform: "), &image)
// require.NoError(t, err)
// })
// t.Run("fails if", func(t *testing.T) {
// t.Run("not a string", func(t *testing.T) {
// var image clade.Image
// err := yaml.Unmarshal([]byte("{platform: {}}"), &image)
// require.ErrorContains(t, err, "map into string")
// })
// t.Run("not a valid boolean expression", func(t *testing.T) {
// var image clade.Image
// err := yaml.Unmarshal([]byte("{platform: foo %% bar}"), &image)
// require.ErrorContains(t, err, "%")
// })
// })
// })
// }
func TestResolvedImage(t *testing.T) {
t.Run("Tagged", func(t *testing.T) {
named, err := reference.ParseNamed("cr.io/repo/name")
require.NoError(t, err)
t.Run("tagged by first tag with its name", func(t *testing.T) {
require := require.New(t)
img := clade.ResolvedImage{
Named: named,
Tags: []string{"foo", "bar"},
}
tagged, err := img.Tagged()
require.NoError(err)
require.Equal(named.Name(), tagged.Name())
require.Equal("foo", tagged.Tag())
})
t.Run("fails if", func(t *testing.T) {
t.Run("it has no tags", func(t *testing.T) {
require := require.New(t)
img := clade.ResolvedImage{Named: named}
_, err := img.Tagged()
require.ErrorContains(err, "not tagged")
})
t.Run("tag is invalid", func(t *testing.T) {
require := require.New(t)
img := clade.ResolvedImage{
Named: named,
Tags: []string{"foo bar"},
}
_, err := img.Tagged()
require.ErrorIs(err, reference.ErrTagInvalidFormat)
})
})
})
}
func TestCalcDerefId(t *testing.T) {
require := require.New(t)
a := []byte{0x42, 0x08, 0xD2}
b := []byte{0x30, 0x9A, 0xAD, 0x51}
c := []byte{0xE0, 0x9C, 0xA8, 0x07, 0xB6}
all := []string{
clade.CalcDerefId(a, b, c),
clade.CalcDerefId(a, c, b),
clade.CalcDerefId(b, a, c),
clade.CalcDerefId(b, c, a),
clade.CalcDerefId(c, a, b),
clade.CalcDerefId(c, b, a),
}
for _, lhs := range all {
for _, rhs := range all {
require.Equal(lhs, rhs)
}
}
}