Skip to content

Commit a780df0

Browse files
committed
Add SetupCleanup test for containerd
Signed-off-by: Evan Lezar <[email protected]>
1 parent 0314a59 commit a780df0

File tree

3 files changed

+132
-5
lines changed

3 files changed

+132
-5
lines changed

cmd/nvidia-ctk-installer/container/container.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ const (
3535

3636
// Options defines the shared options for the CLIs to configure containers runtimes.
3737
type Options struct {
38-
Config string
39-
Socket string
38+
DropInConfig string
39+
Config string
40+
Socket string
4041
// ExecutablePath specifies the path to the container runtime executable.
4142
// This is used to extract the current config, for example.
4243
// If a HostRootMount is specified, this path is relative to the host root
@@ -71,8 +72,12 @@ func (o Options) Unconfigure(cfg engine.Interface) error {
7172

7273
// flush flushes the specified config to disk
7374
func (o Options) flush(cfg engine.Interface) error {
74-
logrus.Infof("Flushing config to %v", o.Config)
75-
n, err := cfg.Save(o.Config)
75+
filepath := o.DropInConfig
76+
if filepath == "" {
77+
filepath = o.Config
78+
}
79+
logrus.Infof("Flushing config to %v", filepath)
80+
n, err := cfg.Save(filepath)
7681
if err != nil {
7782
return fmt.Errorf("unable to flush config: %v", err)
7883
}

cmd/nvidia-ctk-installer/container/runtime/containerd/config_v2_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,132 @@
1616

1717
package containerd
1818

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+
1931
const (
2032
runtimeType = "runtime_type"
2133
)
2234

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+
23145
// func TestUpdateV2ConfigDefaultRuntime(t *testing.T) {
24146
// logger, _ := testlog.NewNullLogger()
25147
// const runtimeDir = "/test/runtime/dir"

cmd/nvidia-ctk-installer/container/runtime/containerd/containerd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func GetLowlevelRuntimePaths(o *container.Options, co *Options) ([]string, error
170170

171171
func getRuntimeConfig(o *container.Options, co *Options) (engine.Interface, error) {
172172
return containerd.New(
173-
containerd.WithPath(o.Config),
173+
containerd.WithTopLevelConfigPath(o.Config),
174174
containerd.WithConfigSource(
175175
toml.LoadFirst(
176176
containerd.CommandLineSource(o.HostRootMount, o.ExecutablePath),

0 commit comments

Comments
 (0)