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