-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathcomponent_test.go
More file actions
769 lines (643 loc) · 20 KB
/
Copy pathcomponent_test.go
File metadata and controls
769 lines (643 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
package nccl
import (
"context"
"fmt"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "github.com/leptonai/gpud/api/v1"
"github.com/leptonai/gpud/components"
"github.com/leptonai/gpud/pkg/eventstore"
"github.com/leptonai/gpud/pkg/kmsg"
"github.com/leptonai/gpud/pkg/nvidia-query/nvml/device"
nvmllib "github.com/leptonai/gpud/pkg/nvidia-query/nvml/lib"
nvidiaproduct "github.com/leptonai/gpud/pkg/nvidia/product"
)
// mockEventBucket implements a mock for eventstore.Bucket
type mockEventBucket struct {
mock.Mock
}
func (m *mockEventBucket) Name() string {
args := m.Called()
return args.String(0)
}
func (m *mockEventBucket) Insert(ctx context.Context, event eventstore.Event) error {
args := m.Called(ctx, event)
return args.Error(0)
}
func (m *mockEventBucket) Find(ctx context.Context, event eventstore.Event) (*eventstore.Event, error) {
args := m.Called(ctx, event)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*eventstore.Event), args.Error(1)
}
func (m *mockEventBucket) Get(ctx context.Context, since time.Time) (eventstore.Events, error) {
args := m.Called(ctx, since)
return args.Get(0).(eventstore.Events), args.Error(1)
}
func (m *mockEventBucket) Latest(ctx context.Context) (*eventstore.Event, error) {
args := m.Called(ctx)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*eventstore.Event), args.Error(1)
}
func (m *mockEventBucket) Purge(ctx context.Context, beforeTimestamp int64) (int, error) {
args := m.Called(ctx, beforeTimestamp)
return args.Int(0), args.Error(1)
}
func (m *mockEventBucket) Close() {
m.Called()
}
// MockEventStore implements a mock for eventstore.Store
type MockEventStore struct {
mock.Mock
}
func (m *MockEventStore) Bucket(name string, opts ...eventstore.OpOption) (eventstore.Bucket, error) {
args := m.Called(name)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(eventstore.Bucket), args.Error(1)
}
// Complete implementation of nvidianvml.Instance for testing
type mockNVMLInstance struct {
mock.Mock
}
func (m *mockNVMLInstance) NVMLExists() bool {
args := m.Called()
return args.Bool(0)
}
func (m *mockNVMLInstance) Library() nvmllib.Library {
args := m.Called()
if args.Get(0) == nil {
return nil
}
return args.Get(0).(nvmllib.Library)
}
func (m *mockNVMLInstance) Devices() map[string]device.Device {
args := m.Called()
return args.Get(0).(map[string]device.Device)
}
func (m *mockNVMLInstance) ProductName() string {
args := m.Called()
return args.String(0)
}
func (m *mockNVMLInstance) Architecture() string {
args := m.Called()
return args.String(0)
}
func (m *mockNVMLInstance) Brand() string {
args := m.Called()
return args.String(0)
}
func (m *mockNVMLInstance) DriverVersion() string {
args := m.Called()
return args.String(0)
}
func (m *mockNVMLInstance) DriverMajor() int {
args := m.Called()
return args.Int(0)
}
func (m *mockNVMLInstance) CUDAVersion() string {
args := m.Called()
return args.String(0)
}
func (m *mockNVMLInstance) FabricManagerSupported() bool {
args := m.Called()
return args.Bool(0)
}
func (m *mockNVMLInstance) FabricStateSupported() bool {
args := m.Called()
return args.Bool(0)
}
func (m *mockNVMLInstance) GetMemoryErrorManagementCapabilities() nvidiaproduct.MemoryErrorManagementCapabilities {
args := m.Called()
return args.Get(0).(nvidiaproduct.MemoryErrorManagementCapabilities)
}
func (m *mockNVMLInstance) Shutdown() error {
args := m.Called()
return args.Error(0)
}
func (m *mockNVMLInstance) InitError() error {
args := m.Called()
return args.Error(0)
}
// TestCheck tests the Check method in various scenarios
func TestCheck(t *testing.T) {
t.Parallel()
t.Run("nil nvmlInstance", func(t *testing.T) {
comp := &component{
nvmlInstance: nil,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeHealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "NVIDIA NVML instance is nil")
})
t.Run("nvml exists but no product name", func(t *testing.T) {
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(true)
mockNvml.On("InitError").Return(nil)
mockNvml.On("ProductName").Return("")
comp := &component{
nvmlInstance: mockNvml,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeHealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "NVIDIA NVML is loaded but GPU is not detected")
})
t.Run("nvml does not exist", func(t *testing.T) {
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(false)
comp := &component{
nvmlInstance: mockNvml,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeHealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "NVIDIA NVML library is not loaded")
})
t.Run("nil readAllKmsg", func(t *testing.T) {
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(true)
mockNvml.On("InitError").Return(nil)
mockNvml.On("ProductName").Return("Test GPU")
mockNvml.On("Devices").Return(map[string]device.Device{})
mockNvml.On("GetMemoryErrorManagementCapabilities").Return(nvidiaproduct.MemoryErrorManagementCapabilities{})
comp := &component{
nvmlInstance: mockNvml,
readAllKmsg: nil,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeHealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "kmsg reader is not set")
})
t.Run("readAllKmsg returns error", func(t *testing.T) {
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(true)
mockNvml.On("InitError").Return(nil)
mockNvml.On("ProductName").Return("Test GPU")
mockNvml.On("Devices").Return(map[string]device.Device{})
mockNvml.On("GetMemoryErrorManagementCapabilities").Return(nvidiaproduct.MemoryErrorManagementCapabilities{})
mockReadAllKmsg := func(ctx context.Context) ([]kmsg.Message, error) {
return nil, assert.AnError
}
comp := &component{
ctx: context.Background(),
nvmlInstance: mockNvml,
readAllKmsg: mockReadAllKmsg,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeUnhealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "failed to read kmsg")
})
t.Run("no matching messages", func(t *testing.T) {
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(true)
mockNvml.On("InitError").Return(nil)
mockNvml.On("ProductName").Return("Test GPU")
mockNvml.On("Devices").Return(map[string]device.Device{})
mockNvml.On("GetMemoryErrorManagementCapabilities").Return(nvidiaproduct.MemoryErrorManagementCapabilities{})
mockReadAllKmsg := func(ctx context.Context) ([]kmsg.Message, error) {
return []kmsg.Message{
{
Message: "non-matching message",
},
}, nil
}
comp := &component{
ctx: context.Background(),
nvmlInstance: mockNvml,
readAllKmsg: mockReadAllKmsg,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeHealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "scanned kmsg(s)")
})
t.Run("with matching messages", func(t *testing.T) {
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(true)
mockNvml.On("InitError").Return(nil)
mockNvml.On("ProductName").Return("Test GPU")
mockNvml.On("Devices").Return(map[string]device.Device{})
mockNvml.On("GetMemoryErrorManagementCapabilities").Return(nvidiaproduct.MemoryErrorManagementCapabilities{})
mockReadAllKmsg := func(ctx context.Context) ([]kmsg.Message, error) {
return []kmsg.Message{
{
Message: "non-matching message",
},
{
Message: "segfault at 123 in libnccl.so.2",
},
}, nil
}
comp := &component{
ctx: context.Background(),
nvmlInstance: mockNvml,
readAllKmsg: mockReadAllKmsg,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeHealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "scanned kmsg(s)")
data, ok := result.(*checkResult)
assert.True(t, ok)
assert.Len(t, data.MatchedKmsgs, 1)
assert.Equal(t, "segfault at 123 in libnccl.so.2", data.MatchedKmsgs[0].Message)
})
}
// TestDataMethods tests the methods of the Data struct
func TestDataMethods(t *testing.T) {
t.Parallel()
t.Run("nil data", func(t *testing.T) {
var cr *checkResult
assert.Equal(t, "", cr.String())
assert.Equal(t, "", cr.Summary())
assert.Equal(t, apiv1.HealthStateType(""), cr.HealthStateType())
assert.Equal(t, "", cr.getError())
states := cr.HealthStates()
require.Len(t, states, 1)
assert.Equal(t, apiv1.HealthStateTypeHealthy, states[0].Health)
assert.Equal(t, "no data yet", states[0].Reason)
})
t.Run("empty data", func(t *testing.T) {
cr := &checkResult{
health: apiv1.HealthStateTypeHealthy,
reason: "test reason",
}
assert.Equal(t, "matched 0 kmsg(s)", cr.String())
assert.Equal(t, "test reason", cr.Summary())
assert.Equal(t, apiv1.HealthStateTypeHealthy, cr.HealthStateType())
assert.Equal(t, "", cr.getError())
states := cr.HealthStates()
require.Len(t, states, 1)
assert.Equal(t, apiv1.HealthStateTypeHealthy, states[0].Health)
assert.Equal(t, "test reason", states[0].Reason)
})
t.Run("data with matched kmsg(s)", func(t *testing.T) {
cr := &checkResult{
MatchedKmsgs: []kmsg.Message{
{Message: "test message 1"},
{Message: "test message 2"},
},
health: apiv1.HealthStateTypeUnhealthy,
reason: "matched kmsg(s)",
err: assert.AnError,
}
assert.Equal(t, "matched 2 kmsg(s)", cr.String())
assert.Equal(t, "matched kmsg(s)", cr.Summary())
assert.Equal(t, apiv1.HealthStateTypeUnhealthy, cr.HealthStateType())
assert.Equal(t, assert.AnError.Error(), cr.getError())
states := cr.HealthStates()
require.Len(t, states, 1)
assert.Equal(t, apiv1.HealthStateTypeUnhealthy, states[0].Health)
assert.Equal(t, "matched kmsg(s)", states[0].Reason)
assert.Equal(t, assert.AnError.Error(), states[0].Error)
})
}
func TestComponentName(t *testing.T) {
t.Parallel()
comp := &component{}
assert.Equal(t, Name, comp.Name())
}
func TestComponentStart(t *testing.T) {
t.Parallel()
comp := &component{}
err := comp.Start()
assert.NoError(t, err)
}
func TestComponentStates(t *testing.T) {
t.Parallel()
comp := &component{}
states := comp.LastHealthStates()
assert.Len(t, states, 1)
assert.Equal(t, apiv1.HealthStateTypeHealthy, states[0].Health)
assert.Equal(t, "no issue", states[0].Reason)
}
func TestComponentEvents(t *testing.T) {
t.Parallel()
// Create mock event bucket
mockEventBucket := new(mockEventBucket)
testTime := time.Now()
testEvents := eventstore.Events{
{
Time: testTime,
Name: "test-nccl-error",
Type: "Warning",
Message: "This is a test NCCL error",
},
}
mockEventBucket.On("Get", mock.Anything, mock.Anything).Return(testEvents, nil)
mockEventBucket.On("Close").Return()
comp := &component{
eventBucket: mockEventBucket,
}
// Call Events
ctx := context.Background()
since := time.Now().Add(-1 * time.Hour)
events, err := comp.Events(ctx, since)
// Verify results
assert.NoError(t, err)
require.NotNil(t, events)
assert.Len(t, events, 1)
assert.Equal(t, "test-nccl-error", events[0].Name)
assert.Equal(t, "This is a test NCCL error", events[0].Message)
mockEventBucket.AssertCalled(t, "Get", mock.Anything, since)
}
func TestComponentClose(t *testing.T) {
t.Parallel()
// Create mock event bucket
mockEventBucket := new(mockEventBucket)
mockEventBucket.On("Close").Return()
cancelCalled := false
comp := &component{
cancel: func() {
cancelCalled = true
},
eventBucket: mockEventBucket,
}
err := comp.Close()
assert.NoError(t, err)
mockEventBucket.AssertCalled(t, "Close")
assert.True(t, cancelCalled)
}
func TestEventsWithNilBucket(t *testing.T) {
t.Parallel()
comp := &component{
eventBucket: nil,
}
ctx := context.Background()
events, err := comp.Events(ctx, time.Now().Add(-1*time.Hour))
assert.NoError(t, err)
assert.Nil(t, events)
}
// TestMatchFunctionality tests the Match function in the component
func TestMatchFunctionality(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
input string
wantEvent string
wantMessage string
}{
{
name: "no match",
input: "some random log line",
wantEvent: "",
wantMessage: "",
},
{
name: "segfault in libnccl",
input: "kernel: process[12345]: segfault at 0x00 ip 0x00 sp 0x00 error 4 in libnccl.so.2",
wantEvent: "nvidia_nccl_segfault_in_libnccl",
wantMessage: "",
},
}
for _, tc := range testCases {
tc := tc // capture range variable
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
gotEvent, gotMessage := Match(tc.input)
assert.Equal(t, tc.wantEvent, gotEvent)
if tc.wantEvent != "" {
assert.NotEmpty(t, gotMessage)
}
})
}
}
func TestLastHealthStates(t *testing.T) {
t.Parallel()
// Create a minimal component instance for testing
c := &component{}
// Call the LastHealthStates method
healthStates := c.LastHealthStates()
// Assert that exactly one health state is returned
assert.Len(t, healthStates, 1)
// Assert that the health state has the expected values
assert.Equal(t, Name, healthStates[0].Component)
assert.Equal(t, apiv1.HealthStateTypeHealthy, healthStates[0].Health)
assert.Equal(t, "no issue", healthStates[0].Reason)
}
func TestCheckResult_HealthStates(t *testing.T) {
t.Parallel()
t.Run("nil check result", func(t *testing.T) {
var cr *checkResult
states := cr.HealthStates()
assert.Len(t, states, 1)
assert.Equal(t, Name, states[0].Component)
assert.Equal(t, Name, states[0].Name)
assert.Equal(t, apiv1.HealthStateTypeHealthy, states[0].Health)
assert.Equal(t, "no data yet", states[0].Reason)
})
t.Run("check result with no error", func(t *testing.T) {
timestamp := time.Now().UTC()
cr := &checkResult{
ts: timestamp,
health: apiv1.HealthStateTypeHealthy,
reason: "all good",
}
states := cr.HealthStates()
assert.Len(t, states, 1)
assert.Equal(t, Name, states[0].Component)
assert.Equal(t, Name, states[0].Name)
assert.Equal(t, apiv1.HealthStateTypeHealthy, states[0].Health)
assert.Equal(t, "all good", states[0].Reason)
assert.Empty(t, states[0].Error)
assert.Equal(t, metav1.NewTime(timestamp), states[0].Time)
})
t.Run("check result with error", func(t *testing.T) {
timestamp := time.Now().UTC()
cr := &checkResult{
ts: timestamp,
health: apiv1.HealthStateTypeUnhealthy,
reason: "something went wrong",
err: assert.AnError,
}
states := cr.HealthStates()
assert.Len(t, states, 1)
assert.Equal(t, Name, states[0].Component)
assert.Equal(t, Name, states[0].Name)
assert.Equal(t, apiv1.HealthStateTypeUnhealthy, states[0].Health)
assert.Equal(t, "something went wrong", states[0].Reason)
assert.Equal(t, assert.AnError.Error(), states[0].Error)
assert.Equal(t, metav1.NewTime(timestamp), states[0].Time)
})
}
func TestCheck_NVML_NotExists(t *testing.T) {
t.Parallel()
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(false)
comp := &component{
nvmlInstance: mockNvml,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeHealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "NVIDIA NVML library is not loaded")
}
func TestCheck_NVML_NoProductName(t *testing.T) {
t.Parallel()
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(true)
mockNvml.On("InitError").Return(nil)
mockNvml.On("ProductName").Return("")
comp := &component{
nvmlInstance: mockNvml,
}
result := comp.Check()
assert.NotNil(t, result)
assert.Equal(t, apiv1.HealthStateTypeHealthy, result.HealthStateType())
assert.Contains(t, result.Summary(), "NVIDIA NVML is loaded but GPU is not detected")
}
func TestNew(t *testing.T) {
t.Parallel()
t.Run("nil event store", func(t *testing.T) {
instance := &components.GPUdInstance{
RootCtx: context.Background(),
NVMLInstance: nil,
EventStore: nil,
}
comp, err := New(instance)
assert.NoError(t, err)
assert.NotNil(t, comp)
assert.Equal(t, Name, comp.Name())
// Clean up
err = comp.Close()
assert.NoError(t, err)
})
t.Run("event store bucket error", func(t *testing.T) {
// Skip this test on non-Linux platforms as the code has Linux-specific paths
if runtime.GOOS != "linux" {
t.Skip("Skipping on non-Linux platform")
return
}
// Create a mock store that returns an error when Bucket is called
mockStore := new(MockEventStore)
mockStore.On("Bucket", Name).Return(nil, fmt.Errorf("test bucket error"))
instance := &components.GPUdInstance{
RootCtx: context.Background(),
NVMLInstance: nil,
EventStore: mockStore,
}
// Call New - should return an error
comp, err := New(instance)
assert.Error(t, err)
assert.Nil(t, comp)
assert.Contains(t, err.Error(), "test bucket error")
mockStore.AssertExpectations(t)
})
t.Run("with event store", func(t *testing.T) {
// Skip this test on non-Linux platforms as the code has Linux-specific paths
if runtime.GOOS != "linux" {
t.Skip("Skipping on non-Linux platform")
}
// Create a real instance but with mock event store
mockStore := new(MockEventStore)
mockBucket := new(mockEventBucket)
mockStore.On("Bucket", Name).Return(mockBucket, nil)
// We won't verify calls to avoid timing issues in tests
mockBucket.On("Close").Maybe().Return()
instance := &components.GPUdInstance{
RootCtx: context.Background(),
NVMLInstance: nil,
EventStore: mockStore,
}
// This may create a kmsg.Syncer depending on runtime conditions
// so we'll just test that New succeeds and not worry about mocks
comp, err := New(instance)
if !assert.NoError(t, err) {
return
}
assert.NotNil(t, comp)
// Validate the component has expected properties
c, ok := comp.(*component)
assert.True(t, ok)
// Based on OS and euid, eventBucket might be set
if runtime.GOOS == "linux" {
assert.NotNil(t, c.eventBucket)
}
// We won't verify the mock calls as they depend on the runtime environment
// Just ensure Close doesn't return an error
err = comp.Close()
assert.NoError(t, err)
})
}
func TestCheckResult_GetError(t *testing.T) {
t.Parallel()
t.Run("nil check result", func(t *testing.T) {
var cr *checkResult
errStr := cr.getError()
assert.Empty(t, errStr)
})
t.Run("check result with nil error", func(t *testing.T) {
cr := &checkResult{
err: nil,
}
errStr := cr.getError()
assert.Empty(t, errStr)
})
t.Run("check result with error", func(t *testing.T) {
testErr := fmt.Errorf("test error")
cr := &checkResult{
err: testErr,
}
errStr := cr.getError()
assert.Equal(t, "test error", errStr)
})
}
// TestTags tests the component's Tags() method
func TestTags(t *testing.T) {
t.Parallel()
comp := &component{}
expectedTags := []string{
"accelerator",
"gpu",
"nvidia",
Name,
}
tags := comp.Tags()
assert.Equal(t, expectedTags, tags, "Component tags should match expected values")
assert.Len(t, tags, 4, "Component should return exactly 4 tags")
}
// TestIsSupported tests the component's IsSupported() method
func TestIsSupported(t *testing.T) {
t.Parallel()
// Test when nvmlInstance is nil
comp := &component{
nvmlInstance: nil,
}
assert.False(t, comp.IsSupported())
// Test when NVMLExists returns false
mockNvml := new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(false)
comp = &component{
nvmlInstance: mockNvml,
}
assert.False(t, comp.IsSupported())
// Test when ProductName returns empty string
mockNvml = new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(true)
mockNvml.On("ProductName").Return("")
comp = &component{
nvmlInstance: mockNvml,
}
assert.False(t, comp.IsSupported())
// Test when all conditions are met
mockNvml = new(mockNVMLInstance)
mockNvml.On("NVMLExists").Return(true)
mockNvml.On("ProductName").Return("Tesla V100")
comp = &component{
nvmlInstance: mockNvml,
}
assert.True(t, comp.IsSupported())
}