|
16 | 16 |
|
17 | 17 | package containerd
|
18 | 18 |
|
| 19 | +import ( |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "github.com/urfave/cli/v3" |
| 26 | + |
| 27 | + "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container" |
| 28 | + "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml" |
| 29 | +) |
| 30 | + |
19 | 31 | const (
|
20 | 32 | runtimeType = "runtime_type"
|
21 | 33 | )
|
22 | 34 |
|
| 35 | +func TestSetupCleanup(t *testing.T) { |
| 36 | + c := &cli.Command{ |
| 37 | + Name: "test", |
| 38 | + } |
| 39 | + testCases := []struct { |
| 40 | + description string |
| 41 | + containerOptions container.Options |
| 42 | + options Options |
| 43 | + prepareEnvironment func(*testing.T, string) error |
| 44 | + expectedSetupError error |
| 45 | + assertSetupPostConditions func(*testing.T, string) error |
| 46 | + expectedCleanupError error |
| 47 | + assertCleanupPostConditions func(*testing.T, string) error |
| 48 | + }{ |
| 49 | + { |
| 50 | + description: "top-level config does not exist", |
| 51 | + containerOptions: container.Options{ |
| 52 | + DropInConfig: "{{ .testRoot }}/conf.d/99-nvidia.toml", |
| 53 | + Config: "{{ .testRoot }}/etc/containerd/config.toml", |
| 54 | + }, |
| 55 | + assertSetupPostConditions: func(t *testing.T, testRoot string) error { |
| 56 | + require.FileExists(t, filepath.Join(testRoot, "/etc/containerd/config.toml")) |
| 57 | + topLevel, err := toml.FromFile(filepath.Join(testRoot, "/etc/containerd/config.toml")).Load() |
| 58 | + require.NoError(t, err) |
| 59 | + require.EqualValues(t, `imports = ["`+filepath.Join(testRoot, "/conf.d/*.toml")+`"] |
| 60 | +version = 2 |
| 61 | +`, topLevel.String()) |
| 62 | + require.FileExists(t, filepath.Join(testRoot, "/conf.d/99-nvidia.toml")) |
| 63 | + dropIn, err := toml.FromFile(filepath.Join(testRoot, "/conf.d/99-nvidia.toml")).Load() |
| 64 | + require.NoError(t, err) |
| 65 | + require.EqualValues(t, `version = 2 |
| 66 | +
|
| 67 | +[plugins] |
| 68 | +
|
| 69 | + [plugins."io.containerd.grpc.v1.cri"] |
| 70 | +
|
| 71 | + [plugins."io.containerd.grpc.v1.cri".containerd] |
| 72 | +
|
| 73 | + [plugins."io.containerd.grpc.v1.cri".containerd.runtimes] |
| 74 | +
|
| 75 | + [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia] |
| 76 | + privileged_without_host_devices = false |
| 77 | + runtime_engine = "" |
| 78 | + runtime_root = "" |
| 79 | + runtime_type = "" |
| 80 | +
|
| 81 | + [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia.options] |
| 82 | + BinaryName = "nvidia-container-runtime" |
| 83 | +
|
| 84 | + [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia-cdi] |
| 85 | + privileged_without_host_devices = false |
| 86 | + runtime_engine = "" |
| 87 | + runtime_root = "" |
| 88 | + runtime_type = "" |
| 89 | +
|
| 90 | + [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia-cdi.options] |
| 91 | + BinaryName = "nvidia-container-runtime.cdi" |
| 92 | +
|
| 93 | + [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia-legacy] |
| 94 | + privileged_without_host_devices = false |
| 95 | + runtime_engine = "" |
| 96 | + runtime_root = "" |
| 97 | + runtime_type = "" |
| 98 | +
|
| 99 | + [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia-legacy.options] |
| 100 | + BinaryName = "nvidia-container-runtime.legacy" |
| 101 | +`, dropIn.String()) |
| 102 | + return nil |
| 103 | + }, |
| 104 | + assertCleanupPostConditions: func(t *testing.T, testRoot string) error { |
| 105 | + require.NoFileExists(t, filepath.Join(testRoot, "/etc/containerd/config.toml")) |
| 106 | + require.NoFileExists(t, filepath.Join(testRoot, "/conf.d/99-nvidia.toml")) |
| 107 | + return nil |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tc := range testCases { |
| 113 | + // TODO: Add tests for restart mode. |
| 114 | + tc.containerOptions.RestartMode = "none" |
| 115 | + t.Run(tc.description, func(t *testing.T) { |
| 116 | + // Update any paths as required: |
| 117 | + testRoot := t.TempDir() |
| 118 | + tc.containerOptions.Config = strings.ReplaceAll(tc.containerOptions.Config, "{{ .testRoot }}", testRoot) |
| 119 | + tc.containerOptions.DropInConfig = strings.ReplaceAll(tc.containerOptions.DropInConfig, "{{ .testRoot }}", testRoot) |
| 120 | + |
| 121 | + // Prepare the test environment |
| 122 | + if tc.prepareEnvironment != nil { |
| 123 | + require.NoError(t, tc.prepareEnvironment(t, testRoot)) |
| 124 | + } |
| 125 | + |
| 126 | + err := Setup(c, &tc.containerOptions, &tc.options) |
| 127 | + require.EqualValues(t, tc.expectedSetupError, err) |
| 128 | + |
| 129 | + // TODO: Check state |
| 130 | + if tc.assertSetupPostConditions != nil { |
| 131 | + require.NoError(t, tc.assertSetupPostConditions(t, testRoot)) |
| 132 | + } |
| 133 | + |
| 134 | + err = Cleanup(c, &tc.containerOptions, &tc.options) |
| 135 | + require.EqualValues(t, tc.expectedCleanupError, err) |
| 136 | + |
| 137 | + if tc.assertCleanupPostConditions != nil { |
| 138 | + require.NoError(t, tc.assertCleanupPostConditions(t, testRoot)) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | + |
| 143 | +} |
| 144 | + |
23 | 145 | // func TestUpdateV2ConfigDefaultRuntime(t *testing.T) {
|
24 | 146 | // logger, _ := testlog.NewNullLogger()
|
25 | 147 | // const runtimeDir = "/test/runtime/dir"
|
|
0 commit comments