Merge "NSB Topology fix for Prox 4 port test case"
[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             self.client.execute("sudo bash pre_setup.sh")
63
64     def _get_controller_node(self, host_list):
65         for host_name in host_list:
66             node = self.nodes.get(host_name, None)
67             node_role = node.get('role', None)
68             if node_role == 'Controller':
69                 return host_name
70         return None
71
72     def setup(self):
73         """scenario setup"""
74         self.setup_script = pkg_resources.resource_filename(
75             'yardstick.benchmark.scenarios.networking',
76             Ping6.SETUP_SCRIPT)
77
78         self.setup_odl_script = pkg_resources.resource_filename(
79             'yardstick.benchmark.scenarios.networking',
80             Ping6.SETUP_ODL_SCRIPT)
81
82         self.pre_setup_script = pkg_resources.resource_filename(
83             'yardstick.benchmark.scenarios.networking',
84             Ping6.PRE_SETUP_SCRIPT)
85
86         self.ping6_metadata_script = pkg_resources.resource_filename(
87             'yardstick.benchmark.scenarios.networking',
88             Ping6.METADATA_SCRIPT)
89
90         self.ping6_radvd_script = pkg_resources.resource_filename(
91             'yardstick.benchmark.scenarios.networking',
92             Ping6.RADVD_SCRIPT)
93
94         host_str = self.options.get("host", 'host1')
95         self.host_list = host_str.split(',')
96         self.host_list.sort()
97         pre_setup = self.options.get("pre_setup", True)
98         if pre_setup:
99             self._pre_setup()
100
101         # log in a contronller node to setup
102         controller_node_name = self._get_controller_node(self.host_list)
103         LOG.debug("The Controller Node is: %s", controller_node_name)
104         if controller_node_name is None:
105             LOG.exception("Can't find controller node in the context!!!")
106         self._ssh_host(controller_node_name)
107         self.client._put_file_shell(
108             self.ping6_metadata_script, '~/metadata.txt')
109
110         # run script to setup ipv6 with nosdn or odl
111         sdn = self.options.get("sdn", 'nosdn')
112         if 'odl' in sdn:
113             self.client._put_file_shell(
114                 self.ping6_radvd_script, '~/br-ex.radvd.conf')
115             self.client._put_file_shell(
116                 self.setup_odl_script, '~/setup_odl.sh')
117             setup_bash_file = "setup_odl.sh"
118         else:
119             self.client._put_file_shell(self.setup_script, '~/setup.sh')
120             setup_bash_file = "setup.sh"
121         cmd = "sudo bash %s %s %s" % \
122               (setup_bash_file, self.openrc, self.external_network)
123         LOG.debug("Executing setup command: %s", cmd)
124         self.client.execute(cmd)
125
126         self.setup_done = True
127
128     def run(self, result):
129         """execute the benchmark"""
130         # ssh vm1
131         self.ping6_script = pkg_resources.resource_filename(
132             'yardstick.benchmark.scenarios.networking',
133             Ping6.TARGET_SCRIPT)
134
135         self.ping6_find_host_script = pkg_resources.resource_filename(
136             'yardstick.benchmark.scenarios.networking',
137             Ping6.FIND_HOST_SCRIPT)
138         if not self.setup_done:
139             host_str = self.options.get("host", 'host1')
140             self.host_list = host_str.split(',')
141             self.host_list.sort()
142             self._ssh_host(self.host_list[0])
143
144         # find ipv4-int-network1 to ssh VM
145         self.client._put_file_shell(
146             self.ping6_find_host_script, '~/find_host.sh')
147         cmd = "sudo bash find_host.sh %s" % self.openrc
148         LOG.debug("Executing find_host command: %s", cmd)
149         status, stdout, stderr = self.client.execute(cmd)
150         host_name = stdout.strip()
151
152         # copy vRouterKey to target host
153         self.client.run("cat ~/vRouterKey",
154                         stdout=open("/tmp/vRouterKey", "w"))
155         self._ssh_host(host_name)
156         self.client.run("cat > ~/vRouterKey",
157                         stdin=open("/tmp/vRouterKey", "rb"))
158
159         # run ping6 benchmark
160         self.client._put_file_shell(self.ping6_script, '~/ping6.sh')
161         cmd = "sudo bash ping6.sh %s %s" % (self.openrc, self.ping_options)
162         LOG.debug("Executing ping6 command: %s", cmd)
163         status, stdout, stderr = self.client.execute(cmd)
164
165         if status:
166             raise RuntimeError(stderr)
167
168         # sla
169         if stdout:
170             result["rtt"] = float(stdout)
171             if "sla" in self.scenario_cfg:
172                 sla_max_rtt = int(self.scenario_cfg["sla"]["max_rtt"])
173                 self.verify_SLA(result["rtt"] <= sla_max_rtt,
174                                 "rtt %f > sla:max_rtt(%f); "
175                                 % (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             self.client.execute("sudo bash post_teardown.sh")