github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/utils/utils_test.go (about) 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 expVal string 14 expOk bool 15 }{ 16 {[]string{"bundle=/path/to/bundle"}, "bundle", "/path/to/bundle", true}, 17 {[]string{"test=a", "test=b"}, "bundle", "", false}, 18 {[]string{"bundle=a", "test=b", "bundle=c"}, "bundle", "a", true}, 19 {[]string{"", "test=a", "bundle=b"}, "bundle", "b", true}, 20 {[]string{"test", "bundle=a"}, "bundle", "a", true}, 21 {[]string{"test=a", "bundle="}, "bundle", "", true}, 22 } 23 24 func TestSearchLabels(t *testing.T) { 25 for _, tt := range labelTest { 26 v, ok := SearchLabels(tt.labels, tt.query) 27 if ok != tt.expOk { 28 t.Errorf("expected ok: %v, got %v", tt.expOk, ok) 29 continue 30 } 31 if v != tt.expVal { 32 t.Errorf("expected value '%s' for query '%s'; got '%s'", tt.expVal, tt.query, v) 33 } 34 } 35 } 36 37 func TestExitStatus(t *testing.T) { 38 status := unix.WaitStatus(0) 39 ex := ExitStatus(status) 40 if ex != 0 { 41 t.Errorf("expected exit status to equal 0 and received %d", ex) 42 } 43 } 44 45 func TestExitStatusSignaled(t *testing.T) { 46 status := unix.WaitStatus(2) 47 ex := ExitStatus(status) 48 if ex != 130 { 49 t.Errorf("expected exit status to equal 130 and received %d", ex) 50 } 51 } 52 53 func TestWriteJSON(t *testing.T) { 54 person := struct { 55 Name string 56 Age int 57 }{ 58 Name: "Alice", 59 Age: 30, 60 } 61 62 var b bytes.Buffer 63 err := WriteJSON(&b, person) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 expected := `{"Name":"Alice","Age":30}` 69 if b.String() != expected { 70 t.Errorf("expected to write %s but was %s", expected, b.String()) 71 } 72 } 73 74 func TestCleanPath(t *testing.T) { 75 path := CleanPath("") 76 if path != "" { 77 t.Errorf("expected to receive empty string and received %s", path) 78 } 79 80 path = CleanPath("rootfs") 81 if path != "rootfs" { 82 t.Errorf("expected to receive 'rootfs' and received %s", path) 83 } 84 85 path = CleanPath("../../../var") 86 if path != "var" { 87 t.Errorf("expected to receive 'var' and received %s", path) 88 } 89 90 path = CleanPath("/../../../var") 91 if path != "/var" { 92 t.Errorf("expected to receive '/var' and received %s", path) 93 } 94 95 path = CleanPath("/foo/bar/") 96 if path != "/foo/bar" { 97 t.Errorf("expected to receive '/foo/bar' and received %s", path) 98 } 99 100 path = CleanPath("/foo/bar/../") 101 if path != "/foo" { 102 t.Errorf("expected to receive '/foo' and received %s", path) 103 } 104 } 105 106 func TestStripRoot(t *testing.T) { 107 for _, test := range []struct { 108 root, path, out string 109 }{ 110 // Works with multiple components. 111 {"/a/b", "/a/b/c", "/c"}, 112 {"/hello/world", "/hello/world/the/quick-brown/fox", "/the/quick-brown/fox"}, 113 // '/' must be a no-op. 114 {"/", "/a/b/c", "/a/b/c"}, 115 // Must be the correct order. 116 {"/a/b", "/a/c/b", "/a/c/b"}, 117 // Must be at start. 118 {"/abc/def", "/foo/abc/def/bar", "/foo/abc/def/bar"}, 119 // Must be a lexical parent. 120 {"/foo/bar", "/foo/barSAMECOMPONENT", "/foo/barSAMECOMPONENT"}, 121 // Must only strip the root once. 122 {"/foo/bar", "/foo/bar/foo/bar/baz", "/foo/bar/baz"}, 123 // Deal with .. in a fairly sane way. 124 {"/foo/bar", "/foo/bar/../baz", "/foo/baz"}, 125 {"/foo/bar", "../../../../../../foo/bar/baz", "/baz"}, 126 {"/foo/bar", "/../../../../../../foo/bar/baz", "/baz"}, 127 {"/foo/bar/../baz", "/foo/baz/bar", "/bar"}, 128 {"/foo/bar/../baz", "/foo/baz/../bar/../baz/./foo", "/foo"}, 129 // All paths are made absolute before stripping. 130 {"foo/bar", "/foo/bar/baz/bee", "/baz/bee"}, 131 {"/foo/bar", "foo/bar/baz/beef", "/baz/beef"}, 132 {"foo/bar", "foo/bar/baz/beets", "/baz/beets"}, 133 } { 134 got := stripRoot(test.root, test.path) 135 if got != test.out { 136 t.Errorf("stripRoot(%q, %q) -- got %q, expected %q", test.root, test.path, got, test.out) 137 } 138 } 139 }