6 import functest.utils.functest_logger as ft_logger
7 import functest.utils.functest_utils as ft_utils
8 import functest.utils.openstack_utils as os_utils
11 parser = argparse.ArgumentParser()
13 parser.add_argument("-r", "--report",
14 help="Create json result file",
17 args = parser.parse_args()
19 """ logging configuration """
20 logger = ft_logger.Logger("ODL_SFC").getLogger()
22 REPO_PATH = os.environ['repos_dir'] + '/functest/'
23 HOME = os.environ['HOME'] + "/"
26 INSTANCE_NAME = "client"
28 IMAGE_NAME = "sf_nsh_colorado"
29 IMAGE_FILENAME = "sf_nsh_colorado.qcow2"
30 IMAGE_FORMAT = "qcow2"
31 IMAGE_PATH = "/home/opnfv/functest/data" + "/" + IMAGE_FILENAME
33 # NEUTRON Private Network parameters
35 NET_NAME = "example-net"
36 SUBNET_NAME = "example-subnet"
37 SUBNET_CIDR = "11.0.0.0/24"
38 ROUTER_NAME = "example-router"
40 SECGROUP_NAME = "example-sg"
41 SECGROUP_DESCR = "Example Security group"
43 INSTANCE_NAME_2 = "server"
45 # TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
47 PRE_SETUP_SCRIPT = 'sfc_pre_setup.bash'
48 TACKER_SCRIPT = 'sfc_tacker.bash'
49 TEARDOWN_SCRIPT = "sfc_teardown.bash"
50 TACKER_CHANGECLASSI = "sfc_change_classi.bash"
55 # Allow any port so that tacker commands reaches the server.
56 # This will be deleted when tacker is included in OPNFV installation
58 ssh_options = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
59 contr_cmd = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
60 " 'fuel node'|grep controller|awk '{print $10}'")
61 logger.info("Executing script to get ip_server: '%s'" % contr_cmd)
62 process = subprocess.Popen(contr_cmd,
64 stdout=subprocess.PIPE)
65 ip_server = process.stdout.readline().rstrip()
67 contr_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
68 " 'fuel node'|grep compute|awk '{print $10}'")
69 logger.info("Executing script to get ip_compute: '%s'" % contr_cmd2)
70 process = subprocess.Popen(contr_cmd2,
72 stdout=subprocess.PIPE)
73 ip_compute = process.stdout.readline().rstrip()
75 iptable_cmd1 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
76 " ssh " + ip_server + " iptables -P INPUT ACCEPT ")
77 iptable_cmd2 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
78 " ssh " + ip_server + " iptables -t nat -P INPUT ACCEPT ")
80 subprocess.call(iptable_cmd1, shell=True)
81 subprocess.call(iptable_cmd2, shell=True)
83 # Getting the different clients
85 nova_client = os_utils.get_nova_client()
86 neutron_client = os_utils.get_neutron_client()
87 glance_client = os_utils.get_glance_client()
91 if not os.path.isfile(IMAGE_PATH):
92 logger.info("Downloading image")
93 ft_utils.download_url(
94 "http://artifacts.opnfv.org/sfc/demo/sf_nsh_colorado.qcow2",
95 "/home/opnfv/functest/data/")
97 logger.info("Using old image")
99 # Create glance image and the neutron network
101 image_id = os_utils.create_glance_image(glance_client,
108 network_dic = os_utils.create_network_full(neutron_client,
115 "There has been a problem when creating the neutron network")
118 network_id = network_dic["net_id"]
120 sg_id = os_utils.create_security_group_full(neutron_client,
121 SECGROUP_NAME, SECGROUP_DESCR)
124 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
126 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
127 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
128 instance = os_utils.create_instance_and_wait_for_active(FLAVOR,
134 logger.error("Error while booting instance.")
136 # Retrieve IP of INSTANCE
137 instance_ip = instance.networks.get(NET_NAME)[0]
138 logger.debug("Instance '%s' got private ip '%s'." %
139 (INSTANCE_NAME, instance_ip))
141 logger.info("Adding '%s' to security group '%s'..."
142 % (INSTANCE_NAME, SECGROUP_NAME))
143 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
145 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
146 floatip_dic = os_utils.create_floating_ip(neutron_client)
147 floatip_client = floatip_dic['fip_addr']
148 # floatip_id = floatip_dic['fip_id']
150 if floatip_client is None:
151 logger.error("Cannot create floating IP.")
153 logger.info("Floating IP created: '%s'" % floatip_client)
155 logger.info("Associating floating ip: '%s' to VM '%s' "
156 % (floatip_client, INSTANCE_NAME))
157 if not os_utils.add_floating_ip(nova_client, instance.id, floatip_client):
158 logger.error("Cannot associate floating IP to VM.")
161 # STARTING SECOND VM (server) ###
164 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
166 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
167 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
168 instance_2 = os_utils.create_instance_and_wait_for_active(FLAVOR,
173 if instance_2 is None:
174 logger.error("Error while booting instance.")
176 # Retrieve IP of INSTANCE
177 instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
178 logger.debug("Instance '%s' got private ip '%s'." %
179 (INSTANCE_NAME_2, instance_ip_2))
181 logger.info("Adding '%s' to security group '%s'..."
182 % (INSTANCE_NAME_2, SECGROUP_NAME))
183 os_utils.add_secgroup_to_instance(nova_client, instance_2.id, sg_id)
185 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME_2)
186 floatip_dic = os_utils.create_floating_ip(neutron_client)
187 floatip_server = floatip_dic['fip_addr']
188 # floatip_id = floatip_dic['fip_id']
190 if floatip_server is None:
191 logger.error("Cannot create floating IP.")
193 logger.info("Floating IP created: '%s'" % floatip_server)
195 logger.info("Associating floating ip: '%s' to VM '%s' "
196 % (floatip_server, INSTANCE_NAME_2))
198 if not os_utils.add_floating_ip(nova_client,
201 logger.error("Cannot associate floating IP to VM.")
204 # CREATION OF THE 2 SF ####
206 tacker_script = "/home/opnfv/repos/functest/testcases/features/sfc/" + \
208 logger.info("Executing tacker script: '%s'" % tacker_script)
209 subprocess.call(tacker_script, shell=True)
211 # SSH CALL TO START HTTP SERVER
212 ssh = paramiko.SSHClient()
213 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
216 ssh.connect(floatip_server, username="root",
217 password="opnfv", timeout=2)
218 command = "python -m SimpleHTTPServer 80 > /dev/null 2>&1 &"
219 logger.info("Starting HTTP server")
220 (stdin, stdout, stderr) = ssh.exec_command(command)
222 logger.debug("Waiting for %s..." % floatip_server)
226 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
229 for instance in instances:
230 if "server" not in instance.name:
231 if "client" not in instance.name:
233 "This is the instance name: %s " % instance.name)
234 floatip_dic = os_utils.create_floating_ip(neutron_client)
235 floatip = floatip_dic['fip_addr']
237 instance.add_floating_ip(floatip)
239 logger.debug("Problems assigning floating IP to SFs")
241 # If no IPs were obtained, then we cant continue
243 logger.error('Failed to obtain IPs, cant continue, exiting')
246 logger.debug("Floating IPs for SFs: %s..." % ips)
248 # SSH TO START THE VXLAN_TOOL ON SF1
249 logger.info("Configuring the SFs")
251 ssh.connect(ips[0], username="root",
252 password="opnfv", timeout=2)
253 command = ("nohup python vxlan_tool.py -i eth0 "
254 "-d forward -v off -b 80 > /dev/null 2>&1 &")
255 (stdin, stdout, stderr) = ssh.exec_command(command)
257 logger.debug("Waiting for %s..." % ips[0])
263 (stdin, stdout, stderr) = ssh.exec_command(
264 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
265 if len(stdout.readlines()) > 0:
266 logger.debug("HTTP firewall started")
269 logger.debug("HTTP firewall not started")
272 logger.exception("vxlan_tool not started in SF1")
274 # SSH TO START THE VXLAN_TOOL ON SF2
276 ssh.connect(ips[1], username="root",
277 password="opnfv", timeout=2)
278 command = ("nohup python vxlan_tool.py -i eth0 "
279 "-d forward -v off -b 22 > /dev/null 2>&1 &")
280 (stdin, stdout, stderr) = ssh.exec_command(command)
282 logger.debug("Waiting for %s..." % ips[1])
288 (stdin, stdout, stderr) = ssh.exec_command(
289 "ps aux | grep \"vxlan_tool.py\" | grep -v grep")
290 if len(stdout.readlines()) > 0:
291 logger.debug("SSH firewall started")
294 logger.debug("SSH firewall not started")
297 logger.exception("vxlan_tool not started in SF2")
299 # SSH to modify the classification flows in compute
301 contr_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
302 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
303 logger.info("Executing script to modify the classi: '%s'" % contr_cmd3)
304 process = subprocess.Popen(contr_cmd3,
306 stdout=subprocess.PIPE)
308 logger.info("Waiting for 60 seconds before TEST")
309 for j in range(0, 6):
310 logger.info("Test starting in {0} seconds".format(str((6 - j)*10)))
315 # SSH TO EXECUTE cmd_client
316 logger.info("TEST STARTED")
318 ssh.connect(floatip_client, username="root",
319 password="opnfv", timeout=2)
320 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
321 (stdin, stdout, stderr) = ssh.exec_command(command)
323 # WRITE THE CORRECT WAY TO DO LOGGING
324 if "timed out" in stdout.readlines()[0]:
325 logger.info('\033[92m' + "TEST 1 [PASSED] "
326 "==> SSH BLOCKED" + '\033[0m')
329 logger.error('\033[91m' + "TEST 1 [FAILED] "
330 "==> SSH NOT BLOCKED" + '\033[0m')
333 logger.debug("Waiting for %s..." % floatip_client)
337 # SSH TO EXECUTE cmd_client
339 ssh.connect(floatip_client, username="root",
340 password="opnfv", timeout=2)
341 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
342 (stdin, stdout, stderr) = ssh.exec_command(command)
344 if "succeeded" in stdout.readlines()[0]:
345 logger.info('\033[92m' + "TEST 2 [PASSED] "
346 "==> HTTP WORKS" + '\033[0m')
349 logger.error('\033[91m' + "TEST 2 [FAILED] "
350 "==> HTTP BLOCKED" + '\033[0m')
353 logger.debug("Waiting for %s..." % floatip_client)
357 # CHANGE OF CLASSIFICATION #
358 logger.info("Changing the classification")
359 tacker_classi = "/home/opnfv/repos/functest/testcases/features/sfc/" + \
361 subprocess.call(tacker_classi, shell=True)
363 logger.info("Wait for ODL to update the classification rules in OVS")
366 # SSH to modify the classification flows in compute
368 contr_cmd4 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
369 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
370 logger.info("Executing script to modify the classi: '%s'" % contr_cmd4)
371 process = subprocess.Popen(contr_cmd4,
373 stdout=subprocess.PIPE)
375 # SSH TO EXECUTE cmd_client
378 ssh.connect(floatip_client, username="root",
379 password="opnfv", timeout=2)
380 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
381 (stdin, stdout, stderr) = ssh.exec_command(command)
383 if "timed out" in stdout.readlines()[0]:
384 logger.info('\033[92m' + "TEST 3 [WORKS] "
385 "==> HTTP BLOCKED" + '\033[0m')
388 logger.error('\033[91m' + "TEST 3 [FAILED] "
389 "==> HTTP NOT BLOCKED" + '\033[0m')
392 logger.debug("Waiting for %s..." % floatip_client)
396 # SSH TO EXECUTE cmd_client
398 ssh.connect(floatip_client, username="root",
399 password="opnfv", timeout=2)
400 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
401 (stdin, stdout, stderr) = ssh.exec_command(command)
403 if "succeeded" in stdout.readlines()[0]:
404 logger.info('\033[92m' + "TEST 4 [WORKS] "
405 "==> SSH WORKS" + '\033[0m')
408 logger.error('\033[91m' + "TEST 4 [FAILED] "
409 "==> SSH BLOCKED" + '\033[0m')
412 logger.debug("Waiting for %s..." % floatip_client)
417 for x in range(0, 5):
418 logger.info('\033[92m' + "SFC TEST WORKED"
419 " :) \n" + '\033[0m')
421 # TODO report results to DB
422 # functest_utils.logger_test_results(logger, "SFC",
425 # see doctor, promise, domino, ...
427 # logger.info("Pushing odl-SFC results")
428 # functest_utils.push_results_to_db("functest",
438 if __name__ == '__main__':