Debian Codegraph @

sdk/resource/host_id_test.go

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