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
12 parser = argparse.ArgumentParser()
14 parser.add_argument("-r", "--report",
15 help="Create json result file",
18 args = parser.parse_args()
20 """ logging configuration """
21 logger = ft_logger.Logger("ODL_SFC").getLogger()
23 FUNCTEST_RESULTS_DIR = '/home/opnfv/functest/results/'
24 FUNCTEST_REPO = ft_utils.FUNCTEST_REPO
26 HOME = os.environ['HOME'] + "/"
29 INSTANCE_NAME = "client"
31 IMAGE_NAME = "sf_nsh_colorado"
32 IMAGE_FILENAME = "sf_nsh_colorado.qcow2"
33 IMAGE_FORMAT = "qcow2"
34 IMAGE_PATH = "/home/opnfv/functest/data" + "/" + IMAGE_FILENAME
36 # NEUTRON Private Network parameters
38 NET_NAME = "example-net"
39 SUBNET_NAME = "example-subnet"
40 SUBNET_CIDR = "11.0.0.0/24"
41 ROUTER_NAME = "example-router"
43 SECGROUP_NAME = "example-sg"
44 SECGROUP_DESCR = "Example Security group"
46 INSTANCE_NAME_2 = "server"
48 # TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
50 PRE_SETUP_SCRIPT = 'sfc_pre_setup.bash'
51 TACKER_SCRIPT = 'sfc_tacker.bash'
52 TEARDOWN_SCRIPT = "sfc_teardown.bash"
53 TACKER_CHANGECLASSI = "sfc_change_classi.bash"
55 ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
59 cmd = "sshpass -p opnfv ssh " + ssh_options + " -q " + ip + " exit"
60 success = subprocess.call(cmd, shell=True) == 0
62 logger.debug("Wating for SSH connectivity in SF with IP: %s" % ip)
68 # Allow any port so that tacker commands reaches the server.
69 # This will be deleted when tacker is included in OPNFV installation
73 start_time = time.time()
76 contr_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
77 " 'fuel node'|grep controller|awk '{print $10}'")
78 logger.info("Executing script to get ip_server: '%s'" % contr_cmd)
79 process = subprocess.Popen(contr_cmd,
81 stdout=subprocess.PIPE,
82 stderr=subprocess.PIPE)
83 ip_server = process.stdout.readline().rstrip()
85 iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
86 " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
87 iptable_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
88 " ssh " + ip_server + " iptables -t nat -P INPUT ACCEPT ")
89 iptable_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
90 " ssh " + ssh_options + " " + ip_server +
91 " iptables -A INPUT -m state"
92 " --state NEW,ESTABLISHED,RELATED -j ACCEPT")
94 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd1)
95 subprocess.call(iptable_cmd1, shell=True, stderr=subprocess.PIPE)
97 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd2)
98 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
100 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd3)
101 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
103 # Getting the different clients
105 nova_client = os_utils.get_nova_client()
106 neutron_client = os_utils.get_neutron_client()
107 glance_client = os_utils.get_glance_client()
111 if not os.path.isfile(IMAGE_PATH):
112 logger.info("Downloading image")
113 ft_utils.download_url(
114 "http://artifacts.opnfv.org/sfc/demo/sf_nsh_colorado.qcow2",
115 "/home/opnfv/functest/data/")
117 logger.info("Using old image")
119 # Create glance image and the neutron network
121 image_id = os_utils.create_glance_image(glance_client,
128 network_dic = os_utils.create_network_full(neutron_client,
135 "There has been a problem when creating the neutron network")
138 network_id = network_dic["net_id"]
140 sg_id = os_utils.create_security_group_full(neutron_client,
141 SECGROUP_NAME, SECGROUP_DESCR)
143 secgroups = os_utils.get_security_groups(neutron_client)
146 os_utils.create_secgroup_rule(neutron_client, sg['id'],
150 os_utils.create_secgroup_rule(neutron_client, sg['id'],
154 os_utils.create_secgroup_rule(neutron_client, sg['id'],
158 os_utils.create_secgroup_rule(neutron_client, sg['id'],
163 _, custom_flv_id = os_utils.get_or_create_flavor(
164 'custom', 1500, 10, 1, public=True)
165 if not custom_flv_id:
166 logger.error("Failed to create custom flavor")
172 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
174 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
175 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
176 instance = os_utils.create_instance_and_wait_for_active(
184 logger.error("Error while booting instance.")
187 # Retrieve IP of INSTANCE
188 instance_ip = instance.networks.get(NET_NAME)[0]
189 logger.debug("Instance '%s' got private ip '%s'." %
190 (INSTANCE_NAME, instance_ip))
192 logger.info("Adding '%s' to security group '%s'..."
193 % (INSTANCE_NAME, SECGROUP_NAME))
194 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
196 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
197 floatip_dic = os_utils.create_floating_ip(neutron_client)
198 floatip_client = floatip_dic['fip_addr']
199 # floatip_id = floatip_dic['fip_id']
201 if floatip_client is None:
202 logger.error("Cannot create floating IP.")
205 logger.info("Floating IP created: '%s'" % floatip_client)
207 logger.info("Associating floating ip: '%s' to VM '%s' "
208 % (floatip_client, INSTANCE_NAME))
209 if not os_utils.add_floating_ip(nova_client,
212 logger.error("Cannot associate floating IP to VM.")
216 # STARTING SECOND VM (server) ###
219 logger.info("Creating instance '%s'..." % INSTANCE_NAME_2)
221 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
222 "network=%s \n" % (INSTANCE_NAME_2, FLAVOR, image_id, network_id))
223 instance_2 = os_utils.create_instance_and_wait_for_active(
230 if instance_2 is None:
231 logger.error("Error while booting instance.")
234 # Retrieve IP of INSTANCE
235 instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
236 logger.debug("Instance '%s' got private ip '%s'." %
237 (INSTANCE_NAME_2, instance_ip_2))
239 logger.info("Adding '%s' to security group '%s'..."
240 % (INSTANCE_NAME_2, SECGROUP_NAME))
241 os_utils.add_secgroup_to_instance(nova_client, instance_2.id, sg_id)
243 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME_2)
244 floatip_dic = os_utils.create_floating_ip(neutron_client)
245 floatip_server = floatip_dic['fip_addr']
246 # floatip_id = floatip_dic['fip_id']
248 if floatip_server is None:
249 logger.error("Cannot create floating IP.")
252 logger.info("Floating IP created: '%s'" % floatip_server)
254 logger.info("Associating floating ip: '%s' to VM '%s' "
255 % (floatip_server, INSTANCE_NAME_2))
257 if not os_utils.add_floating_ip(nova_client,
260 logger.error("Cannot associate floating IP to VM.")
264 # CREATION OF THE 2 SF ####
266 tacker_script = "%s/testcases/features/sfc/%s" % \
267 (FUNCTEST_REPO, TACKER_SCRIPT)
268 logger.info("Executing tacker script: '%s'" % tacker_script)
269 subprocess.call(tacker_script, shell=True)
271 # SSH CALL TO START HTTP SERVER
272 ssh = paramiko.SSHClient()
273 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
276 ssh.connect(floatip_server, username="root",
277 password="opnfv", timeout=2)
278 command = "python -m SimpleHTTPServer 80 > /dev/null 2>&1 &"
279 logger.info("Starting HTTP server")
280 (stdin, stdout, stderr) = ssh.exec_command(command)
282 logger.debug("Waiting for %s..." % floatip_server)
286 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
289 for instance in instances:
290 if "server" not in instance.name:
291 if "client" not in instance.name:
293 "This is the instance name: %s " % instance.name)
294 floatip_dic = os_utils.create_floating_ip(
296 floatip = floatip_dic['fip_addr']
298 instance.add_floating_ip(floatip)
300 logger.debug("Problems assigning floating IP to SFs")
302 # If no IPs were obtained, then we cant continue
304 logger.error('Failed to obtain IPs, cant continue, exiting')
307 logger.debug("Floating IPs for SFs: %s..." % ips)
309 # Check SSH connectivity to VNFs
312 check = [False, False]
314 logger.info("Checking SSH connectivity to the SFs with ips {0}"
316 while r < retries and not all(check):
318 check = [check_ssh(ips[0]), check_ssh(ips[1])]
320 logger.exception("SSH check failed")
321 check = [False, False]
326 logger.error("Cannot establish SSH connection to the SFs")
330 logger.info("SSH connectivity to the SFs established")
332 # SSH TO START THE VXLAN_TOOL ON SF1
333 logger.info("Configuring the SFs")
335 ssh.connect(ips[0], username="root",
336 password="opnfv", timeout=2)
337 command = ("nohup python vxlan_tool.py -i eth0 "
338 "-d forward -v off -b 80 > /dev/null 2>&1 &")
339 (stdin, stdout, stderr) = ssh.exec_command(command)
341 logger.debug("Waiting for %s..." % ips[0])
348 (stdin, stdout, stderr) = ssh.exec_command(
349 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
350 if len(stdout.readlines()) > 0:
351 logger.debug("HTTP firewall started")
357 logger.debug("HTTP firewall not started")
360 logger.exception("vxlan_tool not started in SF1")
362 # SSH TO START THE VXLAN_TOOL ON SF2
364 ssh.connect(ips[1], username="root",
365 password="opnfv", timeout=2)
366 command = ("nohup python vxlan_tool.py -i eth0 "
367 "-d forward -v off -b 22 > /dev/null 2>&1 &")
368 (stdin, stdout, stderr) = ssh.exec_command(command)
370 logger.debug("Waiting for %s..." % ips[1])
377 (stdin, stdout, stderr) = ssh.exec_command(
378 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
379 if len(stdout.readlines()) > 0:
380 logger.debug("SSH firewall started")
386 logger.debug("SSH firewall not started")
389 logger.exception("vxlan_tool not started in SF2")
393 # SSH TO EXECUTE cmd_client
394 logger.info("TEST STARTED")
396 ssh.connect(floatip_client, username="root",
397 password="opnfv", timeout=2)
398 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
399 (stdin, stdout, stderr) = ssh.exec_command(command)
401 # WRITE THE CORRECT WAY TO DO LOGGING
402 if "timed out" in stdout.readlines()[0]:
403 logger.info('\033[92m' + "TEST 1 [PASSED] "
404 "==> SSH BLOCKED" + '\033[0m')
406 json_results.update({"Test 1: SSH Blocked": "Passed"})
408 logger.error('\033[91m' + "TEST 1 [FAILED] "
409 "==> SSH NOT BLOCKED" + '\033[0m')
411 json_results.update({"Test 1: SSH Blocked": "Failed"})
414 logger.debug("Waiting for %s..." % floatip_client)
418 # SSH TO EXECUTE cmd_client
420 ssh.connect(floatip_client, username="root",
421 password="opnfv", timeout=2)
422 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
423 (stdin, stdout, stderr) = ssh.exec_command(command)
425 if "succeeded" in stdout.readlines()[0]:
426 logger.info('\033[92m' + "TEST 2 [PASSED] "
427 "==> HTTP WORKS" + '\033[0m')
429 json_results.update({"Test 2: HTTP works": "Passed"})
431 logger.error('\033[91m' + "TEST 2 [FAILED] "
432 "==> HTTP BLOCKED" + '\033[0m')
434 json_results.update({"Test 2: HTTP works": "Failed"})
437 logger.debug("Waiting for %s..." % floatip_client)
441 # CHANGE OF CLASSIFICATION #
442 logger.info("Changing the classification")
443 tacker_classi = "%s/testcases/features/sfc/%s" % \
444 (FUNCTEST_REPO, TACKER_CHANGECLASSI)
445 subprocess.call(tacker_classi, shell=True)
447 logger.info("Wait for ODL to update the classification rules in OVS")
450 # SSH TO EXECUTE cmd_client
453 ssh.connect(floatip_client, username="root",
454 password="opnfv", timeout=2)
455 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
456 (stdin, stdout, stderr) = ssh.exec_command(command)
458 if "timed out" in stdout.readlines()[0]:
459 logger.info('\033[92m' + "TEST 3 [PASSED] "
460 "==> HTTP BLOCKED" + '\033[0m')
462 json_results.update({"Test 3: HTTP Blocked": "Passed"})
464 logger.error('\033[91m' + "TEST 3 [FAILED] "
465 "==> HTTP NOT BLOCKED" + '\033[0m')
467 json_results.update({"Test 3: HTTP Blocked": "Failed"})
470 logger.debug("Waiting for %s..." % floatip_client)
474 # SSH TO EXECUTE cmd_client
476 ssh.connect(floatip_client, username="root",
477 password="opnfv", timeout=2)
478 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
479 (stdin, stdout, stderr) = ssh.exec_command(command)
481 if "succeeded" in stdout.readlines()[0]:
482 logger.info('\033[92m' + "TEST 4 [PASSED] "
483 "==> SSH WORKS" + '\033[0m')
485 json_results.update({"Test 4: SSH works": "Passed"})
487 logger.error('\033[91m' + "TEST 4 [FAILED] "
488 "==> SSH BLOCKED" + '\033[0m')
490 json_results.update({"Test 4: SSH works": "Failed"})
493 logger.debug("Waiting for %s..." % floatip_client)
499 for x in range(0, 5):
500 logger.info('\033[92m' + "SFC TEST WORKED"
501 " :) \n" + '\033[0m')
504 logger.info("Iterating again!")
505 delete = ("bash delete.sh")
507 subprocess.call(delete, shell=True, stderr=subprocess.PIPE)
510 logger.error("Problem when executing the delete.sh")
511 logger.error("Problem %s" % e)
514 stop_time = time.time()
515 json_results.update({"tests": "4", "failures": int(failures)})
516 logger.debug("Promise Results json: " + str(json_results))
517 ft_utils.push_results_to_db("sfc",
528 if __name__ == '__main__':