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_REPO = ft_utils.FUNCTEST_REPO
25 HOME = os.environ['HOME'] + "/"
28 INSTANCE_NAME = "client"
30 IMAGE_NAME = "sf_nsh_colorado"
31 IMAGE_FILENAME = "sf_nsh_colorado.qcow2"
32 IMAGE_FORMAT = "qcow2"
33 IMAGE_PATH = "/home/opnfv/functest/data" + "/" + IMAGE_FILENAME
35 # NEUTRON Private Network parameters
37 NET_NAME = "example-net"
38 SUBNET_NAME = "example-subnet"
39 SUBNET_CIDR = "11.0.0.0/24"
40 ROUTER_NAME = "example-router"
42 SECGROUP_NAME = "example-sg"
43 SECGROUP_DESCR = "Example Security group"
45 INSTANCE_NAME_2 = "server"
47 # TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
49 PRE_SETUP_SCRIPT = 'sfc_pre_setup.bash'
50 TACKER_SCRIPT = 'sfc_tacker.bash'
51 TEARDOWN_SCRIPT = "sfc_teardown.bash"
52 TACKER_CHANGECLASSI = "sfc_change_classi.bash"
57 # Allow any port so that tacker commands reaches the server.
58 # This will be deleted when tacker is included in OPNFV installation
62 start_time = time.time()
65 ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
66 contr_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
67 " 'fuel node'|grep controller|awk '{print $10}'")
68 logger.info("Executing script to get ip_server: '%s'" % contr_cmd)
69 process = subprocess.Popen(contr_cmd,
71 stdout=subprocess.PIPE,
72 stderr=subprocess.PIPE)
73 ip_server = process.stdout.readline().rstrip()
75 contr_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
76 " 'fuel node'|grep compute|awk '{print $10}'")
77 logger.info("Executing script to get ip_compute: '%s'" % contr_cmd2)
78 process = subprocess.Popen(contr_cmd2,
80 stdout=subprocess.PIPE,
81 stderr=subprocess.PIPE)
82 ip_compute = process.stdout.readline().rstrip()
84 iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
85 " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
86 iptable_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
87 " ssh " + ip_server + " iptables -t nat -P INPUT ACCEPT ")
88 iptable_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
89 " ssh " + ssh_options + " " + ip_server +
90 " iptables -A INPUT -m state"
91 " --state NEW,ESTABLISHED,RELATED -j ACCEPT")
93 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd1)
94 subprocess.call(iptable_cmd1, shell=True, stderr=subprocess.PIPE)
96 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd2)
97 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
99 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd3)
100 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
102 # Getting the different clients
104 nova_client = os_utils.get_nova_client()
105 neutron_client = os_utils.get_neutron_client()
106 glance_client = os_utils.get_glance_client()
110 if not os.path.isfile(IMAGE_PATH):
111 logger.info("Downloading image")
112 ft_utils.download_url(
113 "http://artifacts.opnfv.org/sfc/demo/sf_nsh_colorado.qcow2",
114 "/home/opnfv/functest/data/")
116 logger.info("Using old image")
118 # Create glance image and the neutron network
120 image_id = os_utils.create_glance_image(glance_client,
127 network_dic = os_utils.create_network_full(neutron_client,
134 "There has been a problem when creating the neutron network")
137 network_id = network_dic["net_id"]
139 sg_id = os_utils.create_security_group_full(neutron_client,
140 SECGROUP_NAME, SECGROUP_DESCR)
145 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
147 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
148 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
149 instance = os_utils.create_instance_and_wait_for_active(FLAVOR,
155 logger.error("Error while booting instance.")
157 # Retrieve IP of INSTANCE
158 instance_ip = instance.networks.get(NET_NAME)[0]
159 logger.debug("Instance '%s' got private ip '%s'." %
160 (INSTANCE_NAME, instance_ip))
162 logger.info("Adding '%s' to security group '%s'..."
163 % (INSTANCE_NAME, SECGROUP_NAME))
164 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
166 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
167 floatip_dic = os_utils.create_floating_ip(neutron_client)
168 floatip_client = floatip_dic['fip_addr']
169 # floatip_id = floatip_dic['fip_id']
171 if floatip_client is None:
172 logger.error("Cannot create floating IP.")
174 logger.info("Floating IP created: '%s'" % floatip_client)
176 logger.info("Associating floating ip: '%s' to VM '%s' "
177 % (floatip_client, INSTANCE_NAME))
178 if not os_utils.add_floating_ip(nova_client,
181 logger.error("Cannot associate floating IP to VM.")
184 # STARTING SECOND VM (server) ###
187 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
189 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
190 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
191 instance_2 = os_utils.create_instance_and_wait_for_active(
197 if instance_2 is None:
198 logger.error("Error while booting instance.")
200 # Retrieve IP of INSTANCE
201 instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
202 logger.debug("Instance '%s' got private ip '%s'." %
203 (INSTANCE_NAME_2, instance_ip_2))
205 logger.info("Adding '%s' to security group '%s'..."
206 % (INSTANCE_NAME_2, SECGROUP_NAME))
207 os_utils.add_secgroup_to_instance(nova_client, instance_2.id, sg_id)
209 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME_2)
210 floatip_dic = os_utils.create_floating_ip(neutron_client)
211 floatip_server = floatip_dic['fip_addr']
212 # floatip_id = floatip_dic['fip_id']
214 if floatip_server is None:
215 logger.error("Cannot create floating IP.")
217 logger.info("Floating IP created: '%s'" % floatip_server)
219 logger.info("Associating floating ip: '%s' to VM '%s' "
220 % (floatip_server, INSTANCE_NAME_2))
222 if not os_utils.add_floating_ip(nova_client,
225 logger.error("Cannot associate floating IP to VM.")
228 # CREATION OF THE 2 SF ####
230 tacker_script = "%s/testcases/features/sfc/%s" % \
231 (FUNCTEST_REPO, TACKER_SCRIPT)
232 logger.info("Executing tacker script: '%s'" % tacker_script)
233 subprocess.call(tacker_script, shell=True)
235 # SSH CALL TO START HTTP SERVER
236 ssh = paramiko.SSHClient()
237 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
240 ssh.connect(floatip_server, username="root",
241 password="opnfv", timeout=2)
242 command = "python -m SimpleHTTPServer 80 > /dev/null 2>&1 &"
243 logger.info("Starting HTTP server")
244 (stdin, stdout, stderr) = ssh.exec_command(command)
246 logger.debug("Waiting for %s..." % floatip_server)
250 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
253 for instance in instances:
254 if "server" not in instance.name:
255 if "client" not in instance.name:
257 "This is the instance name: %s " % instance.name)
258 floatip_dic = os_utils.create_floating_ip(
260 floatip = floatip_dic['fip_addr']
262 instance.add_floating_ip(floatip)
264 logger.debug("Problems assigning floating IP to SFs")
266 # If no IPs were obtained, then we cant continue
268 logger.error('Failed to obtain IPs, cant continue, exiting')
271 logger.info("Waiting 60 seconds for floating IP assignment")
272 for j in range(0, 6):
273 logger.debug("Test starting in"
274 " {0} seconds".format(str((6 - j) * 10)))
277 logger.debug("Floating IPs for SFs: %s..." % ips)
279 # SSH TO START THE VXLAN_TOOL ON SF1
280 logger.info("Configuring the SFs")
282 ssh.connect(ips[0], username="root",
283 password="opnfv", timeout=2)
284 command = ("nohup python vxlan_tool.py -i eth0 "
285 "-d forward -v off -b 80 > /dev/null 2>&1 &")
286 (stdin, stdout, stderr) = ssh.exec_command(command)
288 logger.debug("Waiting for %s..." % ips[0])
295 (stdin, stdout, stderr) = ssh.exec_command(
296 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
297 if len(stdout.readlines()) > 0:
298 logger.debug("HTTP firewall started")
304 logger.debug("HTTP firewall not started")
307 logger.exception("vxlan_tool not started in SF1")
309 # SSH TO START THE VXLAN_TOOL ON SF2
311 ssh.connect(ips[1], username="root",
312 password="opnfv", timeout=2)
313 command = ("nohup python vxlan_tool.py -i eth0 "
314 "-d forward -v off -b 22 > /dev/null 2>&1 &")
315 (stdin, stdout, stderr) = ssh.exec_command(command)
317 logger.debug("Waiting for %s..." % ips[1])
324 (stdin, stdout, stderr) = ssh.exec_command(
325 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
326 if len(stdout.readlines()) > 0:
327 logger.debug("SSH firewall started")
333 logger.debug("SSH firewall not started")
336 logger.exception("vxlan_tool not started in SF2")
338 # SSH to modify the classification flows in compute
340 contr_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " "
341 "root@10.20.0.2 'ssh " + ip_compute + " "
342 "'bash correct_classifier.bash''")
343 logger.info("Executing script to modify the classi: '%s'" % contr_cmd3)
344 process = subprocess.Popen(contr_cmd3,
346 stdout=subprocess.PIPE)
350 # SSH TO EXECUTE cmd_client
351 logger.info("TEST STARTED")
353 ssh.connect(floatip_client, username="root",
354 password="opnfv", timeout=2)
355 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
356 (stdin, stdout, stderr) = ssh.exec_command(command)
358 # WRITE THE CORRECT WAY TO DO LOGGING
359 if "timed out" in stdout.readlines()[0]:
360 logger.info('\033[92m' + "TEST 1 [PASSED] "
361 "==> SSH BLOCKED" + '\033[0m')
363 json_results.update({"Test 1: SSH Blocked": "Passed"})
365 logger.error('\033[91m' + "TEST 1 [FAILED] "
366 "==> SSH NOT BLOCKED" + '\033[0m')
368 json_results.update({"Test 1: SSH Blocked": "Failed"})
371 logger.debug("Waiting for %s..." % floatip_client)
375 # SSH TO EXECUTE cmd_client
377 ssh.connect(floatip_client, username="root",
378 password="opnfv", timeout=2)
379 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
380 (stdin, stdout, stderr) = ssh.exec_command(command)
382 if "succeeded" in stdout.readlines()[0]:
383 logger.info('\033[92m' + "TEST 2 [PASSED] "
384 "==> HTTP WORKS" + '\033[0m')
386 json_results.update({"Test 2: HTTP works": "Passed"})
388 logger.error('\033[91m' + "TEST 2 [FAILED] "
389 "==> HTTP BLOCKED" + '\033[0m')
391 json_results.update({"Test 2: HTTP works": "Failed"})
394 logger.debug("Waiting for %s..." % floatip_client)
398 # CHANGE OF CLASSIFICATION #
399 logger.info("Changing the classification")
400 tacker_classi = "%s/testcases/features/sfc/%s" % \
401 (FUNCTEST_REPO, TACKER_CHANGECLASSI)
402 subprocess.call(tacker_classi, shell=True)
404 logger.info("Wait for ODL to update the classification rules in OVS")
407 # SSH to modify the classification flows in compute
409 contr_cmd4 = ("sshpass -p r00tme ssh " + ssh_options + " "
410 "root@10.20.0.2 'ssh " + ip_compute + " "
411 "'bash correct_classifier.bash''")
412 logger.info("Executing script to modify the classi: '%s'" % contr_cmd4)
413 process = subprocess.Popen(contr_cmd4,
415 stdout=subprocess.PIPE)
417 # 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 "timed out" in stdout.readlines()[0]:
426 logger.info('\033[92m' + "TEST 3 [PASSED] "
427 "==> HTTP BLOCKED" + '\033[0m')
429 json_results.update({"Test 3: HTTP Blocked": "Passed"})
431 logger.error('\033[91m' + "TEST 3 [FAILED] "
432 "==> HTTP NOT BLOCKED" + '\033[0m')
434 json_results.update({"Test 3: HTTP Blocked": "Failed"})
437 logger.debug("Waiting for %s..." % floatip_client)
441 # SSH TO EXECUTE cmd_client
443 ssh.connect(floatip_client, username="root",
444 password="opnfv", timeout=2)
445 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
446 (stdin, stdout, stderr) = ssh.exec_command(command)
448 if "succeeded" in stdout.readlines()[0]:
449 logger.info('\033[92m' + "TEST 4 [PASSED] "
450 "==> SSH WORKS" + '\033[0m')
452 json_results.update({"Test 4: SSH works": "Passed"})
454 logger.error('\033[91m' + "TEST 4 [FAILED] "
455 "==> SSH BLOCKED" + '\033[0m')
457 json_results.update({"Test 4: SSH works": "Failed"})
460 logger.debug("Waiting for %s..." % floatip_client)
466 for x in range(0, 5):
467 logger.info('\033[92m' + "SFC TEST WORKED"
468 " :) \n" + '\033[0m')
471 logger.debug("Iterating again!")
472 delete = ("bash delete.sh")
473 subprocess.call(delete, shell=True, stderr=subprocess.PIPE)
477 stop_time = time.time()
478 json_results.update({"tests": "4", "failures": int(failures)})
479 logger.debug("Promise Results json: " + str(json_results))
480 ft_utils.push_results_to_db("sfc",
491 if __name__ == '__main__':