-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathutils_test.go
552 lines (518 loc) · 13.3 KB
/
utils_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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
Copyright 2022 The Dapr Authors
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 utils
import (
"bytes"
"math"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContainerRuntimeUtils(t *testing.T) {
testcases := []struct {
name string
input string
expected string
valid bool
}{
{
name: "podman runtime is valid, and is returned as is",
input: "podman",
expected: "podman",
valid: true,
},
{
name: "podman runtime with extra spaces is valid, and is returned as is",
input: " podman ",
expected: "podman",
valid: true,
},
{
name: "docker runtime is valid, and is returned as is",
input: "docker",
expected: "docker",
valid: true,
},
{
name: "docker runtime with extra spaces is valid, and is returned as is",
input: " docker ",
expected: "docker",
valid: true,
},
{
name: "empty runtime is invalid, and docker is returned as default",
input: "",
expected: "docker",
valid: false,
},
{
name: "invalid runtime is invalid, and docker is returned as default",
input: "invalid",
expected: "docker",
valid: false,
},
{
name: "invalid runtime with extra spaces is invalid, and docker is returned as default",
input: " invalid ",
expected: "docker",
valid: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actualValid := IsValidContainerRuntime(tc.input)
if actualValid != tc.valid {
t.Errorf("expected %v, got %v", tc.valid, actualValid)
}
actualCmd := GetContainerRuntimeCmd(tc.input)
if actualCmd != tc.expected {
t.Errorf("expected %s, got %s", tc.expected, actualCmd)
}
})
}
}
func TestContains(t *testing.T) {
testcases := []struct {
name string
input []string
expected string
valid bool
}{
{
name: "empty list",
input: []string{},
expected: "foo",
valid: false,
},
{
name: "list contains element",
input: []string{"foo", "bar", "baz"},
expected: "foo",
valid: true,
},
{
name: "list does not contain element",
input: []string{"foo", "bar", "baz"},
expected: "qux",
valid: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actualValid := Contains(tc.input, tc.expected)
if actualValid != tc.valid {
t.Errorf("expected %v, got %v", tc.valid, actualValid)
}
})
}
}
func TestGetVersionAndImageVariant(t *testing.T) {
testcases := []struct {
name string
input string
expectedVersion string
expectedImageVariant string
}{
{
name: "image tag contains version and variant",
input: "1.9.0-mariner",
expectedVersion: "1.9.0",
expectedImageVariant: "mariner",
},
{
name: "image tag contains only version",
input: "1.9.0",
expectedVersion: "1.9.0",
expectedImageVariant: "",
},
{
name: "image tag contains only rc version and variant",
input: "1.9.0-rc.1-mariner",
expectedVersion: "1.9.0-rc.1",
expectedImageVariant: "mariner",
},
{
name: "image tag contains only rc version",
input: "1.9.0-rc.1",
expectedVersion: "1.9.0-rc.1",
expectedImageVariant: "",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
version, imageVariant := GetVersionAndImageVariant(tc.input)
assert.Equal(t, tc.expectedVersion, version)
assert.Equal(t, tc.expectedImageVariant, imageVariant)
})
}
}
func TestValidateFilePaths(t *testing.T) {
dirName := createTempDir(t, "test_validate_paths")
defer cleanupTempDir(t, dirName)
validFile := createTempFile(t, dirName, "valid_test_file.yaml")
testcases := []struct {
name string
input string
expectedErr bool
}{
{
name: "empty file path",
input: "",
expectedErr: false,
},
{
name: "valid file path",
input: validFile,
expectedErr: false,
},
{
name: "invalid file path",
input: "invalid_file_path",
expectedErr: true,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actual := ValidateFilePath(tc.input)
assert.Equal(t, tc.expectedErr, actual != nil)
})
}
}
func TestGetAbsPath(t *testing.T) {
ex, err := os.Executable()
assert.NoError(t, err)
baseDir := filepath.Dir(ex)
testcases := []struct {
name string
input string
expected string
}{
{
name: "empty path",
input: "",
expected: "",
},
{
name: "relative path-1",
input: "./relative/path",
expected: filepath.Join(baseDir, "relative", "path"),
},
{
name: "relative path-2",
input: "../relative/path",
expected: filepath.Join(baseDir, "..", "relative", "path"),
},
{
name: "absolute path",
input: "/absolute/path",
expected: "/absolute/path",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actual := GetAbsPath(baseDir, tc.input)
assert.Equal(t, tc.expected, actual)
})
}
}
func TestResolveHomeDir(t *testing.T) {
homeDir, err := os.UserHomeDir()
assert.NoError(t, err)
testcases := []struct {
name string
input string
expected string
skipWindows bool
}{
{
name: "empty path",
input: "",
expected: "",
skipWindows: false,
},
{
name: "home directory prefix with ~/",
input: "~/home/path",
expected: filepath.Join(homeDir, "home", "path"),
skipWindows: true,
},
{
name: "home directory prefix with ~/.",
input: "~/./home/path",
expected: filepath.Join(homeDir, ".", "home", "path"),
skipWindows: true,
},
{
name: "home directory prefix with ~/..",
input: "~/../home/path",
expected: filepath.Join(homeDir, "..", "home", "path"),
skipWindows: true,
},
{
name: "no home directory prefix",
input: "../home/path",
expected: "../home/path",
skipWindows: false,
},
{
name: "absolute path",
input: "/absolute/path",
expected: "/absolute/path",
skipWindows: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
if tc.skipWindows && runtime.GOOS == "windows" {
t.Skip("Skipping test on Windows")
}
t.Parallel()
actual, err := ResolveHomeDir(tc.input)
assert.NoError(t, err)
assert.Equal(t, tc.expected, actual)
})
}
}
func TestReadFile(t *testing.T) {
fileName := createTempFile(t, "", "test_read_file")
defer cleanupTempDir(t, fileName)
testcases := []struct {
name string
input string
expectedErr bool
}{
{
name: "empty file path",
input: "",
expectedErr: true,
},
{
name: "valid file path",
input: fileName,
expectedErr: false,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, actual := ReadFile(tc.input)
assert.Equal(t, tc.expectedErr, actual != nil)
})
}
}
// Following is the directory and file structure created for this test in the os's default temp directory:
// test_find_file_in_dir/valid_dir/dapr.yaml.
// test_find_file_in_dir/valid_dir/test1.yaml.
// test_find_file_in_dir/valid_dir_no_dapr_yaml.
func TestFindFileInDir(t *testing.T) {
nonExistentDirName := "invalid_dir"
validDirNameWithDaprYAMLFile := "valid_dir"
validDirWithNoDaprYAML := "valid_dir_no_dapr_yaml"
dirName := createTempDir(t, "test_find_file_in_dir")
t.Cleanup(func() {
cleanupTempDir(t, dirName)
})
err := os.Mkdir(filepath.Join(dirName, validDirNameWithDaprYAMLFile), 0o755)
assert.NoError(t, err)
err = os.Mkdir(filepath.Join(dirName, validDirWithNoDaprYAML), 0o755)
assert.NoError(t, err)
fl, err := os.Create(filepath.Join(dirName, validDirNameWithDaprYAMLFile, "dapr.yaml"))
assert.NoError(t, err)
fl.Close()
fl, err = os.Create(filepath.Join(dirName, validDirNameWithDaprYAMLFile, "test1.yaml"))
assert.NoError(t, err)
fl.Close()
testcases := []struct {
name string
input string
expectedErr bool
expectedFilePath string
}{
{
name: "valid directory path with dapr.yaml file",
input: filepath.Join(dirName, validDirNameWithDaprYAMLFile),
expectedErr: false,
expectedFilePath: filepath.Join(dirName, validDirNameWithDaprYAMLFile, "dapr.yaml"),
},
{
name: "valid directory path with no dapr.yaml file",
input: filepath.Join(dirName, validDirWithNoDaprYAML),
expectedErr: true,
expectedFilePath: "",
},
{
name: "non existent dir",
input: nonExistentDirName,
expectedErr: true,
expectedFilePath: "",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
filePath, err := FindFileInDir(tc.input, "dapr.yaml")
assert.Equal(t, tc.expectedErr, err != nil)
assert.Equal(t, tc.expectedFilePath, filePath)
})
}
}
func TestPrintDetail(t *testing.T) {
type fooStruct struct {
Field1 string
Field2 int
}
testcases := []struct {
name string
format string
list interface{}
expected string
shouldError bool
}{
{
name: "multiple items in JSON format",
format: "json",
list: []fooStruct{{Field1: "test1", Field2: 1}, {Field1: "test2", Field2: 2}},
expected: "[\n {\n \"Field1\": \"test1\",\n \"Field2\": 1\n },\n {\n \"Field1\": \"test2\",\n \"Field2\": 2\n }\n]",
shouldError: false,
},
{
name: "single item in JSON format",
format: "json",
list: []fooStruct{{Field1: "test1", Field2: 1}},
expected: "[\n {\n \"Field1\": \"test1\",\n \"Field2\": 1\n }\n]",
shouldError: false,
},
{
name: "no items in JSON format",
format: "json",
list: []fooStruct{},
expected: "[]",
shouldError: false,
},
{
name: "multiple items in YAML format",
format: "yaml",
list: []fooStruct{{Field1: "test1", Field2: 1}, {Field1: "test2", Field2: 2}},
expected: "- field1: test1\n field2: 1\n- field1: test2\n field2: 2\n",
shouldError: false,
},
{
name: "single item in YAML format",
format: "yaml",
list: []fooStruct{{Field1: "test1", Field2: 1}},
expected: "- field1: test1\n field2: 1\n",
shouldError: false,
},
{
name: "no items in YAML format",
format: "yaml",
list: []fooStruct{},
expected: "[]\n",
shouldError: false,
},
{
name: "invalid format",
format: "invalid",
list: []fooStruct{},
expected: "",
shouldError: true,
},
{
name: "invalid JSON",
format: "json",
list: math.Inf(1),
expected: "",
shouldError: true,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
err := PrintDetail(&buf, tc.format, tc.list)
if tc.shouldError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expected, buf.String())
}
})
}
}
func createTempDir(t *testing.T, tempDirName string) string {
dirName, err := os.MkdirTemp("", tempDirName)
assert.NoError(t, err)
return dirName
}
func createTempFile(t *testing.T, tempDirName, fileName string) string {
file, err := os.CreateTemp(tempDirName, fileName)
assert.NoError(t, err)
defer file.Close()
return file.Name()
}
func cleanupTempDir(t *testing.T, fileName string) {
err := os.RemoveAll(fileName)
assert.NoError(t, err)
}
func TestSanitizeDir(t *testing.T) {
testcases := []struct {
name string
input string
expected string
}{
{
name: "directory with single quote in three places",
input: "C:\\Use'rs\\sta'rk\\Docum'ents",
expected: "C:\\Use''rs\\sta''rk\\Docum''ents",
},
{
name: "directory with single quote in two places",
input: "C:\\Use'rs\\sta'rk",
expected: "C:\\Use''rs\\sta''rk",
},
{
name: "directory with single quote in username",
input: "C:\\Users\\Debash'ish",
expected: "C:\\Users\\Debash''ish",
},
{
name: "directory with no single quote",
input: "C:\\Users\\Shubham",
expected: "C:\\Users\\Shubham",
},
{
name: "directory with single quote in one place",
input: "C:\\Use'rs\\Shubham",
expected: "C:\\Use''rs\\Shubham",
},
{
name: "directory with single quote in many places in username",
input: "C:\\Users\\Shu'bh'am",
expected: "C:\\Users\\Shu''bh''am",
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
actual := SanitizeDir(tc.input)
assert.Equal(t, tc.expected, actual)
})
}
}