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