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