Merge "Add how to add/modify Yardstick Grafana dashboard in user guide"
[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.run("cat > ~/pre_setup.sh",
73                             stdin=open(self.pre_setup_script, "rb"))
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.run("cat > ~/metadata.txt",
121                         stdin=open(self.ping6_metadata_script, "rb"))
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.run("cat > ~/br-ex.radvd.conf",
127                             stdin=open(self.ping6_radvd_script, "rb"))
128             self.client.run("cat > ~/setup_odl.sh",
129                             stdin=open(self.setup_odl_script, "rb"))
130             setup_bash_file = "setup_odl.sh"
131         else:
132             self.client.run("cat > ~/setup.sh",
133                             stdin=open(self.setup_script, "rb"))
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.run("cat > ~/find_host.sh",
160                         stdin=open(self.ping6_find_host_script, "rb"))
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.run("cat > ~/ping6.sh",
175                         stdin=open(self.ping6_script, "rb"))
176         cmd = "sudo bash ping6.sh %s %s" % (self.openrc, self.ping_options)
177         LOG.debug("Executing ping6 command: %s", cmd)
178         status, stdout, stderr = self.client.execute(cmd)
179
180         if status:
181             raise RuntimeError(stderr)
182
183         # sla
184         if stdout:
185             result["rtt"] = float(stdout)
186             if "sla" in self.scenario_cfg:
187                 sla_max_rtt = int(self.scenario_cfg["sla"]["max_rtt"])
188                 assert result["rtt"] <= sla_max_rtt, \
189                     "rtt %f > sla:max_rtt(%f); " % (result["rtt"], sla_max_rtt)
190         else:
191             LOG.error("ping6 timeout!!!")
192         self.run_done = True
193
194     def teardown(self):
195         """teardown the benchmark"""
196
197         self.post_teardown_script = pkg_resources.resource_filename(
198             'yardstick.benchmark.scenarios.networking',
199             Ping6.POST_TEARDOWN_SCRIPT)
200
201         host_str = self.options.get("host", 'node1')
202         self.host_list = host_str.split(',')
203         self.host_list.sort()
204
205         if not self.run_done:
206             self._ssh_host(self.host_list[0])
207
208         self.teardown_script = pkg_resources.resource_filename(
209             'yardstick.benchmark.scenarios.networking',
210             Ping6.TEARDOWN_SCRIPT)
211         self.client.run("cat > ~/teardown.sh",
212                         stdin=open(self.teardown_script, "rb"))
213         cmd = "sudo bash teardown.sh %s %s" % \
214               (self.openrc, self.external_network)
215         status, stdout, stderr = self.client.execute(cmd)
216
217         post_teardown = self.options.get("post_teardown", True)
218         if post_teardown:
219             self._post_teardown()
220
221         if status:
222             raise RuntimeError(stderr)
223
224         if stdout:
225             pass
226         else:
227             LOG.error("ping6 teardown failed")
228
229     def _post_teardown(self):
230         for node_name in self.host_list:
231             self._ssh_host(node_name)
232             self.client.run("cat > ~/post_teardown.sh",
233                             stdin=open(self.post_teardown_script, "rb"))
234             status, stdout, stderr = self.client.execute(
235                 "sudo bash post_teardown.sh")