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