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_RESULTS_DIR = '/home/opnfv/functest/results/'
25 FUNCTEST_REPO = ft_utils.FUNCTEST_REPO
27 HOME = os.environ['HOME'] + "/"
30 INSTANCE_NAME = "client"
32 IMAGE_NAME = "sf_nsh_colorado"
33 IMAGE_FILENAME = "sf_nsh_colorado.qcow2"
34 IMAGE_FORMAT = "qcow2"
35 IMAGE_PATH = "/home/opnfv/functest/data" + "/" + IMAGE_FILENAME
37 # NEUTRON Private Network parameters
39 NET_NAME = "example-net"
40 SUBNET_NAME = "example-subnet"
41 SUBNET_CIDR = "11.0.0.0/24"
42 ROUTER_NAME = "example-router"
44 SECGROUP_NAME = "example-sg"
45 SECGROUP_DESCR = "Example Security group"
47 INSTANCE_NAME_2 = "server"
49 # TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
51 PRE_SETUP_SCRIPT = 'sfc_pre_setup.bash'
52 TACKER_SCRIPT = 'sfc_tacker.bash'
53 TEARDOWN_SCRIPT = "sfc_teardown.bash"
54 TACKER_CHANGECLASSI = "sfc_change_classi.bash"
56 ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
60 cmd = "sshpass -p opnfv ssh " + ssh_options + " -q " + ip + " exit"
61 success = subprocess.call(cmd, shell=True) == 0
63 logger.debug("Wating for SSH connectivity in SF with IP: %s" % ip)
69 # Allow any port so that tacker commands reaches the server.
70 # This will be deleted when tacker is included in OPNFV installation
74 start_time = time.time()
77 contr_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
78 " 'fuel node'|grep controller|awk '{print $10}'")
79 logger.info("Executing script to get ip_server: '%s'" % contr_cmd)
80 process = subprocess.Popen(contr_cmd,
82 stdout=subprocess.PIPE,
83 stderr=subprocess.PIPE)
84 ip_server = process.stdout.readline().rstrip()
86 contr_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
87 " 'fuel node'|grep compute|awk '{print $10}'")
88 logger.info("Executing script to get ip_compute: '%s'" % contr_cmd2)
89 process = subprocess.Popen(contr_cmd2,
91 stdout=subprocess.PIPE,
92 stderr=subprocess.PIPE)
93 ip_compute = process.stdout.readline().rstrip()
95 iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
96 " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
97 iptable_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
98 " ssh " + ip_server + " iptables -t nat -P INPUT ACCEPT ")
99 iptable_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
100 " ssh " + ssh_options + " " + ip_server +
101 " iptables -A INPUT -m state"
102 " --state NEW,ESTABLISHED,RELATED -j ACCEPT")
104 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd1)
105 subprocess.call(iptable_cmd1, shell=True, stderr=subprocess.PIPE)
107 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd2)
108 subprocess.call(iptable_cmd2, shell=True, stderr=subprocess.PIPE)
110 logger.info("Changing firewall policy in controller: '%s'" % iptable_cmd3)
111 subprocess.call(iptable_cmd3, shell=True, stderr=subprocess.PIPE)
113 # Getting the different clients
115 nova_client = os_utils.get_nova_client()
116 neutron_client = os_utils.get_neutron_client()
117 glance_client = os_utils.get_glance_client()
121 if not os.path.isfile(IMAGE_PATH):
122 logger.info("Downloading image")
123 ft_utils.download_url(
124 "http://artifacts.opnfv.org/sfc/demo/sf_nsh_colorado.qcow2",
125 "/home/opnfv/functest/data/")
127 logger.info("Using old image")
129 # Create glance image and the neutron network
131 image_id = os_utils.create_glance_image(glance_client,
138 network_dic = os_utils.create_network_full(neutron_client,
145 "There has been a problem when creating the neutron network")
148 network_id = network_dic["net_id"]
150 sg_id = os_utils.create_security_group_full(neutron_client,
151 SECGROUP_NAME, SECGROUP_DESCR)
153 secgroups = os_utils.get_security_groups(neutron_client)
156 os_utils.create_secgroup_rule(neutron_client, sg['id'],
160 os_utils.create_secgroup_rule(neutron_client, sg['id'],
165 _, custom_flv_id = os_utils.get_or_create_flavor(
166 'custom', 1500, 10, 1, public=True)
167 if not custom_flv_id:
168 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(FLAVOR,
183 logger.error("Error while booting instance.")
185 # Retrieve IP of INSTANCE
186 instance_ip = instance.networks.get(NET_NAME)[0]
187 logger.debug("Instance '%s' got private ip '%s'." %
188 (INSTANCE_NAME, instance_ip))
190 logger.info("Adding '%s' to security group '%s'..."
191 % (INSTANCE_NAME, SECGROUP_NAME))
192 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
194 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
195 floatip_dic = os_utils.create_floating_ip(neutron_client)
196 floatip_client = floatip_dic['fip_addr']
197 # floatip_id = floatip_dic['fip_id']
199 if floatip_client is None:
200 logger.error("Cannot create floating IP.")
202 logger.info("Floating IP created: '%s'" % floatip_client)
204 logger.info("Associating floating ip: '%s' to VM '%s' "
205 % (floatip_client, INSTANCE_NAME))
206 if not os_utils.add_floating_ip(nova_client, instance.id, floatip_client):
207 logger.error("Cannot associate floating IP to VM.")
210 # STARTING SECOND VM (server) ###
213 logger.info("Creating instance '%s'..." % INSTANCE_NAME_2)
215 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
216 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
217 instance_2 = os_utils.create_instance_and_wait_for_active(FLAVOR,
223 if instance_2 is None:
224 logger.error("Error while booting instance.")
226 # Retrieve IP of INSTANCE
227 instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
228 logger.debug("Instance '%s' got private ip '%s'." %
229 (INSTANCE_NAME_2, instance_ip_2))
231 logger.info("Adding '%s' to security group '%s'..."
232 % (INSTANCE_NAME_2, SECGROUP_NAME))
233 os_utils.add_secgroup_to_instance(nova_client, instance_2.id, sg_id)
235 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME_2)
236 floatip_dic = os_utils.create_floating_ip(neutron_client)
237 floatip_server = floatip_dic['fip_addr']
238 # floatip_id = floatip_dic['fip_id']
240 if floatip_server is None:
241 logger.error("Cannot create floating IP.")
243 logger.info("Floating IP created: '%s'" % floatip_server)
245 logger.info("Associating floating ip: '%s' to VM '%s' "
246 % (floatip_server, INSTANCE_NAME_2))
248 if not os_utils.add_floating_ip(nova_client,
251 logger.error("Cannot associate floating IP to VM.")
254 # CREATION OF THE 2 SF ####
256 tacker_script = "%s/testcases/features/sfc/%s" % \
257 (FUNCTEST_REPO, TACKER_SCRIPT)
258 logger.info("Executing tacker script: '%s'" % tacker_script)
259 subprocess.call(tacker_script, shell=True)
261 # SSH CALL TO START HTTP SERVER
262 ssh = paramiko.SSHClient()
263 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
266 ssh.connect(floatip_server, username="root",
267 password="opnfv", timeout=2)
268 command = "python -m SimpleHTTPServer 80 > /dev/null 2>&1 &"
269 logger.info("Starting HTTP server")
270 (stdin, stdout, stderr) = ssh.exec_command(command)
272 logger.debug("Waiting for %s..." % floatip_server)
276 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
279 for instance in instances:
280 if "server" not in instance.name:
281 if "client" not in instance.name:
283 "This is the instance name: %s " % instance.name)
284 floatip_dic = os_utils.create_floating_ip(neutron_client)
285 floatip = floatip_dic['fip_addr']
287 instance.add_floating_ip(floatip)
289 logger.debug("Problems assigning floating IP to SFs")
291 # If no IPs were obtained, then we cant continue
293 logger.error('Failed to obtain IPs, cant continue, exiting')
296 logger.debug("Floating IPs for SFs: %s..." % ips)
298 # Check SSH connectivity to VNFs
301 check = [False, False]
303 logger.info("Checking SSH connectivity to the SFs with ips %s" % str(ips))
304 while r < retries and not all(check):
306 check = [check_ssh(ips[0]), check_ssh(ips[1])]
308 logger.exception("SSH check failed")
309 check = [False, False]
314 logger.error("Cannot establish SSH connection to the SFs")
317 logger.info("SSH connectivity to the SFs established")
319 # SSH TO START THE VXLAN_TOOL ON SF1
320 logger.info("Configuring the SFs")
322 ssh.connect(ips[0], username="root",
323 password="opnfv", timeout=2)
324 command = ("nohup python vxlan_tool.py -i eth0 "
325 "-d forward -v off -b 80 > /dev/null 2>&1 &")
326 (stdin, stdout, stderr) = ssh.exec_command(command)
328 logger.debug("Waiting for %s..." % ips[0])
334 (stdin, stdout, stderr) = ssh.exec_command(
335 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
336 if len(stdout.readlines()) > 0:
337 logger.debug("HTTP firewall started")
340 logger.debug("HTTP firewall not started")
343 logger.exception("vxlan_tool not started in SF1")
345 # SSH TO START THE VXLAN_TOOL ON SF2
347 ssh.connect(ips[1], username="root",
348 password="opnfv", timeout=2)
349 command = ("nohup python vxlan_tool.py -i eth0 "
350 "-d forward -v off -b 22 > /dev/null 2>&1 &")
351 (stdin, stdout, stderr) = ssh.exec_command(command)
353 logger.debug("Waiting for %s..." % ips[1])
359 (stdin, stdout, stderr) = ssh.exec_command(
360 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
361 if len(stdout.readlines()) > 0:
362 logger.debug("SSH firewall started")
365 logger.debug("SSH firewall not started")
368 logger.exception("vxlan_tool not started in SF2")
370 # SSH to modify the classification flows in compute
372 contr_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
373 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
374 logger.info("Executing script to modify the classi: '%s'" % contr_cmd3)
375 process = subprocess.Popen(contr_cmd3,
377 stdout=subprocess.PIPE)
381 # SSH TO EXECUTE cmd_client
382 logger.info("TEST STARTED")
384 ssh.connect(floatip_client, username="root",
385 password="opnfv", timeout=2)
386 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
387 (stdin, stdout, stderr) = ssh.exec_command(command)
389 # WRITE THE CORRECT WAY TO DO LOGGING
390 if "timed out" in stdout.readlines()[0]:
391 logger.info('\033[92m' + "TEST 1 [PASSED] "
392 "==> SSH BLOCKED" + '\033[0m')
394 json_results.update({"Test 1: SSH Blocked": "Passed"})
396 logger.error('\033[91m' + "TEST 1 [FAILED] "
397 "==> SSH NOT BLOCKED" + '\033[0m')
399 json_results.update({"Test 1: SSH Blocked": "Failed"})
402 logger.debug("Waiting for %s..." % floatip_client)
406 # SSH TO EXECUTE cmd_client
408 ssh.connect(floatip_client, username="root",
409 password="opnfv", timeout=2)
410 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
411 (stdin, stdout, stderr) = ssh.exec_command(command)
413 if "succeeded" in stdout.readlines()[0]:
414 logger.info('\033[92m' + "TEST 2 [PASSED] "
415 "==> HTTP WORKS" + '\033[0m')
417 json_results.update({"Test 2: HTTP works": "Passed"})
419 logger.error('\033[91m' + "TEST 2 [FAILED] "
420 "==> HTTP BLOCKED" + '\033[0m')
422 json_results.update({"Test 2: HTTP works": "Failed"})
425 logger.debug("Waiting for %s..." % floatip_client)
429 # CHANGE OF CLASSIFICATION #
430 logger.info("Changing the classification")
431 tacker_classi = "%s/testcases/features/sfc/%s" % \
432 (FUNCTEST_REPO, TACKER_CHANGECLASSI)
433 subprocess.call(tacker_classi, shell=True)
435 logger.info("Wait for ODL to update the classification rules in OVS")
438 # SSH to modify the classification flows in compute
440 contr_cmd4 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
441 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
442 logger.info("Executing script to modify the classi: '%s'" % contr_cmd4)
443 process = subprocess.Popen(contr_cmd4,
445 stdout=subprocess.PIPE)
447 # SSH TO EXECUTE cmd_client
450 ssh.connect(floatip_client, username="root",
451 password="opnfv", timeout=2)
452 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
453 (stdin, stdout, stderr) = ssh.exec_command(command)
455 if "timed out" in stdout.readlines()[0]:
456 logger.info('\033[92m' + "TEST 3 [PASSED] "
457 "==> HTTP BLOCKED" + '\033[0m')
459 json_results.update({"Test 3: HTTP Blocked": "Passed"})
461 logger.error('\033[91m' + "TEST 3 [FAILED] "
462 "==> HTTP NOT BLOCKED" + '\033[0m')
464 json_results.update({"Test 3: HTTP Blocked": "Failed"})
467 logger.debug("Waiting for %s..." % floatip_client)
471 # SSH TO EXECUTE cmd_client
473 ssh.connect(floatip_client, username="root",
474 password="opnfv", timeout=2)
475 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
476 (stdin, stdout, stderr) = ssh.exec_command(command)
478 if "succeeded" in stdout.readlines()[0]:
479 logger.info('\033[92m' + "TEST 4 [PASSED] "
480 "==> SSH WORKS" + '\033[0m')
482 json_results.update({"Test 4: SSH works": "Passed"})
484 logger.error('\033[91m' + "TEST 4 [FAILED] "
485 "==> SSH BLOCKED" + '\033[0m')
487 json_results.update({"Test 4: SSH works": "Failed"})
490 logger.debug("Waiting for %s..." % floatip_client)
495 for x in range(0, 5):
496 logger.info('\033[92m' + "SFC TEST WORKED"
497 " :) \n" + '\033[0m')
500 stop_time = time.time()
501 json_results.update({"tests": "4", "failures": int(failures)})
502 logger.debug("Promise Results json: " + str(json_results))
503 ft_utils.push_results_to_db("sfc",
514 if __name__ == '__main__':