CSIT-465: Common test setup and teardown
[csit.git] / resources / libraries / python / DUTSetup.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """DUT setup library."""
15
16 from robot.api import logger
17
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
22
23
24 class DUTSetup(object):
25     """Contains methods for setting up DUTs."""
26     @staticmethod
27     def start_vpp_service_on_all_duts(nodes):
28         """Start up the VPP service on all nodes."""
29         ssh = SSH()
30         for node in nodes.values():
31             if node['type'] == NodeType.DUT:
32                 ssh.connect(node)
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'.
39                                     format(node['host']))
40
41     @staticmethod
42     def vpp_show_version_verbose(node):
43         """Run "show version verbose" CLI command.
44
45         :param node: Node to run command on.
46         :type node: dict
47         """
48         vat = VatExecutor()
49         vat.execute_script("show_version_verbose.vat", node, json_out=False)
50
51     @staticmethod
52     def vpp_api_trace_save(node):
53         """Run "api trace save" CLI command.
54
55         :param node: Node to run command on.
56         :type node: dict
57         """
58         vat = VatExecutor()
59         vat.execute_script("api_trace_save.vat", node, json_out=False)
60
61     @staticmethod
62     def vpp_api_trace_dump(node):
63         """Run "api trace custom-dump" CLI command.
64
65         :param node: Node to run command on.
66         :type node: dict
67         """
68         vat = VatExecutor()
69         vat.execute_script("api_trace_dump.vat", node, json_out=False)
70
71     @staticmethod
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)
77
78     @staticmethod
79     def setup_dut(node):
80         """Run script over SSH to setup the DUT node.
81
82         :param node: DUT node to set up.
83         :type node: dict
84
85         :raises Exception: If the DUT setup fails.
86         """
87         ssh = SSH()
88         ssh.connect(node)
89
90         (ret_code, stdout, stderr) = \
91             ssh.exec_command('sudo -Sn bash {0}/{1}/dut_setup.sh'.format(
92                 Constants.REMOTE_FW_DIR, Constants.RESOURCES_LIB_SH), timeout=120)
93         logger.trace(stdout)
94         logger.trace(stderr)
95         if int(ret_code) != 0:
96             logger.debug('DUT {0} setup script failed: "{1}"'.
97                          format(node['host'], stdout + stderr))
98             raise Exception('DUT test setup script failed at node {}'.
99                             format(node['host']))
100
101     @staticmethod
102     def get_vpp_pid(node):
103         """Get PID of running VPP process.
104
105         :param node: DUT node.
106         :type node: dict
107         :return: PID
108         :rtype: int
109         :raises RuntimeError if it is not possible to get the PID.
110         """
111
112         ssh = SSH()
113         ssh.connect(node)
114         ret_code, stdout, stderr = ssh.exec_command('pidof vpp')
115
116         logger.trace(stdout)
117         logger.trace(stderr)
118
119         if int(ret_code) != 0:
120             logger.debug('Not possible to get PID of VPP process on node: '
121                          '"{1}"'.format(node['host'], stdout + stderr))
122             raise RuntimeError('Not possible to get PID of VPP process on node:'
123                                ' {}'.format(node['host']))
124
125         if len(stdout.splitlines()) != 1:
126             raise RuntimeError("More then one VPP PID found on node {0}".
127                                format(node['host']))
128         return int(stdout)
129
130     @staticmethod
131     def get_vpp_pids(nodes):
132         """Get PID of running VPP process on all DUTs.
133
134         :param nodes: DUT nodes.
135         :type nodes: dict
136         :return: PIDs
137         :rtype: dict
138         """
139
140         pids = dict()
141         for node in nodes.values():
142             if node['type'] == NodeType.DUT:
143                 pids[node['host']] = DUTSetup.get_vpp_pid(node)
144         return pids