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