9bf18bb9eb8311ce678038ad5c7c286678a0a715
[csit.git] / resources / libraries / python / DMM / SingleCliSer.py
1 # Copyright (c) 2018 Huawei Technologies Co.,Ltd.
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 """
16 This module exists to provide the vs_epoll ping test for DMM on topology nodes.
17 """
18 import time
19
20 from resources.libraries.python.ssh import SSH
21 from resources.libraries.python.DMM.DMMConstants import DMMConstants as con
22 from resources.libraries.python.topology import Topology
23 from robot.api import logger
24
25 class SingleCliSer(object):
26     """Test the DMM vs_epoll ping function."""
27
28     @staticmethod
29     def exec_the_base_vs_epoll_test(dut1_node, dut2_node):
30         """Execute the vs_epoll on the dut1_node.
31
32         :param dut1_node: Will execute the vs_epoll on this node.
33         :param dut2_node: Will execute the vc_epoll on this node.
34         :type dut1_node: dict
35         :type dut2_node: dict
36         :returns: positive value if packets are sent and received
37         :raises RuntimeError:If failed to execute vs_epoll test on  dut1_node.
38         """
39         dut1_ip = Topology.get_node_hostname(dut1_node)
40         dut2_ip = Topology.get_node_hostname(dut2_node)
41
42         ssh = SSH()
43         ssh.connect(dut1_node)
44
45         cmd = 'cd {0}/{1} && ./run_dmm.sh {2} {3} {4} ' \
46         .format(con.REMOTE_FW_DIR, con.DMM_SCRIPTS, dut1_ip, dut2_ip, 0)
47
48         cmd += '2>&1 | tee log_run_dmm.txt &'
49
50         (ret_code, _, _) = ssh.exec_command(cmd, timeout=6000)
51         if ret_code != 0:
52             raise RuntimeError('Failed to execute vs_epoll test at node {0}'
53                                .format(dut1_node['host']))
54
55         time.sleep(10)
56
57         ssh = SSH()
58         ssh.connect(dut2_node)
59
60         cmd = 'cd {0}/{1} && ./run_dmm.sh {2} {3} {4} ' \
61         .format(con.REMOTE_FW_DIR, con.DMM_SCRIPTS, dut1_ip, dut2_ip, 1)
62
63         cmd += '2>&1 | tee log_run_dmm.txt'
64
65         (ret_code, stdout_cli, _) = ssh.exec_command(cmd, timeout=6000)
66         if ret_code != 0:
67             raise RuntimeError('Failed to execute vs_epoll test at node {0}'
68                                .format(dut1_node['host']))
69
70         return stdout_cli.find("send 50000")
71
72     @staticmethod
73     def get_the_test_result(dut_node):
74         """
75         After executing exec_the_base_vs_epoll_test, use this
76         to get the test result
77
78         :param dut_node: will get the test result in this dut node
79         :type dut_node: dict
80         :returns: str.
81         :rtype: str.
82         :raises RuntimeError: If failed to get the test result.
83         """
84         ssh = SSH()
85         ssh.connect(dut_node)
86         cmd = 'cat {0}/{1}/log_run_dmm.txt | grep "send 50000" | wc -l' \
87         .format(con.REMOTE_FW_DIR, con.DMM_SCRIPTS)
88
89         (ret_code, stdout, _) = ssh.exec_command(cmd, timeout=100)
90         if ret_code != 0:
91             raise RuntimeError('Failed to get test result at node {0}'
92                                .format(dut_node['host']))
93
94         return stdout
95
96     @staticmethod
97     def echo_dmm_logs(dut_node):
98         """
99         :param dut_node:
100         :return:
101         """
102         ssh = SSH()
103         ssh.connect(dut_node)
104         cmd = 'cat {0}/{1}/log_install_dmm.txt' \
105         .format(con.REMOTE_FW_DIR, con.DMM_SCRIPTS)
106
107         (ret_code, stdout, _) = ssh.exec_command(cmd, timeout=100)
108         if ret_code != 0:
109             raise RuntimeError('Failed to get log_install_dmm at node {0}'
110                                .format(dut_node['host']))
111         else:
112             logger.console('....log_install_dmm on node {1}.... {0}'
113                            .format(stdout, dut_node['host']))
114