-
Notifications
You must be signed in to change notification settings - Fork 42
/
txeh_test.go
347 lines (305 loc) · 17.5 KB
/
txeh_test.go
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
package txeh
import (
"os"
"runtime"
"strings"
"testing"
)
var mockHostsData = `127.0.0.1 localhost
127.0.1.1 straylight-desk
127.0.1.1 bad-idea
# /etc/hosts is not DNS and one should not expect consistent behaviour when assigning a host to multiple
# IP addresses. TODO - Support for duplicate hostname for IPv4 and IPv6
# see - "man 5 hosts"
# IP_address canonical_hostname [aliases...]
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.1.27.1 nifi-mtx-2 nifi-mtx-2.d4l-demo nifi-mtx-2.d4l-demo.svc nifi-mtx-2.d4l-demo.svc.cluster.local nifi-mtx-2.d4l-demo.dwdev4 nifi-mtx-2.d4l-demo.svc.dwdev4 nifi-mtx-2.d4l-demo.svc.cluster.dwdev4
127.1.27.2 demo-log-search-api demo-log-search-api.d4l-demo demo-log-search-api.d4l-demo.svc demo-log-search-api.d4l-demo.svc.cluster.local demo-log-search-api.d4l-demo.dwdev4 demo-log-search-api.d4l-demo.svc.dwdev4 demo-log-search-api.d4l-demo.svc.cluster.dwdev4
127.1.27.3 warehouse-cluster-repl warehouse-cluster-repl.d4l-demo warehouse-cluster-repl.d4l-demo.svc warehouse-cluster-repl.d4l-demo.svc.cluster.local warehouse-cluster-repl.d4l-demo.dwdev4 warehouse-cluster-repl.d4l-demo.svc.dwdev4 warehouse-cluster-repl.d4l-demo.svc.cluster.dwdev4
127.1.27.4 superset-redis-headless superset-redis-headless.d4l-demo superset-redis-headless.d4l-demo.svc superset-redis-headless.d4l-demo.svc.cluster.local superset-redis-headless.d4l-demo.dwdev4 superset-redis-headless.d4l-demo.svc.dwdev4 superset-redis-headless.d4l-demo.svc.cluster.dwdev4
127.1.27.5 superset-redis-master-0.superset-redis-headless superset-redis-master-0.superset-redis-headless.d4l-demo superset-redis-master-0.superset-redis-headless.d4l-demo.svc superset-redis-master-0.superset-redis-headless.d4l-demo.svc.cluster.local superset-redis-master-0.superset-redis-headless.d4l-demo.dwdev4 superset-redis-master-0.superset-redis-headless.d4l-demo.svc.dwdev4 superset-redis-master-0.superset-redis-headless.d4l-demo.svc.cluster.dwdev4
127.1.27.6 demo-log-hl-svc demo-log-hl-svc.d4l-demo demo-log-hl-svc.d4l-demo.svc demo-log-hl-svc.d4l-demo.svc.cluster.local demo-log-hl-svc.d4l-demo.dwdev4 demo-log-hl-svc.d4l-demo.svc.dwdev4 demo-log-hl-svc.d4l-demo.svc.cluster.dwdev4
127.1.27.7 demo-log-0.demo-log-hl-svc demo-log-0.demo-log-hl-svc.d4l-demo demo-log-0.demo-log-hl-svc.d4l-demo.svc demo-log-0.demo-log-hl-svc.d4l-demo.svc.cluster.local demo-log-0.demo-log-hl-svc.d4l-demo.dwdev4 demo-log-0.demo-log-hl-svc.d4l-demo.svc.dwdev4 demo-log-0.demo-log-hl-svc.d4l-demo.svc.cluster.dwdev4
127.1.27.8 nifi-ca nifi-ca.d4l-demo nifi-ca.d4l-demo.svc nifi-ca.d4l-demo.svc.cluster.local nifi-ca.d4l-demo.dwdev4 nifi-ca.d4l-demo.svc.dwdev4 nifi-ca.d4l-demo.svc.cluster.dwdev4
127.1.27.9 proxy-api proxy-api.d4l-demo proxy-api.d4l-demo.svc proxy-api.d4l-demo.svc.cluster.local proxy-api.d4l-demo.dwdev4 proxy-api.d4l-demo.svc.dwdev4 proxy-api.d4l-demo.svc.cluster.dwdev4
127.1.27.10 demo-hl demo-hl.d4l-demo demo-hl.d4l-demo.svc demo-hl.d4l-demo.svc.cluster.local demo-hl.d4l-demo.dwdev4 demo-hl.d4l-demo.svc.dwdev4 demo-hl.d4l-demo.svc.cluster.dwdev4
127.1.27.11 demo-pool-0-0.demo-hl demo-pool-0-0.demo-hl.d4l-demo demo-pool-0-0.demo-hl.d4l-demo.svc demo-pool-0-0.demo-hl.d4l-demo.svc.cluster.local demo-pool-0-0.demo-hl.d4l-demo.dwdev4 demo-pool-0-0.demo-hl.d4l-demo.svc.dwdev4 demo-pool-0-0.demo-hl.d4l-demo.svc.cluster.dwdev4
127.1.27.12 demo-pool-0-1.demo-hl demo-pool-0-1.demo-hl.d4l-demo demo-pool-0-1.demo-hl.d4l-demo.svc demo-pool-0-1.demo-hl.d4l-demo.svc.cluster.local demo-pool-0-1.demo-hl.d4l-demo.dwdev4 demo-pool-0-1.demo-hl.d4l-demo.svc.dwdev4 demo-pool-0-1.demo-hl.d4l-demo.svc.cluster.dwdev4
127.1.27.13 demo-pool-0-2.demo-hl demo-pool-0-2.demo-hl.d4l-demo demo-pool-0-2.demo-hl.d4l-demo.svc demo-pool-0-2.demo-hl.d4l-demo.svc.cluster.local demo-pool-0-2.demo-hl.d4l-demo.dwdev4 demo-pool-0-2.demo-hl.d4l-demo.svc.dwdev4 demo-pool-0-2.demo-hl.d4l-demo.svc.cluster.dwdev4
127.1.27.14 proxy-public proxy-public.d4l-demo proxy-public.d4l-demo.svc proxy-public.d4l-demo.svc.cluster.local proxy-public.d4l-demo.dwdev4 proxy-public.d4l-demo.svc.dwdev4 proxy-public.d4l-demo.svc.cluster.dwdev4
127.1.27.15 nifi-port-proxy nifi-port-proxy.d4l-demo nifi-port-proxy.d4l-demo.svc nifi-port-proxy.d4l-demo.svc.cluster.local nifi-port-proxy.d4l-demo.dwdev4 nifi-port-proxy.d4l-demo.svc.dwdev4 nifi-port-proxy.d4l-demo.svc.cluster.dwdev4
127.1.27.16 nifi nifi.d4l-demo nifi.d4l-demo.svc nifi.d4l-demo.svc.cluster.local nifi.d4l-demo.dwdev4 nifi.d4l-demo.svc.dwdev4 nifi.d4l-demo.svc.cluster.dwdev4
127.1.27.19 nifi-2.nifi nifi-2.nifi.d4l-demo nifi-2.nifi.d4l-demo.svc nifi-2.nifi.d4l-demo.svc.cluster.local nifi-2.nifi.d4l-demo.dwdev4 nifi-2.nifi.d4l-demo.svc.dwdev4 nifi-2.nifi.d4l-demo.svc.cluster.dwdev4
127.1.27.17 nifi-0.nifi nifi-0.nifi.d4l-demo nifi-0.nifi.d4l-demo.svc nifi-0.nifi.d4l-demo.svc.cluster.local nifi-0.nifi.d4l-demo.dwdev4 nifi-0.nifi.d4l-demo.svc.dwdev4 nifi-0.nifi.d4l-demo.svc.cluster.dwdev4
127.1.27.18 nifi-1.nifi nifi-1.nifi.d4l-demo nifi-1.nifi.d4l-demo.svc nifi-1.nifi.d4l-demo.svc.cluster.local nifi-1.nifi.d4l-demo.dwdev4 nifi-1.nifi.d4l-demo.svc.dwdev4 nifi-1.nifi.d4l-demo.svc.cluster.dwdev4
127.1.27.20 demo-prometheus-hl-svc demo-prometheus-hl-svc.d4l-demo demo-prometheus-hl-svc.d4l-demo.svc demo-prometheus-hl-svc.d4l-demo.svc.cluster.local demo-prometheus-hl-svc.d4l-demo.dwdev4 demo-prometheus-hl-svc.d4l-demo.svc.dwdev4 demo-prometheus-hl-svc.d4l-demo.svc.cluster.dwdev4
127.1.27.21 demo-prometheus-0.demo-prometheus-hl-svc demo-prometheus-0.demo-prometheus-hl-svc.d4l-demo demo-prometheus-0.demo-prometheus-hl-svc.d4l-demo.svc demo-prometheus-0.demo-prometheus-hl-svc.d4l-demo.svc.cluster.local demo-prometheus-0.demo-prometheus-hl-svc.d4l-demo.dwdev4 demo-prometheus-0.demo-prometheus-hl-svc.d4l-demo.svc.dwdev4 demo-prometheus-0.demo-prometheus-hl-svc.d4l-demo.svc.cluster.dwdev4
# existing comment
127.1.27.22 zk-cs zk-cs.d4l-demo zk-cs.d4l-demo.svc zk-cs.d4l-demo.svc.cluster.local zk-cs.d4l-demo.dwdev4 zk-cs.d4l-demo.svc.dwdev4 zk-cs.d4l-demo.svc.cluster.dwdev4
127.1.27.23 nifi-mtx-1 nifi-mtx-1.d4l-demo nifi-mtx-1.d4l-demo.svc nifi-mtx-1.d4l-demo.svc.cluster.local nifi-mtx-1.d4l-demo.dwdev4 nifi-mtx-1.d4l-demo.svc.dwdev4 nifi-mtx-1.d4l-demo.svc.cluster.dwdev4
127.1.27.24 pga;dmin pgadmin.d4l-demo pgadmin.d4l-demo.svc pgadmin.d4l-demo.svc.cluster.local pgadmin.d4l-demo.dwdev4 pgadmin.d4l-demo.svc.dwdev4 pgadmin.d4l-demo.svc.cluster.dwdev4 #another comment
127.1.27.25 superset-redis-master superset-redis-master.d4l-demo superset-redis-master.d4l-demo.svc superset-redis-master.d4l-demo.svc.cluster.local superset-redis-master.d4l-demo.dwdev4 superset-redis-master.d4l-demo.svc.dwdev4 superset-redis-master.d4l-demo.svc.cluster.dwdev4
127.1.27.27 keycloak keycloak.d4l-demo keycloak.d4l-demo.svc keycloak.d4l-demo.svc.cluster.local keycloak.d4l-demo.dwdev4 keycloak.d4l-demo.svc.dwdev4 keycloak.d4l-demo.svc.cluster.dwdev4
127.1.27.28 hub hub.d4l-demo hub.d4l-demo.svc hub.d4l-demo.svc.cluster.local hub.d4l-demo.dwdev4 hub.d4l-demo.svc.dwdev4 hub.d4l-demo.svc.cluster.dwdev4
127.1.27.29 nifi-registry nifi-registry.d4l-demo nifi-registry.d4l-demo.svc nifi-registry.d4l-demo.svc.cluster.local nifi-registry.d4l-demo.dwdev4 nifi-registry.d4l-demo.svc.dwdev4 nifi-registry.d4l-demo.svc.cluster.dwdev4
127.1.27.30 demo-console demo-console.d4l-demo demo-console.d4l-demo.svc demo-console.d4l-demo.svc.cluster.local demo-console.d4l-demo.dwdev4 demo-console.d4l-demo.svc.dwdev4 demo-console.d4l-demo.svc.cluster.dwdev4
127.1.27.31 superset superset.d4l-demo superset.d4l-demo.svc superset.d4l-demo.svc.cluster.local superset.d4l-demo.dwdev4 superset.d4l-demo.svc.dwdev4 superset.d4l-demo.svc.cluster.dwdev4
127.1.27.32 zk-hs zk-hs.d4l-demo zk-hs.d4l-demo.svc zk-hs.d4l-demo.svc.cluster.local zk-hs.d4l-demo.dwdev4 zk-hs.d4l-demo.svc.dwdev4 zk-hs.d4l-demo.svc.cluster.dwdev4
127.1.27.35 zk-2.zk-hs zk-2.zk-hs.d4l-demo zk-2.zk-hs.d4l-demo.svc zk-2.zk-hs.d4l-demo.svc.cluster.local zk-2.zk-hs.d4l-demo.dwdev4 zk-2.zk-hs.d4l-demo.svc.dwdev4 zk-2.zk-hs.d4l-demo.svc.cluster.dwdev4
127.1.27.33 zk-0.zk-hs zk-0.zk-hs.d4l-demo zk-0.zk-hs.d4l-demo.svc zk-0.zk-hs.d4l-demo.svc.cluster.local zk-0.zk-hs.d4l-demo.dwdev4 zk-0.zk-hs.d4l-demo.svc.dwdev4 zk-0.zk-hs.d4l-demo.svc.cluster.dwdev4
127.1.27.34 zk-1.zk-hs zk-1.zk-hs.d4l-demo zk-1.zk-hs.d4l-demo.svc zk-1.zk-hs.d4l-demo.svc.cluster.local zk-1.zk-hs.d4l-demo.dwdev4 zk-1.zk-hs.d4l-demo.svc.dwdev4 zk-1.zk-hs.d4l-demo.svc.cluster.dwdev4
127.1.27.36 nifi-mtx-0 nifi-mtx-0.d4l-demo nifi-mtx-0.d4l-demo.svc nifi-mtx-0.d4l-demo.svc.cluster.local nifi-mtx-0.d4l-demo.dwdev4 nifi-mtx-0.d4l-demo.svc.dwdev4 nifi-mtx-0.d4l-demo.svc.cluster.dwdev4
127.1.27.37 opsdb-cluster-repl opsdb-cluster-repl.d4l-demo opsdb-cluster-repl.d4l-demo.svc opsdb-cluster-repl.d4l-demo.svc.cluster.local opsdb-cluster-repl.d4l-demo.dwdev4 opsdb-cluster-repl.d4l-demo.svc.dwdev4 opsdb-cluster-repl.d4l-demo.svc.cluster.dwdev4
`
func TestNewHostsDefault(t *testing.T) {
hosts, err := NewHostsDefault()
if err != nil {
panic(err)
}
testHostName := "test-new-hosts-default"
hosts.AddHost("127.100.100.100", testHostName)
foundHostnames := hosts.ListHostsByIP("127.100.100.100")
found := false
for _, hn := range foundHostnames {
if hn == testHostName {
found = true
break
}
}
if !found {
t.Fatal("TestNewHostsDefault was unable find the hostname it added")
}
}
func TestNewHosts(t *testing.T) {
rawData := ""
blankHosts, err := NewHosts(&HostsConfig{
RawText: &rawData,
})
if err != nil {
t.Fatalf("TestNewHosts failed on NewHosts: %v", err)
}
testHostName := "test-new-hosts-default"
blankHosts.AddHost("127.100.100.100", testHostName)
foundHostnames := blankHosts.ListHostsByIP("127.100.100.100")
found := false
for _, hn := range foundHostnames {
if hn == testHostName {
found = true
break
}
}
if !found {
t.Fatal("TestNewHosts was unable find the hostname it added")
}
renderedData := blankHosts.RenderHostsFile()
fromRenderedHosts, err := NewHosts(&HostsConfig{
RawText: &renderedData,
})
for _, k := range []string{"A", "T", "T", "X", "X", "E", "E", "H", "H"} {
fromRenderedHosts.AddHost("127.100.100.100", k)
}
fromRenderedHosts.RemoveHosts([]string{"A "})
err = fromRenderedHosts.Save()
if err == nil {
t.Fatalf("Should not be able to save when using RawText.")
}
foundHostnames = fromRenderedHosts.ListHostsByIP("127.100.100.100")
found = false
for _, hn := range foundHostnames {
if hn == testHostName {
found = true
break
}
}
if !found {
t.Fatal("ListHostsByIP was unable find an IP")
}
}
func TestMethods(t *testing.T) {
mockHosts, err := NewHosts(&HostsConfig{
RawText: &mockHostsData,
})
if err != nil {
t.Fatalf("TestMockHosts failed on NewHosts: %v", err)
}
// get IP for localhost
ok, ipString, _ := mockHosts.HostAddressLookup("localhost", IPFamilyV4)
if !ok {
t.Fatalf("TestMockHosts could not find IPv4 address for localhost")
}
if ipString != "127.0.0.1" {
t.Fatalf("TestMockHosts returned incorrect IPv4 address for localhost")
}
// Test ListAddressesByHost exact
hostList := mockHosts.ListAddressesByHost("nifi", true)
if len(hostList) != 1 {
t.Fatalf("ListAddressesByHost returned returned invalid number of hosts for exact match")
}
// Test ListAddressesByHost
hostList = mockHosts.ListAddressesByHost("nifi", false)
if len(hostList) != 70 {
t.Fatalf("ListAddressesByHost returned returned invalid number of hosts")
}
// Test ListHostsByCIDR expect one
hostList = mockHosts.ListHostsByCIDR("127.0.0.0/24")
if len(hostList) != 1 {
t.Fatalf("ListHostsByCIDR returned returned "+
"invalid number of hosts for 127.0.0.0/24 got %v expecting 1", len(hostList))
}
// Test ListHostsByCIDR expect multiple
hostList = mockHosts.ListHostsByCIDR("127.1.27.0/24")
if len(hostList) != 252 {
t.Fatalf("ListHostsByCIDR returned returned "+
"invalid number of hosts for 127.0.0.0/24 got %v expecting 252", len(hostList))
}
// Test ListHostsByIP expect one
ipHostList := mockHosts.ListHostsByIP("127.0.0.1")
if len(ipHostList) != 1 {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for 127.0.0.1 got %v expecting 1", len(ipHostList))
}
// Test ListHostsByIP expect multiple
ipHostList = mockHosts.ListHostsByIP("127.1.27.1")
if len(ipHostList) != 7 {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for 127.1.27.1 got %v expecting 7", len(ipHostList))
}
// Test ListHostsByIP expect multiple
ipHostList = mockHosts.ListHostsByIP("127.1.27.2")
if len(ipHostList) != 7 {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for 127.1.27.2 got %v expecting 7", len(ipHostList))
}
// Test RemoveAddress
mockHosts.RemoveAddress("127.1.27.1")
ipHostList = mockHosts.ListHostsByIP("127.1.27.1")
if len(ipHostList) != 0 {
t.Fatalf("RemoveAddress then ListHostsByIP returned returned "+
"invalid number of hosts for 127.1.27.1 got %v expecting 0", len(ipHostList))
}
// Get list of hosts for 127.1.27.2 and 127.1.27.3
ipHostList2Len := len(mockHosts.ListHostsByIP("127.1.27.2"))
ipHostList3Len := len(mockHosts.ListHostsByIP("127.1.27.3"))
if ipHostList2Len+ipHostList3Len != 14 {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for 127.1.27.2 and 127.1.27.3 got %v expecting 14", ipHostList2Len+ipHostList3Len)
}
// Test RemoveAddresses
mockHosts.RemoveAddresses([]string{"127.1.27.2", "127.1.27.3"})
ipHostList2Len = len(mockHosts.ListHostsByIP("127.1.27.2"))
ipHostList3Len = len(mockHosts.ListHostsByIP("127.1.27.3"))
if ipHostList2Len+ipHostList3Len != 0 {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for 127.1.27.2 and 127.1.27.3 got %v expecting 0", ipHostList2Len+ipHostList3Len)
}
supersetAddresses := mockHosts.ListAddressesByHost("superset", true)
if len(supersetAddresses) != 1 {
t.Fatalf("ListAddressesByHost returned returned "+
"invalid number of hosts for superset got %v expecting 1", len(supersetAddresses))
}
hostsAtAddress := mockHosts.ListHostsByIP(supersetAddresses[0][0])
if len(hostsAtAddress) != 7 {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for %v got %v expecting 7", supersetAddresses[0][0], len(hostsAtAddress))
}
// Test RemoveHost
mockHosts.RemoveHost("superset")
hostsAtAddress = mockHosts.ListHostsByIP(supersetAddresses[0][0])
if len(hostsAtAddress) != 6 {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for %v got %v expecting 7", supersetAddresses[0][0], len(hostsAtAddress))
}
// Test RemoveHosts
mockHosts.RemoveHosts([]string{"superset.d4l-demo.svc", "superset.d4l-demo.svc.dwdev4"})
hostsAtAddress = mockHosts.ListHostsByIP(supersetAddresses[0][0])
if len(hostsAtAddress) != 4 {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for %v got %v expecting 7", supersetAddresses[0][0], len(hostsAtAddress))
}
// Test Add Host
ip := "127.1.27.5"
expect := 7
hostsAtAddress = mockHosts.ListHostsByIP(ip)
if len(hostsAtAddress) != expect {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for %v got %v expecting %v", ip, len(hostsAtAddress), expect)
}
mockHosts.AddHost(ip, "test")
expect = 8
hostsAtAddress = mockHosts.ListHostsByIP(ip)
if len(hostsAtAddress) != expect {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for %v got %v expecting %v", ip, len(hostsAtAddress), expect)
}
mockHosts.AddHosts(ip, []string{"test-0", "test-1", "test-2"})
expect = 11
hostsAtAddress = mockHosts.ListHostsByIP(ip)
if len(hostsAtAddress) != expect {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for %v got %v expecting %v", ip, len(hostsAtAddress), expect)
}
// test line with comment and bad host should treat existing bad host as legit host
// since this is not a "cleaner" utility
ip = "127.1.27.24"
expect = 7
hostsAtAddress = mockHosts.ListHostsByIP(ip)
if len(hostsAtAddress) != expect {
t.Fatalf("ListHostsByIP returned returned "+
"invalid number of hosts for %v got %v expecting %v", ip, len(hostsAtAddress), expect)
}
// remove addresses in 127.1.27.0/16 range
mockHosts.RemoveCIDRs([]string{"127.1.27.0/16"})
hfl := strings.Split(mockHosts.RenderHostsFile(), "\n")
lines := 19
if len(hfl) != lines {
t.Fatalf("Expeced %d lines and got %d", lines, len(hfl))
}
line := 17
expectString := "# existing comment"
if hfl[line] != expectString {
t.Fatalf("Expeced \"%s\" on line %d. Got \"%s\"", expectString, line, hfl[line])
}
}
func TestWinDefaultHostsFile(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping windows test")
}
tc := []struct {
Name string
EnvVaule string
Expect string
}{
{
Name: "Correct SystemRoot environment variable",
EnvVaule: "E:\\Windows",
Expect: "E:\\Windows\\System32\\drivers\\etc\\hosts",
},
{
Name: "SystemRoot with trailing slash",
EnvVaule: "E:\\Windows\\",
Expect: "E:\\Windows\\System32\\drivers\\etc\\hosts",
},
{
Name: "No systemRoot environment variable",
EnvVaule: "",
Expect: "C:\\Windows\\System32\\drivers\\etc\\hosts",
},
}
for _, tt := range tc {
t.Run(tt.Name, func(t *testing.T) {
if err := os.Setenv("SystemRoot", tt.EnvVaule); err != nil {
panic(err)
}
hostFile := winDefaultHostsFile()
if hostFile != tt.Expect {
t.Fatalf("TestWinDefaultHostsFile failed to get expected path: "+
"expect: %v, got: %v", tt.Expect, hostFile)
}
})
}
}