-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachineid_internal_test.go
More file actions
495 lines (423 loc) · 15.8 KB
/
machineid_internal_test.go
File metadata and controls
495 lines (423 loc) · 15.8 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
package machineid
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"testing"
)
// TestHashIdentifiersEmpty tests hashing with empty identifiers.
func TestHashIdentifiersEmpty(t *testing.T) {
result := hashIdentifiers([]string{}, "", Format64)
if len(result) != 64 {
t.Errorf("Expected 64-character hash, got %d", len(result))
}
}
// TestHashIdentifiersSorting tests that identifiers are sorted before hashing.
func TestHashIdentifiersSorting(t *testing.T) {
ids1 := []string{"cpu:intel", "uuid:123"}
ids2 := []string{"uuid:123", "cpu:intel"}
hash1 := hashIdentifiers(ids1, "test", Format64)
hash2 := hashIdentifiers(ids2, "test", Format64)
if hash1 != hash2 {
t.Error("Hash should be same regardless of input order")
}
}
// TestHashIdentifiersWithoutSalt tests hashing without salt.
func TestHashIdentifiersWithoutSalt(t *testing.T) {
ids := []string{"test1", "test2"}
withSalt := hashIdentifiers(ids, "mysalt", Format64)
withoutSalt := hashIdentifiers(ids, "", Format64)
if withSalt == withoutSalt {
t.Error("Hash with salt should differ from hash without salt")
}
}
// TestAppendIdentifierIfValidEmpty tests with empty value.
func TestAppendIdentifierIfValidEmpty(t *testing.T) {
diag := &DiagnosticInfo{Errors: make(map[string]error)}
getValue := func() (string, error) {
return "", nil
}
result := appendIdentifierIfValid([]string{"existing"}, getValue, "prefix:", diag, "test", nil)
if len(result) != 1 {
t.Errorf("Expected 1 identifier, got %d", len(result))
}
diagErr, ok := diag.Errors["test"]
if !ok {
t.Fatal("Expected error recorded in diagnostics for empty value")
}
if !errors.Is(diagErr, ErrEmptyValue) {
t.Errorf("Expected ErrEmptyValue in diagnostic, got %v", diagErr)
}
var compErr *ComponentError
if !errors.As(diagErr, &compErr) {
t.Fatal("Expected ComponentError in diagnostic")
}
if compErr.Component != "test" {
t.Errorf("ComponentError.Component = %q, want %q", compErr.Component, "test")
}
}
// TestAppendIdentifierIfValidError tests with error.
func TestAppendIdentifierIfValidError(t *testing.T) {
diag := &DiagnosticInfo{Errors: make(map[string]error)}
getValue := func() (string, error) {
return "", fmt.Errorf("test error")
}
result := appendIdentifierIfValid([]string{"existing"}, getValue, "prefix:", diag, "test", nil)
if len(result) != 1 {
t.Errorf("Expected 1 identifier (original), got %d", len(result))
}
diagErr, ok := diag.Errors["test"]
if !ok {
t.Fatal("Expected error recorded in diagnostics")
}
var compErr *ComponentError
if !errors.As(diagErr, &compErr) {
t.Fatal("Expected ComponentError in diagnostic")
}
if compErr.Component != "test" {
t.Errorf("ComponentError.Component = %q, want %q", compErr.Component, "test")
}
}
// TestAppendIdentifierIfValidSuccess tests with valid value.
func TestAppendIdentifierIfValidSuccess(t *testing.T) {
diag := &DiagnosticInfo{Errors: make(map[string]error)}
getValue := func() (string, error) {
return "good-value", nil
}
result := appendIdentifierIfValid([]string{"existing"}, getValue, "prefix:", diag, "test", nil)
if len(result) != 2 {
t.Errorf("Expected 2 identifiers, got %d", len(result))
}
if len(diag.Collected) != 1 || diag.Collected[0] != "test" {
t.Errorf("Expected 'test' in collected, got %v", diag.Collected)
}
}
// TestAppendIdentifiersIfValidEmpty tests with empty array.
func TestAppendIdentifiersIfValidEmpty(t *testing.T) {
diag := &DiagnosticInfo{Errors: make(map[string]error)}
getValues := func() ([]string, error) {
return []string{}, nil
}
result := appendIdentifiersIfValid([]string{"existing"}, getValues, "prefix:", diag, "test", nil)
if len(result) != 1 {
t.Errorf("Expected 1 identifier, got %d", len(result))
}
}
// TestAppendIdentifiersIfValidError tests with error.
func TestAppendIdentifiersIfValidError(t *testing.T) {
diag := &DiagnosticInfo{Errors: make(map[string]error)}
getValues := func() ([]string, error) {
return nil, fmt.Errorf("test error")
}
result := appendIdentifiersIfValid([]string{"existing"}, getValues, "prefix:", diag, "test", nil)
if len(result) != 1 {
t.Errorf("Expected 1 identifier (original), got %d", len(result))
}
diagErr, ok := diag.Errors["test"]
if !ok {
t.Fatal("Expected error recorded in diagnostics")
}
var compErr *ComponentError
if !errors.As(diagErr, &compErr) {
t.Fatal("Expected ComponentError in diagnostic")
}
if compErr.Component != "test" {
t.Errorf("ComponentError.Component = %q, want %q", compErr.Component, "test")
}
}
// TestAppendIdentifiersIfValidMultiple tests with multiple values.
func TestAppendIdentifiersIfValidMultiple(t *testing.T) {
diag := &DiagnosticInfo{Errors: make(map[string]error)}
getValues := func() ([]string, error) {
return []string{"val1", "val2", "val3"}, nil
}
result := appendIdentifiersIfValid([]string{"existing"}, getValues, "prefix:", diag, "test", nil)
if len(result) != 4 {
t.Errorf("Expected 4 identifiers, got %d", len(result))
}
// Check that prefix was added
if result[1] != "prefix:val1" {
t.Errorf("Expected 'prefix:val1', got '%s'", result[1])
}
// Check diagnostics
if len(diag.Collected) != 1 || diag.Collected[0] != "test" {
t.Errorf("Expected 'test' in collected, got %v", diag.Collected)
}
}
// TestAppendIdentifierNilDiag tests that nil diagnostics don't panic.
func TestAppendIdentifierNilDiag(t *testing.T) {
getValue := func() (string, error) {
return "value", nil
}
result := appendIdentifierIfValid(nil, getValue, "prefix:", nil, "test", nil)
if len(result) != 1 {
t.Errorf("Expected 1 identifier, got %d", len(result))
}
}
// TestAppendIdentifierIfValidWithLogger tests all logger paths in appendIdentifierIfValid.
func TestAppendIdentifierIfValidWithLogger(t *testing.T) {
t.Run("error with logger", func(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
diag := &DiagnosticInfo{Errors: make(map[string]error)}
result := appendIdentifierIfValid(nil, func() (string, error) {
return "", fmt.Errorf("test error")
}, "prefix:", diag, "test-comp", logger)
if len(result) != 0 {
t.Errorf("Expected 0 identifiers, got %d", len(result))
}
if !bytes.Contains(buf.Bytes(), []byte("component failed")) {
t.Error("Expected 'component failed' in log output")
}
})
t.Run("empty value with logger", func(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
diag := &DiagnosticInfo{Errors: make(map[string]error)}
result := appendIdentifierIfValid(nil, func() (string, error) {
return "", nil
}, "prefix:", diag, "test-comp", logger)
if len(result) != 0 {
t.Errorf("Expected 0 identifiers, got %d", len(result))
}
if !bytes.Contains(buf.Bytes(), []byte("component returned empty value")) {
t.Error("Expected 'component returned empty value' in log output")
}
})
t.Run("success with logger", func(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
diag := &DiagnosticInfo{Errors: make(map[string]error)}
result := appendIdentifierIfValid(nil, func() (string, error) {
return "good-value", nil
}, "prefix:", diag, "test-comp", logger)
if len(result) != 1 {
t.Errorf("Expected 1 identifier, got %d", len(result))
}
if !bytes.Contains(buf.Bytes(), []byte("component collected")) {
t.Error("Expected 'component collected' in log output")
}
if !bytes.Contains(buf.Bytes(), []byte("component value")) {
t.Error("Expected 'component value' in log output")
}
})
}
// TestAppendIdentifiersIfValidWithLogger tests all logger paths in appendIdentifiersIfValid.
func TestAppendIdentifiersIfValidWithLogger(t *testing.T) {
t.Run("error with logger", func(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
diag := &DiagnosticInfo{Errors: make(map[string]error)}
result := appendIdentifiersIfValid(nil, func() ([]string, error) {
return nil, fmt.Errorf("test error")
}, "prefix:", diag, "test-comp", logger)
if len(result) != 0 {
t.Errorf("Expected 0 identifiers, got %d", len(result))
}
if !bytes.Contains(buf.Bytes(), []byte("component failed")) {
t.Error("Expected 'component failed' in log output")
}
})
t.Run("empty values with logger", func(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
diag := &DiagnosticInfo{Errors: make(map[string]error)}
result := appendIdentifiersIfValid(nil, func() ([]string, error) {
return []string{}, nil
}, "prefix:", diag, "test-comp", logger)
if len(result) != 0 {
t.Errorf("Expected 0 identifiers, got %d", len(result))
}
if !bytes.Contains(buf.Bytes(), []byte("component returned no values")) {
t.Error("Expected 'component returned no values' in log output")
}
})
t.Run("success with logger", func(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
diag := &DiagnosticInfo{Errors: make(map[string]error)}
result := appendIdentifiersIfValid(nil, func() ([]string, error) {
return []string{"val1", "val2"}, nil
}, "prefix:", diag, "test-comp", logger)
if len(result) != 2 {
t.Errorf("Expected 2 identifiers, got %d", len(result))
}
if !bytes.Contains(buf.Bytes(), []byte("component collected")) {
t.Error("Expected 'component collected' in log output")
}
if !bytes.Contains(buf.Bytes(), []byte("component values")) {
t.Error("Expected 'component values' in log output")
}
})
}
// TestAppendIdentifiersIfValidEmptyWithDiag tests empty values record ErrNoValues in diagnostics.
func TestAppendIdentifiersIfValidEmptyWithDiag(t *testing.T) {
diag := &DiagnosticInfo{Errors: make(map[string]error)}
result := appendIdentifiersIfValid(nil, func() ([]string, error) {
return []string{}, nil
}, "prefix:", diag, "test", nil)
if len(result) != 0 {
t.Errorf("Expected 0 identifiers, got %d", len(result))
}
diagErr, ok := diag.Errors["test"]
if !ok {
t.Fatal("Expected error recorded in diagnostics for empty values")
}
if !errors.Is(diagErr, ErrNoValues) {
t.Errorf("Expected ErrNoValues in diagnostic, got %v", diagErr)
}
var compErr *ComponentError
if !errors.As(diagErr, &compErr) {
t.Fatal("Expected ComponentError in diagnostic")
}
if compErr.Component != "test" {
t.Errorf("ComponentError.Component = %q, want %q", compErr.Component, "test")
}
}
// TestFormatHashInvalidLength tests formatHash with non-64-char input.
func TestFormatHashInvalidLength(t *testing.T) {
short := "abc123"
result := formatHash(short, Format64)
if result != short {
t.Errorf("Expected input returned unchanged for invalid length, got %q", result)
}
}
// TestFormatHashDefaultCase tests formatHash with an unknown FormatMode.
func TestFormatHashDefaultCase(t *testing.T) {
// Create a valid 64-char hex string
hash := "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"
result := formatHash(hash, FormatMode(999))
if result != hash {
t.Errorf("Expected input returned unchanged for unknown format mode, got %q", result)
}
}
// TestFormatHashAllModes tests formatHash produces correct lengths for all modes.
func TestFormatHashAllModes(t *testing.T) {
hash := "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"
tests := []struct {
mode FormatMode
wantLength int
}{
{Format32, 32},
{Format64, 64},
{Format128, 128},
{Format256, 256},
}
for _, tt := range tests {
result := formatHash(hash, tt.mode)
if len(result) != tt.wantLength {
t.Errorf("formatHash(mode=%d) length = %d, want %d", tt.mode, len(result), tt.wantLength)
}
}
}
// TestLogMethodsNilLogger tests that log methods don't panic with nil logger.
func TestLogMethodsNilLogger(t *testing.T) {
p := New() // no logger set
// These should not panic
p.logDebug("test debug")
p.logInfo("test info")
p.logWarn("test warn")
}
// TestLogMethodsWithLogger tests that log methods produce output with logger set.
func TestLogMethodsWithLogger(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
p := New().WithLogger(logger)
p.logDebug("test debug msg")
p.logInfo("test info msg")
p.logWarn("test warn msg")
output := buf.String()
if !bytes.Contains([]byte(output), []byte("test debug msg")) {
t.Error("Expected 'test debug msg' in log output")
}
if !bytes.Contains([]byte(output), []byte("test info msg")) {
t.Error("Expected 'test info msg' in log output")
}
if !bytes.Contains([]byte(output), []byte("test warn msg")) {
t.Error("Expected 'test warn msg' in log output")
}
}
// TestEnabledComponents tests that enabledComponents returns correct names.
func TestEnabledComponents(t *testing.T) {
p := New().WithCPU().WithSystemUUID().WithDisk()
components := p.enabledComponents()
if len(components) != 3 {
t.Fatalf("Expected 3 components, got %d: %v", len(components), components)
}
want := []string{ComponentCPU, ComponentSystemUUID, ComponentDisk}
for i, c := range components {
if c != want[i] {
t.Errorf("Component[%d] = %q, want %q", i, c, want[i])
}
}
}
// TestEnabledComponentsAll tests all components enabled.
func TestEnabledComponentsAll(t *testing.T) {
p := New().WithCPU().WithMotherboard().WithSystemUUID().WithMAC().WithDisk()
components := p.enabledComponents()
if len(components) != 5 {
t.Fatalf("Expected 5 components, got %d: %v", len(components), components)
}
}
// TestEnabledComponentsNone tests no components enabled.
func TestEnabledComponentsNone(t *testing.T) {
p := New()
components := p.enabledComponents()
if len(components) != 0 {
t.Errorf("Expected 0 components, got %d: %v", len(components), components)
}
}
// TestProviderWithLoggerNoIdentifiersWarning tests logWarn path when no identifiers collected.
func TestProviderWithLoggerNoIdentifiersWarning(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
p := New().WithLogger(logger)
// No components enabled → ErrNoIdentifiers
_, err := p.ID(context.Background())
if !errors.Is(err, ErrNoIdentifiers) {
t.Errorf("Expected ErrNoIdentifiers, got %v", err)
}
if !bytes.Contains(buf.Bytes(), []byte("no hardware identifiers collected")) {
t.Error("Expected 'no hardware identifiers collected' warning in log output")
}
}
// TestExecuteCommandWithLogger tests executeCommand logger output paths.
func TestExecuteCommandWithLogger(t *testing.T) {
t.Run("success with logger", func(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
mock := newMockExecutor()
mock.setOutput("testcmd", "output")
result, err := executeCommand(context.Background(), mock, logger, "testcmd", "arg1")
if err != nil {
t.Fatalf("executeCommand error: %v", err)
}
if result != "output" {
t.Errorf("Expected 'output', got %q", result)
}
if !bytes.Contains(buf.Bytes(), []byte("executing command")) {
t.Error("Expected 'executing command' in log output")
}
if !bytes.Contains(buf.Bytes(), []byte("command completed")) {
t.Error("Expected 'command completed' in log output")
}
})
t.Run("error with logger", func(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
mock := newMockExecutor()
mock.setError("testcmd", fmt.Errorf("mock failure"))
_, err := executeCommand(context.Background(), mock, logger, "testcmd")
if err == nil {
t.Fatal("Expected error")
}
if !bytes.Contains(buf.Bytes(), []byte("executing command")) {
t.Error("Expected 'executing command' in log output")
}
if !bytes.Contains(buf.Bytes(), []byte("command failed")) {
t.Error("Expected 'command failed' in log output")
}
})
}