Merge "ci in huawei-pod1 do not run test case after tc074"
[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         target_user = target.get('user', 'root')
57         target_ssh_port = target.get('ssh_port', ssh.DEFAULT_PORT)
58         target_pwd = target.get('password', 'opnfv')
59         target_ip = target.get('ip', None)
60
61         """ webserver start automatically during the vm boot """
62         LOG.info("user:%s, target:%s", target_user, target_ip)
63         self.server = ssh.SSH(target_user, target_ip, password=target_pwd,
64                               port=target_ssh_port)
65         self.server.wait(timeout=600)
66         self.server._put_file_shell(self.server_script, '~/server.sh')
67         cmd_server = "sudo bash server.sh"
68         LOG.debug("Executing command: %s", cmd_server)
69         status, stdout, stderr = self.server.execute(cmd_server)
70         LOG.debug("Output server command: %s", status)
71
72         ips = sfc_openstack.get_an_IP()
73
74         target = self.context_cfg['target']
75         SF1_user = target.get('user', 'root')
76         SF1_ssh_port = target.get('ssh_port', ssh.DEFAULT_PORT)
77         SF1_pwd = target.get('password', 'opnfv')
78         SF1_ip = ips[0]
79
80         LOG.info("user:%s, host:%s", SF1_user, SF1_ip)
81         self.server = ssh.SSH(SF1_user, SF1_ip, password=SF1_pwd,
82                               port=SF1_ssh_port)
83         self.server.wait(timeout=600)
84         cmd_SF1 = ("nohup python vxlan_tool.py -i eth0 "
85                    "-d forward -v off -b 80 &")
86         LOG.debug("Starting HTTP firewall in SF1")
87         status, stdout, stderr = self.server.execute(cmd_SF1)
88         result = self.server.execute("ps lax | grep python")
89         if "vxlan_tool.py" in result[1]:  # pragma: no cover
90             LOG.debug("HTTP firewall started")
91
92         SF2_user = target.get('user', 'root')
93         SF2_ssh_port = target.get('ssh_port', ssh.DEFAULT_PORT)
94         SF2_pwd = target.get('password', 'opnfv')
95         SF2_ip = ips[1]
96
97         LOG.info("user:%s, host:%s", SF2_user, SF2_ip)
98         self.server = ssh.SSH(SF2_user, SF2_ip, password=SF2_pwd,
99                               port=SF2_ssh_port)
100         self.server.wait(timeout=600)
101         cmd_SF2 = ("nohup python vxlan_tool.py -i eth0 "
102                    "-d forward -v off -b 22 &")
103         LOG.debug("Starting SSH firewall in SF2")
104         status, stdout, stderr = self.server.execute(cmd_SF2)
105
106         result = self.server.execute("ps lax | grep python")
107         if "vxlan_tool.py" in result[1]:  # pragma: no cover
108             LOG.debug("SSH firewall started")
109
110         self.setup_done = True
111
112     def run(self, result):
113         """ Creating client and server VMs to perform the test"""
114         host = self.context_cfg['host']
115         host_user = host.get('user', 'root')
116         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
117         host_pwd = host.get('password', 'opnfv')
118         host_ip = host.get('ip', None)
119
120         LOG.info("user:%s, host:%s", host_user, host_ip)
121         self.client = ssh.SSH(host_user, host_ip, password=host_pwd,
122                               port=ssh_port)
123         self.client.wait(timeout=600)
124
125         if not self.setup_done:  # pragma: no cover
126             self.setup()
127
128         target = self.context_cfg['target']
129         target_ip = target.get('ip', None)
130
131         cmd_client = "nc -w 5 -zv " + target_ip + " 22"
132         result = self.client.execute(cmd_client)
133
134         i = 0
135         if "timed out" in result[2]:  # pragma: no cover
136             LOG.info('\033[92m' + "TEST 1 [PASSED] "
137                      "==> SSH BLOCKED" + '\033[0m')
138             i = i + 1
139         else:  # pragma: no cover
140             LOG.debug('\033[91m' + "TEST 1 [FAILED] "
141                       "==> SSH NOT BLOCKED" + '\033[0m')
142             return
143
144         cmd_client = "nc -w 5 -zv " + target_ip + " 80"
145         LOG.info("Executing command: %s", cmd_client)
146         result = self.client.execute(cmd_client)
147         if "succeeded" in result[2]:  # pragma: no cover
148             LOG.info('\033[92m' + "TEST 2 [PASSED] "
149                      "==> HTTP WORKS" + '\033[0m')
150             i = i + 1
151         else:  # pragma: no cover
152             LOG.debug('\033[91m' + "TEST 2 [FAILED] "
153                       "==> HTTP BLOCKED" + '\033[0m')
154             return
155
156         self.tacker_classi = pkg_resources.resource_filename(
157             'yardstick.benchmark.scenarios.networking',
158             Sfc.TACKER_CHANGECLASSI)
159
160         """ calling Tacker to change the classifier """
161         cmd_tacker = "%s" % (self.tacker_classi)
162         subprocess.call(cmd_tacker, shell=True)
163
164         cmd_client = "nc -w 5 -zv " + target_ip + " 80"
165         LOG.info("Executing command: %s", cmd_client)
166         result = self.client.execute(cmd_client)
167         LOG.info("Output client command: %s", result)
168         if "timed out" in result[2]:  # pragma: no cover
169             LOG.info('\033[92m' + "TEST 3 [WORKS] "
170                      "==> HTTP BLOCKED" + '\033[0m')
171             i = i + 1
172         else:  # pragma: no cover
173             LOG.debug('\033[91m' + "TEST 3 [FAILED] "
174                       "==> HTTP NOT BLOCKED" + '\033[0m')
175             return
176
177         cmd_client = "nc -zv " + target_ip + " 22"
178         result = self.client.execute(cmd_client + " \r")
179         LOG.debug(result)
180
181         if "succeeded" in result[2]:  # pragma: no cover
182             LOG.info('\033[92m' + "TEST 4 [WORKS] "
183                      "==> SSH WORKS" + '\033[0m')
184             i = i + 1
185         else:  # pragma: no cover
186             LOG.debug('\033[91m' + "TEST 4 [FAILED] "
187                       "==> SSH BLOCKED" + '\033[0m')
188             return
189
190         if i == 4:  # pragma: no cover
191             for x in range(0, 5):
192                 LOG.info('\033[92m' + "SFC TEST WORKED"
193                          " :) \n" + '\033[0m')
194
195     def teardown(self):
196         """ for scenario teardown remove tacker VNFs, chains and classifiers"""
197         self.teardown_script = pkg_resources.resource_filename(
198             "yardstick.benchmark.scenarios.networking",
199             Sfc.TEARDOWN_SCRIPT)
200         subprocess.call(self.teardown_script, shell=True)
201         self.teardown_done = True
202
203
204 """def _test():  # pragma: no cover
205
206     internal test function
207     logger = logging.getLogger("Sfc Yardstick")
208     logger.setLevel(logging.DEBUG)
209
210     result = {}
211
212     sfc = Sfc(scenario_cfg, context_cfg)
213     sfc.setup()
214     sfc.run(result)
215     print(result)
216     sfc.teardown()
217
218 if __name__ == '__main__':  # pragma: no cover
219     _test()"""