-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference_test.go
133 lines (127 loc) · 3.44 KB
/
reference_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
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 TestImageReferenceUnmarshalYaml(t *testing.T) {
tcs := []struct {
desc string
data string
expected clade.ImageReference
}{
{
desc: "tagged string",
data: `cr.io/repo/name:tag`,
expected: clade.ImageReference{
Named: must(reference.ParseNamed("cr.io/repo/name")),
Tag: (*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "tag")))),
},
},
{
desc: "digested string",
data: `cr.io/repo/name@algo:0123456789abcdef0123456789abcdef`,
expected: clade.ImageReference{
Named: must(reference.ParseNamed("cr.io/repo/name")),
Tag: (*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "algo:0123456789abcdef0123456789abcdef")))),
},
},
{
desc: "pipelined string",
data: `cr.io/repo/name:(foo "bar")`,
expected: clade.ImageReference{
Named: must(reference.ParseNamed("cr.io/repo/name")),
Tag: (*clade.Pipeline)(pl.NewPl(must(pl.NewFn("foo", "bar")))),
},
},
{
desc: "tagged map",
data: `{"name": "cr.io/repo/name", "tag": "tag"}`,
expected: clade.ImageReference{
Named: must(reference.ParseNamed("cr.io/repo/name")),
Tag: (*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "tag")))),
},
},
{
desc: "digested map",
data: `{"name": "cr.io/repo/name", "tag": "algo:0123456789abcdef0123456789abcdef"}`,
expected: clade.ImageReference{
Named: must(reference.ParseNamed("cr.io/repo/name")),
Tag: (*clade.Pipeline)(pl.NewPl(must(pl.NewFn("pass", "algo:0123456789abcdef0123456789abcdef")))),
},
},
{
desc: "pipelined map",
data: `{"name": "cr.io/repo/name", "tag": "(foo \"bar\")"}`,
expected: clade.ImageReference{
Named: must(reference.ParseNamed("cr.io/repo/name")),
Tag: (*clade.Pipeline)(pl.NewPl(must(pl.NewFn("foo", "bar")))),
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
var actual clade.ImageReference
err := yaml.Unmarshal([]byte(tc.data), &actual)
require.NoError(err)
require.Equal(tc.expected, actual)
})
}
t.Run("fails is", 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 name in string format",
data: "name:tag",
msgs: []string{"canonical"},
},
{
desc: "invalid tag in string format",
data: "cr.io/repo/name:foo bar",
msgs: []string{"tag format"},
},
{
desc: "invalid digest in string format",
data: "cr.io/repo/name@foo",
msgs: []string{"digest format"},
},
{
desc: "invalid name in map format",
data: `{"name": "name", "tag": "tag"}`,
msgs: []string{"canonical"},
},
{
desc: "invalid tag in map format",
data: `{"name": "cr.io/repo/name", "tag": "foo bar"}`,
msgs: []string{"tag format"},
},
{
desc: "invalid digest in map format",
data: `{"name": "cr.io/repo/name", "tag": "algo:short"}`,
msgs: []string{"digest format"},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
var actual clade.ImageReference
err := yaml.Unmarshal([]byte(tc.data), &actual)
for _, msg := range tc.msgs {
require.ErrorContains(err, msg)
}
})
}
})
}