1
1
//
2
- // Copyright (c) 2017 Intel Corporation
2
+ // Copyright (c) 2018 Intel Corporation
3
3
//
4
4
// SPDX-License-Identifier: Apache-2.0
5
5
//
@@ -8,12 +8,15 @@ package main
8
8
9
9
import (
10
10
"net"
11
+ "os"
11
12
"reflect"
13
+ "runtime"
12
14
"testing"
13
15
14
16
pb "github.com/kata-containers/agent/protocols/grpc"
15
17
"github.com/stretchr/testify/assert"
16
18
"github.com/vishvananda/netlink"
19
+ "github.com/vishvananda/netns"
17
20
)
18
21
19
22
func TestUpdateRemoveInterface (t * testing.T ) {
@@ -74,7 +77,6 @@ func TestUpdateRemoveInterface(t *testing.T) {
74
77
"Interface created didn't match: got %+v, expecting %+v" , resultingIfc , ifc )
75
78
76
79
// Try with garbage:
77
- //
78
80
ifc .Mtu = 999999999999
79
81
resultingIfc , err = s .updateInterface (netHandle , & ifc )
80
82
// expecting this failed
@@ -84,30 +86,104 @@ func TestUpdateRemoveInterface(t *testing.T) {
84
86
assert .True (t , reflect .DeepEqual (resultingIfc , & ifc ),
85
87
"Resulting inteface should have been unchanged: got %+v, expecting %+v" , resultingIfc , ifc )
86
88
87
- //
88
- // Test adding routes:
89
- //
90
- route := pb.Route {
91
- Dest : "192.168.3.0/24" ,
92
- Gateway : "192.168.0.1" ,
93
- Device : "enoNumber" ,
94
- }
95
- err = s .addRoute (netHandle , & route )
96
- assert .Nil (t , err , "add route failed: %v" , err )
97
-
98
- //
99
- // Test remove routes:
100
- //
101
- err = s .removeRoute (netHandle , & route )
102
- assert .Nil (t , err , "remove route failed: %v" , err )
103
-
104
- //
105
89
// Exercise the removeInterface code:
106
- //
107
90
_ , err = s .removeInterface (netHandle , & ifc )
108
91
assert .Nil (t , err , "remove interface failed: %v" , err )
109
92
110
93
// Try to remove non existent interface:
111
94
_ , err = s .removeInterface (netHandle , & ifc )
112
95
assert .NotNil (t , err , "Expected failed removal: %v" , err )
113
96
}
97
+
98
+ type teardownNetworkTest func ()
99
+
100
+ func skipUnlessRoot (t * testing.T ) {
101
+ if os .Getuid () != 0 {
102
+ t .Skip ("" )
103
+ }
104
+ }
105
+
106
+ func setupNetworkTest (t * testing.T ) teardownNetworkTest {
107
+ skipUnlessRoot (t )
108
+
109
+ // new temporary namespace so we don't pollute the host
110
+ // lock thread since the namespace is thread local
111
+ runtime .LockOSThread ()
112
+ var err error
113
+ ns , err := netns .New ()
114
+ if err != nil {
115
+ t .Fatal ("Failed to create newns" , ns )
116
+ }
117
+
118
+ return func () {
119
+ ns .Close ()
120
+ runtime .UnlockOSThread ()
121
+ }
122
+ }
123
+
124
+ func TestUpdateRoutes (t * testing.T ) {
125
+ tearDown := setupNetworkTest (t )
126
+ defer tearDown ()
127
+
128
+ s := sandbox {}
129
+
130
+ // create a dummy link which we'll play with
131
+ macAddr := net.HardwareAddr {0x02 , 0x00 , 0xCA , 0xFE , 0x00 , 0x48 }
132
+ link := & netlink.Dummy {
133
+ LinkAttrs : netlink.LinkAttrs {
134
+ MTU : 1500 ,
135
+ TxQLen : - 1 ,
136
+ Name : "ifc-name" ,
137
+ HardwareAddr : macAddr ,
138
+ },
139
+ }
140
+ netHandle , _ := netlink .NewHandle ()
141
+ defer netHandle .Delete ()
142
+
143
+ netHandle .LinkAdd (link )
144
+ if err := netHandle .LinkSetUp (link ); err != nil {
145
+ t .Fatal (err )
146
+ }
147
+ netlinkAddr , _ := netlink .ParseAddr ("192.168.0.2/16" )
148
+ netHandle .AddrAdd (link , netlinkAddr )
149
+
150
+ //Test a simple route setup:
151
+ inputRoutesSimple := []* pb.Route {
152
+ {Dest : "" , Gateway : "192.168.0.1" , Source : "" , Scope : 0 , Device : "ifc-name" },
153
+ {Dest : "192.168.0.0/16" , Gateway : "" , Source : "192.168.0.2" , Scope : 253 , Device : "ifc-name" },
154
+ }
155
+
156
+ testRoutes := & pb.Routes {
157
+ Routes : inputRoutesSimple ,
158
+ }
159
+
160
+ results , err := s .updateRoutes (netHandle , testRoutes )
161
+ assert .Nil (t , err , "Unexpected update interface failure: %v" , err )
162
+ assert .True (t , reflect .DeepEqual (results , testRoutes ),
163
+ "Interface created didn't match: got %+v, expecting %+v" , results , testRoutes )
164
+
165
+ //Test a route setup mimicking what could be provided by PTP CNI plugin:
166
+ inputRoutesPTPExample := []* pb.Route {
167
+ {Dest : "" , Gateway : "192.168.0.1" , Source : "" , Scope : 0 , Device : "ifc-name" },
168
+ {Dest : "192.168.0.144/16" , Gateway : "192.168.0.1" , Source : "192.168.0.2" , Scope : 0 , Device : "ifc-name" },
169
+ {Dest : "192.168.0.1/32" , Gateway : "" , Source : "192.168.0.2" , Scope : 254 , Device : "ifc-name" },
170
+ }
171
+ testRoutes .Routes = inputRoutesPTPExample
172
+
173
+ results , err = s .updateRoutes (netHandle , testRoutes )
174
+ assert .Nil (t , err , "Unexpected update interface failure: %v" , err )
175
+ assert .True (t , reflect .DeepEqual (results , testRoutes ),
176
+ "Interface created didn't match: got %+v, expecting %+v" , results , testRoutes )
177
+
178
+ //Test unreachable example (no scope provided for initial link route)
179
+ inputRoutesNoScope := []* pb.Route {
180
+ {Dest : "" , Gateway : "192.168.0.1" , Source : "" , Scope : 0 , Device : "ifc-name" },
181
+ {Dest : "192.168.0.0/16" , Gateway : "" , Source : "192.168.0.2" , Scope : 0 , Device : "ifc-name" },
182
+ }
183
+ testRoutes .Routes = inputRoutesNoScope
184
+ results , err = s .updateRoutes (netHandle , testRoutes )
185
+ assert .NotNil (t , err , "Expected to observe unreachable route failure" )
186
+
187
+ assert .True (t , reflect .DeepEqual (results .Routes [0 ], testRoutes .Routes [1 ]),
188
+ "Interface created didn't match: got %+v, expecting %+v" , results .Routes [0 ], testRoutes .Routes [1 ])
189
+ }
0 commit comments