Skip to content

Commit 19dfc82

Browse files
committed
TOFIX: Proposal for TestCDIHookCreator_Create
Signed-off-by: Evan Lezar <[email protected]>
1 parent 95feacc commit 19dfc82

File tree

1 file changed

+64
-125
lines changed

1 file changed

+64
-125
lines changed

internal/discover/hooks_test.go

Lines changed: 64 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,17 @@ func TestNewHookCreator(t *testing.T) {
170170
}
171171
func TestCDIHookCreator_Create(t *testing.T) {
172172
testCases := []struct {
173-
name string
174-
nvidiaCDIHookPath string
175-
hookName HookName
176-
args []string
177-
disabledHooks []HookName
178-
enabledHooks []HookName
179-
debugLogging bool
180-
expectedHook *Hook
173+
name string
174+
hookCreator HookCreator
175+
hookName HookName
176+
args []string
177+
expectedHook *Hook
181178
}{
182179
{
183-
name: "CreateSymlinksHook with args",
184-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
185-
hookName: CreateSymlinksHook,
186-
args: []string{"/source:/target", "/source2:/target2"},
180+
name: "CreateSymlinksHook with args",
181+
hookCreator: NewHookCreator(),
182+
hookName: CreateSymlinksHook,
183+
args: []string{"/source:/target", "/source2:/target2"},
187184
expectedHook: &Hook{
188185
Lifecycle: "createContainer",
189186
Path: "/usr/bin/nvidia-cdi-hook",
@@ -192,18 +189,19 @@ func TestCDIHookCreator_Create(t *testing.T) {
192189
},
193190
},
194191
{
195-
name: "CreateSymlinksHook without args returns nil",
196-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
197-
hookName: CreateSymlinksHook,
198-
args: []string{},
199-
expectedHook: nil,
192+
name: "CreateSymlinksHook without args returns nil",
193+
hookCreator: NewHookCreator(),
194+
hookName: CreateSymlinksHook,
195+
args: []string{},
196+
expectedHook: nil,
200197
},
201198
{
202-
name: "ChmodHook with args (when enabled)",
203-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
204-
hookName: ChmodHook,
205-
args: []string{"/path/to/file1", "/path/to/file2"},
206-
enabledHooks: []HookName{ChmodHook}, // Enable ChmodHook
199+
name: "ChmodHook with args (when enabled)",
200+
hookCreator: NewHookCreator(
201+
WithEnabledHooks(ChmodHook),
202+
),
203+
hookName: ChmodHook,
204+
args: []string{"/path/to/file1", "/path/to/file2"},
207205
expectedHook: &Hook{
208206
Lifecycle: "createContainer",
209207
Path: "/usr/bin/nvidia-cdi-hook",
@@ -212,17 +210,17 @@ func TestCDIHookCreator_Create(t *testing.T) {
212210
},
213211
},
214212
{
215-
name: "ChmodHook disabled by default returns nil",
216-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
217-
hookName: ChmodHook,
218-
args: []string{"/path/to/file"},
219-
expectedHook: nil, // ChmodHook is disabled by default
213+
name: "ChmodHook disabled by default returns nil",
214+
hookCreator: NewHookCreator(),
215+
hookName: ChmodHook,
216+
args: []string{"/path/to/file"},
217+
expectedHook: nil,
220218
},
221219
{
222-
name: "UpdateLDCacheHook with no args",
223-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
224-
hookName: UpdateLDCacheHook,
225-
args: []string{},
220+
name: "UpdateLDCacheHook with no args",
221+
hookCreator: NewHookCreator(),
222+
hookName: UpdateLDCacheHook,
223+
args: []string{},
226224
expectedHook: &Hook{
227225
Lifecycle: "createContainer",
228226
Path: "/usr/bin/nvidia-cdi-hook",
@@ -231,10 +229,10 @@ func TestCDIHookCreator_Create(t *testing.T) {
231229
},
232230
},
233231
{
234-
name: "UpdateLDCacheHook with args",
235-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
236-
hookName: UpdateLDCacheHook,
237-
args: []string{"--folder", "/usr/lib64"},
232+
name: "UpdateLDCacheHook with args",
233+
hookCreator: NewHookCreator(),
234+
hookName: UpdateLDCacheHook,
235+
args: []string{"--folder", "/usr/lib64"},
238236
expectedHook: &Hook{
239237
Lifecycle: "createContainer",
240238
Path: "/usr/bin/nvidia-cdi-hook",
@@ -243,10 +241,10 @@ func TestCDIHookCreator_Create(t *testing.T) {
243241
},
244242
},
245243
{
246-
name: "EnableCudaCompatHook",
247-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
248-
hookName: EnableCudaCompatHook,
249-
args: []string{"--root", "/some/root"},
244+
name: "EnableCudaCompatHook",
245+
hookCreator: NewHookCreator(),
246+
hookName: EnableCudaCompatHook,
247+
args: []string{"--root", "/some/root"},
250248
expectedHook: &Hook{
251249
Lifecycle: "createContainer",
252250
Path: "/usr/bin/nvidia-cdi-hook",
@@ -255,10 +253,10 @@ func TestCDIHookCreator_Create(t *testing.T) {
255253
},
256254
},
257255
{
258-
name: "DisableDeviceNodeModificationHook",
259-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
260-
hookName: DisableDeviceNodeModificationHook,
261-
args: []string{},
256+
name: "DisableDeviceNodeModificationHook",
257+
hookCreator: NewHookCreator(),
258+
hookName: DisableDeviceNodeModificationHook,
259+
args: []string{},
262260
expectedHook: &Hook{
263261
Lifecycle: "createContainer",
264262
Path: "/usr/bin/nvidia-cdi-hook",
@@ -267,10 +265,12 @@ func TestCDIHookCreator_Create(t *testing.T) {
267265
},
268266
},
269267
{
270-
name: "nvidia-ctk binary uses different args format",
271-
nvidiaCDIHookPath: "/usr/bin/nvidia-ctk",
272-
hookName: UpdateLDCacheHook,
273-
args: []string{},
268+
name: "nvidia-ctk binary uses different args format",
269+
hookCreator: NewHookCreator(
270+
WithNVIDIACDIHookPath("/usr/bin/nvidia-ctk"),
271+
),
272+
hookName: UpdateLDCacheHook,
273+
args: []string{},
274274
expectedHook: &Hook{
275275
Lifecycle: "createContainer",
276276
Path: "/usr/bin/nvidia-ctk",
@@ -279,43 +279,21 @@ func TestCDIHookCreator_Create(t *testing.T) {
279279
},
280280
},
281281
{
282-
name: "nvidia-ctk in /usr/sbin (RPM-based systems)",
283-
nvidiaCDIHookPath: "/usr/sbin/nvidia-ctk",
284-
hookName: CreateSymlinksHook,
285-
args: []string{"/source:/target"},
286-
expectedHook: &Hook{
287-
Lifecycle: "createContainer",
288-
Path: "/usr/sbin/nvidia-ctk",
289-
Args: []string{"nvidia-ctk", "hook", "create-symlinks", "--link", "/source:/target"},
290-
Env: []string{"NVIDIA_CTK_DEBUG=false"},
291-
},
282+
name: "hook disabled when in disabledHooks list",
283+
hookCreator: NewHookCreator(
284+
WithDisabledHooks(UpdateLDCacheHook),
285+
),
286+
hookName: UpdateLDCacheHook,
287+
args: []string{},
288+
expectedHook: nil,
292289
},
293290
{
294-
name: "nvidia-ctk in custom NVIDIA toolkit path",
295-
nvidiaCDIHookPath: "/usr/local/nvidia/toolkit/nvidia-ctk",
296-
hookName: EnableCudaCompatHook,
297-
args: []string{"--root", "/some/root"},
298-
expectedHook: &Hook{
299-
Lifecycle: "createContainer",
300-
Path: "/usr/local/nvidia/toolkit/nvidia-ctk",
301-
Args: []string{"nvidia-ctk", "hook", "enable-cuda-compat", "--root", "/some/root"},
302-
Env: []string{"NVIDIA_CTK_DEBUG=false"},
303-
},
304-
},
305-
{
306-
name: "hook disabled when in disabledHooks list",
307-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
308-
hookName: UpdateLDCacheHook,
309-
args: []string{},
310-
disabledHooks: []HookName{UpdateLDCacheHook},
311-
expectedHook: nil,
312-
},
313-
{
314-
name: "debug logging enabled",
315-
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
316-
hookName: UpdateLDCacheHook,
317-
args: []string{},
318-
debugLogging: true,
291+
name: "debug logging enabled",
292+
hookCreator: NewHookCreator(
293+
WithDebugLogging(true),
294+
),
295+
hookName: UpdateLDCacheHook,
296+
args: []string{},
319297
expectedHook: &Hook{
320298
Lifecycle: "createContainer",
321299
Path: "/usr/bin/nvidia-cdi-hook",
@@ -327,23 +305,7 @@ func TestCDIHookCreator_Create(t *testing.T) {
327305

328306
for _, tc := range testCases {
329307
t.Run(tc.name, func(t *testing.T) {
330-
opts := []Option{
331-
WithNVIDIACDIHookPath(tc.nvidiaCDIHookPath),
332-
}
333-
if len(tc.disabledHooks) > 0 {
334-
opts = append(opts, WithDisabledHooks(tc.disabledHooks...))
335-
}
336-
if len(tc.enabledHooks) > 0 {
337-
opts = append(opts, WithEnabledHooks(tc.enabledHooks...))
338-
}
339-
340-
hookCreator := NewHookCreator(opts...)
341-
// Set debug logging if needed
342-
if creator, ok := hookCreator.(*cdiHookCreator); ok {
343-
creator.debugLogging = tc.debugLogging
344-
}
345-
346-
hook := hookCreator.Create(tc.hookName, tc.args...)
308+
hook := tc.hookCreator.Create(tc.hookName, tc.args...)
347309
require.Equal(t, tc.expectedHook, hook)
348310
})
349311
}
@@ -501,34 +463,11 @@ func TestCDIHookCreator_isDisabled(t *testing.T) {
501463

502464
for _, tt := range tests {
503465
t.Run(tt.name, func(t *testing.T) {
504-
o := &hookCreatorOptions{
505-
nvidiaCDIHookPath: defaultNvidiaCDIHookPath,
506-
}
507-
opts := []Option{
466+
testCreator := NewHookCreator(
508467
WithDisabledHooks(tt.disabledHooks...),
509468
WithEnabledHooks(tt.enabledHooks...),
510-
}
511-
for _, opt := range opts {
512-
opt(o)
513-
}
514-
o.disabledHooks = append(o.disabledHooks, defaultDisabledHooks...)
515-
516-
disabledHooks := make(map[HookName]bool)
517-
for _, h := range o.disabledHooks {
518-
disabledHooks[h] = true
519-
}
520-
521-
for _, h := range o.enabledHooks {
522-
disabledHooks[h] = false
523-
}
524-
525-
testCreator := &cdiHookCreator{
526-
nvidiaCDIHookPath: o.nvidiaCDIHookPath,
527-
disabledHooks: disabledHooks,
528-
fixedArgs: getFixedArgsForCDIHookCLI(o.nvidiaCDIHookPath),
529-
}
530-
531-
require.Equal(t, testCreator.isDisabled(tt.hookName, tt.args...), tt.expectedResult)
469+
)
470+
require.Equal(t, testCreator.(*cdiHookCreator).isDisabled(tt.hookName, tt.args...), tt.expectedResult)
532471
})
533472
}
534473
}

0 commit comments

Comments
 (0)