-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhosts_test.go
152 lines (139 loc) · 2.59 KB
/
hosts_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
package main
import (
"fmt"
"testing"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func createNode(name string, nodeAddresses []corev1.NodeAddress) *corev1.Node {
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Status: corev1.NodeStatus{
Addresses: nodeAddresses,
},
}
}
func hostsFileDiff(got, want string) string {
return fmt.Sprintf(`got
BOM
%s
EOM
want
BOM
%s
EOM
`, got, want)
}
func TestHostsFileString(t *testing.T) {
hf := hostsFile{
"host4": "4.4.4.4",
"host1": "1.1.1.1",
"host3": "3.3.3.3",
"host2": "2.2.2.2",
}
want := fmt.Sprintf("%s\n"+
"1.1.1.1\t\thost1\n"+
"2.2.2.2\t\thost2\n"+
"3.3.3.3\t\thost3\n"+
"4.4.4.4\t\thost4\n"+
"", hostsFileHeader)
got := hf.String()
if got != want {
t.Error(hostsFileDiff(got, want))
}
}
func TestHostRecordString(t *testing.T) {
tests := []struct {
name string
in hostRecord
want string
}{
{
name: "adding host record",
in: hostRecord{
ipAddr: "1.1.1.1",
hostname: "host",
},
want: "host -> 1.1.1.1",
},
{
name: "removing host record",
in: hostRecord{
ipAddr: "",
hostname: "host",
},
want: "host -> <removed>",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.in.String()
if got != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
})
}
}
func TestToHostRecord(t *testing.T) {
const hostname = "host"
tests := []struct {
name string
nodeAddresses []corev1.NodeAddress
want hostRecord
}{
{
name: "internal IP address only",
nodeAddresses: []corev1.NodeAddress{
{
Type: corev1.NodeInternalIP,
Address: "1.1.1.1",
},
},
want: hostRecord{
ipAddr: "1.1.1.1",
hostname: hostname,
},
},
{
name: "external IP address only",
nodeAddresses: []corev1.NodeAddress{
{
Type: corev1.NodeExternalIP,
Address: "1.1.1.1",
},
},
want: hostRecord{
ipAddr: "1.1.1.1",
hostname: hostname,
},
},
{
name: "internal and external IP addresses",
nodeAddresses: []corev1.NodeAddress{
{
Type: corev1.NodeInternalIP,
Address: "1.1.1.1",
},
{
Type: corev1.NodeExternalIP,
Address: "2.2.2.2",
},
},
want: hostRecord{
ipAddr: "1.1.1.1",
hostname: hostname,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
node := createNode(hostname, test.nodeAddresses)
got := toHostRecord(node)
if got != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
})
}
}