forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork_test.go
324 lines (274 loc) · 9.62 KB
/
network_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
//
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"net"
"reflect"
"runtime"
"testing"
"github.com/kata-containers/agent/pkg/types"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
)
func TestUpdateRemoveInterface(t *testing.T) {
skipUnlessRoot(t)
s := sandbox{}
ifc := types.Interface{
Name: "enoNumber",
Mtu: 1500,
HwAddr: "02:00:ca:fe:00:48",
}
ip := types.IPAddress{
Family: 0,
Address: "192.168.0.101",
Mask: "24",
}
ifc.IPAddresses = append(ifc.IPAddresses, &ip)
netHandle, _ := netlink.NewHandle()
defer netHandle.Delete()
//
// Initial test: try to update a device which doens't exist:
//
_, err := s.updateInterface(netHandle, &ifc)
assert.NotNil(t, err, "Expected to observe interface couldn't be found error")
// create a dummy link that we can test update on
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x48}
link := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
MTU: 1500,
TxQLen: -1,
Name: "ifc-name",
HardwareAddr: macAddr,
},
}
netHandle.LinkAdd(link)
netHandle.LinkSetUp(link)
//
// With a link populated, check to see if we can successfully update:
//
resultingIfc, err := s.updateInterface(netHandle, &ifc)
assert.Nil(t, err, "Unexpected update interface failure: %v", err)
assert.True(t, reflect.DeepEqual(resultingIfc, &ifc),
"Interface created didn't match: got %+v, expecting %+v", resultingIfc, ifc)
// Try with a different valid MTU. Make sure we can assign a new set of IP addresses
ifc.Mtu = 500
ifc.IPAddresses[0].Address = "192.168.0.102"
ip2 := types.IPAddress{
Family: 0,
Address: "182.168.0.103",
Mask: "24",
}
ifc.IPAddresses = append(ifc.IPAddresses, &ip2)
resultingIfc, err = s.updateInterface(netHandle, &ifc)
assert.Nil(t, err, "Unexpected update interface failure: %v", err)
assert.True(t, reflect.DeepEqual(resultingIfc, &ifc),
"Interface created didn't match: got %+v, expecting %+v", resultingIfc, ifc)
// Try with garbage:
ifc.Mtu = 999999999999
resultingIfc, err = s.updateInterface(netHandle, &ifc)
// expecting this failed
assert.NotNil(t, err, "Expected failure")
ifc.Mtu = 500
assert.True(t, reflect.DeepEqual(resultingIfc, &ifc),
"Resulting inteface should have been unchanged: got %+v, expecting %+v", resultingIfc, ifc)
// Exercise the removeInterface code:
_, err = s.removeInterface(netHandle, &ifc)
assert.Nil(t, err, "remove interface failed: %v", err)
// Try to remove non existent interface:
_, err = s.removeInterface(netHandle, &ifc)
assert.NotNil(t, err, "Expected failed removal: %v", err)
}
type teardownNetworkTest func()
func setupNetworkTest(t *testing.T) teardownNetworkTest {
skipUnlessRoot(t)
// new temporary namespace so we don't pollute the host
// lock thread since the namespace is thread local
runtime.LockOSThread()
var err error
ns, err := netns.New()
if err != nil {
t.Fatal("Failed to create newns", ns)
}
return func() {
ns.Close()
runtime.UnlockOSThread()
}
}
func TestUpdateRoutes(t *testing.T) {
tearDown := setupNetworkTest(t)
defer tearDown()
s := sandbox{}
// create a dummy link which we'll play with
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x48}
link := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
MTU: 1500,
TxQLen: -1,
Name: "ifc-name",
HardwareAddr: macAddr,
},
}
netHandle, _ := netlink.NewHandle()
defer netHandle.Delete()
netHandle.LinkAdd(link)
if err := netHandle.LinkSetUp(link); err != nil {
t.Fatal(err)
}
netlinkAddr, _ := netlink.ParseAddr("192.168.0.2/16")
netHandle.AddrAdd(link, netlinkAddr)
//Test a simple route setup:
inputRoutesSimple := []*types.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.0/16", Gateway: "", Source: "192.168.0.2", Scope: 253, Device: "ifc-name"},
}
testRoutes := &pb.Routes{
Routes: inputRoutesSimple,
}
results, err := s.updateRoutes(netHandle, testRoutes)
assert.Nil(t, err, "Unexpected update interface failure: %v", err)
assert.True(t, reflect.DeepEqual(results, testRoutes),
"Interface created didn't match: got %+v, expecting %+v", results, testRoutes)
//Test a route setup mimicking what could be provided by PTP CNI plugin:
inputRoutesPTPExample := []*types.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.144/16", Gateway: "192.168.0.1", Source: "192.168.0.2", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.1/32", Gateway: "", Source: "192.168.0.2", Scope: 254, Device: "ifc-name"},
}
testRoutes.Routes = inputRoutesPTPExample
results, err = s.updateRoutes(netHandle, testRoutes)
assert.Nil(t, err, "Unexpected update interface failure: %v", err)
assert.True(t, reflect.DeepEqual(results, testRoutes),
"Interface created didn't match: got %+v, expecting %+v", results, testRoutes)
//Test unreachable example (no scope provided for initial link route)
inputRoutesNoScope := []*types.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.0/16", Gateway: "", Source: "192.168.0.2", Scope: 0, Device: "ifc-name"},
}
testRoutes.Routes = inputRoutesNoScope
results, err = s.updateRoutes(netHandle, testRoutes)
assert.NotNil(t, err, "Expected to observe unreachable route failure")
assert.True(t, reflect.DeepEqual(results.Routes[0], testRoutes.Routes[1]),
"Interface created didn't match: got %+v, expecting %+v", results.Routes[0], testRoutes.Routes[1])
}
func TestUpdateRoutesIPVlan(t *testing.T) {
tearDown := setupNetworkTest(t)
defer tearDown()
s := sandbox{}
testRoutes := &pb.Routes{}
// create a dummy link which we'll play with
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x48}
link := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
MTU: 1500,
TxQLen: -1,
Name: "ifc-name",
HardwareAddr: macAddr,
},
}
netHandle, _ := netlink.NewHandle()
defer netHandle.Delete()
netHandle.LinkAdd(link)
if err := netHandle.LinkSetUp(link); err != nil {
t.Fatal(err)
}
netlinkAddr, _ := netlink.ParseAddr("192.168.0.2/16")
netHandle.AddrAdd(link, netlinkAddr)
//Test a route setup mimicking what could be provided by ipvlan CNI plugin:
inputRoutesIPVlanExample := []*types.Route{
// route "default dev ifc-name scope link"
{Dest: "", Gateway: "", Source: "", Scope: 1, Device: "ifc-name"},
// route "192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.2"
// TODO : We dont really handle route protocol currently. We need to add this and
// test that protocol is handled.
// Issue: https://github.com/kata-containers/agent/issues/405
{Dest: "192.168.0.0/24", Gateway: "", Source: "192.168.0.2", Scope: 1, Device: "ifc-name"},
}
testRoutes.Routes = inputRoutesIPVlanExample
results, err := s.updateRoutes(netHandle, testRoutes)
assert.Nil(t, err, "Unexpected update interface failure: %v", err)
assert.True(t, reflect.DeepEqual(results, testRoutes),
"Interface created didn't match: got %+v, expecting %+v", results, testRoutes)
}
func TestListInterfaces(t *testing.T) {
tearDown := setupNetworkTest(t)
defer tearDown()
assert := assert.New(t)
s := sandbox{}
ifc := types.Interface{
Name: "enoNumber",
Mtu: 1500,
HwAddr: "02:00:ca:fe:00:48",
}
ip := types.IPAddress{
Family: 0,
Address: "192.168.0.101",
Mask: "24",
}
ifc.IPAddresses = append(ifc.IPAddresses, &ip)
netHandle, _ := netlink.NewHandle()
defer netHandle.Delete()
// create a dummy link that we can test update on
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x48}
link := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
MTU: 1500,
TxQLen: -1,
Name: "ifc-name",
HardwareAddr: macAddr,
},
}
netHandle.LinkAdd(link)
netHandle.LinkSetUp(link)
s.updateInterface(netHandle, &ifc)
//
// With a link populated, check to see if we can successfully list:
//
results, err := s.listInterfaces(nil)
assert.Nil(err, "Expected to list all interfaces")
assert.True(reflect.DeepEqual(results.Interfaces[1], &ifc),
"Interface listed didn't match: got %+v, expecting %+v", results.Interfaces[1], &ifc)
}
func TestListRoutes(t *testing.T) {
tearDown := setupNetworkTest(t)
defer tearDown()
assert := assert.New(t)
s := sandbox{}
// create a dummy link which we'll play with
macAddr := net.HardwareAddr{0x02, 0x00, 0xCA, 0xFE, 0x00, 0x48}
link := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
MTU: 1500,
TxQLen: -1,
Name: "ifc-name",
HardwareAddr: macAddr,
},
}
netHandle, _ := netlink.NewHandle()
defer netHandle.Delete()
netHandle.LinkAdd(link)
if err := netHandle.LinkSetUp(link); err != nil {
t.Fatal(err)
}
netlinkAddr, _ := netlink.ParseAddr("192.168.0.2/16")
netHandle.AddrAdd(link, netlinkAddr)
//Test a simple route setup:
inputRoutesSimple := []*types.Route{
{Dest: "", Gateway: "192.168.0.1", Source: "", Scope: 0, Device: "ifc-name"},
{Dest: "192.168.0.0/16", Gateway: "", Source: "192.168.0.2", Scope: 253, Device: "ifc-name"},
}
testRoutes := &pb.Routes{
Routes: inputRoutesSimple,
}
s.updateRoutes(netHandle, testRoutes)
results, err := s.listRoutes(nil)
assert.Nil(err, "Expected to list all routes")
assert.True(reflect.DeepEqual(results.Routes[0], inputRoutesSimple[0]),
"Route listed didn't match: got %+v, expecting %+v", results.Routes[0], inputRoutesSimple[0])
assert.True(reflect.DeepEqual(results.Routes[1], inputRoutesSimple[1]),
"Route listed didn't match: got %+v, expecting %+v", results.Routes[1], inputRoutesSimple[1])
}