1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package resource
16
17 import (
18 "errors"
19 "testing"
20
21 "github.com/stretchr/testify/require"
22 )
23
24 var (
25 expectedHostID = "f2c668b579780554f70f72a063dc0864"
26
27 readFileNoError = func(filename string) (string, error) {
28 return expectedHostID + "\n", nil
29 }
30
31 readFileError = func(filename string) (string, error) {
32 return "", errors.New("not found")
33 }
34
35 execCommandNoError = func(string, ...string) (string, error) {
36 return expectedHostID + "\n", nil
37 }
38
39 execCommandError = func(string, ...string) (string, error) {
40 return "", errors.New("not found")
41 }
42 )
43
44 func SetDefaultHostIDProvider() {
45 SetHostIDProvider(defaultHostIDProvider)
46 }
47
48 func SetHostIDProvider(hostIDProvider hostIDProvider) {
49 hostID = hostIDProvider
50 }
51
52 func TestHostIDReaderBSD(t *testing.T) {
53 tt := []struct {
54 name string
55 fileReader fileReader
56 commandExecutor commandExecutor
57 expectedHostID string
58 expectError bool
59 }{
60 {
61 name: "hostIDReaderBSD valid primary",
62 fileReader: readFileNoError,
63 commandExecutor: execCommandError,
64 expectedHostID: expectedHostID,
65 expectError: false,
66 },
67 {
68 name: "hostIDReaderBSD invalid primary",
69 fileReader: readFileError,
70 commandExecutor: execCommandNoError,
71 expectedHostID: expectedHostID,
72 expectError: false,
73 },
74 {
75 name: "hostIDReaderBSD invalid primary and secondary",
76 fileReader: readFileError,
77 commandExecutor: execCommandError,
78 expectedHostID: "",
79 expectError: true,
80 },
81 }
82
83 for _, tc := range tt {
84 tc := tc
85
86 t.Run(tc.name, func(t *testing.T) {
87 reader := hostIDReaderBSD{
88 readFile: tc.fileReader,
89 execCommand: tc.commandExecutor,
90 }
91 hostID, err := reader.read()
92 require.Equal(t, tc.expectError, err != nil)
93 require.Equal(t, tc.expectedHostID, hostID)
94 })
95 }
96 }
97
98 func TestHostIDReaderLinux(t *testing.T) {
99 readFilePrimaryError := func(filename string) (string, error) {
100 if filename == "/var/lib/dbus/machine-id" {
101 return readFileNoError(filename)
102 }
103 return readFileError(filename)
104 }
105
106 tt := []struct {
107 name string
108 fileReader fileReader
109 expectedHostID string
110 expectError bool
111 }{
112 {
113 name: "hostIDReaderLinux valid primary",
114 fileReader: readFileNoError,
115 expectedHostID: expectedHostID,
116 expectError: false,
117 },
118 {
119 name: "hostIDReaderLinux invalid primary",
120 fileReader: readFilePrimaryError,
121 expectedHostID: expectedHostID,
122 expectError: false,
123 },
124 {
125 name: "hostIDReaderLinux invalid primary and secondary",
126 fileReader: readFileError,
127 expectedHostID: "",
128 expectError: true,
129 },
130 }
131
132 for _, tc := range tt {
133 tc := tc
134
135 t.Run(tc.name, func(t *testing.T) {
136 reader := hostIDReaderLinux{
137 readFile: tc.fileReader,
138 }
139 hostID, err := reader.read()
140 require.Equal(t, tc.expectError, err != nil)
141 require.Equal(t, tc.expectedHostID, hostID)
142 })
143 }
144 }
145
146 func TestHostIDReaderDarwin(t *testing.T) {
147 validOutput := `+-o J316sAP <class IOPlatformExpertDevice, id 0x10000024d, registered, matched, active, busy 0 (132196 ms), retain 37>
148 {
149 "IOPolledInterface" = "AppleARMWatchdogTimerHibernateHandler is not serializable"
150 "#address-cells" = <02000000>
151 "AAPL,phandle" = <01000000>
152 "serial-number" = <94e1c79ec04cd3f153f600000000000000000000000000000000000000000000>
153 "IOBusyInterest" = "IOCommand is not serializable"
154 "target-type" = <"J316s">
155 "platform-name" = <7436303030000000000000000000000000000000000000000000000000000000>
156 "secure-root-prefix" = <"md">
157 "name" = <"device-tree">
158 "region-info" = <4c4c2f4100000000000000000000000000000000000000000000000000000000>
159 "manufacturer" = <"Apple Inc.">
160 "compatible" = <"J316sAP","MacBookPro18,1","AppleARM">
161 "config-number" = <00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000>
162 "IOPlatformSerialNumber" = "HDWLIF2LM7"
163 "regulatory-model-number" = <4132343835000000000000000000000000000000000000000000000000000000>
164 "time-stamp" = <"Fri Aug 5 20:25:38 PDT 2022">
165 "clock-frequency" = <00366e01>
166 "model" = <"MacBookPro18,1">
167 "mlb-serial-number" = <5c92d268d6cd789e475ffafc0d363fc950000000000000000000000000000000>
168 "model-number" = <5a31345930303136430000000000000000000000000000000000000000000000>
169 "IONWInterrupts" = "IONWInterrupts"
170 "model-config" = <"ICT;MoPED=0x03D053A605C84ED11C455A18D6C643140B41A239">
171 "device_type" = <"bootrom">
172 "#size-cells" = <02000000>
173 "IOPlatformUUID" = "81895B8D-9EF9-4EBB-B5DE-B00069CF53F0"
174 }
175 `
176 execCommandValid := func(string, ...string) (string, error) {
177 return validOutput, nil
178 }
179
180 execCommandInvalid := func(string, ...string) (string, error) {
181 return "wasn't expecting this", nil
182 }
183
184 tt := []struct {
185 name string
186 fileReader fileReader
187 commandExecutor commandExecutor
188 expectedHostID string
189 expectError bool
190 }{
191 {
192 name: "hostIDReaderDarwin valid output",
193 commandExecutor: execCommandValid,
194 expectedHostID: "81895B8D-9EF9-4EBB-B5DE-B00069CF53F0",
195 expectError: false,
196 },
197 {
198 name: "hostIDReaderDarwin invalid output",
199 commandExecutor: execCommandInvalid,
200 expectedHostID: "",
201 expectError: true,
202 },
203 {
204 name: "hostIDReaderDarwin error",
205 commandExecutor: execCommandError,
206 expectedHostID: "",
207 expectError: true,
208 },
209 }
210
211 for _, tc := range tt {
212 tc := tc
213 t.Run(tc.name, func(t *testing.T) {
214 reader := hostIDReaderDarwin{
215 execCommand: tc.commandExecutor,
216 }
217 hostID, err := reader.read()
218 require.Equal(t, tc.expectError, err != nil)
219 require.Equal(t, tc.expectedHostID, hostID)
220 })
221 }
222 }
223
View as plain text