github.com/Mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/manager/cri_test.go (about) 1 /* 2 Copyright 2018 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 manager 18 19 import ( 20 "reflect" 21 "testing" 22 23 kubeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2" 24 25 "github.com/Mirantis/virtlet/pkg/metadata/fake" 26 "github.com/Mirantis/virtlet/pkg/metadata/types" 27 "github.com/Mirantis/virtlet/pkg/network" 28 "github.com/Mirantis/virtlet/tests/criapi" 29 "github.com/Mirantis/virtlet/tests/gm" 30 ) 31 32 func TestConversions(t *testing.T) { 33 configs := fake.GetSandboxes(2) 34 criConfigs := criapi.GetSandboxes(2) 35 criConfigWithAltLogDir := *criConfigs[0] 36 criConfigWithAltLogDir.LogDirectory = "/some/pod/log/dir/" + criConfigWithAltLogDir.Metadata.Uid 37 csn := &network.ContainerSideNetwork{ 38 NsPath: "/var/run/netns/bae464f1-6ee7-4ee2-826e-33293a9de95e", 39 } 40 podSandboxInfos := []interface{}{ 41 &types.PodSandboxInfo{ 42 PodID: configs[0].Uid, 43 Config: configs[0], 44 CreatedAt: 1496175540000000000, 45 State: types.PodSandboxState_SANDBOX_READY, 46 }, 47 &types.PodSandboxInfo{ 48 PodID: configs[1].Uid, 49 Config: configs[1], 50 CreatedAt: 1496175550000000000, 51 State: types.PodSandboxState_SANDBOX_NOTREADY, 52 }, 53 } 54 containerInfos := []interface{}{ 55 &types.ContainerInfo{ 56 Id: "f1bfb494-af3d-48ab-b8b1-2c850e1e8a00", 57 Name: "testcontainer", 58 CreatedAt: 1496175540000000000, 59 StartedAt: 1496175550000000000, 60 State: types.ContainerState_CONTAINER_CREATED, 61 Config: types.VMConfig{ 62 PodSandboxID: configs[0].Uid, 63 Image: "testImage", 64 ContainerLabels: map[string]string{ 65 "containerLabel": "foo", 66 }, 67 ContainerAnnotations: map[string]string{ 68 "containerAnnotation": "foobar", 69 }, 70 LogPath: "testcontainer_0.log", 71 }, 72 }, 73 &types.ContainerInfo{ 74 Id: "13bdedae-540d-4131-959b-366c6343d5b4", 75 Name: "testcontainer1", 76 CreatedAt: 1496175560000000000, 77 StartedAt: 1496175570000000000, 78 State: types.ContainerState_CONTAINER_EXITED, 79 Config: types.VMConfig{ 80 PodSandboxID: configs[1].Uid, 81 Image: "testImage1", 82 ContainerLabels: map[string]string{ 83 "containerLabel": "foo1", 84 }, 85 ContainerAnnotations: map[string]string{ 86 "containerAnnotation": "foobar1", 87 }, 88 LogPath: "testcontainer1_0.log", 89 }, 90 }, 91 } 92 for _, tc := range []struct { 93 name string 94 converter interface{} 95 in []interface{} 96 }{ 97 { 98 name: "PodSandboxInfoToCRIPodSandboxStatus", 99 converter: PodSandboxInfoToCRIPodSandboxStatus, 100 in: podSandboxInfos, 101 }, 102 { 103 name: "PodSandboxInfoToCRIPodSandbox", 104 converter: PodSandboxInfoToCRIPodSandbox, 105 in: podSandboxInfos, 106 }, 107 { 108 name: "CRIPodSandboxConfigToPodSandboxConfig", 109 converter: CRIPodSandboxConfigToPodSandboxConfig, 110 in: []interface{}{ 111 criConfigs[0], 112 criConfigs[1], 113 }, 114 }, 115 { 116 name: "CRIPodSandboxFilterToPodSandboxFilter", 117 converter: CRIPodSandboxFilterToPodSandboxFilter, 118 in: []interface{}{ 119 (*kubeapi.PodSandboxFilter)(nil), 120 &kubeapi.PodSandboxFilter{}, 121 &kubeapi.PodSandboxFilter{ 122 Id: "a393e311-5f4f-402b-8567-864d2ab81b83", 123 }, 124 &kubeapi.PodSandboxFilter{ 125 Id: "a393e311-5f4f-402b-8567-864d2ab81b83", 126 State: &kubeapi.PodSandboxStateValue{ 127 kubeapi.PodSandboxState_SANDBOX_NOTREADY, 128 }, 129 }, 130 &kubeapi.PodSandboxFilter{ 131 Id: "a393e311-5f4f-402b-8567-864d2ab81b83", 132 State: &kubeapi.PodSandboxStateValue{ 133 kubeapi.PodSandboxState_SANDBOX_READY, 134 }, 135 }, 136 &kubeapi.PodSandboxFilter{ 137 Id: "a393e311-5f4f-402b-8567-864d2ab81b83", 138 State: &kubeapi.PodSandboxStateValue{ 139 kubeapi.PodSandboxState_SANDBOX_READY, 140 }, 141 LabelSelector: map[string]string{ 142 "foo": "bar", 143 }, 144 }, 145 }, 146 }, 147 { 148 name: "GetVMConfig", 149 converter: func(in *kubeapi.CreateContainerRequest) *types.VMConfig { 150 r, err := GetVMConfig(in, csn) 151 if err != nil { 152 t.Fatalf("bad CreateContainerRequest: %#v", in) 153 } 154 return r 155 }, 156 in: []interface{}{ 157 &kubeapi.CreateContainerRequest{ 158 PodSandboxId: criConfigs[0].Metadata.Uid, 159 Config: &kubeapi.ContainerConfig{ 160 Image: &kubeapi.ImageSpec{Image: "testImage"}, 161 Labels: map[string]string{"foo": "bar", "fizz": "buzz"}, 162 Metadata: &kubeapi.ContainerMetadata{ 163 Name: "testcontainer", 164 }, 165 }, 166 SandboxConfig: criConfigs[0], 167 }, 168 &kubeapi.CreateContainerRequest{ 169 PodSandboxId: criConfigs[0].Metadata.Uid, 170 Config: &kubeapi.ContainerConfig{ 171 Image: &kubeapi.ImageSpec{Image: "testImage"}, 172 Labels: map[string]string{"foo": "bar", "fizz": "buzz"}, 173 Metadata: &kubeapi.ContainerMetadata{ 174 Name: "testcontainer", 175 }, 176 LogPath: "some_logpath_0.log", 177 }, 178 SandboxConfig: &criConfigWithAltLogDir, 179 }, 180 &kubeapi.CreateContainerRequest{ 181 PodSandboxId: criConfigs[1].Metadata.Uid, 182 Config: &kubeapi.ContainerConfig{ 183 Image: &kubeapi.ImageSpec{Image: "testImage"}, 184 Labels: map[string]string{"foo": "bar", "fizz": "buzz"}, 185 Mounts: []*kubeapi.Mount{ 186 { 187 ContainerPath: "/mnt", 188 HostPath: "/whatever", 189 }, 190 }, 191 Metadata: &kubeapi.ContainerMetadata{ 192 Name: "testcontainer", 193 }, 194 }, 195 SandboxConfig: criConfigs[1], 196 }, 197 }, 198 }, 199 { 200 name: "CRIContainerFilterToContainerFilter", 201 converter: CRIContainerFilterToContainerFilter, 202 in: []interface{}{ 203 (*kubeapi.ContainerFilter)(nil), 204 &kubeapi.ContainerFilter{}, 205 &kubeapi.ContainerFilter{ 206 Id: "a393e311-5f4f-402b-8567-864d2ab81b83", 207 }, 208 &kubeapi.ContainerFilter{ 209 PodSandboxId: "c5134b10-6474-4139-8bf9-f6f61a1f5906", 210 }, 211 &kubeapi.ContainerFilter{ 212 Id: "a393e311-5f4f-402b-8567-864d2ab81b83", 213 State: &kubeapi.ContainerStateValue{ 214 kubeapi.ContainerState_CONTAINER_CREATED, 215 }, 216 }, 217 &kubeapi.ContainerFilter{ 218 Id: "a393e311-5f4f-402b-8567-864d2ab81b83", 219 State: &kubeapi.ContainerStateValue{ 220 kubeapi.ContainerState_CONTAINER_RUNNING, 221 }, 222 }, 223 &kubeapi.ContainerFilter{ 224 Id: "a393e311-5f4f-402b-8567-864d2ab81b83", 225 State: &kubeapi.ContainerStateValue{ 226 kubeapi.ContainerState_CONTAINER_EXITED, 227 }, 228 LabelSelector: map[string]string{ 229 "foo": "bar", 230 }, 231 }, 232 }, 233 }, 234 { 235 name: "ContainerInfoToCRIContainer", 236 converter: ContainerInfoToCRIContainer, 237 in: containerInfos, 238 }, 239 { 240 name: "ContainerInfoToCRIContainerStatus", 241 converter: ContainerInfoToCRIContainerStatus, 242 in: containerInfos, 243 }, 244 } { 245 t.Run(tc.name, func(t *testing.T) { 246 conv := reflect.ValueOf(tc.converter) 247 var r []map[string]interface{} 248 for _, v := range tc.in { 249 rv := conv.Call([]reflect.Value{reflect.ValueOf(v)}) 250 if len(rv) != 1 { 251 t.Fatalf("more than one value returned by the converter for %#v", v) 252 } 253 r = append(r, map[string]interface{}{ 254 "in": v, 255 "out": rv[0].Interface(), 256 }) 257 } 258 gm.Verify(t, gm.NewYamlVerifier(r)) 259 }) 260 } 261 }