7 import functest.utils.functest_logger as ft_logger
8 import functest.utils.functest_utils as ft_utils
9 import functest.utils.openstack_utils as os_utils
11 from functest.utils.functest_utils import FUNCTEST_REPO as FUNCTEST_REPO
13 parser = argparse.ArgumentParser()
15 parser.add_argument("-r", "--report",
16 help="Create json result file",
19 args = parser.parse_args()
21 """ logging configuration """
22 logger = ft_logger.Logger("ODL_SFC").getLogger()
24 HOME = os.environ['HOME'] + "/"
27 INSTANCE_NAME = "client"
29 IMAGE_NAME = "sf_nsh_colorado"
30 IMAGE_FILENAME = "sf_nsh_colorado.qcow2"
31 IMAGE_FORMAT = "qcow2"
32 IMAGE_PATH = "/home/opnfv/functest/data" + "/" + IMAGE_FILENAME
34 # NEUTRON Private Network parameters
36 NET_NAME = "example-net"
37 SUBNET_NAME = "example-subnet"
38 SUBNET_CIDR = "11.0.0.0/24"
39 ROUTER_NAME = "example-router"
41 SECGROUP_NAME = "example-sg"
42 SECGROUP_DESCR = "Example Security group"
44 INSTANCE_NAME_2 = "server"
46 # TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
48 PRE_SETUP_SCRIPT = 'sfc_pre_setup.bash'
49 TACKER_SCRIPT = 'sfc_tacker.bash'
50 TEARDOWN_SCRIPT = "sfc_teardown.bash"
51 TACKER_CHANGECLASSI = "sfc_change_classi.bash"
56 # Allow any port so that tacker commands reaches the server.
57 # This will be deleted when tacker is included in OPNFV installation
59 ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
60 contr_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
61 " 'fuel node'|grep controller|awk '{print $10}'")
62 logger.info("Executing script to get ip_server: '%s'" % contr_cmd)
63 process = subprocess.Popen(contr_cmd,
65 stdout=subprocess.PIPE,
66 stderr=subprocess.PIPE)
67 ip_server = process.stdout.readline().rstrip()
69 contr_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
70 " 'fuel node'|grep compute|awk '{print $10}'")
71 logger.info("Executing script to get ip_compute: '%s'" % contr_cmd2)
72 process = subprocess.Popen(contr_cmd2,
74 stdout=subprocess.PIPE,
75 stderr=subprocess.PIPE)
76 ip_compute = process.stdout.readline().rstrip()
78 iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
79 " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
80 iptable_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
81 " ssh " + ip_server + " iptables -t nat -P INPUT ACCEPT ")
83 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd1)
84 subprocess.call(iptable_cmd1, shell=True, stderr=subprocess.PIPE)
86 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd2)
87 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
89 # Getting the different clients
91 nova_client = os_utils.get_nova_client()
92 neutron_client = os_utils.get_neutron_client()
93 glance_client = os_utils.get_glance_client()
97 if not os.path.isfile(IMAGE_PATH):
98 logger.info("Downloading image")
99 ft_utils.download_url(
100 "http://artifacts.opnfv.org/sfc/demo/sf_nsh_colorado.qcow2",
101 "/home/opnfv/functest/data/")
103 logger.info("Using old image")
105 # Create glance image and the neutron network
107 image_id = os_utils.create_glance_image(glance_client,
114 network_dic = os_utils.create_network_full(neutron_client,
121 "There has been a problem when creating the neutron network")
124 network_id = network_dic["net_id"]
126 sg_id = os_utils.create_security_group_full(neutron_client,
127 SECGROUP_NAME, SECGROUP_DESCR)
130 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
132 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
133 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
134 instance = os_utils.create_instance_and_wait_for_active(FLAVOR,
140 logger.error("Error while booting instance.")
142 # Retrieve IP of INSTANCE
143 instance_ip = instance.networks.get(NET_NAME)[0]
144 logger.debug("Instance '%s' got private ip '%s'." %
145 (INSTANCE_NAME, instance_ip))
147 logger.info("Adding '%s' to security group '%s'..."
148 % (INSTANCE_NAME, SECGROUP_NAME))
149 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
151 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
152 floatip_dic = os_utils.create_floating_ip(neutron_client)
153 floatip_client = floatip_dic['fip_addr']
154 # floatip_id = floatip_dic['fip_id']
156 if floatip_client is None:
157 logger.error("Cannot create floating IP.")
159 logger.info("Floating IP created: '%s'" % floatip_client)
161 logger.info("Associating floating ip: '%s' to VM '%s' "
162 % (floatip_client, INSTANCE_NAME))
163 if not os_utils.add_floating_ip(nova_client, instance.id, floatip_client):
164 logger.error("Cannot associate floating IP to VM.")
167 # STARTING SECOND VM (server) ###
170 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
172 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
173 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
174 instance_2 = os_utils.create_instance_and_wait_for_active(FLAVOR,
179 if instance_2 is None:
180 logger.error("Error while booting instance.")
182 # Retrieve IP of INSTANCE
183 instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
184 logger.debug("Instance '%s' got private ip '%s'." %
185 (INSTANCE_NAME_2, instance_ip_2))
187 logger.info("Adding '%s' to security group '%s'..."
188 % (INSTANCE_NAME_2, SECGROUP_NAME))
189 os_utils.add_secgroup_to_instance(nova_client, instance_2.id, sg_id)
191 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME_2)
192 floatip_dic = os_utils.create_floating_ip(neutron_client)
193 floatip_server = floatip_dic['fip_addr']
194 # floatip_id = floatip_dic['fip_id']
196 if floatip_server is None:
197 logger.error("Cannot create floating IP.")
199 logger.info("Floating IP created: '%s'" % floatip_server)
201 logger.info("Associating floating ip: '%s' to VM '%s' "
202 % (floatip_server, INSTANCE_NAME_2))
204 if not os_utils.add_floating_ip(nova_client,
207 logger.error("Cannot associate floating IP to VM.")
210 # CREATION OF THE 2 SF ####
212 tacker_script = "%s/testcases/features/sfc/%s" % \
213 (FUNCTEST_REPO, TACKER_SCRIPT)
214 logger.info("Executing tacker script: '%s'" % tacker_script)
215 subprocess.call(tacker_script, shell=True)
217 # SSH CALL TO START HTTP SERVER
218 ssh = paramiko.SSHClient()
219 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
222 ssh.connect(floatip_server, username="root",
223 password="opnfv", timeout=2)
224 command = "python -m SimpleHTTPServer 80 > /dev/null 2>&1 &"
225 logger.info("Starting HTTP server")
226 (stdin, stdout, stderr) = ssh.exec_command(command)
228 logger.debug("Waiting for %s..." % floatip_server)
232 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
235 for instance in instances:
236 if "server" not in instance.name:
237 if "client" not in instance.name:
239 "This is the instance name: %s " % instance.name)
240 floatip_dic = os_utils.create_floating_ip(neutron_client)
241 floatip = floatip_dic['fip_addr']
243 instance.add_floating_ip(floatip)
245 logger.debug("Problems assigning floating IP to SFs")
247 # If no IPs were obtained, then we cant continue
249 logger.error('Failed to obtain IPs, cant continue, exiting')
252 logger.debug("Floating IPs for SFs: %s..." % ips)
254 # SSH TO START THE VXLAN_TOOL ON SF1
255 logger.info("Configuring the SFs")
257 ssh.connect(ips[0], username="root",
258 password="opnfv", timeout=2)
259 command = ("nohup python vxlan_tool.py -i eth0 "
260 "-d forward -v off -b 80 > /dev/null 2>&1 &")
261 (stdin, stdout, stderr) = ssh.exec_command(command)
263 logger.debug("Waiting for %s..." % ips[0])
269 (stdin, stdout, stderr) = ssh.exec_command(
270 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
271 if len(stdout.readlines()) > 0:
272 logger.debug("HTTP firewall started")
275 logger.debug("HTTP firewall not started")
278 logger.exception("vxlan_tool not started in SF1")
280 # SSH TO START THE VXLAN_TOOL ON SF2
282 ssh.connect(ips[1], username="root",
283 password="opnfv", timeout=2)
284 command = ("nohup python vxlan_tool.py -i eth0 "
285 "-d forward -v off -b 22 > /dev/null 2>&1 &")
286 (stdin, stdout, stderr) = ssh.exec_command(command)
288 logger.debug("Waiting for %s..." % ips[1])
294 (stdin, stdout, stderr) = ssh.exec_command(
295 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
296 if len(stdout.readlines()) > 0:
297 logger.debug("SSH firewall started")
300 logger.debug("SSH firewall not started")
303 logger.exception("vxlan_tool not started in SF2")
305 # SSH to modify the classification flows in compute
307 contr_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
308 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
309 logger.info("Executing script to modify the classi: '%s'" % contr_cmd3)
310 process = subprocess.Popen(contr_cmd3,
312 stdout=subprocess.PIPE)
314 logger.info("Waiting for 60 seconds before TEST")
315 for j in range(0, 6):
316 logger.info("Test starting in {0} seconds".format(str((6 - j)*10)))
321 # SSH TO EXECUTE cmd_client
322 logger.info("TEST STARTED")
324 ssh.connect(floatip_client, username="root",
325 password="opnfv", timeout=2)
326 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
327 (stdin, stdout, stderr) = ssh.exec_command(command)
329 # WRITE THE CORRECT WAY TO DO LOGGING
330 if "timed out" in stdout.readlines()[0]:
331 logger.info('\033[92m' + "TEST 1 [PASSED] "
332 "==> SSH BLOCKED" + '\033[0m')
335 logger.error('\033[91m' + "TEST 1 [FAILED] "
336 "==> SSH NOT BLOCKED" + '\033[0m')
339 logger.debug("Waiting for %s..." % floatip_client)
343 # SSH TO EXECUTE cmd_client
345 ssh.connect(floatip_client, username="root",
346 password="opnfv", timeout=2)
347 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
348 (stdin, stdout, stderr) = ssh.exec_command(command)
350 if "succeeded" in stdout.readlines()[0]:
351 logger.info('\033[92m' + "TEST 2 [PASSED] "
352 "==> HTTP WORKS" + '\033[0m')
355 logger.error('\033[91m' + "TEST 2 [FAILED] "
356 "==> HTTP BLOCKED" + '\033[0m')
359 logger.debug("Waiting for %s..." % floatip_client)
363 # CHANGE OF CLASSIFICATION #
364 logger.info("Changing the classification")
365 tacker_classi = "%s/testcases/features/sfc/%s" % \
366 (FUNCTEST_REPO, TACKER_CHANGECLASSI)
367 subprocess.call(tacker_classi, shell=True)
369 logger.info("Wait for ODL to update the classification rules in OVS")
372 # SSH to modify the classification flows in compute
374 contr_cmd4 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
375 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
376 logger.info("Executing script to modify the classi: '%s'" % contr_cmd4)
377 process = subprocess.Popen(contr_cmd4,
379 stdout=subprocess.PIPE)
381 # SSH TO EXECUTE cmd_client
384 ssh.connect(floatip_client, username="root",
385 password="opnfv", timeout=2)
386 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
387 (stdin, stdout, stderr) = ssh.exec_command(command)
389 if "timed out" in stdout.readlines()[0]:
390 logger.info('\033[92m' + "TEST 3 [WORKS] "
391 "==> HTTP BLOCKED" + '\033[0m')
394 logger.error('\033[91m' + "TEST 3 [FAILED] "
395 "==> HTTP NOT BLOCKED" + '\033[0m')
398 logger.debug("Waiting for %s..." % floatip_client)
402 # SSH TO EXECUTE cmd_client
404 ssh.connect(floatip_client, username="root",
405 password="opnfv", timeout=2)
406 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
407 (stdin, stdout, stderr) = ssh.exec_command(command)
409 if "succeeded" in stdout.readlines()[0]:
410 logger.info('\033[92m' + "TEST 4 [WORKS] "
411 "==> SSH WORKS" + '\033[0m')
414 logger.error('\033[91m' + "TEST 4 [FAILED] "
415 "==> SSH BLOCKED" + '\033[0m')
418 logger.debug("Waiting for %s..." % floatip_client)
423 for x in range(0, 5):
424 logger.info('\033[92m' + "SFC TEST WORKED"
425 " :) \n" + '\033[0m')
427 # TODO report results to DB
428 # functest_utils.logger_test_results("SFC",
431 # see doctor, promise, domino, ...
433 # logger.info("Pushing odl-SFC results")
434 # functest_utils.push_results_to_db("functest",
443 if __name__ == '__main__':