github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/metadata/sandboxes_test.go (about) 1 /* 2 Copyright 2017 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package metadata 18 19 import ( 20 "fmt" 21 "reflect" 22 "testing" 23 "time" 24 25 "github.com/jonboulle/clockwork" 26 27 "github.com/Mirantis/virtlet/pkg/metadata/fake" 28 "github.com/Mirantis/virtlet/pkg/metadata/types" 29 ) 30 31 func TestRemovePodSandbox(t *testing.T) { 32 sandboxes := fake.GetSandboxes(1) 33 sandbox := sandboxes[0] 34 35 tests := []struct { 36 sandbox *types.PodSandboxConfig 37 error bool 38 }{ 39 { 40 sandbox: sandbox, 41 error: false, 42 }, 43 { 44 sandbox: nil, 45 error: true, 46 }, 47 } 48 49 for _, tc := range tests { 50 store, err := NewFakeStore() 51 if err != nil { 52 t.Fatal(err) 53 } 54 55 uid := "" 56 if tc.sandbox != nil { 57 uid = tc.sandbox.Uid 58 psi, _ := NewPodSandboxInfo(tc.sandbox, nil, types.PodSandboxState_SANDBOX_READY, clockwork.NewRealClock()) 59 if err := store.PodSandbox(uid).Save(func(c *types.PodSandboxInfo) (*types.PodSandboxInfo, error) { 60 return psi, nil 61 }); err != nil { 62 t.Fatal(err) 63 } 64 dumpDB(t, store, "before delete") 65 } 66 if err := store.PodSandbox(uid).Save(func(c *types.PodSandboxInfo) (*types.PodSandboxInfo, error) { 67 return nil, nil 68 }); err != nil { 69 if tc.error { 70 71 continue 72 } 73 74 t.Fatal(err) 75 } else { 76 _, err = store.PodSandbox(uid).Retrieve() 77 if err == nil { 78 t.Error("Sandbox wasn't deleted") 79 } 80 dumpDB(t, store, "after delete") 81 } 82 } 83 } 84 85 func TestRetrieve(t *testing.T) { 86 sandboxes := fake.GetSandboxes(2) 87 88 fakeClock := clockwork.NewFakeClockAt(time.Now()) 89 store := setUpTestStore(t, sandboxes, []*fake.ContainerTestConfig{}, fakeClock) 90 91 for _, sandbox := range sandboxes { 92 expectedSandboxInfo, err := NewPodSandboxInfo(sandbox, nil, types.PodSandboxState_SANDBOX_READY, fakeClock) 93 if err != nil { 94 t.Fatal(err) 95 } 96 if expectedSandboxInfo.PodID != "" { 97 t.Error("podID must be empty for new PodSandboxInfo object") 98 } 99 sandboxManager := store.PodSandbox(sandbox.Uid) 100 actualSandboxInfo, err := sandboxManager.Retrieve() 101 if err != nil { 102 t.Fatal(err) 103 } 104 if actualSandboxInfo == nil { 105 t.Fatal(fmt.Errorf("missing PodSandboxInfo for sandbox %q", sandbox.Uid)) 106 } 107 if actualSandboxInfo.PodID != sandboxManager.GetID() { 108 t.Errorf("invalid podID for retrieved PodSandboxInfo: %s != %s", actualSandboxInfo.PodID, sandboxManager.GetID()) 109 } 110 expectedSandboxInfo.PodID = sandboxManager.GetID() 111 if !reflect.DeepEqual(expectedSandboxInfo, actualSandboxInfo) { 112 t.Error("retrieved sandbox info object is not equal to expected value") 113 } 114 } 115 } 116 117 func TestSetGetPodSandboxStatus(t *testing.T) { 118 sandboxes := fake.GetSandboxes(2) 119 120 store := setUpTestStore(t, sandboxes, []*fake.ContainerTestConfig{}, nil) 121 122 for _, sandbox := range sandboxes { 123 sandboxInfo, err := store.PodSandbox(sandbox.Uid).Retrieve() 124 if err != nil { 125 t.Fatal(err) 126 } 127 if sandboxInfo == nil { 128 t.Fatal(fmt.Errorf("missing PodSandboxInfo for sandbox %q", sandbox.Uid)) 129 } 130 131 if sandboxInfo.State != types.PodSandboxState_SANDBOX_READY { 132 t.Errorf("Sandbox state not ready") 133 } 134 135 if !reflect.DeepEqual(sandboxInfo.Config.Labels, sandbox.Labels) { 136 t.Errorf("Expected %v, instead got %v", sandbox.Labels, sandboxInfo.Config.Labels) 137 } 138 139 if !reflect.DeepEqual(sandboxInfo.Config.Annotations, sandbox.Annotations) { 140 t.Errorf("Expected %v, instead got %v", sandbox.Annotations, sandboxInfo.Config.Annotations) 141 } 142 143 if sandboxInfo.Config.Name != sandbox.Name { 144 t.Errorf("Expected %s, instead got %s", sandbox.Name, sandboxInfo.Config.Name) 145 } 146 } 147 } 148 149 func TestListPodSandbox(t *testing.T) { 150 genSandboxes := fake.GetSandboxes(2) 151 152 firstSandboxConfig := genSandboxes[0] 153 secondSandboxConfig := genSandboxes[1] 154 155 firstSandboxConfig.Labels = map[string]string{"unique": "first", "common": "both"} 156 secondSandboxConfig.Labels = map[string]string{"unique": "second", "common": "both"} 157 158 sandboxConfigs := []*types.PodSandboxConfig{firstSandboxConfig, secondSandboxConfig} 159 stateReady := types.PodSandboxState_SANDBOX_READY 160 stateNotReady := types.PodSandboxState_SANDBOX_NOTREADY 161 162 tests := []struct { 163 filter *types.PodSandboxFilter 164 expectedIds []string 165 }{ 166 { 167 filter: &types.PodSandboxFilter{}, 168 expectedIds: []string{firstSandboxConfig.Uid, secondSandboxConfig.Uid}, 169 }, 170 { 171 filter: &types.PodSandboxFilter{ 172 Id: firstSandboxConfig.Uid, 173 }, 174 expectedIds: []string{firstSandboxConfig.Uid}, 175 }, 176 { 177 filter: &types.PodSandboxFilter{ 178 State: &stateReady, 179 }, 180 expectedIds: []string{firstSandboxConfig.Uid, secondSandboxConfig.Uid}, 181 }, 182 { 183 filter: &types.PodSandboxFilter{ 184 State: &stateNotReady, 185 }, 186 expectedIds: []string{}, 187 }, 188 { 189 filter: &types.PodSandboxFilter{ 190 LabelSelector: map[string]string{"unique": "first"}, 191 }, 192 expectedIds: []string{firstSandboxConfig.Uid}, 193 }, 194 { 195 filter: &types.PodSandboxFilter{ 196 LabelSelector: map[string]string{"common": "both"}, 197 }, 198 expectedIds: []string{firstSandboxConfig.Uid, secondSandboxConfig.Uid}, 199 }, 200 { 201 filter: &types.PodSandboxFilter{ 202 LabelSelector: map[string]string{"unique": "second", "common": "both"}, 203 }, 204 expectedIds: []string{secondSandboxConfig.Uid}, 205 }, 206 { 207 filter: &types.PodSandboxFilter{ 208 Id: firstSandboxConfig.Uid, 209 LabelSelector: map[string]string{"unique": "second", "common": "both"}, 210 }, 211 expectedIds: []string{}, 212 }, 213 { 214 filter: &types.PodSandboxFilter{ 215 Id: firstSandboxConfig.Uid, 216 LabelSelector: map[string]string{"unique": "first", "common": "both"}, 217 }, 218 expectedIds: []string{firstSandboxConfig.Uid}, 219 }, 220 { 221 filter: &types.PodSandboxFilter{ 222 Id: firstSandboxConfig.Uid, 223 LabelSelector: map[string]string{"common": "both"}, 224 }, 225 expectedIds: []string{firstSandboxConfig.Uid}, 226 }, 227 } 228 229 cc := fake.GetContainersConfig(sandboxConfigs) 230 b := setUpTestStore(t, sandboxConfigs, cc, nil) 231 232 for _, tc := range tests { 233 sandboxes, err := b.ListPodSandboxes(tc.filter) 234 if err != nil { 235 t.Fatal(err) 236 } 237 238 if len(sandboxes) != len(tc.expectedIds) { 239 t.Errorf("Expected %d sandboxes, instead got %d", len(tc.expectedIds), len(sandboxes)) 240 } 241 242 for _, id := range tc.expectedIds { 243 found := false 244 for _, podSandbox := range sandboxes { 245 if id == podSandbox.GetID() { 246 found = true 247 break 248 } 249 } 250 if !found { 251 t.Errorf("Didn't find expected sandbox id %d in returned sandbox list %v", len(tc.expectedIds), sandboxes) 252 } 253 } 254 } 255 }