forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_unit_test.go
280 lines (259 loc) · 6.99 KB
/
function_unit_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
//go:build !integration
// +build !integration
package function
import (
"os"
"path/filepath"
"reflect"
"testing"
"gopkg.in/yaml.v2"
fnlabels "knative.dev/func/k8s/labels"
. "knative.dev/func/testing"
)
// TestFunction_Validate ensures that we are permissive on what we accept and
// strict on what we emit. This takes the form of not validating a function
// on instantiation, but rather on write. A function is expected to be in a
// partial, even invalid state on disk; mostly due to the possibility of manual
// editing of the func.yaml. Writing, however, should always to write a
// function in a known valid state.
func TestFunction_Validate(t *testing.T) {
root, cleanup := Mktemp(t)
t.Cleanup(cleanup)
var f Function
var err error
// Loading a nonexistent (new) function should not fail
// I.e. it will not run .Validate, or it would error that the function at
// root has no language or name.
if f, err = NewFunction(root); err != nil {
t.Fatal(err)
}
// Attempting to write the function will fail as being invalid
invalidEnv := "*invalid"
f.Build.BuildEnvs = []Env{{Name: &invalidEnv}}
if err = f.Write(); err == nil {
t.Fatalf("expected error writing an incomplete (invalid) function")
}
// Write the invalid Function
//
// Write this intentionally invalid function to disk.
// NOTE: this depends on an implementation detail of the package: the yaml
// serialization of the Function struct to a known filename. This is why this
// test belongs here in the same package as the implementation rather than in
// package function_test which treats the function package as an opaque-box.
path := filepath.Join(root, FunctionFile)
bb, err := yaml.Marshal(&f)
if err != nil {
t.Fatal(err)
}
if err = os.WriteFile(path, bb, os.ModePerm); err != nil {
t.Fatal(err)
}
// Loading the invalid function should not fail, but validation should.
if f, err = NewFunction(root); err != nil {
t.Fatal(err)
}
if err = f.Validate(); err == nil { // axiom check; not strictly part of this test
t.Fatal("did not receive an error validating a known-invlaid (incomplete) function")
}
// Remove the invalid structures... write should complete without error.
f.Build.BuildEnvs = []Env{}
if err = f.Write(); err != nil {
t.Fatal(err)
}
if f, err = NewFunction(root); err != nil {
t.Fatal(err)
}
if err = f.Validate(); err != nil {
t.Fatal(err)
}
}
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:50000/default/bar", ImageDigest: "42"},
want: "image-registry.openshift-image-registry.svc.cluster.local:50000/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 := 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)
}
})
}
}
// TestFunction_ImageName ensures that the full image name is
// returned for a Function, based on the Function's Registry and Name,
// including utilizing the DefaultRegistry if the Function's defined
// registry is a single token (just the namespace).
func TestFunction_ImageName(t *testing.T) {
var (
f Function
got string
err error
)
tests := []struct {
registry string
name string
expectedImage string
expectError bool
}{
{"alice", "myfunc", DefaultRegistry + "/alice/myfunc:latest", false},
{"quay.io/alice", "myfunc", "quay.io/alice/myfunc:latest", false},
{"docker.io/alice", "myfunc", "docker.io/alice/myfunc:latest", false},
{"docker.io/alice/sub", "myfunc", "docker.io/alice/sub/myfunc:latest", false},
{"alice", "", "", true},
{"", "myfunc", "", true},
}
for _, test := range tests {
f = Function{Registry: test.registry, Name: test.name}
got, err = f.ImageName()
if test.expectError && err == nil {
t.Errorf("registry '%v' and name '%v' did not yield the expected error",
test.registry, test.name)
}
if got != test.expectedImage {
t.Errorf("expected registry '%v' name '%v' to yield image '%v', got '%v'",
test.registry, test.name, test.expectedImage, got)
}
}
}
func Test_LabelsMap(t *testing.T) {
key1 := "key1"
key2 := "key2"
value1 := "value1"
value2 := "value2"
t.Setenv("BAD_EXAMPLE", ":invalid")
valueLocalEnvIncorrect4 := "{{env:BAD_EXAMPLE}}"
t.Setenv("GOOD_EXAMPLE", "valid")
valueLocalEnv4 := "{{env:GOOD_EXAMPLE}}"
tests := []struct {
name string
labels []Label
expectErr bool
expectedMap map[string]string
}{
{
name: "invalid Labels should return err",
labels: []Label{
{
Value: &value1,
},
},
expectErr: true,
},
{
name: "with valid env var",
labels: []Label{
{
Key: &key1,
Value: &valueLocalEnv4,
},
},
expectErr: false,
expectedMap: map[string]string{
key1: "valid",
},
},
{
name: "with invalid env var",
labels: []Label{
{
Key: &key1,
Value: &valueLocalEnvIncorrect4,
},
},
expectErr: true,
},
{
name: "empty labels allowed. returns default labels",
labels: []Label{
{
Key: &key1,
},
},
expectErr: false,
expectedMap: map[string]string{
key1: "",
},
},
{
name: "full set of labels",
labels: []Label{
{
Key: &key1,
Value: &value1,
},
{
Key: &key2,
Value: &value2,
},
},
expectErr: false,
expectedMap: map[string]string{
key1: value1,
key2: value2,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := Function{
Name: "some-function",
Runtime: "golang",
Deploy: DeploySpec{Labels: tt.labels},
}
got, err := f.LabelsMap()
if tt.expectErr {
if err == nil {
t.Error("expected error but didn't get an error from LabelsMap")
}
} else {
if err != nil {
t.Errorf("got unexpected err: %s", err)
}
}
if err == nil {
defaultLabels := expectedDefaultLabels(f)
for k, v := range defaultLabels {
tt.expectedMap[k] = v
}
if res := reflect.DeepEqual(got, tt.expectedMap); !res {
t.Errorf("mismatch in actual and expected labels return. actual: %#v, expected: %#v", got, tt.expectedMap)
}
}
})
}
}
func expectedDefaultLabels(f Function) map[string]string {
return map[string]string{
fnlabels.FunctionKey: fnlabels.FunctionValue,
fnlabels.FunctionNameKey: f.Name,
fnlabels.FunctionRuntimeKey: f.Runtime,
fnlabels.DeprecatedFunctionKey: fnlabels.FunctionValue,
fnlabels.DeprecatedFunctionRuntimeKey: f.Runtime,
}
}