1
2
3
4 package network
5
6 import (
7 "context"
8 "os"
9 "path/filepath"
10 "testing"
11 "time"
12 )
13
14 func Test_GenerateResolvConfContent(t *testing.T) {
15 type testcase struct {
16 name string
17
18 searches []string
19 servers []string
20 options []string
21
22 expectedContent string
23 expectErr bool
24 }
25 testcases := []*testcase{
26 {
27 name: "Empty",
28 },
29 {
30 name: "MaxSearches",
31 searches: []string{"1", "2", "3", "4", "5", "6", "7"},
32 expectErr: true,
33 },
34 {
35 name: "ValidSearches",
36 searches: []string{"a.com", "b.com"},
37 expectedContent: "search a.com b.com\n",
38 },
39 {
40 name: "ValidServers",
41 servers: []string{"8.8.8.8", "8.8.4.4"},
42 expectedContent: "nameserver 8.8.8.8\nnameserver 8.8.4.4\n",
43 },
44 {
45 name: "ValidOptions",
46 options: []string{"timeout:30", "inet6"},
47 expectedContent: "options timeout:30 inet6\n",
48 },
49 {
50 name: "All",
51 searches: []string{"a.com", "b.com"},
52 servers: []string{"8.8.8.8", "8.8.4.4"},
53 options: []string{"timeout:30", "inet6"},
54 expectedContent: "search a.com b.com\nnameserver 8.8.8.8\nnameserver 8.8.4.4\noptions timeout:30 inet6\n",
55 },
56 }
57 for _, tc := range testcases {
58 t.Run(tc.name, func(t *testing.T) {
59 c, err := GenerateResolvConfContent(context.Background(), tc.searches, tc.servers, tc.options)
60 if tc.expectErr && err == nil {
61 t.Fatal("expected err got nil")
62 } else if !tc.expectErr && err != nil {
63 t.Fatalf("expected no error got %v:", err)
64 }
65
66 if c != tc.expectedContent {
67 t.Fatalf("expected content: %q got: %q", tc.expectedContent, c)
68 }
69 })
70 }
71 }
72
73 func Test_MergeValues(t *testing.T) {
74 type testcase struct {
75 name string
76
77 first []string
78 second []string
79
80 expected []string
81 }
82 testcases := []*testcase{
83 {
84 name: "BothEmpty",
85 },
86 {
87 name: "FirstEmpty",
88 second: []string{"a", "b"},
89 expected: []string{"a", "b"},
90 },
91 {
92 name: "SecondEmpty",
93 first: []string{"a", "b"},
94 expected: []string{"a", "b"},
95 },
96 {
97 name: "AllUnique",
98 first: []string{"a", "c", "d"},
99 second: []string{"b", "e"},
100 expected: []string{"a", "c", "d", "b", "e"},
101 },
102 {
103 name: "NonUnique",
104 first: []string{"a", "c", "d"},
105 second: []string{"a", "b", "c", "d"},
106 expected: []string{"a", "c", "d", "b"},
107 },
108 }
109 for _, tc := range testcases {
110 t.Run(tc.name, func(t *testing.T) {
111 m := MergeValues(tc.first, tc.second)
112 if len(m) != len(tc.expected) {
113 t.Fatalf("expected %d entries got: %d", len(tc.expected), len(m))
114 }
115 for i := 0; i < len(tc.expected); i++ {
116 if tc.expected[i] != m[i] {
117 t.Logf("%v :: %v", tc.expected, m)
118 t.Fatalf("expected value: %q at index: %d got: %q", tc.expected[i], i, m[i])
119 }
120 }
121 })
122 }
123 }
124
125 func Test_GenerateEtcHostsContent(t *testing.T) {
126 type testcase struct {
127 name string
128
129 hostname string
130
131 expectedContent string
132 }
133 testcases := []*testcase{
134 {
135 name: "Net BIOS Name",
136 hostname: "Test",
137 expectedContent: `127.0.0.1 localhost
138 127.0.0.1 Test
139
140 # The following lines are desirable for IPv6 capable hosts
141 ::1 ip6-localhost ip6-loopback
142 fe00::0 ip6-localnet
143 ff00::0 ip6-mcastprefix
144 ff02::1 ip6-allnodes
145 ff02::2 ip6-allrouters
146 `,
147 },
148 {
149 name: "FQDN",
150 hostname: "test.rules.domain.com",
151 expectedContent: `127.0.0.1 localhost
152 127.0.0.1 test.rules.domain.com test
153
154 # The following lines are desirable for IPv6 capable hosts
155 ::1 ip6-localhost ip6-loopback
156 fe00::0 ip6-localnet
157 ff00::0 ip6-mcastprefix
158 ff02::1 ip6-allnodes
159 ff02::2 ip6-allrouters
160 `,
161 },
162 }
163 for _, tc := range testcases {
164 t.Run(tc.name, func(t *testing.T) {
165 c := GenerateEtcHostsContent(context.Background(), tc.hostname)
166 if c != tc.expectedContent {
167 t.Fatalf("expected content: %q got: %q", tc.expectedContent, c)
168 }
169 })
170 }
171 }
172
173
174 type testDirEntry struct {
175 FileName string
176 IsDirectory bool
177 }
178
179 func (t *testDirEntry) Name() string {
180 return t.FileName
181 }
182 func (t *testDirEntry) Type() os.FileMode {
183 if t.IsDirectory {
184 return os.ModeDir
185 }
186 return 0
187 }
188 func (t *testDirEntry) IsDir() bool {
189 return t.IsDirectory
190 }
191 func (t *testDirEntry) Info() (os.FileInfo, error) {
192 return nil, nil
193 }
194
195 var _ = (os.DirEntry)(&testDirEntry{})
196
197 func Test_InstanceIDToName(t *testing.T) {
198 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
199 defer cancel()
200
201 vmBusGUID := "1111-2222-3333-4444"
202 testIfName := "test-eth0"
203
204 vmbusWaitForDevicePath = func(_ context.Context, vmBusGUIDPattern string) (string, error) {
205 vmBusPath := filepath.Join("/sys/bus/vmbus/devices", vmBusGUIDPattern)
206 return vmBusPath, nil
207 }
208
209 storageWaitForFileMatchingPattern = func(_ context.Context, pattern string) (string, error) {
210 return pattern, nil
211 }
212
213 ioReadDir = func(dirname string) ([]os.DirEntry, error) {
214 info := &testDirEntry{
215 FileName: testIfName,
216 IsDirectory: false,
217 }
218 return []os.DirEntry{info}, nil
219 }
220 actualIfName, err := InstanceIDToName(ctx, vmBusGUID, false)
221 if err != nil {
222 t.Fatalf("expected no error, instead got %v", err)
223 }
224 if actualIfName != testIfName {
225 t.Fatalf("expected to get %v ifname, instead got %v", testIfName, actualIfName)
226 }
227 }
228
229 func Test_InstanceIDToName_VPCI(t *testing.T) {
230 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
231 defer cancel()
232
233 vmBusGUID := "1111-2222-3333-4444"
234 testIfName := "test-eth0-vpci"
235
236 pciFindDeviceFullPath = func(_ context.Context, vmBusGUID string) (string, error) {
237 return filepath.Join("/sys/bus/vmbus/devices", vmBusGUID), nil
238 }
239
240 storageWaitForFileMatchingPattern = func(_ context.Context, pattern string) (string, error) {
241 return pattern, nil
242 }
243
244 ioReadDir = func(dirname string) ([]os.DirEntry, error) {
245 info := &testDirEntry{
246 FileName: testIfName,
247 IsDirectory: false,
248 }
249 return []os.DirEntry{info}, nil
250 }
251 actualIfName, err := InstanceIDToName(ctx, vmBusGUID, true)
252 if err != nil {
253 t.Fatalf("expected no error, instead got %v", err)
254 }
255 if actualIfName != testIfName {
256 t.Fatalf("expected to get %v ifname, instead got %v", testIfName, actualIfName)
257 }
258 }
259
View as plain text