Merge "Add host&targer in scenario['options']['server_name'] support"
[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         self.client = ssh.SSH.from_node(node, defaults={"user": "ubuntu"})
55         self.client.wait(timeout=60)
56
57     def _pre_setup(self):
58         for node_name in self.host_list:
59             self._ssh_host(node_name)
60             self.client._put_file_shell(
61                 self.pre_setup_script, '~/pre_setup.sh')
62             status, stdout, stderr = self.client.execute(
63                 "sudo bash pre_setup.sh")
64
65     def _get_controller_node(self, host_list):
66         for host_name in host_list:
67             node = self.nodes.get(host_name, None)
68             node_role = node.get('role', None)
69             if node_role == 'Controller':
70                 return host_name
71         return None
72
73     def setup(self):
74         """scenario setup"""
75         self.setup_script = pkg_resources.resource_filename(
76             'yardstick.benchmark.scenarios.networking',
77             Ping6.SETUP_SCRIPT)
78
79         self.setup_odl_script = pkg_resources.resource_filename(
80             'yardstick.benchmark.scenarios.networking',
81             Ping6.SETUP_ODL_SCRIPT)
82
83         self.pre_setup_script = pkg_resources.resource_filename(
84             'yardstick.benchmark.scenarios.networking',
85             Ping6.PRE_SETUP_SCRIPT)
86
87         self.ping6_metadata_script = pkg_resources.resource_filename(
88             'yardstick.benchmark.scenarios.networking',
89             Ping6.METADATA_SCRIPT)
90
91         self.ping6_radvd_script = pkg_resources.resource_filename(
92             'yardstick.benchmark.scenarios.networking',
93             Ping6.RADVD_SCRIPT)
94
95         host_str = self.options.get("host", 'host1')
96         self.host_list = host_str.split(',')
97         self.host_list.sort()
98         pre_setup = self.options.get("pre_setup", True)
99         if pre_setup:
100             self._pre_setup()
101
102         # log in a contronller node to setup
103         controller_node_name = self._get_controller_node(self.host_list)
104         LOG.debug("The Controller Node is: %s", controller_node_name)
105         if controller_node_name is None:
106             LOG.exception("Can't find controller node in the context!!!")
107         self._ssh_host(controller_node_name)
108         self.client._put_file_shell(
109             self.ping6_metadata_script, '~/metadata.txt')
110
111         # run script to setup ipv6 with nosdn or odl
112         sdn = self.options.get("sdn", 'nosdn')
113         if 'odl' in sdn:
114             self.client._put_file_shell(
115                 self.ping6_radvd_script, '~/br-ex.radvd.conf')
116             self.client._put_file_shell(
117                 self.setup_odl_script, '~/setup_odl.sh')
118             setup_bash_file = "setup_odl.sh"
119         else:
120             self.client._put_file_shell(self.setup_script, '~/setup.sh')
121             setup_bash_file = "setup.sh"
122         cmd = "sudo bash %s %s %s" % \
123               (setup_bash_file, self.openrc, self.external_network)
124         LOG.debug("Executing setup command: %s", cmd)
125         status, stdout, stderr = self.client.execute(cmd)
126
127         self.setup_done = True
128
129     def run(self, result):
130         """execute the benchmark"""
131         # ssh vm1
132         self.ping6_script = pkg_resources.resource_filename(
133             'yardstick.benchmark.scenarios.networking',
134             Ping6.TARGET_SCRIPT)
135
136         self.ping6_find_host_script = pkg_resources.resource_filename(
137             'yardstick.benchmark.scenarios.networking',
138             Ping6.FIND_HOST_SCRIPT)
139         if not self.setup_done:
140             host_str = self.options.get("host", 'host1')
141             self.host_list = host_str.split(',')
142             self.host_list.sort()
143             self._ssh_host(self.host_list[0])
144
145         # find ipv4-int-network1 to ssh VM
146         self.client._put_file_shell(
147             self.ping6_find_host_script, '~/find_host.sh')
148         cmd = "sudo bash find_host.sh %s" % self.openrc
149         LOG.debug("Executing find_host command: %s", cmd)
150         status, stdout, stderr = self.client.execute(cmd)
151         host_name = stdout.strip()
152
153         # copy vRouterKey to target host
154         self.client.run("cat ~/vRouterKey",
155                         stdout=open("/tmp/vRouterKey", "w"))
156         self._ssh_host(host_name)
157         self.client.run("cat > ~/vRouterKey",
158                         stdin=open("/tmp/vRouterKey", "rb"))
159
160         # run ping6 benchmark
161         self.client._put_file_shell(self.ping6_script, '~/ping6.sh')
162         cmd = "sudo bash ping6.sh %s %s" % (self.openrc, self.ping_options)
163         LOG.debug("Executing ping6 command: %s", cmd)
164         status, stdout, stderr = self.client.execute(cmd)
165
166         if status:
167             raise RuntimeError(stderr)
168
169         # sla
170         if stdout:
171             result["rtt"] = float(stdout)
172             if "sla" in self.scenario_cfg:
173                 sla_max_rtt = int(self.scenario_cfg["sla"]["max_rtt"])
174                 assert result["rtt"] <= sla_max_rtt, \
175                     "rtt %f > sla:max_rtt(%f); " % (result["rtt"], sla_max_rtt)
176         else:
177             LOG.error("ping6 timeout!!!")
178         self.run_done = True
179
180     def teardown(self):
181         """teardown the benchmark"""
182
183         self.post_teardown_script = pkg_resources.resource_filename(
184             'yardstick.benchmark.scenarios.networking',
185             Ping6.POST_TEARDOWN_SCRIPT)
186
187         host_str = self.options.get("host", 'node1')
188         self.host_list = host_str.split(',')
189         self.host_list.sort()
190
191         if not self.run_done:
192             self._ssh_host(self.host_list[0])
193
194         self.teardown_script = pkg_resources.resource_filename(
195             'yardstick.benchmark.scenarios.networking',
196             Ping6.TEARDOWN_SCRIPT)
197         self.client._put_file_shell(self.teardown_script, '~/teardown.sh')
198         cmd = "sudo bash teardown.sh %s %s" % \
199               (self.openrc, self.external_network)
200         status, stdout, stderr = self.client.execute(cmd)
201
202         post_teardown = self.options.get("post_teardown", True)
203         if post_teardown:
204             self._post_teardown()
205
206         if status:
207             raise RuntimeError(stderr)
208
209         if stdout:
210             pass
211         else:
212             LOG.error("ping6 teardown failed")
213
214     def _post_teardown(self):
215         for node_name in self.host_list:
216             self._ssh_host(node_name)
217             self.client._put_file_shell(
218                 self.post_teardown_script, '~/post_teardown.sh')
219             status, stdout, stderr = self.client.execute(
220                 "sudo bash post_teardown.sh")