Fix flake8 errors
[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     SETUP_SCRIPT = 'ping6_setup.bash'
29     TEARDOWN_SCRIPT = 'ping6_teardown.bash'
30     METADATA_SCRIPT = 'ping6_metadata.txt'
31
32     def __init__(self, scenario_cfg, context_cfg):
33         self.scenario_cfg = scenario_cfg
34         self.context_cfg = context_cfg
35         self.setup_done = False
36         self.run_done = False
37
38     def _ssh_host(self):
39         # ssh host1
40         host = self.context_cfg['host']
41         host_user = host.get('user', 'ubuntu')
42         host_ip = host.get('ip', None)
43         host_pwd = host.get('password', 'root')
44         LOG.info("user:%s, host:%s", host_user, host_ip)
45         self.client = ssh.SSH(host_user, host_ip, password=host_pwd)
46         self.client.wait(timeout=600)
47
48     def setup(self):
49         '''scenario setup'''
50         self.setup_script = pkg_resources.resource_filename(
51             'yardstick.benchmark.scenarios.networking',
52             Ping6.SETUP_SCRIPT)
53
54         self.ping6_metadata_script = pkg_resources.resource_filename(
55             'yardstick.benchmark.scenarios.networking',
56             Ping6.METADATA_SCRIPT)
57         # ssh host1
58         self._ssh_host()
59         # run script to setup ipv6
60         self.client.run("cat > ~/setup.sh",
61                         stdin=open(self.setup_script, "rb"))
62         self.client.run("cat > ~/metadata.txt",
63                         stdin=open(self.ping6_metadata_script, "rb"))
64         cmd = "sudo bash setup.sh"
65         status, stdout, stderr = self.client.execute(cmd)
66
67         self.setup_done = True
68
69     def run(self, result):
70         """execute the benchmark"""
71         # ssh vm1
72         self.ping6_script = pkg_resources.resource_filename(
73             'yardstick.benchmark.scenarios.networking',
74             Ping6.TARGET_SCRIPT)
75
76         if not self.setup_done:
77             self._ssh_host()
78
79         self.client.run("cat > ~/ping6.sh",
80                         stdin=open(self.ping6_script, "rb"))
81         cmd = "sudo bash ping6.sh"
82         LOG.debug("Executing command: %s", cmd)
83         status, stdout, stderr = self.client.execute(cmd)
84         print stdout
85         if status:
86             raise RuntimeError(stderr)
87
88         if stdout:
89             result["rtt"] = float(stdout)
90
91             if "sla" in self.scenario_cfg:
92                 sla_max_rtt = int(self.scenario_cfg["sla"]["max_rtt"])
93                 assert result["rtt"] <= sla_max_rtt, \
94                     "rtt %f > sla:max_rtt(%f); " % (result["rtt"], sla_max_rtt)
95         else:
96             LOG.error("ping6 timeout")
97         self.run_done = True
98
99     def teardown(self):
100         """teardown the benchmark"""
101
102         if not self.run_done:
103             self._ssh_host()
104
105         self.teardown_script = pkg_resources.resource_filename(
106             'yardstick.benchmark.scenarios.networking',
107             Ping6.TEARDOWN_SCRIPT)
108         self.client.run("cat > ~/teardown.sh",
109                         stdin=open(self.teardown_script, "rb"))
110         cmd = "sudo bash teardown.sh"
111         status, stdout, stderr = self.client.execute(cmd)
112
113         if status:
114             raise RuntimeError(stderr)
115
116         if stdout:
117             pass
118         else:
119             LOG.error("ping6 teardown failed")