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
:
24 def post_suite(self
, index
):
25 '''run commands after test_runner goes into a test loop'''
26 super().post_suite(index
)
28 print('{}.post_suite'.format(self
.sub_class
))
30 if self
.args
.namespace
:
35 def add_args(self
, parser
):
36 super().add_args(parser
)
37 self
.argparser_group
= self
.argparser
.add_argument_group(
39 'options for nsPlugin(run commands in net namespace)')
40 self
.argparser_group
.add_argument(
41 '-N', '--no-namespace', action
='store_false', default
=True,
42 dest
='namespace', help='Don\'t run commands in namespace')
45 def adjust_command(self
, stage
, command
):
46 super().adjust_command(stage
, command
)
50 if not self
.args
.namespace
:
54 print('{}.adjust_command'.format(self
.sub_class
))
56 if not isinstance(command
, list):
58 cmdlist
= command
.split()
61 if stage
== 'setup' or stage
== 'execute' or stage
== 'verify' or stage
== 'teardown':
63 print('adjust_command: stage is {}; inserting netns stuff in command [{}] list [{}]'.format(stage
, command
, cmdlist
))
64 cmdlist
.insert(0, self
.args
.NAMES
['NS'])
65 cmdlist
.insert(0, 'exec')
66 cmdlist
.insert(0, 'netns')
67 cmdlist
.insert(0, self
.args
.NAMES
['IP'])
72 command
= ' '.join(cmdlist
)
77 print('adjust_command: return command [{}]'.format(command
))
80 def _ports_create(self
):
81 cmd
= '$IP link add $DEV0 type veth peer name $DEV1'
82 self
._exec
_cmd
('pre', cmd
)
83 cmd
= '$IP link set $DEV0 up'
84 self
._exec
_cmd
('pre', cmd
)
85 if not self
.args
.namespace
:
86 cmd
= '$IP link set $DEV1 up'
87 self
._exec
_cmd
('pre', cmd
)
89 def _ports_destroy(self
):
90 cmd
= '$IP link del $DEV0'
91 self
._exec
_cmd
('post', cmd
)
95 Create the network namespace in which the tests will be run and set up
96 the required network devices for it.
99 if self
.args
.namespace
:
100 cmd
= '$IP netns add {}'.format(self
.args
.NAMES
['NS'])
101 self
._exec
_cmd
('pre', cmd
)
102 cmd
= '$IP link set $DEV1 netns {}'.format(self
.args
.NAMES
['NS'])
103 self
._exec
_cmd
('pre', cmd
)
104 cmd
= '$IP -n {} link set $DEV1 up'.format(self
.args
.NAMES
['NS'])
105 self
._exec
_cmd
('pre', cmd
)
107 cmd
= '$IP link set $DEV2 netns {}'.format(self
.args
.NAMES
['NS'])
108 self
._exec
_cmd
('pre', cmd
)
109 cmd
= '$IP -n {} link set $DEV2 up'.format(self
.args
.NAMES
['NS'])
110 self
._exec
_cmd
('pre', cmd
)
112 def _ns_destroy(self
):
114 Destroy the network namespace for testing (and any associated network
117 if self
.args
.namespace
:
118 cmd
= '$IP netns delete {}'.format(self
.args
.NAMES
['NS'])
119 self
._exec
_cmd
('post', cmd
)
121 def _exec_cmd(self
, stage
, command
):
123 Perform any required modifications on an executable command, then run
124 it in a subprocess and return the results.
127 command
= self
._replace
_keywords
(command
)
129 self
.adjust_command(stage
, command
)
130 if self
.args
.verbose
:
131 print('_exec_cmd: command "{}"'.format(command
))
132 proc
= subprocess
.Popen(command
,
134 stdout
=subprocess
.PIPE
,
135 stderr
=subprocess
.PIPE
,
137 (rawout
, serr
) = proc
.communicate()
139 if proc
.returncode
!= 0 and len(serr
) > 0:
140 foutput
= serr
.decode("utf-8")
142 foutput
= rawout
.decode("utf-8")
148 def _replace_keywords(self
, cmd
):
150 For a given executable command, substitute any known
151 variables contained within NAMES with the correct values
154 subcmd
= tcmd
.safe_substitute(self
.args
.NAMES
)