|
| 1 | +/* |
| 2 | + Copyright 2026 The CDI Authors |
| 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 cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + oci "github.com/opencontainers/runtime-spec/specs-go" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestCollectCDIDevicesFromOCISpec(t *testing.T) { |
| 27 | + for _, tc := range []struct { |
| 28 | + name string |
| 29 | + spec *oci.Spec |
| 30 | + devices []string |
| 31 | + linuxDevs []oci.LinuxDevice |
| 32 | + emptyDevice bool |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "collect CDI devices and keep non-CDI devices", |
| 36 | + spec: &oci.Spec{ |
| 37 | + Linux: &oci.Linux{ |
| 38 | + Devices: []oci.LinuxDevice{ |
| 39 | + {Path: "vendor.com/device=dev0"}, |
| 40 | + {Path: "/dev/null", Type: "c", Major: 1, Minor: 3}, |
| 41 | + {Path: "/dev/null", Type: "c", Major: 1, Minor: 5}, |
| 42 | + {Path: "vendor.com/device=dev1"}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + devices: []string{ |
| 47 | + "vendor.com/device=dev0", |
| 48 | + "vendor.com/device=dev1", |
| 49 | + }, |
| 50 | + linuxDevs: []oci.LinuxDevice{ |
| 51 | + {Path: "/dev/null", Type: "c", Major: 1, Minor: 5}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "all CDI devices leave an empty device slice", |
| 56 | + spec: &oci.Spec{ |
| 57 | + Linux: &oci.Linux{ |
| 58 | + Devices: []oci.LinuxDevice{ |
| 59 | + {Path: "vendor.com/device=dev0"}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + devices: []string{"vendor.com/device=dev0"}, |
| 64 | + linuxDevs: []oci.LinuxDevice{}, |
| 65 | + emptyDevice: true, |
| 66 | + }, |
| 67 | + } { |
| 68 | + t.Run(tc.name, func(t *testing.T) { |
| 69 | + devices := collectCDIDevicesFromOCISpec(tc.spec) |
| 70 | + |
| 71 | + require.Equal(t, tc.devices, devices) |
| 72 | + require.Equal(t, tc.linuxDevs, tc.spec.Linux.Devices) |
| 73 | + if tc.emptyDevice && tc.spec.Linux.Devices == nil { |
| 74 | + require.NotNil(t, tc.spec.Linux.Devices) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments