17
17
netlink = False
18
18
print ("!!! Consider installing pyroute2 !!!" )
19
19
20
- def prepare_suite (obj , test ):
21
- original = obj .args .NAMES
22
-
23
- if 'skip' in test and test ['skip' ] == 'yes' :
24
- return
25
-
26
- if 'nsPlugin' not in test ['plugins' ]:
27
- return
28
-
29
- shadow = {}
30
- shadow ['IP' ] = original ['IP' ]
31
- shadow ['TC' ] = original ['TC' ]
32
- shadow ['NS' ] = '{}-{}' .format (original ['NS' ], test ['random' ])
33
- shadow ['DEV0' ] = '{}id{}' .format (original ['DEV0' ], test ['id' ])
34
- shadow ['DEV1' ] = '{}id{}' .format (original ['DEV1' ], test ['id' ])
35
- shadow ['DUMMY' ] = '{}id{}' .format (original ['DUMMY' ], test ['id' ])
36
- shadow ['DEV2' ] = original ['DEV2' ]
37
- obj .args .NAMES = shadow
38
-
39
- if netlink == True :
40
- obj ._nl_ns_create ()
41
- else :
42
- obj ._ns_create ()
43
-
44
- # Make sure the netns is visible in the fs
45
- while True :
46
- obj ._proc_check ()
47
- try :
48
- ns = obj .args .NAMES ['NS' ]
49
- f = open ('/run/netns/{}' .format (ns ))
50
- f .close ()
51
- break
52
- except :
53
- time .sleep (0.1 )
54
- continue
55
-
56
- obj .args .NAMES = original
57
-
58
20
class SubPlugin (TdcPlugin ):
59
21
def __init__ (self ):
60
22
self .sub_class = 'ns/SubPlugin'
@@ -65,37 +27,63 @@ def pre_suite(self, testcount, testlist):
65
27
66
28
super ().pre_suite (testcount , testlist )
67
29
68
- print ("Setting up namespaces and devices..." )
30
+ def prepare_test (self , test ):
31
+ if 'skip' in test and test ['skip' ] == 'yes' :
32
+ return
69
33
70
- with Pool (self .args .mp ) as p :
71
- it = zip (cycle ([self ]), testlist )
72
- p .starmap (prepare_suite , it )
34
+ if 'nsPlugin' not in test ['plugins' ]:
35
+ return
73
36
74
- def pre_case (self , caseinfo , test_skip ):
37
+ if netlink == True :
38
+ self ._nl_ns_create ()
39
+ else :
40
+ self ._ns_create ()
41
+
42
+ # Make sure the netns is visible in the fs
43
+ ticks = 20
44
+ while True :
45
+ if ticks == 0 :
46
+ raise TimeoutError
47
+ self ._proc_check ()
48
+ try :
49
+ ns = self .args .NAMES ['NS' ]
50
+ f = open ('/run/netns/{}' .format (ns ))
51
+ f .close ()
52
+ break
53
+ except :
54
+ time .sleep (0.1 )
55
+ ticks -= 1
56
+ continue
57
+
58
+ def pre_case (self , test , test_skip ):
75
59
if self .args .verbose :
76
60
print ('{}.pre_case' .format (self .sub_class ))
77
61
78
62
if test_skip :
79
63
return
80
64
65
+ self .prepare_test (test )
66
+
81
67
def post_case (self ):
82
68
if self .args .verbose :
83
69
print ('{}.post_case' .format (self .sub_class ))
84
70
85
- self ._ns_destroy ()
71
+ if netlink == True :
72
+ self ._nl_ns_destroy ()
73
+ else :
74
+ self ._ns_destroy ()
86
75
87
76
def post_suite (self , index ):
88
77
if self .args .verbose :
89
78
print ('{}.post_suite' .format (self .sub_class ))
90
79
91
80
# Make sure we don't leak resources
92
- for f in os .listdir ('/run/netns/' ):
93
- cmd = self ._replace_keywords ("$IP netns del {}" .format (f ))
81
+ cmd = "$IP -a netns del"
94
82
95
- if self .args .verbose > 3 :
96
- print ('_exec_cmd: command "{}"' .format (cmd ))
83
+ if self .args .verbose > 3 :
84
+ print ('_exec_cmd: command "{}"' .format (cmd ))
97
85
98
- subprocess .run (cmd , shell = True , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
86
+ subprocess .run (cmd , shell = True , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
99
87
100
88
def adjust_command (self , stage , command ):
101
89
super ().adjust_command (stage , command )
@@ -143,7 +131,10 @@ def _nl_ns_create(self):
143
131
with IPRoute () as ip :
144
132
ip .link ('add' , ifname = dev1 , kind = 'veth' , peer = {'ifname' : dev0 , 'net_ns_fd' :'/proc/1/ns/net' })
145
133
ip .link ('add' , ifname = dummy , kind = 'dummy' )
134
+ ticks = 20
146
135
while True :
136
+ if ticks == 0 :
137
+ raise TimeoutError
147
138
try :
148
139
dev1_idx = ip .link_lookup (ifname = dev1 )[0 ]
149
140
dummy_idx = ip .link_lookup (ifname = dummy )[0 ]
@@ -152,17 +143,22 @@ def _nl_ns_create(self):
152
143
break
153
144
except :
154
145
time .sleep (0.1 )
146
+ ticks -= 1
155
147
continue
156
148
netns .popns ()
157
149
158
150
with IPRoute () as ip :
151
+ ticks = 20
159
152
while True :
153
+ if ticks == 0 :
154
+ raise TimeoutError
160
155
try :
161
156
dev0_idx = ip .link_lookup (ifname = dev0 )[0 ]
162
157
ip .link ('set' , index = dev0_idx , state = 'up' )
163
158
break
164
159
except :
165
160
time .sleep (0.1 )
161
+ ticks -= 1
166
162
continue
167
163
168
164
def _ns_create_cmds (self ):
@@ -192,6 +188,10 @@ def _ns_create(self):
192
188
'''
193
189
self ._exec_cmd_batched ('pre' , self ._ns_create_cmds ())
194
190
191
+ def _nl_ns_destroy (self ):
192
+ ns = self .args .NAMES ['NS' ]
193
+ netns .remove (ns )
194
+
195
195
def _ns_destroy_cmd (self ):
196
196
return self ._replace_keywords ('netns delete {}' .format (self .args .NAMES ['NS' ]))
197
197
0 commit comments