@@ -13,12 +13,18 @@ use crate::utils::test_utils::{check_container_created, CreateOptions};
13
13
use crate :: utils:: { test_inside_container, test_outside_container} ;
14
14
15
15
static NETNS_COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
16
+ static DEVICE_COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
16
17
17
18
fn create_unique_netns_name ( prefix : & str ) -> String {
18
19
let count = NETNS_COUNTER . fetch_add ( 1 , Ordering :: SeqCst ) ;
19
20
format ! ( "{}-{}" , prefix, count)
20
21
}
21
22
23
+ pub fn create_unique_device_name ( prefix : & str ) -> String {
24
+ let count = DEVICE_COUNTER . fetch_add ( 1 , Ordering :: SeqCst ) ;
25
+ format ! ( "{}-{}" , prefix, count)
26
+ }
27
+
22
28
fn create_netns ( name : & str ) -> Result < ( ) > {
23
29
std:: process:: Command :: new ( "ip" )
24
30
. args ( vec ! [ "netns" , "add" , name] )
@@ -90,104 +96,104 @@ fn create_spec_with_netns(net_devices: HashMap<String, LinuxNetDevice>, netns: S
90
96
}
91
97
92
98
fn check_net_device ( ) -> TestResult {
93
- const DUMMY_DEVICE : & str = "dummy" ;
99
+ let device_name = create_unique_device_name ( "dummy" ) ;
94
100
95
- if let Err ( e) = create_dummy_device ( DUMMY_DEVICE ) {
101
+ if let Err ( e) = create_dummy_device ( & device_name ) {
96
102
return TestResult :: Failed ( anyhow ! ( "Failed to create dummy device: {}" , e) ) ;
97
103
}
98
104
99
105
let mut net_devices = HashMap :: new ( ) ;
100
- net_devices. insert ( DUMMY_DEVICE . to_string ( ) , LinuxNetDevice :: default ( ) ) ;
106
+ net_devices. insert ( device_name . clone ( ) , LinuxNetDevice :: default ( ) ) ;
101
107
let spec = create_spec ( net_devices) ;
102
108
test_inside_container ( & spec, & CreateOptions :: default ( ) , & |_| Ok ( ( ) ) ) ;
103
109
104
- match check_device_exists ( DUMMY_DEVICE ) {
110
+ match check_device_exists ( & device_name ) {
105
111
Ok ( true ) => {
106
- if let Err ( e) = delete_dummy_device ( DUMMY_DEVICE ) {
112
+ if let Err ( e) = delete_dummy_device ( & device_name ) {
107
113
return TestResult :: Failed ( anyhow ! ( "Failed to delete device: {}" , e) ) ;
108
114
}
109
- return TestResult :: Failed ( anyhow ! ( "Device still exists after test" ) ) ;
115
+ TestResult :: Failed ( anyhow ! ( "The device still exists after test" ) )
110
116
}
111
117
Ok ( false ) => TestResult :: Passed ,
112
118
Err ( e) => {
113
- if let Err ( e) = delete_dummy_device ( DUMMY_DEVICE ) {
119
+ if let Err ( e) = delete_dummy_device ( & device_name ) {
114
120
return TestResult :: Failed ( anyhow ! ( "Failed to delete device: {}" , e) ) ;
115
121
}
116
- return TestResult :: Failed ( anyhow ! ( "Failed to check device: {}" , e) ) ;
122
+ TestResult :: Failed ( anyhow ! ( "Failed to check device: {}" , e) )
117
123
}
118
124
}
119
125
}
120
126
121
127
fn check_net_device_rename ( ) -> TestResult {
122
- const DUMMY_DEVICE : & str = "dummy-rename" ;
123
- const DUMMY_DEVICE_RENAMED : & str = "dummy1 -renamed";
128
+ let device_name = create_unique_device_name ( "dummy-rename" ) ;
129
+ let device_name_rename = create_unique_device_name ( "dummy -renamed") ;
124
130
125
- if let Err ( e) = create_dummy_device ( DUMMY_DEVICE ) {
131
+ if let Err ( e) = create_dummy_device ( & device_name ) {
126
132
return TestResult :: Failed ( anyhow ! ( "Failed to create dummy device: {}" , e) ) ;
127
133
}
128
134
129
135
let mut net_devices = HashMap :: new ( ) ;
130
136
net_devices. insert (
131
- DUMMY_DEVICE . to_string ( ) ,
137
+ device_name . clone ( ) ,
132
138
LinuxNetDeviceBuilder :: default ( )
133
- . name ( DUMMY_DEVICE_RENAMED )
139
+ . name ( & device_name_rename )
134
140
. build ( )
135
141
. unwrap ( ) ,
136
142
) ;
137
143
let spec = create_spec ( net_devices) ;
138
144
test_inside_container ( & spec, & CreateOptions :: default ( ) , & |_| Ok ( ( ) ) ) ;
139
145
140
- match check_device_exists ( DUMMY_DEVICE ) {
146
+ match check_device_exists ( & device_name ) {
141
147
Ok ( true ) => {
142
- if let Err ( e) = delete_dummy_device ( DUMMY_DEVICE ) {
148
+ if let Err ( e) = delete_dummy_device ( & device_name ) {
143
149
return TestResult :: Failed ( anyhow ! ( "Failed to delete device: {}" , e) ) ;
144
150
}
145
- return TestResult :: Failed ( anyhow ! ( "Device still exists after test" ) ) ;
151
+ TestResult :: Failed ( anyhow ! ( "The device still exists after test" ) )
146
152
}
147
153
Ok ( false ) => TestResult :: Passed ,
148
154
Err ( e) => {
149
- if let Err ( e) = delete_dummy_device ( DUMMY_DEVICE ) {
155
+ if let Err ( e) = delete_dummy_device ( & device_name ) {
150
156
return TestResult :: Failed ( anyhow ! ( "Failed to delete device: {}" , e) ) ;
151
157
}
152
- return TestResult :: Failed ( anyhow ! ( "Failed to check device: {}" , e) ) ;
158
+ TestResult :: Failed ( anyhow ! ( "Failed to check device: {}" , e) )
153
159
}
154
160
}
155
161
}
156
162
157
163
fn check_net_devices ( ) -> TestResult {
158
- const DUMMY_DEVICE1 : & str = "dummy1" ;
159
- const DUMMY_DEVICE2 : & str = "dummy2" ;
164
+ let device_name1 = create_unique_device_name ( "dummy1" ) ;
165
+ let device_name2 = create_unique_device_name ( "dummy2" ) ;
160
166
161
- if let Err ( e) = create_dummy_device ( DUMMY_DEVICE1 ) {
167
+ if let Err ( e) = create_dummy_device ( & device_name1 ) {
162
168
return TestResult :: Failed ( anyhow ! ( "Failed to create dummy device: {}" , e) ) ;
163
169
}
164
170
165
- if let Err ( e) = create_dummy_device ( DUMMY_DEVICE2 ) {
171
+ if let Err ( e) = create_dummy_device ( & device_name2 ) {
166
172
return TestResult :: Failed ( anyhow ! ( "Failed to create dummy device: {}" , e) ) ;
167
173
}
168
174
169
175
let mut net_devices = HashMap :: new ( ) ;
170
- net_devices. insert ( DUMMY_DEVICE1 . to_string ( ) , LinuxNetDevice :: default ( ) ) ;
171
- net_devices. insert ( DUMMY_DEVICE2 . to_string ( ) , LinuxNetDevice :: default ( ) ) ;
176
+ net_devices. insert ( device_name1 . clone ( ) , LinuxNetDevice :: default ( ) ) ;
177
+ net_devices. insert ( device_name2 . clone ( ) , LinuxNetDevice :: default ( ) ) ;
172
178
let spec = create_spec ( net_devices) ;
173
179
test_inside_container ( & spec, & CreateOptions :: default ( ) , & |_| Ok ( ( ) ) ) ;
174
180
175
181
let mut result = TestResult :: Passed ;
176
182
177
- match check_device_exists ( DUMMY_DEVICE1 ) {
183
+ match check_device_exists ( & device_name1 ) {
178
184
Ok ( true ) => {
179
- result = TestResult :: Failed ( anyhow ! ( "Device1 still exists after test" ) ) ;
185
+ result = TestResult :: Failed ( anyhow ! ( "The device1 still exists after test" ) ) ;
180
186
}
181
187
Ok ( false ) => { }
182
188
Err ( e) => {
183
189
result = TestResult :: Failed ( anyhow ! ( "Failed to check device1: {}" , e) ) ;
184
190
}
185
191
}
186
192
187
- match check_device_exists ( DUMMY_DEVICE2 ) {
193
+ match check_device_exists ( & device_name2 ) {
188
194
Ok ( true ) => {
189
195
if let TestResult :: Passed = result {
190
- result = TestResult :: Failed ( anyhow ! ( "Device2 still exists after test" ) ) ;
196
+ result = TestResult :: Failed ( anyhow ! ( "The device2 still exists after test" ) ) ;
191
197
}
192
198
}
193
199
Ok ( false ) => { }
@@ -199,16 +205,17 @@ fn check_net_devices() -> TestResult {
199
205
}
200
206
201
207
// cleanup both devices regardless of test result
202
- let _ = delete_dummy_device ( DUMMY_DEVICE1 ) ;
203
- let _ = delete_dummy_device ( DUMMY_DEVICE2 ) ;
208
+ let _ = delete_dummy_device ( & device_name1 ) ;
209
+ let _ = delete_dummy_device ( & device_name2 ) ;
204
210
205
211
result
206
212
}
207
213
208
214
fn check_empty_net_devices ( ) -> TestResult {
209
- const DUMMY_DEVICE : & str = "dummy-empty" ;
215
+ let device_name = create_unique_device_name ( "dummy-empty" ) ;
216
+
210
217
let mut net_devices = HashMap :: new ( ) ;
211
- net_devices. insert ( DUMMY_DEVICE . to_string ( ) , LinuxNetDevice :: default ( ) ) ;
218
+ net_devices. insert ( device_name . clone ( ) , LinuxNetDevice :: default ( ) ) ;
212
219
let spec = create_spec ( net_devices) ;
213
220
let result = test_inside_container ( & spec, & CreateOptions :: default ( ) , & |_| Ok ( ( ) ) ) ;
214
221
@@ -224,13 +231,13 @@ fn check_empty_net_devices() -> TestResult {
224
231
225
232
fn check_back_device ( ) -> TestResult {
226
233
let netns_name = create_unique_netns_name ( "netns-back" ) ;
227
- const DUMMY_DEVICE : & str = "dummy-back" ;
234
+ let device_name = create_unique_device_name ( "dummy-back" ) ;
228
235
229
236
let mut net_devices = HashMap :: new ( ) ;
230
237
net_devices. insert (
231
- DUMMY_DEVICE . to_string ( ) ,
238
+ device_name . clone ( ) ,
232
239
LinuxNetDeviceBuilder :: default ( )
233
- . name ( DUMMY_DEVICE . to_string ( ) )
240
+ . name ( & device_name )
234
241
. build ( )
235
242
. unwrap ( ) ,
236
243
) ;
@@ -239,7 +246,7 @@ fn check_back_device() -> TestResult {
239
246
return TestResult :: Failed ( anyhow ! ( "Failed to create netns: {}" , e) ) ;
240
247
}
241
248
242
- if let Err ( e) = create_dummy_device ( DUMMY_DEVICE ) {
249
+ if let Err ( e) = create_dummy_device ( & device_name ) {
243
250
return TestResult :: Failed ( anyhow ! ( "Failed to create dummy device: {}" , e) ) ;
244
251
}
245
252
@@ -269,7 +276,7 @@ fn check_back_device() -> TestResult {
269
276
"link" ,
270
277
"set" ,
271
278
"dev" ,
272
- DUMMY_DEVICE ,
279
+ & device_name ,
273
280
"netns" ,
274
281
"1" ,
275
282
] )
@@ -279,11 +286,11 @@ fn check_back_device() -> TestResult {
279
286
}
280
287
281
288
// Check that the device exists
282
- if let Err ( e) = check_device_exists ( DUMMY_DEVICE ) {
289
+ if let Err ( e) = check_device_exists ( & device_name ) {
283
290
return TestResult :: Failed ( anyhow ! ( "Failed to check device: {}" , e) ) ;
284
291
}
285
292
286
- if let Err ( e) = delete_dummy_device ( DUMMY_DEVICE ) {
293
+ if let Err ( e) = delete_dummy_device ( & device_name ) {
287
294
return TestResult :: Failed ( anyhow ! ( "Failed to delete device: {}" , e) ) ;
288
295
}
289
296
@@ -296,14 +303,14 @@ fn check_back_device() -> TestResult {
296
303
297
304
fn check_address ( ) -> TestResult {
298
305
let netns_name = create_unique_netns_name ( "netns-address" ) ;
299
- const DUMMY_DEVICE : & str = "dummy-address" ;
306
+ let device_name = create_unique_device_name ( "dummy-address" ) ;
300
307
const DUMMY_ADDRESS : & str = "244.178.44.111/24" ;
301
308
302
309
let mut net_devices = HashMap :: new ( ) ;
303
310
net_devices. insert (
304
- DUMMY_DEVICE . to_string ( ) ,
311
+ device_name . clone ( ) ,
305
312
LinuxNetDeviceBuilder :: default ( )
306
- . name ( DUMMY_DEVICE . to_string ( ) )
313
+ . name ( & device_name )
307
314
. build ( )
308
315
. unwrap ( ) ,
309
316
) ;
@@ -312,13 +319,13 @@ fn check_address() -> TestResult {
312
319
return TestResult :: Failed ( anyhow ! ( "Failed to create netns: {}" , e) ) ;
313
320
}
314
321
315
- if let Err ( e) = create_dummy_device ( DUMMY_DEVICE ) {
322
+ if let Err ( e) = create_dummy_device ( & device_name ) {
316
323
return TestResult :: Failed ( anyhow ! ( "Failed to create dummy device: {}" , e) ) ;
317
324
}
318
325
319
326
// Add address to the device
320
327
if let Err ( e) = std:: process:: Command :: new ( "ip" )
321
- . args ( vec ! [ "addr" , "add" , DUMMY_ADDRESS , "dev" , DUMMY_DEVICE ] )
328
+ . args ( vec ! [ "addr" , "add" , DUMMY_ADDRESS , "dev" , & device_name ] )
322
329
. output ( )
323
330
{
324
331
return TestResult :: Failed ( anyhow ! ( "Failed to add address: {}" , e) ) ;
@@ -352,7 +359,7 @@ fn check_address() -> TestResult {
352
359
Ok ( out) => out,
353
360
Err ( e) => return TestResult :: Failed ( anyhow ! ( "Failed to parse output: {}" , e) ) ,
354
361
} ;
355
- if !out. contains ( DUMMY_DEVICE ) || !out. contains ( DUMMY_ADDRESS ) {
362
+ if !out. contains ( & device_name ) || !out. contains ( DUMMY_ADDRESS ) {
356
363
return TestResult :: Failed ( anyhow ! ( "Address not found in output" ) ) ;
357
364
}
358
365
0 commit comments