Skip to content

Commit 19c7b36

Browse files
[no-relnote] Add unittest for cmd/ctk/installer/container/crio
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent b5105a9 commit 19c7b36

File tree

2 files changed

+721
-119
lines changed

2 files changed

+721
-119
lines changed

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

Lines changed: 59 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -33,75 +33,113 @@ const (
3333
runtimeDir = "/test/runtime/dir"
3434
)
3535

36-
// TestUpdateV2Config_NoConfigFile tests the scenario when there is no
37-
// containerd config file present
38-
func TestUpdateV2Config_NoConfigFile(t *testing.T) {
39-
logger, _ := testlog.NewNullLogger()
36+
// testCase represents a test case configuration used across all
37+
// UpdateV2Config test functions. Each field controls a specific aspect
38+
// of the nvidia runtime configuration to test different permutations.
39+
type testCase struct {
40+
// name provides a descriptive identifier for the test case that clearly
41+
// states the configuration being tested
42+
name string
43+
// runtimeName is the name of the nvidia runtime to configure (e.g., "nvidia"
44+
// or a custom name like "gpu-runtime")
45+
runtimeName string
46+
// enableCDI controls whether CDI (Container Device Interface) support should
47+
// be enabled in the containerd configuration
48+
enableCDI bool
49+
// setAsDefault controls whether the nvidia runtime should be set as the
50+
// default runtime for containerd
51+
setAsDefault bool
52+
// withRunc controls whether a runc runtime exists in the configuration that
53+
// the nvidia runtime can inherit settings from
54+
withRunc bool
55+
}
4056

41-
testCases := []struct {
42-
name string
43-
runtimeName string
44-
enableCDI bool
45-
setAsDefault bool
46-
withRunc bool
47-
}{
57+
// getSharedTestCases returns a comprehensive set of test cases that validate
58+
// the core functionality of nvidia runtime configuration across different
59+
// containerd config scenarios.
60+
//
61+
// These test cases are designed to test the same functionality in three
62+
// different contexts:
63+
// - No existing config file (TestUpdateV2Config_NoConfigFile)
64+
// - Existing config without nvidia entries (TestUpdateV2Config_ExistingConfigWithoutNvidia)
65+
// - Existing config with nvidia entries (TestUpdateV2Config_ExistingConfigWithNvidia)
66+
//
67+
// Each test case represents a specific permutation of configuration options:
68+
// - Runtime name: "nvidia" (standard) or custom names
69+
// - CDI enablement: enabled or disabled
70+
// - Default runtime: whether to set the nvidia runtime as default
71+
// - Runc inheritance: whether runc runtime exists to inherit settings from
72+
//
73+
// This approach ensures consistent test coverage across all scenarios and makes
74+
// it easy to verify that the same feature works correctly regardless of the
75+
// initial state of the containerd configuration.
76+
func getSharedTestCases() []testCase {
77+
return []testCase{
4878
{
49-
name: "basic nvidia runtime",
79+
name: "nvidia runtime without CDI, not default, no runc",
5080
runtimeName: "nvidia",
5181
enableCDI: false,
5282
setAsDefault: false,
5383
withRunc: false,
5484
},
5585
{
56-
name: "nvidia runtime with CDI enabled",
86+
name: "nvidia runtime with CDI enabled, not default, no runc",
5787
runtimeName: "nvidia",
5888
enableCDI: true,
5989
setAsDefault: false,
6090
withRunc: false,
6191
},
6292
{
63-
name: "nvidia runtime as default",
93+
name: "nvidia runtime without CDI, set as default, no runc",
6494
runtimeName: "nvidia",
6595
enableCDI: false,
6696
setAsDefault: true,
6797
withRunc: false,
6898
},
6999
{
70-
name: "nvidia runtime with CDI and as default",
100+
name: "nvidia runtime with CDI enabled, set as default, no runc",
71101
runtimeName: "nvidia",
72102
enableCDI: true,
73103
setAsDefault: true,
74104
withRunc: false,
75105
},
76106
{
77-
name: "custom runtime name",
107+
name: "custom runtime (CUSTOM) without CDI, not default, no runc",
78108
runtimeName: "CUSTOM",
79109
enableCDI: false,
80110
setAsDefault: false,
81111
withRunc: false,
82112
},
83113
{
84-
name: "custom runtime with CDI and as default",
114+
name: "custom runtime (CUSTOM) with CDI enabled, set as default, no runc",
85115
runtimeName: "CUSTOM",
86116
enableCDI: true,
87117
setAsDefault: true,
88118
withRunc: false,
89119
},
90120
{
91-
name: "nvidia runtime with runc present",
121+
name: "nvidia runtime without CDI, not default, with runc",
92122
runtimeName: "nvidia",
93123
enableCDI: false,
94124
setAsDefault: false,
95125
withRunc: true,
96126
},
97127
{
98-
name: "nvidia runtime with runc, CDI and as default",
128+
name: "nvidia runtime with CDI enabled, set as default, with runc",
99129
runtimeName: "nvidia",
100130
enableCDI: true,
101131
setAsDefault: true,
102132
withRunc: true,
103133
},
104134
}
135+
}
136+
137+
// TestUpdateV2Config_NoConfigFile tests the scenario when there is no
138+
// containerd config file present
139+
func TestUpdateV2Config_NoConfigFile(t *testing.T) {
140+
logger, _ := testlog.NewNullLogger()
141+
142+
testCases := getSharedTestCases()
105143

106144
for _, tc := range testCases {
107145
t.Run(tc.name, func(t *testing.T) {
@@ -218,56 +256,7 @@ func TestUpdateV2Config_ExistingConfigWithoutNvidia(t *testing.T) {
218256
},
219257
}
220258

221-
testCases := []struct {
222-
name string
223-
runtimeName string
224-
enableCDI bool
225-
setAsDefault bool
226-
withRunc bool
227-
}{
228-
{
229-
name: "add nvidia runtime to existing config",
230-
runtimeName: "nvidia",
231-
enableCDI: false,
232-
setAsDefault: false,
233-
withRunc: false,
234-
},
235-
{
236-
name: "add nvidia runtime with CDI to existing config",
237-
runtimeName: "nvidia",
238-
enableCDI: true,
239-
setAsDefault: false,
240-
withRunc: false,
241-
},
242-
{
243-
name: "add nvidia runtime as default to existing config",
244-
runtimeName: "nvidia",
245-
enableCDI: false,
246-
setAsDefault: true,
247-
withRunc: false,
248-
},
249-
{
250-
name: "add custom runtime with all features to existing config",
251-
runtimeName: "gpu-runtime",
252-
enableCDI: true,
253-
setAsDefault: true,
254-
withRunc: false,
255-
},
256-
{
257-
name: "add nvidia runtime to existing config with runc",
258-
runtimeName: "nvidia",
259-
enableCDI: false,
260-
setAsDefault: false,
261-
withRunc: true,
262-
},
263-
{
264-
name: "add nvidia runtime with all features and runc",
265-
runtimeName: "nvidia",
266-
enableCDI: true,
267-
setAsDefault: true,
268-
withRunc: true,
269-
},
270-
}
259+
testCases := getSharedTestCases()
271260

272261
for _, tc := range testCases {
273262
t.Run(tc.name, func(t *testing.T) {
@@ -391,56 +380,7 @@ func TestUpdateV2Config_ExistingConfigWithNvidia(t *testing.T) {
391380
},
392381
}
393382

394-
testCases := []struct {
395-
name string
396-
runtimeName string
397-
enableCDI bool
398-
setAsDefault bool
399-
withRunc bool
400-
}{
401-
{
402-
name: "update existing nvidia runtime",
403-
runtimeName: "nvidia",
404-
enableCDI: false,
405-
setAsDefault: false,
406-
withRunc: false,
407-
},
408-
{
409-
name: "update nvidia runtime with CDI",
410-
runtimeName: "nvidia",
411-
enableCDI: true,
412-
setAsDefault: false,
413-
withRunc: false,
414-
},
415-
{
416-
name: "update nvidia runtime as default",
417-
runtimeName: "nvidia",
418-
enableCDI: false,
419-
setAsDefault: true,
420-
withRunc: false,
421-
},
422-
{
423-
name: "add custom runtime alongside existing nvidia",
424-
runtimeName: "gpu-runtime",
425-
enableCDI: true,
426-
setAsDefault: true,
427-
withRunc: false,
428-
},
429-
{
430-
name: "update with runc present",
431-
runtimeName: "nvidia",
432-
enableCDI: false,
433-
setAsDefault: false,
434-
withRunc: true,
435-
},
436-
{
437-
name: "update all features with runc",
438-
runtimeName: "nvidia",
439-
enableCDI: true,
440-
setAsDefault: true,
441-
withRunc: true,
442-
},
443-
}
383+
testCases := getSharedTestCases()
444384

445385
for _, tc := range testCases {
446386
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)