add yardstick iruya 9.0.0 release notes
[yardstick.git] / yardstick / benchmark / scenarios / networking / sfc.py
1 ##############################################################################
2 # Copyright (c) 2017 Ericsson AB 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 from __future__ import absolute_import
10
11 import logging
12 import subprocess
13
14 import pkg_resources
15 from six.moves import range
16
17 import yardstick.ssh as ssh
18 from yardstick.benchmark.scenarios import base
19 from yardstick.benchmark.scenarios.networking import sfc_openstack
20
21 LOG = logging.getLogger(__name__)
22
23
24 class Sfc(base.Scenario):  # pragma: no cover
25     """ SFC scenario class """
26
27     __scenario_type__ = "sfc"
28
29     PRE_SETUP_SCRIPT = 'sfc_pre_setup.bash'
30     TACKER_SCRIPT = 'sfc_tacker.bash'
31     SERVER_SCRIPT = 'sfc_server.bash'
32     TEARDOWN_SCRIPT = "sfc_teardown.bash"
33     TACKER_CHANGECLASSI = "sfc_change_classi.bash"
34
35     def __init__(self, scenario_cfg, context_cfg):  # pragma: no cover
36         self.scenario_cfg = scenario_cfg
37         self.context_cfg = context_cfg
38         self.setup_done = False
39         self.teardown_done = False
40
41     def setup(self):
42         """scenario setup"""
43         self.tacker_script = pkg_resources.resource_filename(
44             'yardstick.benchmark.scenarios.networking',
45             Sfc.TACKER_SCRIPT)
46
47         self.server_script = pkg_resources.resource_filename(
48             'yardstick.benchmark.scenarios.networking',
49             Sfc.SERVER_SCRIPT)
50
51         """ calling Tacker to instantiate VNFs and Service Chains """
52         cmd_tacker = "%s" % (self.tacker_script)
53         subprocess.call(cmd_tacker, shell=True)
54
55         target = self.context_cfg['target']
56
57         """ webserver start automatically during the vm boot """
58         LOG.info("user:%s, target:%s", target['user'], target['ip'])
59         self.server = ssh.SSH.from_node(target, defaults={
60             "user": "root", "password": "opnfv"
61         })
62         self.server.wait(timeout=600)
63         self.server._put_file_shell(self.server_script, '~/server.sh')
64         cmd_server = "sudo bash server.sh"
65         LOG.debug("Executing command: %s", cmd_server)
66         status, stdout, stderr = self.server.execute(cmd_server)
67         LOG.debug("Output server command: %s", status)
68
69         ips = sfc_openstack.get_an_IP()
70
71         target = self.context_cfg['target']
72
73         LOG.info("user:%s, target:%s", target['user'], target['ip'])
74         self.server = ssh.SSH.from_node(
75             target,
76             defaults={"user": "root", "password": "opnfv"},
77             # we must override ip
78             overrides={"ip": ips[0]}
79         )
80         self.server.wait(timeout=600)
81         cmd_SF1 = ("nohup python vxlan_tool.py -i eth0 "
82                    "-d forward -v off -b 80 &")
83         LOG.debug("Starting HTTP firewall in SF1")
84         self.server.execute(cmd_SF1)
85         result = self.server.execute("ps lax | grep python")
86         if "vxlan_tool.py" in result[1]:  # pragma: no cover
87             LOG.debug("HTTP firewall started")
88
89         LOG.info("user:%s, target:%s", target['user'], target['ip'])
90         self.server = ssh.SSH.from_node(
91             target,
92             defaults={"user": "root", "password": "opnfv"},
93             # we must override ip
94             overrides={"ip": ips[1]}
95         )
96         self.server.wait(timeout=600)
97         cmd_SF2 = ("nohup python vxlan_tool.py -i eth0 "
98                    "-d forward -v off -b 22 &")
99         LOG.debug("Starting SSH firewall in SF2")
100         self.server.execute(cmd_SF2)
101
102         result = self.server.execute("ps lax | grep python")
103         if "vxlan_tool.py" in result[1]:  # pragma: no cover
104             LOG.debug("SSH firewall started")
105
106         self.setup_done = True
107
108     def run(self, result):
109         """ Creating client and server VMs to perform the test"""
110         host = self.context_cfg['host']
111
112         LOG.info("user:%s, host:%s", host['user'], host['ip'])
113         self.client = ssh.SSH.from_node(host, defaults={
114             "user": "root", "password": "opnfv"
115         })
116         self.client.wait(timeout=600)
117
118         if not self.setup_done:  # pragma: no cover
119             self.setup()
120
121         target = self.context_cfg['target']
122         target_ip = target.get('ip', None)
123
124         cmd_client = "nc -w 5 -zv " + target_ip + " 22"
125         result = self.client.execute(cmd_client)
126
127         i = 0
128         if "timed out" in result[2]:  # pragma: no cover
129             LOG.info('\033[92m' + "TEST 1 [PASSED] "
130                      "==> SSH BLOCKED" + '\033[0m')
131             i = i + 1
132         else:  # pragma: no cover
133             LOG.debug('\033[91m' + "TEST 1 [FAILED] "
134                       "==> SSH NOT BLOCKED" + '\033[0m')
135             return
136
137         cmd_client = "nc -w 5 -zv " + target_ip + " 80"
138         LOG.info("Executing command: %s", cmd_client)
139         result = self.client.execute(cmd_client)
140         if "succeeded" in result[2]:  # pragma: no cover
141             LOG.info('\033[92m' + "TEST 2 [PASSED] "
142                      "==> HTTP WORKS" + '\033[0m')
143             i = i + 1
144         else:  # pragma: no cover
145             LOG.debug('\033[91m' + "TEST 2 [FAILED] "
146                       "==> HTTP BLOCKED" + '\033[0m')
147             return
148
149         self.tacker_classi = pkg_resources.resource_filename(
150             'yardstick.benchmark.scenarios.networking',
151             Sfc.TACKER_CHANGECLASSI)
152
153         """ calling Tacker to change the classifier """
154         cmd_tacker = "%s" % (self.tacker_classi)
155         subprocess.call(cmd_tacker, shell=True)
156
157         cmd_client = "nc -w 5 -zv " + target_ip + " 80"
158         LOG.info("Executing command: %s", cmd_client)
159         result = self.client.execute(cmd_client)
160         LOG.info("Output client command: %s", result)
161         if "timed out" in result[2]:  # pragma: no cover
162             LOG.info('\033[92m' + "TEST 3 [WORKS] "
163                      "==> HTTP BLOCKED" + '\033[0m')
164             i = i + 1
165         else:  # pragma: no cover
166             LOG.debug('\033[91m' + "TEST 3 [FAILED] "
167                       "==> HTTP NOT BLOCKED" + '\033[0m')
168             return
169
170         cmd_client = "nc -zv " + target_ip + " 22"
171         result = self.client.execute(cmd_client + " \r")
172         LOG.debug(result)
173
174         if "succeeded" in result[2]:  # pragma: no cover
175             LOG.info('\033[92m' + "TEST 4 [WORKS] "
176                      "==> SSH WORKS" + '\033[0m')
177             i = i + 1
178         else:  # pragma: no cover
179             LOG.debug('\033[91m' + "TEST 4 [FAILED] "
180                       "==> SSH BLOCKED" + '\033[0m')
181             return
182
183         if i == 4:  # pragma: no cover
184             for x in range(0, 5):
185                 LOG.info('\033[92m' + "SFC TEST WORKED"
186                          " :) \n" + '\033[0m')
187
188     def teardown(self):
189         """ for scenario teardown remove tacker VNFs, chains and classifiers"""
190         self.teardown_script = pkg_resources.resource_filename(
191             "yardstick.benchmark.scenarios.networking",
192             Sfc.TEARDOWN_SCRIPT)
193         subprocess.call(self.teardown_script, shell=True)
194         self.teardown_done = True
195
196
197 """def _test():  # pragma: no cover
198
199     internal test function
200     logger = logging.getLogger("Sfc Yardstick")
201     logger.setLevel(logging.DEBUG)
202
203     result = {}
204
205     sfc = Sfc(scenario_cfg, context_cfg)
206     sfc.setup()
207     sfc.run(result)
208     print(result)
209     sfc.teardown()
210
211 if __name__ == '__main__':  # pragma: no cover
212     _test()"""