forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_test.go
144 lines (134 loc) · 3.77 KB
/
function_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
// +build !integration
package function_test
import (
"testing"
fn "knative.dev/kn-plugin-func"
)
func TestFunction_ImageWithDigest(t *testing.T) {
type fields struct {
Image string
ImageDigest string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Full path with port",
fields: fields{Image: "image-registry.openshift-image-registry.svc.cluster.local:5000/default/bar", ImageDigest: "42"},
want: "image-registry.openshift-image-registry.svc.cluster.local:5000/default/bar@42",
},
{
name: "Path with namespace",
fields: fields{Image: "johndoe/bar", ImageDigest: "42"},
want: "johndoe/bar@42",
},
{
name: "Just image name",
fields: fields{Image: "bar:latest", ImageDigest: "42"},
want: "bar@42",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := fn.Function{
Image: tt.fields.Image,
ImageDigest: tt.fields.ImageDigest,
}
if got := f.ImageWithDigest(); got != tt.want {
t.Errorf("ImageWithDigest() = %v, want %v", got, tt.want)
}
})
}
}
func Test_DerivedImage(t *testing.T) {
tests := []struct {
name string
fnName string
image string
registry string
want string
}{
{
name: "No change",
fnName: "testDerivedImage",
image: "docker.io/foo/testDerivedImage:latest",
registry: "docker.io/foo",
want: "docker.io/foo/testDerivedImage:latest",
},
{
name: "Same registry without docker.io/, original with docker.io/",
fnName: "testDerivedImage0",
image: "docker.io/foo/testDerivedImage0:latest",
registry: "foo",
want: "docker.io/foo/testDerivedImage0:latest",
},
{
name: "Same registry, original without docker.io/",
fnName: "testDerivedImage1",
image: "foo/testDerivedImage1:latest",
registry: "foo",
want: "docker.io/foo/testDerivedImage1:latest",
},
{
name: "Different registry without docker.io/, original without docker.io/",
fnName: "testDerivedImage2",
image: "foo/testDerivedImage2:latest",
registry: "bar",
want: "docker.io/bar/testDerivedImage2:latest",
},
{
name: "Different registry with docker.io/, original without docker.io/",
fnName: "testDerivedImage3",
image: "foo/testDerivedImage3:latest",
registry: "docker.io/bar",
want: "docker.io/bar/testDerivedImage3:latest",
},
{
name: "Different registry with docker.io/, original with docker.io/",
fnName: "testDerivedImage4",
image: "docker.io/foo/testDerivedImage4:latest",
registry: "docker.io/bar",
want: "docker.io/bar/testDerivedImage4:latest",
},
{
name: "Different registry with quay.io/, original without docker.io/",
fnName: "testDerivedImage5",
image: "foo/testDerivedImage5:latest",
registry: "quay.io/foo",
want: "quay.io/foo/testDerivedImage5:latest",
},
{
name: "Different registry with quay.io/, original with docker.io/",
fnName: "testDerivedImage6",
image: "docker.io/foo/testDerivedImage6:latest",
registry: "quay.io/foo",
want: "quay.io/foo/testDerivedImage6:latest",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := "testdata/" + tt.fnName
defer using(t, root)()
f := fn.Function{
Name: tt.fnName,
Root: root,
Image: tt.image,
}
// write out the function
client := fn.New()
err := client.Create(f)
if err != nil {
t.Fatal(err)
}
got, err := fn.DerivedImage(f.Root, tt.registry)
if err != nil {
t.Errorf("DerivedImage() for image %v and registry %v; got error %v", tt.image, tt.registry, err)
}
if got != tt.want {
t.Errorf("DerivedImage() for image %v and registry %v; got %v, want %v", tt.image, tt.registry, got, tt.want)
}
})
}
}