3 from string import Template
6 from TdcPlugin import TdcPlugin
8 from tdc_config import *
10 class SubPlugin(TdcPlugin):
12 self.sub_class = 'ns/SubPlugin'
15 def pre_suite(self, testcount, testidlist):
16 '''run commands before test_runner goes into a test loop'''
17 super().pre_suite(testcount, testidlist)
19 if self.args.namespace:
22 def post_suite(self, index):
23 '''run commands after test_runner goes into a test loop'''
24 super().post_suite(index)
26 print('{}.post_suite'.format(self.sub_class))
28 if self.args.namespace:
31 def add_args(self, parser):
32 super().add_args(parser)
33 self.argparser_group = self.argparser.add_argument_group(
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')
41 def adjust_command(self, stage, command):
42 super().adjust_command(stage, command)
46 if not self.args.namespace:
50 print('{}.adjust_command'.format(self.sub_class))
52 if not isinstance(command, list):
54 cmdlist = command.split()
57 if stage == 'setup' or stage == 'execute' or stage == 'verify' or stage == 'teardown':
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')
68 command = ' '.join(cmdlist)
73 print('adjust_command: return command [{}]'.format(command))
78 Create the network namespace in which the tests will be run and set up
79 the required network devices for it.
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)
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)
98 def _ns_destroy(self):
100 Destroy the network namespace for testing (and any associated network
103 if self.args.namespace:
104 cmd = 'ip netns delete {}'.format(self.args.NAMES['NS'])
105 self._exec_cmd('post', cmd)
107 def _exec_cmd(self, stage, command):
109 Perform any required modifications on an executable command, then run
110 it in a subprocess and return the results.
113 command = self._replace_keywords(command)
115 self.adjust_command(stage, command)
116 if self.args.verbose:
117 print('_exec_cmd: command "{}"'.format(command))
118 proc = subprocess.Popen(command,
120 stdout=subprocess.PIPE,
121 stderr=subprocess.PIPE,
123 (rawout, serr) = proc.communicate()
125 if proc.returncode != 0 and len(serr) > 0:
126 foutput = serr.decode("utf-8")
128 foutput = rawout.decode("utf-8")
134 def _replace_keywords(self, cmd):
136 For a given executable command, substitute any known
137 variables contained within NAMES with the correct values
140 subcmd = tcmd.safe_substitute(self.args.NAMES)