1 package utils
2
3 import (
4 "bytes"
5 "testing"
6
7 "golang.org/x/sys/unix"
8 )
9
10 var labelTest = []struct {
11 labels []string
12 query string
13 expectedValue string
14 }{
15 {[]string{"bundle=/path/to/bundle"}, "bundle", "/path/to/bundle"},
16 {[]string{"test=a", "test=b"}, "bundle", ""},
17 {[]string{"bundle=a", "test=b", "bundle=c"}, "bundle", "a"},
18 {[]string{"", "test=a", "bundle=b"}, "bundle", "b"},
19 {[]string{"test", "bundle=a"}, "bundle", "a"},
20 {[]string{"test=a", "bundle="}, "bundle", ""},
21 }
22
23 func TestSearchLabels(t *testing.T) {
24 for _, tt := range labelTest {
25 if v := SearchLabels(tt.labels, tt.query); v != tt.expectedValue {
26 t.Errorf("expected value '%s' for query '%s'; got '%s'", tt.expectedValue, tt.query, v)
27 }
28 }
29 }
30
31 func TestExitStatus(t *testing.T) {
32 status := unix.WaitStatus(0)
33 ex := ExitStatus(status)
34 if ex != 0 {
35 t.Errorf("expected exit status to equal 0 and received %d", ex)
36 }
37 }
38
39 func TestExitStatusSignaled(t *testing.T) {
40 status := unix.WaitStatus(2)
41 ex := ExitStatus(status)
42 if ex != 130 {
43 t.Errorf("expected exit status to equal 130 and received %d", ex)
44 }
45 }
46
47 func TestWriteJSON(t *testing.T) {
48 person := struct {
49 Name string
50 Age int
51 }{
52 Name: "Alice",
53 Age: 30,
54 }
55
56 var b bytes.Buffer
57 err := WriteJSON(&b, person)
58 if err != nil {
59 t.Fatal(err)
60 }
61
62 expected := `{"Name":"Alice","Age":30}`
63 if b.String() != expected {
64 t.Errorf("expected to write %s but was %s", expected, b.String())
65 }
66 }
67
68 func TestCleanPath(t *testing.T) {
69 path := CleanPath("")
70 if path != "" {
71 t.Errorf("expected to receive empty string and received %s", path)
72 }
73
74 path = CleanPath("rootfs")
75 if path != "rootfs" {
76 t.Errorf("expected to receive 'rootfs' and received %s", path)
77 }
78
79 path = CleanPath("../../../var")
80 if path != "var" {
81 t.Errorf("expected to receive 'var' and received %s", path)
82 }
83
84 path = CleanPath("/../../../var")
85 if path != "/var" {
86 t.Errorf("expected to receive '/var' and received %s", path)
87 }
88
89 path = CleanPath("/foo/bar/")
90 if path != "/foo/bar" {
91 t.Errorf("expected to receive '/foo/bar' and received %s", path)
92 }
93
94 path = CleanPath("/foo/bar/../")
95 if path != "/foo" {
96 t.Errorf("expected to receive '/foo' and received %s", path)
97 }
98 }
99
100 func TestStripRoot(t *testing.T) {
101 for _, test := range []struct {
102 root, path, out string
103 }{
104
105 {"/a/b", "/a/b/c", "/c"},
106 {"/hello/world", "/hello/world/the/quick-brown/fox", "/the/quick-brown/fox"},
107
108 {"/", "/a/b/c", "/a/b/c"},
109
110 {"/a/b", "/a/c/b", "/a/c/b"},
111
112 {"/abc/def", "/foo/abc/def/bar", "/foo/abc/def/bar"},
113
114 {"/foo/bar", "/foo/barSAMECOMPONENT", "/foo/barSAMECOMPONENT"},
115
116 {"/foo/bar", "/foo/bar/foo/bar/baz", "/foo/bar/baz"},
117
118 {"/foo/bar", "/foo/bar/../baz", "/foo/baz"},
119 {"/foo/bar", "../../../../../../foo/bar/baz", "/baz"},
120 {"/foo/bar", "/../../../../../../foo/bar/baz", "/baz"},
121 {"/foo/bar/../baz", "/foo/baz/bar", "/bar"},
122 {"/foo/bar/../baz", "/foo/baz/../bar/../baz/./foo", "/foo"},
123
124 {"foo/bar", "/foo/bar/baz/bee", "/baz/bee"},
125 {"/foo/bar", "foo/bar/baz/beef", "/baz/beef"},
126 {"foo/bar", "foo/bar/baz/beets", "/baz/beets"},
127 } {
128 got := stripRoot(test.root, test.path)
129 if got != test.out {
130 t.Errorf("stripRoot(%q, %q) -- got %q, expected %q", test.root, test.path, got, test.out)
131 }
132 }
133 }
134
View as plain text