-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachineid_internal_darwin_test.go
More file actions
358 lines (298 loc) · 8.95 KB
/
machineid_internal_darwin_test.go
File metadata and controls
358 lines (298 loc) · 8.95 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
//go:build darwin
package machineid
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"testing"
)
// TestProviderWithMockExecutor tests using a mock executor for deterministic testing.
func TestProviderWithMockExecutor(t *testing.T) {
mock := newMockExecutor()
mock.setOutput("sysctl", "Test CPU Brand String")
g := New().
WithExecutor(mock).
WithCPU()
id, err := g.ID(context.Background())
if err != nil {
t.Fatalf("ID() with mock executor error = %v", err)
}
if len(id) != 64 {
t.Errorf("ID() returned ID of length %d, expected 64", len(id))
}
// Verify the ID is consistent with the same mock
id2, err := g.ID(context.Background())
if err != nil {
t.Fatalf("Second ID() call error = %v", err)
}
if id != id2 {
t.Error("ID() returned different IDs with same mock executor")
}
}
// TestProviderErrorHandling tests various error conditions.
func TestProviderErrorHandling(t *testing.T) {
tests := []struct {
name string
setupMock func(*mockExecutor)
configure func(*Provider) *Provider
expectError bool
wantErr error
}{
{
name: "command execution fails but no fallback available",
setupMock: func(m *mockExecutor) {
m.setError("sysctl", fmt.Errorf("command not found"))
},
configure: func(p *Provider) *Provider {
return p.WithCPU()
},
expectError: true,
wantErr: ErrNoIdentifiers,
},
{
name: "no identifiers collected",
setupMock: func(m *mockExecutor) {
// All commands fail
m.setError("sysctl", fmt.Errorf("failed"))
m.setError("ioreg", fmt.Errorf("failed"))
m.setError("system_profiler", fmt.Errorf("failed"))
},
configure: func(p *Provider) *Provider {
return p.WithCPU().WithSystemUUID()
},
expectError: true,
wantErr: ErrNoIdentifiers,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := newMockExecutor()
if tt.setupMock != nil {
tt.setupMock(mock)
}
p := New().WithExecutor(mock)
p = tt.configure(p)
_, err := p.ID(context.Background())
if tt.expectError && err == nil {
t.Error("Expected error but got none")
}
if !tt.expectError && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if tt.expectError && err != nil && tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) {
t.Errorf("got error %v, want %v", err, tt.wantErr)
}
}
})
}
}
// TestValidateError tests Validate method when ID generation fails.
func TestValidateError(t *testing.T) {
mock := newMockExecutor()
mock.setError("sysctl", fmt.Errorf("command failed"))
p := New().WithExecutor(mock).WithCPU()
valid, err := p.Validate(context.Background(), "some-id")
if err == nil {
t.Error("Expected error when ID generation fails")
}
if valid {
t.Error("Validation should fail when error occurs")
}
}
// TestDiagnosticsAvailableAfterID tests that Diagnostics() returns data after ID().
func TestDiagnosticsAvailableAfterID(t *testing.T) {
mock := newMockExecutor()
mock.setOutput("sysctl", "Test CPU")
mock.setOutput("system_profiler", `{
"SPHardwareDataType": [{
"chip_type": "Apple M1",
"machine_model": "Mac",
"platform_UUID": "UUID-123",
"serial_number": "SERIAL"
}]
}`)
p := New().WithExecutor(mock).WithCPU().WithSystemUUID()
// Before ID(), Diagnostics should be nil
if p.Diagnostics() != nil {
t.Error("Diagnostics should be nil before ID() call")
}
_, err := p.ID(context.Background())
if err != nil {
t.Fatalf("ID() error: %v", err)
}
diag := p.Diagnostics()
if diag == nil {
t.Fatal("Diagnostics should not be nil after ID() call")
}
if len(diag.Collected) == 0 {
t.Error("Expected at least one collected component")
}
}
// TestDiagnosticsRecordsFailures tests that failed components are recorded.
func TestDiagnosticsRecordsFailures(t *testing.T) {
mock := newMockExecutor()
mock.setOutput("sysctl", "Test CPU")
mock.setOutput("system_profiler", `{
"SPHardwareDataType": [{
"chip_type": "Apple M1",
"machine_model": "Mac",
"platform_UUID": "",
"serial_number": ""
}]
}`)
mock.setError("ioreg", fmt.Errorf("ioreg not available"))
p := New().WithExecutor(mock).WithCPU().WithSystemUUID()
_, err := p.ID(context.Background())
if err != nil {
t.Fatalf("ID() error: %v", err)
}
diag := p.Diagnostics()
if diag == nil {
t.Fatal("Diagnostics should not be nil")
}
// CPU should succeed, UUID should fail (empty in JSON + ioreg fails)
if len(diag.Collected) == 0 {
t.Error("Expected at least one collected component")
}
}
// TestProviderCachedIDNotModified tests that cached ID is not modified on subsequent calls.
func TestProviderCachedIDNotModified(t *testing.T) {
mock := newMockExecutor()
mock.setOutput("sysctl", "CPU1")
p := New().WithExecutor(mock).WithCPU()
id1, err := p.ID(context.Background())
if err != nil {
t.Fatalf("First ID() call failed: %v", err)
}
// Change the mock output
mock.setOutput("sysctl", "CPU2")
// Should still return cached value
id2, err := p.ID(context.Background())
if err != nil {
t.Fatalf("Second ID() call failed: %v", err)
}
if id1 != id2 {
t.Error("Cached ID was modified on subsequent call")
}
// Verify mock was only called once (due to caching)
if mock.callCount["sysctl"] > 2 {
t.Errorf("Expected sysctl to be called at most twice, got %d", mock.callCount["sysctl"])
}
}
// TestProviderAllIdentifiers tests using all identifier types.
func TestProviderAllIdentifiers(t *testing.T) {
mock := newMockExecutor()
mock.setOutput("sysctl", "Intel CPU")
mock.setOutput("system_profiler", `platform_UUID: "UUID123"`)
mock.setOutput("ioreg", "some data")
mock.setOutput("diskutil", `<string>/dev/disk0</string>`)
p := New().
WithExecutor(mock).
WithCPU().
WithSystemUUID().
WithMotherboard().
WithMAC().
WithDisk()
id, err := p.ID(context.Background())
if err != nil {
t.Fatalf("ID() with all identifiers failed: %v", err)
}
if len(id) != 64 {
t.Errorf("Expected 64-character ID (default Format64), got %d", len(id))
}
}
// TestCollectIdentifiersError tests when collectIdentifiers returns an error.
func TestCollectIdentifiersError(t *testing.T) {
mock := newMockExecutor()
// Don't set any outputs, so all commands will fail with "not configured"
p := New().WithExecutor(mock).WithCPU()
_, err := p.ID(context.Background())
if err == nil {
t.Error("Expected error when collectIdentifiers fails")
}
}
// TestProviderValidateMismatch tests validation with mismatched ID.
func TestProviderValidateMismatch(t *testing.T) {
mock := newMockExecutor()
mock.setOutput("sysctl", "CPU1")
p := New().WithExecutor(mock).WithCPU()
// Generate ID
id, err := p.ID(context.Background())
if err != nil {
t.Fatalf("ID() failed: %v", err)
}
// Validate with different ID
valid, err := p.Validate(context.Background(), id+"different")
if err != nil {
t.Errorf("Validate() error: %v", err)
}
if valid {
t.Error("Expected validation to fail for different ID")
}
}
// TestWithLoggerOutput verifies that log output appears when a logger is set.
func TestWithLoggerOutput(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
mock := newMockExecutor()
mock.setOutput("sysctl", "Test CPU Brand")
p := New().
WithExecutor(mock).
WithLogger(logger).
WithCPU()
_, err := p.ID(context.Background())
if err != nil {
t.Fatalf("ID() error: %v", err)
}
output := buf.String()
if output == "" {
t.Error("Expected log output when logger is set, got empty string")
}
// Check for key log messages
if !bytes.Contains(buf.Bytes(), []byte("generating machine ID")) {
t.Error("Expected 'generating machine ID' in log output")
}
if !bytes.Contains(buf.Bytes(), []byte("machine ID generated")) {
t.Error("Expected 'machine ID generated' in log output")
}
if !bytes.Contains(buf.Bytes(), []byte("component collected")) {
t.Error("Expected 'component collected' in log output")
}
}
// TestWithoutLoggerNoOutput verifies that no logging occurs without a logger.
func TestWithoutLoggerNoOutput(t *testing.T) {
mock := newMockExecutor()
mock.setOutput("sysctl", "Test CPU Brand")
p := New().
WithExecutor(mock).
WithCPU()
// Should not panic or produce any output
_, err := p.ID(context.Background())
if err != nil {
t.Fatalf("ID() error: %v", err)
}
}
// TestProviderCachedIDWithLogger tests the cached ID debug log path.
func TestProviderCachedIDWithLogger(t *testing.T) {
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
mock := newMockExecutor()
mock.setOutput("sysctl", "Test CPU")
p := New().WithExecutor(mock).WithLogger(logger).WithCPU()
// First call generates ID
_, err := p.ID(context.Background())
if err != nil {
t.Fatalf("First ID() error: %v", err)
}
// Second call returns cached
_, err = p.ID(context.Background())
if err != nil {
t.Fatalf("Second ID() error: %v", err)
}
if !bytes.Contains(buf.Bytes(), []byte("returning cached machine ID")) {
t.Error("Expected 'returning cached machine ID' in log output")
}
}