gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/runsc/cmd/list_test.go (about) 1 // Copyright 2022 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "bytes" 19 "os" 20 "strings" 21 "testing" 22 23 "gvisor.dev/gvisor/pkg/test/testutil" 24 "gvisor.dev/gvisor/runsc/container" 25 ) 26 27 func TestList(t *testing.T) { 28 dir, err := os.MkdirTemp(testutil.TmpDir(), "list") 29 if err != nil { 30 t.Fatal(err) 31 } 32 defer os.RemoveAll(dir) 33 34 for _, tc := range []struct { 35 name string 36 list List 37 ids []container.FullID 38 want []string 39 }{ 40 { 41 name: "single", 42 list: List{quiet: true}, 43 ids: []container.FullID{ 44 {SandboxID: "abc", ContainerID: "123"}, 45 }, 46 want: []string{"123"}, 47 }, 48 { 49 name: "multiple", 50 list: List{quiet: true}, 51 ids: []container.FullID{ 52 {SandboxID: "abc", ContainerID: "123"}, 53 {SandboxID: "def", ContainerID: "123"}, 54 }, 55 want: []string{"123", "123"}, 56 }, 57 { 58 name: "multicontainer", 59 list: List{quiet: true}, 60 ids: []container.FullID{ 61 {SandboxID: "abc", ContainerID: "123"}, 62 {SandboxID: "abc", ContainerID: "456"}, 63 }, 64 want: []string{"123", "456"}, 65 }, 66 { 67 name: "multiple-multicontainer", 68 list: List{quiet: true}, 69 ids: []container.FullID{ 70 {SandboxID: "abc", ContainerID: "123"}, 71 {SandboxID: "abc", ContainerID: "456"}, 72 {SandboxID: "def", ContainerID: "123"}, 73 {SandboxID: "def", ContainerID: "789"}, 74 {SandboxID: "ghi", ContainerID: "012"}, 75 }, 76 want: []string{"123", "456", "123", "789", "012"}, 77 }, 78 { 79 name: "sandbox-single", 80 list: List{quiet: true, sandbox: true}, 81 ids: []container.FullID{ 82 {SandboxID: "abc", ContainerID: "123"}, 83 }, 84 want: []string{"abc"}, 85 }, 86 { 87 name: "sandbox-multiple", 88 list: List{quiet: true, sandbox: true}, 89 ids: []container.FullID{ 90 {SandboxID: "abc", ContainerID: "123"}, 91 {SandboxID: "def", ContainerID: "123"}, 92 }, 93 want: []string{"abc", "def"}, 94 }, 95 { 96 name: "sandbox-multicontainer", 97 list: List{quiet: true, sandbox: true}, 98 ids: []container.FullID{ 99 {SandboxID: "abc", ContainerID: "123"}, 100 {SandboxID: "abc", ContainerID: "456"}, 101 }, 102 want: []string{"abc"}, 103 }, 104 { 105 name: "sandbox-multiple-multicontainer", 106 list: List{quiet: true, sandbox: true}, 107 ids: []container.FullID{ 108 {SandboxID: "abc", ContainerID: "123"}, 109 {SandboxID: "abc", ContainerID: "456"}, 110 {SandboxID: "def", ContainerID: "123"}, 111 {SandboxID: "def", ContainerID: "789"}, 112 {SandboxID: "ghi", ContainerID: "012"}, 113 }, 114 want: []string{"abc", "def", "ghi"}, 115 }, 116 } { 117 t.Run(tc.name, func(t *testing.T) { 118 for _, id := range tc.ids { 119 saver := container.StateFile{RootDir: dir, ID: id} 120 if err := saver.LockForNew(); err != nil { 121 t.Fatal(err) 122 } 123 defer saver.Destroy() 124 defer saver.UnlockOrDie() 125 126 if err := saver.SaveLocked(nil); err != nil { 127 t.Fatal(err) 128 } 129 } 130 131 out := &bytes.Buffer{} 132 if err := tc.list.execute(dir, out); err != nil { 133 t.Fatal(err) 134 } 135 136 // Handle IDs returned out of order. 137 got := make(map[string]struct{}) 138 for _, id := range strings.Split(out.String(), "\n") { 139 got[id] = struct{}{} 140 } 141 for _, want := range tc.want { 142 if _, ok := got[want]; !ok { 143 t.Errorf("container ID not found in result want: %q, got: %q", want, out) 144 } 145 } 146 }) 147 } 148 }