456457f9f6c55efa031bfe188bc45b8b90974252
[csit.git] / resources / libraries / python / SFC / SFCTest.py
1 # Copyright (c) 2017 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 """
15 This module implements functionality which configure and start
16 the NSH SFC functional test.
17 """
18
19 from resources.libraries.python.ssh import SSH
20 from resources.libraries.python.constants import Constants as con
21 from resources.libraries.python.topology import Topology
22
23 class SFCTest(object):
24     """Configure and Start the NSH SFC functional tests."""
25
26     @staticmethod
27     def config_and_start_SFC_test(dut_node, dut_port, adj_mac, testtype):
28         """
29         Start the SFC functional on the dut_node.
30
31         :param dut_node: Will execute the SFC on this node.
32         :param dut_port: The ingress interface on the DUT.
33         :param adj_mac: The adjacency interface MAC.
34         :param testtype: The SFC functional test type.
35                          (Classifier, Proxy Inbound, Proxy Outbound, SFF).
36         :type dut_node: dict
37         :type dut_port: str
38         :type adj_mac: str
39         :type testtype: str
40         :returns: none
41         :raises RuntimeError: If the script execute fails.
42         """
43
44         vpp_intf_name = Topology.get_interface_name(dut_node, dut_port)
45
46         ssh = SSH()
47         ssh.connect(dut_node)
48
49         if testtype == "Classifier":
50             exec_shell = "set_sfc_classifier.sh"
51         elif testtype == "Proxy Inbound":
52             exec_shell = "set_nsh_proxy_inbound.sh"
53         elif testtype == "Proxy Outbound":
54             exec_shell = "set_nsh_proxy_outbound.sh"
55         else:
56             exec_shell = "set_sfc_sff.sh"
57
58         cmd = 'cd {0}/nsh_sfc_tests/sfc_scripts/ && sudo ./{1} {2} ' \
59               '{3} {4}'.format(con.REMOTE_FW_DIR, exec_shell, vpp_intf_name,
60                                adj_mac, dut_port)
61
62         (ret_code, _, _) = ssh.exec_command(cmd, timeout=600)
63         if ret_code != 0:
64             raise RuntimeError('Failed to execute SFC setup script ' \
65                  '{0} at node {1}'.format(exec_shell, dut_node['host']))
66
67     @staticmethod
68     def start_the_tcpdump_on_the_node(from_node, from_port, filter_ip):
69         """
70         Start the tcpdump on the frome_node.
71
72         :param from_node: Will execute the tcpdump on this node.
73         :param from_port: Will capture the packets on this interface.
74         :param filter_ip: filter the dest ip.
75         :type from_node: dict
76         :type from_port: str
77         :type filter_ip: str
78         :returns: none
79         :raises RuntimeError: If the script "start_tcpdump.sh" fails.
80         """
81
82         interface_name = Topology.get_interface_name(from_node, from_port)
83
84         ssh = SSH()
85         ssh.connect(from_node)
86
87         cmd = 'cd {0}/nsh_sfc_tests/sfc_scripts/ && sudo ./start_tcpdump.sh ' \
88               '{1} {2}'.format(con.REMOTE_FW_DIR, interface_name, filter_ip)
89
90         (ret_code, _, _) = ssh.exec_command(cmd, timeout=600)
91         if ret_code != 0:
92             raise RuntimeError('Failed to exec start_tcpdump.sh at node {0}'.
93                                format(from_node['host']))