8 import functest.utils.functest_logger as ft_logger
9 import functest.utils.functest_utils as ft_utils
10 import functest.utils.openstack_utils as os_utils
11 import SSHUtils as ssh_utils
14 parser = argparse.ArgumentParser()
16 parser.add_argument("-r", "--report",
17 help="Create json result file",
20 args = parser.parse_args()
22 """ logging configuration """
23 logger = ft_logger.Logger("ODL_SFC").getLogger()
25 FUNCTEST_RESULTS_DIR = '/home/opnfv/functest/results/odl-sfc'
26 FUNCTEST_REPO = ft_utils.FUNCTEST_REPO
28 HOME = os.environ['HOME'] + "/"
31 INSTANCE_NAME = "client"
33 IMAGE_NAME = "sf_nsh_colorado"
34 IMAGE_FILENAME = "sf_nsh_colorado.qcow2"
35 IMAGE_FORMAT = "qcow2"
36 IMAGE_PATH = "/home/opnfv/functest/data" + "/" + IMAGE_FILENAME
38 # NEUTRON Private Network parameters
40 NET_NAME = "example-net"
41 SUBNET_NAME = "example-subnet"
42 SUBNET_CIDR = "11.0.0.0/24"
43 ROUTER_NAME = "example-router"
45 SECGROUP_NAME = "example-sg"
46 SECGROUP_DESCR = "Example Security group"
48 INSTANCE_NAME_2 = "server"
50 # TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
52 PRE_SETUP_SCRIPT = 'sfc_pre_setup.bash'
53 TACKER_SCRIPT = 'sfc_tacker.bash'
54 TEARDOWN_SCRIPT = "sfc_teardown.bash"
55 TACKER_CHANGECLASSI = "sfc_change_classi.bash"
57 ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
67 cmd = "sshpass -p opnfv ssh " + ssh_options + " -q " + ip + " exit"
68 success = subprocess.call(cmd, shell=True) == 0
70 logger.debug("Wating for SSH connectivity in SF with IP: %s" % ip)
76 # Allow any port so that tacker commands reaches the server.
77 # This will be deleted when tacker is included in OPNFV installation
81 start_time = time.time()
84 contr_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
85 " 'fuel node'|grep controller|awk '{print $10}'")
86 logger.info("Executing script to get ip_server: '%s'" % contr_cmd)
87 process = subprocess.Popen(contr_cmd,
89 stdout=subprocess.PIPE,
90 stderr=subprocess.PIPE)
91 ip_server = process.stdout.readline().rstrip()
93 comp_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
94 " 'fuel node'|grep compute|awk '{print $10}'")
95 logger.info("Executing script to get compute IPs: '%s'" % comp_cmd)
96 process = subprocess.Popen(comp_cmd,
98 stdout=subprocess.PIPE,
99 stderr=subprocess.PIPE)
100 ip_computes = [ip.strip() for ip in process.stdout.readlines()]
102 iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
103 " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
104 iptable_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
105 " ssh " + ip_server + " iptables -t nat -P INPUT ACCEPT ")
106 iptable_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
107 " ssh " + ssh_options + " " + ip_server +
108 " iptables -A INPUT -m state"
109 " --state NEW,ESTABLISHED,RELATED -j ACCEPT")
111 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd1)
112 subprocess.call(iptable_cmd1, shell=True, stderr=subprocess.PIPE)
114 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd2)
115 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
117 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd3)
118 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
120 # Getting the different clients
122 nova_client = os_utils.get_nova_client()
123 neutron_client = os_utils.get_neutron_client()
124 glance_client = os_utils.get_glance_client()
126 ovs_logger = ovs_utils.OVSLogger(
127 os.path.join(os.getcwd(), 'ovs-logs'),
128 FUNCTEST_RESULTS_DIR)
130 controller_clients = [ssh_utils.get_ssh_client(ip_server,
134 for c_ip in ip_computes:
135 c_client = ssh_utils.get_ssh_client(c_ip,
138 compute_clients.append(c_client)
142 if not os.path.isfile(IMAGE_PATH):
143 logger.info("Downloading image")
144 ft_utils.download_url(
145 "http://artifacts.opnfv.org/sfc/demo/sf_nsh_colorado.qcow2",
146 "/home/opnfv/functest/data/")
148 logger.info("Using old image")
150 # Create glance image and the neutron network
152 image_id = os_utils.create_glance_image(glance_client,
159 network_dic = os_utils.create_network_full(neutron_client,
166 "There has been a problem when creating the neutron network")
169 network_id = network_dic["net_id"]
171 sg_id = os_utils.create_security_group_full(neutron_client,
172 SECGROUP_NAME, SECGROUP_DESCR)
174 secgroups = os_utils.get_security_groups(neutron_client)
177 os_utils.create_secgroup_rule(neutron_client, sg['id'],
181 os_utils.create_secgroup_rule(neutron_client, sg['id'],
185 os_utils.create_secgroup_rule(neutron_client, sg['id'],
189 os_utils.create_secgroup_rule(neutron_client, sg['id'],
193 os_utils.create_secgroup_rule(neutron_client, sg['id'],
197 os_utils.create_secgroup_rule(neutron_client, sg['id'],
202 _, custom_flv_id = os_utils.get_or_create_flavor(
203 'custom', 1500, 10, 1, public=True)
204 if not custom_flv_id:
205 logger.error("Failed to create custom flavor")
211 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
213 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
214 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
215 instance = os_utils.create_instance_and_wait_for_active(
223 logger.error("Error while booting instance.")
226 # Retrieve IP of INSTANCE
227 instance_ip = instance.networks.get(NET_NAME)[0]
228 logger.debug("Instance '%s' got private ip '%s'." %
229 (INSTANCE_NAME, instance_ip))
231 logger.info("Adding '%s' to security group '%s'..."
232 % (INSTANCE_NAME, SECGROUP_NAME))
233 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
235 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
236 floatip_dic = os_utils.create_floating_ip(neutron_client)
237 floatip_client = floatip_dic['fip_addr']
238 # floatip_id = floatip_dic['fip_id']
240 if floatip_client is None:
241 logger.error("Cannot create floating IP.")
244 logger.info("Floating IP created: '%s'" % floatip_client)
246 logger.info("Associating floating ip: '%s' to VM '%s' "
247 % (floatip_client, INSTANCE_NAME))
248 if not os_utils.add_floating_ip(nova_client,
251 logger.error("Cannot associate floating IP to VM.")
255 # STARTING SECOND VM (server) ###
258 logger.info("Creating instance '%s'..." % INSTANCE_NAME_2)
260 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
261 "network=%s \n" % (INSTANCE_NAME_2, FLAVOR, image_id, network_id))
262 instance_2 = os_utils.create_instance_and_wait_for_active(
269 if instance_2 is None:
270 logger.error("Error while booting instance.")
273 # Retrieve IP of INSTANCE
274 instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
275 logger.debug("Instance '%s' got private ip '%s'." %
276 (INSTANCE_NAME_2, instance_ip_2))
278 logger.info("Adding '%s' to security group '%s'..."
279 % (INSTANCE_NAME_2, SECGROUP_NAME))
280 os_utils.add_secgroup_to_instance(nova_client, instance_2.id, sg_id)
282 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME_2)
283 floatip_dic = os_utils.create_floating_ip(neutron_client)
284 floatip_server = floatip_dic['fip_addr']
285 # floatip_id = floatip_dic['fip_id']
287 if floatip_server is None:
288 logger.error("Cannot create floating IP.")
291 logger.info("Floating IP created: '%s'" % floatip_server)
293 logger.info("Associating floating ip: '%s' to VM '%s' "
294 % (floatip_server, INSTANCE_NAME_2))
296 if not os_utils.add_floating_ip(nova_client,
299 logger.error("Cannot associate floating IP to VM.")
303 # CREATION OF THE 2 SF ####
305 tacker_script = "%s/testcases/features/sfc/%s" % \
306 (FUNCTEST_REPO, TACKER_SCRIPT)
307 logger.info("Executing tacker script: '%s'" % tacker_script)
308 subprocess.call(tacker_script, shell=True)
310 # SSH CALL TO START HTTP SERVER
311 ssh = paramiko.SSHClient()
312 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
315 ssh.connect(floatip_server, username="root",
316 password="opnfv", timeout=2)
317 command = "python -m SimpleHTTPServer 80 > /dev/null 2>&1 &"
318 logger.info("Starting HTTP server")
319 (stdin, stdout, stderr) = ssh.exec_command(command)
321 logger.debug("Waiting for %s..." % floatip_server)
325 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
328 for instance in instances:
329 if "server" not in instance.name:
330 if "client" not in instance.name:
332 "This is the instance name: %s " % instance.name)
333 floatip_dic = os_utils.create_floating_ip(
335 floatip = floatip_dic['fip_addr']
337 instance.add_floating_ip(floatip)
339 logger.debug("Problems assigning floating IP to SFs")
341 # If no IPs were obtained, then we cant continue
343 logger.error('Failed to obtain IPs, cant continue, exiting')
346 logger.debug("Floating IPs for SFs: %s..." % ips)
348 # Check SSH connectivity to VNFs
351 check = [False, False]
353 logger.info("Checking SSH connectivity to the SFs with ips {0}"
355 while r < retries and not all(check):
357 check = [check_ssh(ips[0]), check_ssh(ips[1])]
359 logger.exception("SSH check failed")
360 check = [False, False]
365 logger.error("Cannot establish SSH connection to the SFs")
369 logger.info("SSH connectivity to the SFs established")
371 # SSH TO START THE VXLAN_TOOL ON SF1
372 logger.info("Configuring the SFs")
374 ssh.connect(ips[0], username="root",
375 password="opnfv", timeout=2)
376 command = ("nohup python vxlan_tool.py -i eth0 "
377 "-d forward -v off -b 80 > /dev/null 2>&1 &")
378 (stdin, stdout, stderr) = ssh.exec_command(command)
380 logger.debug("Waiting for %s..." % ips[0])
387 (stdin, stdout, stderr) = ssh.exec_command(
388 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
389 if len(stdout.readlines()) > 0:
390 logger.debug("HTTP firewall started")
396 logger.debug("HTTP firewall not started")
399 logger.exception("vxlan_tool not started in SF1")
401 # SSH TO START THE VXLAN_TOOL ON SF2
403 ssh.connect(ips[1], username="root",
404 password="opnfv", timeout=2)
405 command = ("nohup python vxlan_tool.py -i eth0 "
406 "-d forward -v off -b 22 > /dev/null 2>&1 &")
407 (stdin, stdout, stderr) = ssh.exec_command(command)
409 logger.debug("Waiting for %s..." % ips[1])
416 (stdin, stdout, stderr) = ssh.exec_command(
417 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
418 if len(stdout.readlines()) > 0:
419 logger.debug("SSH firewall started")
425 logger.debug("SSH firewall not started")
428 logger.exception("vxlan_tool not started in SF2")
432 # SSH TO EXECUTE cmd_client
433 logger.info("TEST STARTED")
436 ssh.connect(floatip_client, username="root",
437 password="opnfv", timeout=2)
438 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
439 (stdin, stdout, stderr) = ssh.exec_command(command)
441 # WRITE THE CORRECT WAY TO DO LOGGING
442 if "timed out" in stdout.readlines()[0]:
443 logger.info('\033[92m' + "TEST 1 [PASSED] "
444 "==> SSH BLOCKED" + '\033[0m')
446 json_results.update({"Test 1: SSH Blocked": "Passed"})
448 timestamp = time.strftime("%Y%m%d-%H%M%S")
449 error = ('\033[91m' + "TEST 1 [FAILED] "
450 "==> SSH NOT BLOCKED" + '\033[0m')
452 ovs_logger.dump_ovs_logs(controller_clients,
457 json_results.update({"Test 1: SSH Blocked": "Failed"})
460 logger.debug("Waiting for %s..." % floatip_client)
464 # SSH TO EXECUTE cmd_client
466 ssh.connect(floatip_client, username="root",
467 password="opnfv", timeout=2)
468 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
469 (stdin, stdout, stderr) = ssh.exec_command(command)
471 if "succeeded" in stdout.readlines()[0]:
472 logger.info('\033[92m' + "TEST 2 [PASSED] "
473 "==> HTTP WORKS" + '\033[0m')
475 json_results.update({"Test 2: HTTP works": "Passed"})
477 timestamp = time.strftime("%Y%m%d-%H%M%S")
478 error = ('\033[91m' + "TEST 2 [FAILED] "
479 "==> HTTP BLOCKED" + '\033[0m')
481 ovs_logger.dump_ovs_logs(controller_clients,
486 json_results.update({"Test 2: HTTP works": "Failed"})
489 logger.debug("Waiting for %s..." % floatip_client)
493 # CHANGE OF CLASSIFICATION #
494 logger.info("Changing the classification")
495 tacker_classi = "%s/testcases/features/sfc/%s" % \
496 (FUNCTEST_REPO, TACKER_CHANGECLASSI)
497 subprocess.call(tacker_classi, shell=True)
499 logger.info("Wait for ODL to update the classification rules in OVS")
502 # SSH TO EXECUTE cmd_client
505 ssh.connect(floatip_client, username="root",
506 password="opnfv", timeout=2)
507 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
508 (stdin, stdout, stderr) = ssh.exec_command(command)
510 if "timed out" in stdout.readlines()[0]:
511 logger.info('\033[92m' + "TEST 3 [PASSED] "
512 "==> HTTP BLOCKED" + '\033[0m')
514 json_results.update({"Test 3: HTTP Blocked": "Passed"})
516 timestamp = time.strftime("%Y%m%d-%H%M%S")
517 error = ('\033[91m' + "TEST 3 [FAILED] "
518 "==> HTTP NOT BLOCKED" + '\033[0m')
520 ovs_logger.dump_ovs_logs(controller_clients,
525 json_results.update({"Test 3: HTTP Blocked": "Failed"})
528 logger.debug("Waiting for %s..." % floatip_client)
532 # SSH TO EXECUTE cmd_client
534 ssh.connect(floatip_client, username="root",
535 password="opnfv", timeout=2)
536 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
537 (stdin, stdout, stderr) = ssh.exec_command(command)
539 if "succeeded" in stdout.readlines()[0]:
540 logger.info('\033[92m' + "TEST 4 [PASSED] "
541 "==> SSH WORKS" + '\033[0m')
543 json_results.update({"Test 4: SSH works": "Passed"})
545 timestamp = time.strftime("%Y%m%d-%H%M%S")
546 error = ('\033[91m' + "TEST 4 [FAILED] "
547 "==> SSH BLOCKED" + '\033[0m')
549 ovs_logger.dump_ovs_logs(controller_clients,
554 json_results.update({"Test 4: SSH works": "Failed"})
557 logger.debug("Waiting for %s..." % floatip_client)
561 ovs_logger.create_artifact_archive()
565 for x in range(0, 5):
566 logger.info('\033[92m' + "SFC TEST WORKED"
567 " :) \n" + '\033[0m')
570 logger.info("Iterating again!")
571 delete = "bash %s/testcases/features/sfc/delete.sh" % \
574 subprocess.call(delete, shell=True, stderr=subprocess.PIPE)
577 logger.error("Problem when executing the delete.sh")
578 logger.error("Problem %s" % e)
581 stop_time = time.time()
582 json_results.update({"tests": "4", "failures": int(failures)})
583 logger.debug("Promise Results json: " + str(json_results))
584 ft_utils.push_results_to_db("sfc",
595 if __name__ == '__main__':