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