aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/testing/selftests/tc-testing/plugin-lib/nsPlugin.py141
-rwxr-xr-xtools/testing/selftests/tc-testing/tdc.py45
2 files changed, 142 insertions, 44 deletions
diff --git a/tools/testing/selftests/tc-testing/plugin-lib/nsPlugin.py b/tools/testing/selftests/tc-testing/plugin-lib/nsPlugin.py
new file mode 100644
index 000000000000..a194b1af2b30
--- /dev/null
+++ b/tools/testing/selftests/tc-testing/plugin-lib/nsPlugin.py
@@ -0,0 +1,141 @@
1import os
2import signal
3from string import Template
4import subprocess
5import time
6from TdcPlugin import TdcPlugin
7
8from tdc_config import *
9
10class SubPlugin(TdcPlugin):
11 def __init__(self):
12 self.sub_class = 'ns/SubPlugin'
13 super().__init__()
14
15 def pre_suite(self, testcount, testidlist):
16 '''run commands before test_runner goes into a test loop'''
17 super().pre_suite(testcount, testidlist)
18
19 if self.args.namespace:
20 self._ns_create()
21
22 def post_suite(self, index):
23 '''run commands after test_runner goes into a test loop'''
24 super().post_suite(index)
25 if self.args.verbose:
26 print('{}.post_suite'.format(self.sub_class))
27
28 if self.args.namespace:
29 self._ns_destroy()
30
31 def add_args(self, parser):
32 super().add_args(parser)
33 self.argparser_group = self.argparser.add_argument_group(
34 'netns',
35 'options for nsPlugin(run commands in net namespace)')
36 self.argparser_group.add_argument(
37 '-n', '--namespace', action='store_true',
38 help='Run commands in namespace')
39 return self.argparser
40
41 def adjust_command(self, stage, command):
42 super().adjust_command(stage, command)
43 cmdform = 'list'
44 cmdlist = list()
45
46 if not self.args.namespace:
47 return command
48
49 if self.args.verbose:
50 print('{}.adjust_command'.format(self.sub_class))
51
52 if not isinstance(command, list):
53 cmdform = 'str'
54 cmdlist = command.split()
55 else:
56 cmdlist = command
57 if stage == 'setup' or stage == 'execute' or stage == 'verify' or stage == 'teardown':
58 if self.args.verbose:
59 print('adjust_command: stage is {}; inserting netns stuff in command [{}] list [{}]'.format(stage, command, cmdlist))
60 cmdlist.insert(0, self.args.NAMES['NS'])
61 cmdlist.insert(0, 'exec')
62 cmdlist.insert(0, 'netns')
63 cmdlist.insert(0, 'ip')
64 else:
65 pass
66
67 if cmdform == 'str':
68 command = ' '.join(cmdlist)
69 else:
70 command = cmdlist
71
72 if self.args.verbose:
73 print('adjust_command: return command [{}]'.format(command))
74 return command
75
76 def _ns_create(self):
77 '''
78 Create the network namespace in which the tests will be run and set up
79 the required network devices for it.
80 '''
81 if self.args.namespace:
82 cmd = 'ip netns add {}'.format(self.args.NAMES['NS'])
83 self._exec_cmd('pre', cmd)
84 cmd = 'ip link add $DEV0 type veth peer name $DEV1'
85 self._exec_cmd('pre', cmd)
86 cmd = 'ip link set $DEV1 netns {}'.format(self.args.NAMES['NS'])
87 self._exec_cmd('pre', cmd)
88 cmd = 'ip link set $DEV0 up'
89 self._exec_cmd('pre', cmd)
90 cmd = 'ip -n {} link set $DEV1 up'.format(self.args.NAMES['NS'])
91 self._exec_cmd('pre', cmd)
92 if self.args.device:
93 cmd = 'ip link set $DEV2 netns {}'.format(self.args.NAMES['NS'])
94 self._exec_cmd('pre', cmd)
95 cmd = 'ip -n {} link set $DEV2 up'.format(self.args.NAMES['NS'])
96 self._exec_cmd('pre', cmd)
97
98 def _ns_destroy(self):
99 '''
100 Destroy the network namespace for testing (and any associated network
101 devices as well)
102 '''
103 if self.args.namespace:
104 cmd = 'ip netns delete {}'.format(self.args.NAMES['NS'])
105 self._exec_cmd('post', cmd)
106
107 def _exec_cmd(self, stage, command):
108 '''
109 Perform any required modifications on an executable command, then run
110 it in a subprocess and return the results.
111 '''
112 if '$' in command:
113 command = self._replace_keywords(command)
114
115 self.adjust_command(stage, command)
116 if self.args.verbose:
117 print('_exec_cmd: command "{}"'.format(command))
118 proc = subprocess.Popen(command,
119 shell=True,
120 stdout=subprocess.PIPE,
121 stderr=subprocess.PIPE,
122 env=ENVIR)
123 (rawout, serr) = proc.communicate()
124
125 if proc.returncode != 0 and len(serr) > 0:
126 foutput = serr.decode("utf-8")
127 else:
128 foutput = rawout.decode("utf-8")
129
130 proc.stdout.close()
131 proc.stderr.close()
132 return proc, foutput
133
134 def _replace_keywords(self, cmd):
135 """
136 For a given executable command, substitute any known
137 variables contained within NAMES with the correct values
138 """
139 tcmd = Template(cmd)
140 subcmd = tcmd.safe_substitute(self.args.NAMES)
141 return subcmd
diff --git a/tools/testing/selftests/tc-testing/tdc.py b/tools/testing/selftests/tc-testing/tdc.py
index a718d2b57739..b3754b9aa302 100755
--- a/tools/testing/selftests/tc-testing/tdc.py
+++ b/tools/testing/selftests/tc-testing/tdc.py
@@ -23,8 +23,6 @@ from tdc_helper import *
23 23
24import TdcPlugin 24import TdcPlugin
25 25
26USE_NS = True
27
28class PluginMgr: 26class PluginMgr:
29 def __init__(self, argparser): 27 def __init__(self, argparser):
30 super().__init__() 28 super().__init__()
@@ -107,16 +105,13 @@ def replace_keywords(cmd):
107 return subcmd 105 return subcmd
108 106
109 107
110def exec_cmd(args, pm, stage, command, nsonly=True): 108def exec_cmd(args, pm, stage, command):
111 """ 109 """
112 Perform any required modifications on an executable command, then run 110 Perform any required modifications on an executable command, then run
113 it in a subprocess and return the results. 111 it in a subprocess and return the results.
114 """ 112 """
115 if len(command.strip()) == 0: 113 if len(command.strip()) == 0:
116 return None, None 114 return None, None
117 if (USE_NS and nsonly):
118 command = 'ip netns exec $NS ' + command
119
120 if '$' in command: 115 if '$' in command:
121 command = replace_keywords(command) 116 command = replace_keywords(command)
122 117
@@ -265,39 +260,6 @@ def test_runner(pm, args, filtered_tests):
265 260
266 return tap 261 return tap
267 262
268
269def ns_create(args, pm):
270 """
271 Create the network namespace in which the tests will be run and set up
272 the required network devices for it.
273 """
274 if (USE_NS):
275 cmd = 'ip netns add $NS'
276 exec_cmd(args, pm, 'pre', cmd, False)
277 cmd = 'ip link add $DEV0 type veth peer name $DEV1'
278 exec_cmd(args, pm, 'pre', cmd, False)
279 cmd = 'ip link set $DEV1 netns $NS'
280 exec_cmd(args, pm, 'pre', cmd, False)
281 cmd = 'ip link set $DEV0 up'
282 exec_cmd(args, pm, 'pre', cmd, False)
283 cmd = 'ip -n $NS link set $DEV1 up'
284 exec_cmd(args, pm, 'pre', cmd, False)
285 cmd = 'ip link set $DEV2 netns $NS'
286 exec_cmd(args, pm, 'pre', cmd, False)
287 cmd = 'ip -n $NS link set $DEV2 up'
288 exec_cmd(args, pm, 'pre', cmd, False)
289
290
291def ns_destroy(args, pm):
292 """
293 Destroy the network namespace for testing (and any associated network
294 devices as well)
295 """
296 if (USE_NS):
297 cmd = 'ip netns delete $NS'
298 exec_cmd(args, pm, 'post', cmd, False)
299
300
301def has_blank_ids(idlist): 263def has_blank_ids(idlist):
302 """ 264 """
303 Search the list for empty ID fields and return true/false accordingly. 265 Search the list for empty ID fields and return true/false accordingly.
@@ -579,17 +541,12 @@ def set_operation_mode(pm, args):
579 list_test_cases(alltests) 541 list_test_cases(alltests)
580 exit(0) 542 exit(0)
581 543
582 ns_create(args, pm)
583
584 if len(alltests): 544 if len(alltests):
585 catresults = test_runner(pm, args, alltests) 545 catresults = test_runner(pm, args, alltests)
586 else: 546 else:
587 catresults = 'No tests found\n' 547 catresults = 'No tests found\n'
588 print('All test results: \n\n{}'.format(catresults)) 548 print('All test results: \n\n{}'.format(catresults))
589 549
590 ns_destroy(args, pm)
591
592
593def main(): 550def main():
594 """ 551 """
595 Start of execution; set up argument parser and get the arguments, 552 Start of execution; set up argument parser and get the arguments,