Merge "enhance test cases description and provide more info(in progress)"
[yardstick.git] / yardstick / benchmark / scenarios / networking / ping6.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import pkg_resources
11 import logging
12
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
15
16 LOG = logging.getLogger(__name__)
17
18
19 class Ping6(base.Scenario):  # pragma: no cover
20     """Execute ping6 between two hosts
21
22     read link below for more ipv6 info description:
23     http://wiki.opnfv.org/ipv6_opnfv_project
24     """
25     __scenario_type__ = "Ping6"
26
27     TARGET_SCRIPT = 'ping6_benchmark.bash'
28     PRE_SETUP_SCRIPT = 'ping6_pre_setup.bash'
29     SETUP_SCRIPT = 'ping6_setup.bash'
30     SETUP_ODL_SCRIPT = 'ping6_setup_with_odl.bash'
31     FIND_HOST_SCRIPT = 'ping6_find_host.bash'
32     TEARDOWN_SCRIPT = 'ping6_teardown.bash'
33     METADATA_SCRIPT = 'ping6_metadata.txt'
34     RADVD_SCRIPT = 'ping6_radvd.conf'
35     POST_TEARDOWN_SCRIPT = 'ping6_post_teardown.bash'
36
37     def __init__(self, scenario_cfg, context_cfg):
38         self.scenario_cfg = scenario_cfg
39         self.context_cfg = context_cfg
40         self.nodes = context_cfg['nodes']
41         self.options = scenario_cfg['options']
42         self.setup_done = False
43         self.run_done = False
44         self.external_network = self.options.get("external_network", "ext-net")
45         self.ping_options = "-s %s -c %s" % \
46             (self.options.get("packetsize", '56'),
47              self.options.get("ping_count", '5'))
48         self.openrc = self.options.get("openrc", "/opt/admin-openrc.sh")
49
50     def _ssh_host(self, node_name):
51         # ssh host
52         node = self.nodes.get(node_name, None)
53         user = node.get('user', 'ubuntu')
54         ssh_port = node.get("ssh_port", ssh.DEFAULT_PORT)
55         ip = node.get('ip', None)
56         pwd = node.get('password', None)
57         key_fname = node.get('key_filename', '/root/.ssh/id_rsa')
58         if pwd is not None:
59             LOG.debug("Log in via pw, user:%s, host:%s, password:%s",
60                       user, ip, pwd)
61             self.client = ssh.SSH(user, ip, password=pwd, port=ssh_port)
62         else:
63             LOG.debug("Log in via key, user:%s, host:%s, key_filename:%s",
64                       user, ip, key_fname)
65             self.client = ssh.SSH(user, ip, key_filename=key_fname,
66                                   port=ssh_port)
67         self.client.wait(timeout=60)
68
69     def _pre_setup(self):
70         for node_name in self.host_list:
71             self._ssh_host(node_name)
72             self.client._put_file_shell(
73                 self.pre_setup_script, '~/pre_setup.sh')
74             status, stdout, stderr = self.client.execute(
75                 "sudo bash pre_setup.sh")
76
77     def _get_controller_node(self, host_list):
78         for host_name in host_list:
79             node = self.nodes.get(host_name, None)
80             node_role = node.get('role', None)
81             if node_role == 'Controller':
82                 return host_name
83         return None
84
85     def setup(self):
86         '''scenario setup'''
87         self.setup_script = pkg_resources.resource_filename(
88             'yardstick.benchmark.scenarios.networking',
89             Ping6.SETUP_SCRIPT)
90
91         self.setup_odl_script = pkg_resources.resource_filename(
92             'yardstick.benchmark.scenarios.networking',
93             Ping6.SETUP_ODL_SCRIPT)
94
95         self.pre_setup_script = pkg_resources.resource_filename(
96             'yardstick.benchmark.scenarios.networking',
97             Ping6.PRE_SETUP_SCRIPT)
98
99         self.ping6_metadata_script = pkg_resources.resource_filename(
100             'yardstick.benchmark.scenarios.networking',
101             Ping6.METADATA_SCRIPT)
102
103         self.ping6_radvd_script = pkg_resources.resource_filename(
104             'yardstick.benchmark.scenarios.networking',
105             Ping6.RADVD_SCRIPT)
106
107         host_str = self.options.get("host", 'host1')
108         self.host_list = host_str.split(',')
109         self.host_list.sort()
110         pre_setup = self.options.get("pre_setup", True)
111         if pre_setup:
112             self._pre_setup()
113
114         # log in a contronller node to setup
115         controller_node_name = self._get_controller_node(self.host_list)
116         LOG.debug("The Controller Node is: %s", controller_node_name)
117         if controller_node_name is None:
118             LOG.exception("Can't find controller node in the context!!!")
119         self._ssh_host(controller_node_name)
120         self.client._put_file_shell(
121             self.ping6_metadata_script, '~/metadata.txt')
122
123         # run script to setup ipv6 with nosdn or odl
124         sdn = self.options.get("sdn", 'nosdn')
125         if 'odl' in sdn:
126             self.client._put_file_shell(
127                 self.ping6_radvd_script, '~/br-ex.radvd.conf')
128             self.client._put_file_shell(
129                 self.setup_odl_script, '~/setup_odl.sh')
130             setup_bash_file = "setup_odl.sh"
131         else:
132             self.client._put_file_shell(self.setup_script, '~/setup.sh')
133             setup_bash_file = "setup.sh"
134         cmd = "sudo bash %s %s %s" % \
135               (setup_bash_file, self.openrc, self.external_network)
136         LOG.debug("Executing setup command: %s", cmd)
137         status, stdout, stderr = self.client.execute(cmd)
138
139         self.setup_done = True
140
141     def run(self, result):
142         """execute the benchmark"""
143         # ssh vm1
144         self.ping6_script = pkg_resources.resource_filename(
145             'yardstick.benchmark.scenarios.networking',
146             Ping6.TARGET_SCRIPT)
147
148         self.ping6_find_host_script = pkg_resources.resource_filename(
149             'yardstick.benchmark.scenarios.networking',
150             Ping6.FIND_HOST_SCRIPT)
151         if not self.setup_done:
152             host_str = self.options.get("host", 'host1')
153             self.host_list = host_str.split(',')
154             self.host_list.sort()
155             self._ssh_host(self.host_list[0])
156
157         # find ipv4-int-network1 to ssh VM
158         self.client._put_file_shell(
159             self.ping6_find_host_script, '~/find_host.sh')
160         cmd = "sudo bash find_host.sh %s" % self.openrc
161         LOG.debug("Executing find_host command: %s", cmd)
162         status, stdout, stderr = self.client.execute(cmd)
163         host_name = stdout.strip()
164
165         # copy vRouterKey to target host
166         self.client.run("cat ~/vRouterKey",
167                         stdout=open("/tmp/vRouterKey", "w"))
168         self._ssh_host(host_name)
169         self.client.run("cat > ~/vRouterKey",
170                         stdin=open("/tmp/vRouterKey", "rb"))
171
172         # run ping6 benchmark
173         self.client._put_file_shell(self.ping6_script, '~/ping6.sh')
174         cmd = "sudo bash ping6.sh %s %s" % (self.openrc, self.ping_options)
175         LOG.debug("Executing ping6 command: %s", cmd)
176         status, stdout, stderr = self.client.execute(cmd)
177
178         if status:
179             raise RuntimeError(stderr)
180
181         # sla
182         if stdout:
183             result["rtt"] = float(stdout)
184             if "sla" in self.scenario_cfg:
185                 sla_max_rtt = int(self.scenario_cfg["sla"]["max_rtt"])
186                 assert result["rtt"] <= sla_max_rtt, \
187                     "rtt %f > sla:max_rtt(%f); " % (result["rtt"], sla_max_rtt)
188         else:
189             LOG.error("ping6 timeout!!!")
190         self.run_done = True
191
192     def teardown(self):
193         """teardown the benchmark"""
194
195         self.post_teardown_script = pkg_resources.resource_filename(
196             'yardstick.benchmark.scenarios.networking',
197             Ping6.POST_TEARDOWN_SCRIPT)
198
199         host_str = self.options.get("host", 'node1')
200         self.host_list = host_str.split(',')
201         self.host_list.sort()
202
203         if not self.run_done:
204             self._ssh_host(self.host_list[0])
205
206         self.teardown_script = pkg_resources.resource_filename(
207             'yardstick.benchmark.scenarios.networking',
208             Ping6.TEARDOWN_SCRIPT)
209         self.client._put_file_shell(self.teardown_script, '~/teardown.sh')
210         cmd = "sudo bash teardown.sh %s %s" % \
211               (self.openrc, self.external_network)
212         status, stdout, stderr = self.client.execute(cmd)
213
214         post_teardown = self.options.get("post_teardown", True)
215         if post_teardown:
216             self._post_teardown()
217
218         if status:
219             raise RuntimeError(stderr)
220
221         if stdout:
222             pass
223         else:
224             LOG.error("ping6 teardown failed")
225
226     def _post_teardown(self):
227         for node_name in self.host_list:
228             self._ssh_host(node_name)
229             self.client._put_file_shell(
230                 self.post_teardown_script, '~/post_teardown.sh')
231             status, stdout, stderr = self.client.execute(
232                 "sudo bash post_teardown.sh")