|
1 | 1 | package container_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "path/filepath" |
| 7 | + |
4 | 8 | . "github.com/onsi/ginkgo" |
5 | 9 | . "github.com/onsi/gomega" |
| 10 | + |
| 11 | + rspec "github.com/opencontainers/runtime-spec/specs-go" |
| 12 | + |
| 13 | + "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi" |
6 | 14 | "github.com/opencontainers/runc/libcontainer/devices" |
7 | 15 | types "k8s.io/cri-api/pkg/apis/runtime/v1" |
8 | 16 | ) |
@@ -180,4 +188,152 @@ var _ = t.Describe("Container", func() { |
180 | 188 | }) |
181 | 189 | } |
182 | 190 | }) |
| 191 | + |
| 192 | + t.Describe("SpecAdd(CDI)Devices", func() { |
| 193 | + writeCDISpecFiles := func(content []string) error { |
| 194 | + if len(content) == 0 { |
| 195 | + return nil |
| 196 | + } |
| 197 | + |
| 198 | + dir := t.MustTempDir("cdi") |
| 199 | + for idx, data := range content { |
| 200 | + file := filepath.Join(dir, fmt.Sprintf("spec-%d.yaml", idx)) |
| 201 | + err := ioutil.WriteFile(file, []byte(data), 0o644) |
| 202 | + if err != nil { |
| 203 | + return err |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return cdi.GetRegistry(cdi.WithSpecDirs(dir)).Refresh() |
| 208 | + } |
| 209 | + |
| 210 | + type testdata struct { |
| 211 | + testDescription string |
| 212 | + cdiSpecFiles []string |
| 213 | + annotations map[string]string |
| 214 | + expectError bool |
| 215 | + expectDevices []rspec.LinuxDevice |
| 216 | + expectEnv []string |
| 217 | + } |
| 218 | + |
| 219 | + tests := []testdata{ |
| 220 | + { |
| 221 | + testDescription: "Expect no CDI error for nil annotations", |
| 222 | + }, |
| 223 | + { |
| 224 | + testDescription: "Expect no CDI error for empty annotations", |
| 225 | + annotations: map[string]string{}, |
| 226 | + }, |
| 227 | + { |
| 228 | + testDescription: "Expect CDI error for invalid CDI device reference in annotations", |
| 229 | + annotations: map[string]string{ |
| 230 | + cdi.AnnotationPrefix + "devices": "foobar", |
| 231 | + }, |
| 232 | + expectError: true, |
| 233 | + }, |
| 234 | + { |
| 235 | + testDescription: "Expect CDI error for unresolvable devices", |
| 236 | + annotations: map[string]string{ |
| 237 | + cdi.AnnotationPrefix + "vendor1_devices": "vendor1.com/device=no-such-dev", |
| 238 | + }, |
| 239 | + expectError: true, |
| 240 | + }, |
| 241 | + { |
| 242 | + testDescription: "Expect properly injected resolvable CDI devices", |
| 243 | + cdiSpecFiles: []string{ |
| 244 | + ` |
| 245 | +cdiVersion: "0.2.0" |
| 246 | +kind: "vendor1.com/device" |
| 247 | +devices: |
| 248 | + - name: foo |
| 249 | + containerEdits: |
| 250 | + deviceNodes: |
| 251 | + - path: /dev/loop8 |
| 252 | + type: b |
| 253 | + major: 7 |
| 254 | + minor: 8 |
| 255 | + env: |
| 256 | + - FOO=injected |
| 257 | +containerEdits: |
| 258 | + env: |
| 259 | + - "VENDOR1=present" |
| 260 | +`, |
| 261 | + ` |
| 262 | +cdiVersion: "0.2.0" |
| 263 | +kind: "vendor2.com/device" |
| 264 | +devices: |
| 265 | + - name: bar |
| 266 | + containerEdits: |
| 267 | + deviceNodes: |
| 268 | + - path: /dev/loop9 |
| 269 | + type: b |
| 270 | + major: 7 |
| 271 | + minor: 9 |
| 272 | + env: |
| 273 | + - BAR=injected |
| 274 | +containerEdits: |
| 275 | + env: |
| 276 | + - "VENDOR2=present" |
| 277 | +`, |
| 278 | + }, |
| 279 | + annotations: map[string]string{ |
| 280 | + cdi.AnnotationPrefix + "vendor1_devices": "vendor1.com/device=foo", |
| 281 | + cdi.AnnotationPrefix + "vendor2_devices": "vendor2.com/device=bar", |
| 282 | + }, |
| 283 | + expectDevices: []rspec.LinuxDevice{ |
| 284 | + { |
| 285 | + Path: "/dev/loop8", |
| 286 | + Type: "b", |
| 287 | + Major: 7, |
| 288 | + Minor: 8, |
| 289 | + }, |
| 290 | + { |
| 291 | + Path: "/dev/loop9", |
| 292 | + Type: "b", |
| 293 | + Major: 7, |
| 294 | + Minor: 9, |
| 295 | + }, |
| 296 | + }, |
| 297 | + expectEnv: []string{ |
| 298 | + "FOO=injected", |
| 299 | + "VENDOR1=present", |
| 300 | + "BAR=injected", |
| 301 | + "VENDOR2=present", |
| 302 | + }, |
| 303 | + }, |
| 304 | + } |
| 305 | + |
| 306 | + for _, test := range tests { |
| 307 | + test := test |
| 308 | + It(test.testDescription, func() { |
| 309 | + // Given |
| 310 | + config := &types.ContainerConfig{ |
| 311 | + Metadata: &types.ContainerMetadata{Name: "name"}, |
| 312 | + Annotations: test.annotations, |
| 313 | + Linux: &types.LinuxContainerConfig{ |
| 314 | + SecurityContext: &types.LinuxContainerSecurityContext{}, |
| 315 | + }, |
| 316 | + Devices: []*types.Device{}, |
| 317 | + } |
| 318 | + sboxConfig := &types.PodSandboxConfig{ |
| 319 | + Linux: &types.LinuxPodSandboxConfig{ |
| 320 | + SecurityContext: &types.LinuxSandboxSecurityContext{}, |
| 321 | + }, |
| 322 | + } |
| 323 | + Expect(sut.SetConfig(config, sboxConfig)).To(BeNil()) |
| 324 | + Expect(sut.SetPrivileged()).To(BeNil()) |
| 325 | + Expect(writeCDISpecFiles(test.cdiSpecFiles)).To(BeNil()) |
| 326 | + |
| 327 | + // When |
| 328 | + err := sut.SpecAddDevices(nil, nil, false, false) |
| 329 | + |
| 330 | + // Then |
| 331 | + Expect(err != nil).To(Equal(test.expectError)) |
| 332 | + if err == nil { |
| 333 | + Expect(sut.Spec().Config.Process.Env).Should(ContainElements(test.expectEnv)) |
| 334 | + Expect(sut.Spec().Config.Linux.Devices).Should(ContainElements(test.expectDevices)) |
| 335 | + } |
| 336 | + }) |
| 337 | + } |
| 338 | + }) |
183 | 339 | }) |
0 commit comments