9 import functest.utils.functest_logger as ft_logger
10 import functest.utils.functest_utils as ft_utils
11 import functest.utils.openstack_utils as os_utils
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 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 contr_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
86 " 'fuel node'|grep compute|awk '{print $10}'")
87 logger.info("Executing script to get ip_compute: '%s'" % contr_cmd2)
88 process = subprocess.Popen(contr_cmd2,
90 stdout=subprocess.PIPE,
91 stderr=subprocess.PIPE)
92 ip_compute = process.stdout.readline().rstrip()
94 iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
95 " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
96 iptable_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
97 " ssh " + ip_server + " iptables -t nat -P INPUT ACCEPT ")
98 iptable_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
99 " ssh " + ssh_options + " " + ip_server +
100 " iptables -A INPUT -m state"
101 " --state NEW,ESTABLISHED,RELATED -j ACCEPT")
103 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd1)
104 subprocess.call(iptable_cmd1, shell=True, stderr=subprocess.PIPE)
106 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd2)
107 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
109 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd3)
110 subprocess.call(iptable_cmd3, shell=True, stderr=subprocess.PIPE)
112 # Getting the different clients
114 nova_client = os_utils.get_nova_client()
115 neutron_client = os_utils.get_neutron_client()
116 glance_client = os_utils.get_glance_client()
120 if not os.path.isfile(IMAGE_PATH):
121 logger.info("Downloading image")
122 ft_utils.download_url(
123 "http://artifacts.opnfv.org/sfc/demo/sf_nsh_colorado.qcow2",
124 "/home/opnfv/functest/data/")
126 logger.info("Using old image")
128 # Create glance image and the neutron network
130 image_id = os_utils.create_glance_image(glance_client,
137 network_dic = os_utils.create_network_full(neutron_client,
144 "There has been a problem when creating the neutron network")
147 network_id = network_dic["net_id"]
149 sg_id = os_utils.create_security_group_full(neutron_client,
150 SECGROUP_NAME, SECGROUP_DESCR)
152 secgroups = os_utils.get_security_groups(neutron_client)
155 os_utils.create_secgroup_rule(neutron_client, sg['id'],
159 os_utils.create_secgroup_rule(neutron_client, sg['id'],
164 _, custom_flv_id = os_utils.get_or_create_flavor(
165 'custom', 1500, 10, 1, public=True)
166 if not custom_flv_id:
167 logger.error("Failed to create custom flavor")
171 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
173 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
174 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
175 instance = os_utils.create_instance_and_wait_for_active(FLAVOR,
182 logger.error("Error while booting instance.")
184 # Retrieve IP of INSTANCE
185 instance_ip = instance.networks.get(NET_NAME)[0]
186 logger.debug("Instance '%s' got private ip '%s'." %
187 (INSTANCE_NAME, instance_ip))
189 logger.info("Adding '%s' to security group '%s'..."
190 % (INSTANCE_NAME, SECGROUP_NAME))
191 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
193 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
194 floatip_dic = os_utils.create_floating_ip(neutron_client)
195 floatip_client = floatip_dic['fip_addr']
196 # floatip_id = floatip_dic['fip_id']
198 if floatip_client is None:
199 logger.error("Cannot create floating IP.")
201 logger.info("Floating IP created: '%s'" % floatip_client)
203 logger.info("Associating floating ip: '%s' to VM '%s' "
204 % (floatip_client, INSTANCE_NAME))
205 if not os_utils.add_floating_ip(nova_client, instance.id, floatip_client):
206 logger.error("Cannot associate floating IP to VM.")
209 # STARTING SECOND VM (server) ###
212 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
214 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
215 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
216 instance_2 = os_utils.create_instance_and_wait_for_active(FLAVOR,
222 if instance_2 is None:
223 logger.error("Error while booting instance.")
225 # Retrieve IP of INSTANCE
226 instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
227 logger.debug("Instance '%s' got private ip '%s'." %
228 (INSTANCE_NAME_2, instance_ip_2))
230 logger.info("Adding '%s' to security group '%s'..."
231 % (INSTANCE_NAME_2, SECGROUP_NAME))
232 os_utils.add_secgroup_to_instance(nova_client, instance_2.id, sg_id)
234 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME_2)
235 floatip_dic = os_utils.create_floating_ip(neutron_client)
236 floatip_server = floatip_dic['fip_addr']
237 # floatip_id = floatip_dic['fip_id']
239 if floatip_server is None:
240 logger.error("Cannot create floating IP.")
242 logger.info("Floating IP created: '%s'" % floatip_server)
244 logger.info("Associating floating ip: '%s' to VM '%s' "
245 % (floatip_server, INSTANCE_NAME_2))
247 if not os_utils.add_floating_ip(nova_client,
250 logger.error("Cannot associate floating IP to VM.")
253 # CREATION OF THE 2 SF ####
255 tacker_script = "%s/testcases/features/sfc/%s" % \
256 (FUNCTEST_REPO, TACKER_SCRIPT)
257 logger.info("Executing tacker script: '%s'" % tacker_script)
258 subprocess.call(tacker_script, shell=True)
260 # SSH CALL TO START HTTP SERVER
261 ssh = paramiko.SSHClient()
262 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
265 ssh.connect(floatip_server, username="root",
266 password="opnfv", timeout=2)
267 command = "python -m SimpleHTTPServer 80 > /dev/null 2>&1 &"
268 logger.info("Starting HTTP server")
269 (stdin, stdout, stderr) = ssh.exec_command(command)
271 logger.debug("Waiting for %s..." % floatip_server)
275 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
278 for instance in instances:
279 if "server" not in instance.name:
280 if "client" not in instance.name:
282 "This is the instance name: %s " % instance.name)
283 floatip_dic = os_utils.create_floating_ip(neutron_client)
284 floatip = floatip_dic['fip_addr']
286 instance.add_floating_ip(floatip)
288 logger.debug("Problems assigning floating IP to SFs")
290 # If no IPs were obtained, then we cant continue
292 logger.error('Failed to obtain IPs, cant continue, exiting')
295 logger.debug("Floating IPs for SFs: %s..." % ips)
297 # Check SSH connectivity to VNFs
300 check = [False, False]
302 logger.info("Checking SSH connectivity to the SFs with ips %s" % str(ips))
303 while r < retries and not all(check):
305 check = [check_ssh(ips[0]), check_ssh(ips[1])]
307 logger.exception("SSH check failed")
308 check = [False, False]
313 logger.error("Cannot establish SSH connection to the SFs")
316 logger.info("SSH connectivity to the SFs established")
318 # SSH TO START THE VXLAN_TOOL ON SF1
319 logger.info("Configuring the SFs")
321 ssh.connect(ips[0], username="root",
322 password="opnfv", timeout=2)
323 command = ("nohup python vxlan_tool.py -i eth0 "
324 "-d forward -v off -b 80 > /dev/null 2>&1 &")
325 (stdin, stdout, stderr) = ssh.exec_command(command)
327 logger.debug("Waiting for %s..." % ips[0])
333 (stdin, stdout, stderr) = ssh.exec_command(
334 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
335 if len(stdout.readlines()) > 0:
336 logger.debug("HTTP firewall started")
339 logger.debug("HTTP firewall not started")
342 logger.exception("vxlan_tool not started in SF1")
344 # SSH TO START THE VXLAN_TOOL ON SF2
346 ssh.connect(ips[1], username="root",
347 password="opnfv", timeout=2)
348 command = ("nohup python vxlan_tool.py -i eth0 "
349 "-d forward -v off -b 22 > /dev/null 2>&1 &")
350 (stdin, stdout, stderr) = ssh.exec_command(command)
352 logger.debug("Waiting for %s..." % ips[1])
358 (stdin, stdout, stderr) = ssh.exec_command(
359 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
360 if len(stdout.readlines()) > 0:
361 logger.debug("SSH firewall started")
364 logger.debug("SSH firewall not started")
367 logger.exception("vxlan_tool not started in SF2")
369 # SSH to modify the classification flows in compute
371 contr_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
372 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
373 logger.info("Executing script to modify the classi: '%s'" % contr_cmd3)
374 process = subprocess.Popen(contr_cmd3,
376 stdout=subprocess.PIPE)
380 # SSH TO EXECUTE cmd_client
381 logger.info("TEST STARTED")
383 ssh.connect(floatip_client, username="root",
384 password="opnfv", timeout=2)
385 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
386 (stdin, stdout, stderr) = ssh.exec_command(command)
388 # WRITE THE CORRECT WAY TO DO LOGGING
389 if "timed out" in stdout.readlines()[0]:
390 logger.info('\033[92m' + "TEST 1 [PASSED] "
391 "==> SSH BLOCKED" + '\033[0m')
393 json_results.update({"Test 1: SSH Blocked": "Passed"})
395 logger.error('\033[91m' + "TEST 1 [FAILED] "
396 "==> SSH NOT BLOCKED" + '\033[0m')
398 json_results.update({"Test 1: SSH Blocked": "Failed"})
401 logger.debug("Waiting for %s..." % floatip_client)
405 # SSH TO EXECUTE cmd_client
407 ssh.connect(floatip_client, username="root",
408 password="opnfv", timeout=2)
409 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
410 (stdin, stdout, stderr) = ssh.exec_command(command)
412 if "succeeded" in stdout.readlines()[0]:
413 logger.info('\033[92m' + "TEST 2 [PASSED] "
414 "==> HTTP WORKS" + '\033[0m')
416 json_results.update({"Test 2: HTTP works": "Passed"})
418 logger.error('\033[91m' + "TEST 2 [FAILED] "
419 "==> HTTP BLOCKED" + '\033[0m')
421 json_results.update({"Test 2: HTTP works": "Failed"})
424 logger.debug("Waiting for %s..." % floatip_client)
428 # CHANGE OF CLASSIFICATION #
429 logger.info("Changing the classification")
430 tacker_classi = "%s/testcases/features/sfc/%s" % \
431 (FUNCTEST_REPO, TACKER_CHANGECLASSI)
432 subprocess.call(tacker_classi, shell=True)
434 logger.info("Wait for ODL to update the classification rules in OVS")
437 # SSH to modify the classification flows in compute
439 contr_cmd4 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
440 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
441 logger.info("Executing script to modify the classi: '%s'" % contr_cmd4)
442 process = subprocess.Popen(contr_cmd4,
444 stdout=subprocess.PIPE)
446 # SSH TO EXECUTE cmd_client
449 ssh.connect(floatip_client, username="root",
450 password="opnfv", timeout=2)
451 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
452 (stdin, stdout, stderr) = ssh.exec_command(command)
454 if "timed out" in stdout.readlines()[0]:
455 logger.info('\033[92m' + "TEST 3 [PASSED] "
456 "==> HTTP BLOCKED" + '\033[0m')
458 json_results.update({"Test 3: HTTP Blocked": "Passed"})
460 logger.error('\033[91m' + "TEST 3 [FAILED] "
461 "==> HTTP NOT BLOCKED" + '\033[0m')
463 json_results.update({"Test 3: HTTP Blocked": "Failed"})
466 logger.debug("Waiting for %s..." % floatip_client)
470 # SSH TO EXECUTE cmd_client
472 ssh.connect(floatip_client, username="root",
473 password="opnfv", timeout=2)
474 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
475 (stdin, stdout, stderr) = ssh.exec_command(command)
477 if "succeeded" in stdout.readlines()[0]:
478 logger.info('\033[92m' + "TEST 4 [PASSED] "
479 "==> SSH WORKS" + '\033[0m')
481 json_results.update({"Test 4: SSH works": "Passed"})
483 logger.error('\033[91m' + "TEST 4 [FAILED] "
484 "==> SSH BLOCKED" + '\033[0m')
486 json_results.update({"Test 4: SSH works": "Failed"})
489 logger.debug("Waiting for %s..." % floatip_client)
494 for x in range(0, 5):
495 logger.info('\033[92m' + "SFC TEST WORKED"
496 " :) \n" + '\033[0m')
499 stop_time = time.time()
500 json_results.update({"tests": "4", "failures": int(failures)})
501 logger.debug("Promise Results json: " + str(json_results))
502 ft_utils.push_results_to_db("sfc",
513 if __name__ == '__main__':