forked from GoogleCloudPlatform/compute-image-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
424 lines (391 loc) · 10.8 KB
/
common_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package daisy
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"google.golang.org/api/compute/v1"
)
func TestFilter(t *testing.T) {
ss := []string{"my", "name", "is", "daisy", "what", "is", "yours"}
tests := []struct {
desc, toFilter string
want []string
}{
{"normal case", "daisy", []string{"my", "name", "is", "what", "is", "yours"}},
{"edge -- start of slice", "my", []string{"name", "is", "daisy", "what", "is", "yours"}},
{"edge -- end of slice", "yours", []string{"my", "name", "is", "daisy", "what", "is"}},
{"filter multiple", "is", []string{"my", "name", "daisy", "what", "yours"}},
{"filter string that DNE", "dne", []string{"my", "name", "is", "daisy", "what", "is", "yours"}},
}
for _, tt := range tests {
result := filter(ss, tt.toFilter)
if !reflect.DeepEqual(result, tt.want) {
t.Errorf("%s failed. input: s=%s; got: %s; want: %s", tt.desc, tt.toFilter, result, tt.want)
}
}
// Edge case -- empty slice.
result := filter([]string{}, "hello")
if !reflect.DeepEqual(result, []string{}) {
t.Error("remove on empty slice failed")
}
}
func TestMinInt(t *testing.T) {
tests := []struct {
desc string
x int
ys []int
want int
}{
{"single int case", 1, nil, 1},
{"first int case", 2, []int{1}, 1},
{"same ints case", 2, []int{2}, 2},
{"third int case", 4, []int{3, 2}, 2},
}
for _, tt := range tests {
if got := minInt(tt.x, tt.ys...); got != tt.want {
t.Errorf("%s: %d != %d", tt.desc, got, tt.want)
}
}
}
func TestRandString(t *testing.T) {
for i := 0; i < 10; i++ {
l := len(randString(i))
if l != i {
t.Fatalf("wrong string length: %d != %d", l, i)
}
}
}
func TestStrIn(t *testing.T) {
ss := []string{"hello", "world", "my", "name", "is", "daisy"}
// True case.
if !strIn("hello", ss) {
t.Fatal("hello not found in slice")
}
// False case.
if strIn("dne", ss) {
t.Fatal("dne found in slice")
}
// Edge case -- empty slice.
if strIn("dne", []string{}) {
t.Fatal("string found in empty slice")
}
}
func TestStrLitPtr(t *testing.T) {
s1 := "foo"
s2 := strLitPtr(s1)
if *s2 != s1 {
t.Errorf("%q != %q", *s2, s1)
}
}
func TestStrOr(t *testing.T) {
tests := []struct {
desc string
s string
ss []string
expected string
}{
{"just one string", "foo", nil, "foo"},
{"second string is result", "", []string{"bar", "baz"}, "bar"},
{"third string is result", "", []string{"", "baz"}, "baz"},
{"all empty", "", []string{""}, ""},
}
for _, tt := range tests {
result := strOr(tt.s, tt.ss...)
if result != tt.expected {
t.Errorf("%s: wanted %q, got %q", tt.desc, tt.expected, result)
}
}
}
func TestSubstituteSourceVars(t *testing.T) {
type test struct {
String string
}
tests := []struct {
got, want test
wantErr bool
}{
{ // 0
test{String: "${SOURCE:foo}"},
test{String: "this is a test"},
false,
},
{ // 1
test{String: "${BADSOURCE:foo}"},
test{String: "${BADSOURCE:foo}"},
false,
},
{ // 2
test{String: "${SOURCE:bar}"},
test{String: "${SOURCE:bar}"},
true,
},
{ // 3
test{String: "${SOURCE:baz}"},
test{String: "${SOURCE:baz}"},
true,
},
{ // 4
test{String: "${SOURCE:big}"},
test{String: "${SOURCE:big}"},
true,
},
{ // 5
test{String: "Did you know that ${SOURCE:foo}?"},
test{String: "Did you know that this is a test?"},
false,
},
{ // 6
test{String: "Now with this expansion it crossed the limits: ${SOURCE:almost}?"},
test{String: "Now with this expansion it crossed the limits: ${SOURCE:almost}?"},
true,
},
{ // 7
test{String: "${SOURCE:foo} and ${SOURCE:fu}"},
test{String: "this is a test and this is another test"},
false,
},
}
td, err := ioutil.TempDir(os.TempDir(), "")
if err != nil {
t.Fatalf("error creating temp dir: %v", err)
}
defer os.RemoveAll(td)
almost := filepath.Join(td, "almost_too_big")
ioutil.WriteFile(almost, []byte(strings.Repeat("a", (1024*256)-10)), 0600)
big := filepath.Join(td, "big_string")
ioutil.WriteFile(big, []byte(strings.Repeat("a", (1024*256)+10)), 0600)
ctx := context.Background()
w := testWorkflow()
w.Sources = map[string]string{
"foo": "./test_data/test.txt",
"fu": "./test_data/test_2.txt",
"bar": "./test_data/notexist.txt",
"big": big,
"almost": almost}
for i, tt := range tests {
s := reflect.ValueOf(&tt.got).Elem()
err := w.substituteSourceVars(ctx, s)
if !tt.wantErr && err != nil {
t.Fatalf("test %d: %v", i, err)
} else if tt.wantErr && err == nil {
t.Fatalf("test %d: expected error", i)
}
if diffRes := diff(tt.got, tt.want, 0); diffRes != "" {
t.Errorf("test %d: post substituteSourceVars workflow does not match expectation: (-got +want)\n%s", i, diffRes)
}
}
}
func TestSubstitute(t *testing.T) {
type test struct {
String string
StringMap map[string]string
SliceStringMap map[string][]string
Steps map[string]*Step
private string
}
tests := []struct {
replacer *strings.Replacer
got, want test
}{
{ // 0
strings.NewReplacer(),
test{String: "", private: ""},
test{String: "", private: ""},
},
{ // 1
strings.NewReplacer(),
test{String: "string"},
test{String: "string"},
},
{ // 2
strings.NewReplacer("key", "value"),
test{String: "key-string", private: "private-key-string"},
test{String: "value-string", private: "private-key-string"},
},
{ // 3
strings.NewReplacer("key", "value"),
test{String: "key-key"},
test{String: "value-value"},
},
{ // 4
strings.NewReplacer("key1", "value1", "key2", "value2"),
test{String: "key1-key2"},
test{String: "value1-value2"},
},
{ // 5
strings.NewReplacer(),
test{StringMap: map[string]string{"key1": "value1"}},
test{StringMap: map[string]string{"key1": "value1"}},
},
{ // 6
strings.NewReplacer("key1", "value1", "key2", "value2", "key3", "value3"),
test{StringMap: map[string]string{"key1": "key2key2", "key3": "value"}},
test{StringMap: map[string]string{"value1": "value2value2", "value3": "value"}},
},
{ // 7
strings.NewReplacer("key1", "value1", "key2", "value2", "key3", "value3"),
test{SliceStringMap: map[string][]string{"key": {"value1", "value2"}}},
test{SliceStringMap: map[string][]string{"key": {"value1", "value2"}}},
},
{ // 8
strings.NewReplacer("key1", "value1", "key2", "value2", "key3", "value3"),
test{
SliceStringMap: map[string][]string{
"key1": {"key1", "value2"},
"key2": {"key2", "value2"},
},
},
test{
SliceStringMap: map[string][]string{
"value1": {"value1", "value2"},
"value2": {"value2", "value2"},
},
},
},
{ // 9
strings.NewReplacer("key1", "value1", "key2", "value2", "key3", "value3"),
test{
StringMap: map[string]string{"key1": "key2key2", "key3": "value"},
SliceStringMap: map[string][]string{"key": {"value1", "value2"}},
Steps: map[string]*Step{
"create disks key1": {
CreateDisks: &CreateDisks{
{
Disk: compute.Disk{
Name: "key1",
},
},
},
},
"key2": {
CreateInstances: &CreateInstances{
Instances: []*Instance{
{
Instance: compute.Instance{
Disks: []*compute.AttachedDisk{{Source: "key1"}},
},
Metadata: map[string]string{"test_metadata": "key3"},
},
},
},
},
"typed slice step": {
WaitForInstancesSignal: &WaitForInstancesSignal{
{Name: "key1"}, {Name: "foo-instance"}, {Name: "key2"},
},
},
},
},
test{
StringMap: map[string]string{"value1": "value2value2", "value3": "value"},
SliceStringMap: map[string][]string{"key": {"value1", "value2"}},
Steps: map[string]*Step{
"create disks value1": {
CreateDisks: &CreateDisks{
{
Disk: compute.Disk{
Name: "value1",
},
},
},
},
"value2": {
CreateInstances: &CreateInstances{
Instances: []*Instance{
{
Instance: compute.Instance{
Disks: []*compute.AttachedDisk{{Source: "value1"}},
},
Metadata: map[string]string{"test_metadata": "value3"},
},
},
},
},
"typed slice step": {
WaitForInstancesSignal: &WaitForInstancesSignal{
{Name: "value1"}, {Name: "foo-instance"}, {Name: "value2"},
},
},
},
},
},
}
for i, tt := range tests {
s := reflect.ValueOf(&tt.got).Elem()
substitute(s, tt.replacer)
if diffRes := diff(tt.got, tt.want, 0); diffRes != "" {
t.Errorf("test %d: post substitute workflow does not match expectation: (-got +want)\n%s", i, diffRes)
}
}
}
func TestCombineGuestOSFeatures(t *testing.T) {
tests := []struct {
currentFeatures []*compute.GuestOsFeature
additionalFeatures []string
want []*compute.GuestOsFeature
}{
{
currentFeatures: featuresOf(),
additionalFeatures: []string{},
want: featuresOf(),
},
{
currentFeatures: featuresOf("WINDOWS"),
additionalFeatures: []string{},
want: featuresOf("WINDOWS"),
},
{
currentFeatures: featuresOf(),
additionalFeatures: []string{"WINDOWS"},
want: featuresOf("WINDOWS"),
},
{
currentFeatures: featuresOf("WINDOWS"),
additionalFeatures: []string{"WINDOWS"},
want: featuresOf("WINDOWS"),
},
{
currentFeatures: featuresOf("MULTI_IP_SUBNET"),
additionalFeatures: []string{"WINDOWS"},
want: featuresOf("MULTI_IP_SUBNET", "WINDOWS"),
},
{
currentFeatures: featuresOf("MULTI_IP_SUBNET", "UEFI_COMPATIBLE"),
additionalFeatures: []string{"WINDOWS", "UEFI_COMPATIBLE"},
want: featuresOf("MULTI_IP_SUBNET", "UEFI_COMPATIBLE", "WINDOWS"),
},
}
for _, test := range tests {
got := CombineGuestOSFeatures(test.currentFeatures, test.additionalFeatures...)
if !reflect.DeepEqual(got, test.want) {
t.Errorf("CombineGuestOSFeatures(%v, %v) = %v, want %v",
test.currentFeatures, test.additionalFeatures, got, test.want)
}
}
}
func featuresOf(features ...string) []*compute.GuestOsFeature {
ret := make([]*compute.GuestOsFeature, 0)
for _, feature := range features {
ret = append(ret, &compute.GuestOsFeature{
Type: feature,
})
}
return ret
}