-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_linux_test.go
More file actions
89 lines (73 loc) · 2.76 KB
/
example_linux_test.go
File metadata and controls
89 lines (73 loc) · 2.76 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
//go:build linux
package machineid_test
import (
"context"
"fmt"
"github.com/slashdevops/machineid"
)
// ExampleProvider_Diagnostics demonstrates inspecting which hardware components
// were successfully collected on Linux.
func ExampleProvider_Diagnostics() {
provider := machineid.New().
WithCPU().
WithSystemUUID()
//nolint:errcheck // Example: error handling omitted for brevity
provider.ID(context.Background())
diag := provider.Diagnostics()
if diag == nil {
fmt.Println("no diagnostics")
return
}
// On Linux, the number of collected components depends on system access:
// /proc/cpuinfo is always readable (cpu)
// /sys/class/dmi/id/product_uuid may require root (uuid)
// /etc/machine-id is usually readable (machine-id)
fmt.Printf("Has collected data: %v\n", len(diag.Collected) > 0)
fmt.Printf("At least one component: %v\n", len(diag.Collected) >= 1)
// Output:
// Has collected data: true
// At least one component: true
}
// Example_integrity demonstrates that salt produces different IDs on the
// same hardware, and that the same configuration is consistent across calls.
func Example_integrity() {
// Salt-based differentiation works regardless of hardware access
p1 := machineid.New().WithCPU().WithSystemUUID().WithSalt("app1")
p2 := machineid.New().WithCPU().WithSystemUUID().WithSalt("app2")
id1, _ := p1.ID(context.Background()) //nolint:errcheck // Example
id2, _ := p2.ID(context.Background()) //nolint:errcheck // Example
// Same configuration always produces same ID
id1Again, _ := machineid.New().WithCPU().WithSystemUUID().WithSalt("app1").ID(context.Background()) //nolint:errcheck // Example
fmt.Printf("Consistency: %v\n", id1 == id1Again)
// Different salts produce different IDs
fmt.Printf("Different salts: %v\n", id1 != id2)
// All IDs are 64 characters (power of 2)
fmt.Printf("All are 64 chars: %v\n", len(id1) == 64 && len(id2) == 64)
// Output:
// Consistency: true
// Different salts: true
// All are 64 chars: true
}
// Example_linuxFileSources shows that Linux reads hardware data from
// filesystem paths rather than spawning external commands.
func Example_linuxFileSources() {
// On Linux, most hardware identifiers are read directly from /proc and /sys:
// CPU: /proc/cpuinfo
// UUID: /sys/class/dmi/id/product_uuid
// Machine ID: /etc/machine-id
// Motherboard: /sys/class/dmi/id/board_serial
// Disk: lsblk + /sys/block/*/device/serial
//
// File reads are fast — no process startup overhead.
// Using CPU only since /proc/cpuinfo is always readable.
provider := machineid.New().
WithCPU()
id, err := provider.ID(context.Background())
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
fmt.Printf("ID length: %d\n", len(id))
// Output:
// ID length: 64
}