1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """DUT setup library."""
16 from robot.api import logger
18 from resources.libraries.python.topology import NodeType
19 from resources.libraries.python.ssh import SSH
20 from resources.libraries.python.constants import Constants
21 from resources.libraries.python.VatExecutor import VatExecutor
24 class DUTSetup(object):
25 """Contains methods for setting up DUTs."""
27 def start_vpp_service_on_all_duts(nodes):
28 """Start up the VPP service on all nodes."""
30 for node in nodes.values():
31 if node['type'] == NodeType.DUT:
33 (ret_code, stdout, stderr) = \
34 ssh.exec_command_sudo('service vpp restart', timeout=120)
35 if int(ret_code) != 0:
36 logger.debug('stdout: {0}'.format(stdout))
37 logger.debug('stderr: {0}'.format(stderr))
38 raise Exception('DUT {0} failed to start VPP service'.
42 def vpp_show_version_verbose(node):
43 """Run "show version verbose" CLI command.
45 :param node: Node to run command on.
49 vat.execute_script("show_version_verbose.vat", node, json_out=False)
52 def vpp_api_trace_save(node):
53 """Run "api trace save" CLI command.
55 :param node: Node to run command on.
59 vat.execute_script("api_trace_save.vat", node, json_out=False)
62 def vpp_api_trace_dump(node):
63 """Run "api trace custom-dump" CLI command.
65 :param node: Node to run command on.
69 vat.execute_script("api_trace_dump.vat", node, json_out=False)
72 def setup_all_duts(nodes):
73 """Prepare all DUTs in given topology for test execution."""
74 for node in nodes.values():
75 if node['type'] == NodeType.DUT:
76 DUTSetup.setup_dut(node)
80 """Run script over SSH to setup the DUT node.
82 :param node: DUT node to set up.
85 :raises Exception: If the DUT setup fails.
90 (ret_code, stdout, stderr) = \
91 ssh.exec_command('sudo -Sn bash {0}/{1}/dut_setup.sh'.
92 format(Constants.REMOTE_FW_DIR,
93 Constants.RESOURCES_LIB_SH), timeout=120)
96 if int(ret_code) != 0:
97 logger.debug('DUT {0} setup script failed: "{1}"'.
98 format(node['host'], stdout + stderr))
99 raise Exception('DUT test setup script failed at node {}'.
100 format(node['host']))
103 def get_vpp_pid(node):
104 """Get PID of running VPP process.
106 :param node: DUT node.
110 :raises RuntimeError if it is not possible to get the PID.
115 ret_code, stdout, stderr = ssh.exec_command('pidof vpp')
120 if int(ret_code) != 0:
121 logger.debug('Not possible to get PID of VPP process on node: '
122 '{0}\n {1}'.format(node['host'], stdout + stderr))
123 raise RuntimeError('Not possible to get PID of VPP process on node:'
124 ' {}'.format(node['host']))
126 if len(stdout.splitlines()) != 1:
127 raise RuntimeError("More then one VPP PID found on node {0}".
128 format(node['host']))
132 def get_vpp_pids(nodes):
133 """Get PID of running VPP process on all DUTs.
135 :param nodes: DUT nodes.
142 for node in nodes.values():
143 if node['type'] == NodeType.DUT:
144 pids[node['host']] = DUTSetup.get_vpp_pid(node)