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 logger.debug("Floating IPs for SFs: %s..." % ips)
243 # SSH TO START THE VXLAN_TOOL ON SF1
244 logger.info("Configuring the SFs")
246 ssh.connect(ips[0], username="root",
247 password="opnfv", timeout=2)
248 command = ("nohup python vxlan_tool.py -i eth0 "
249 "-d forward -v off -b 80 > /dev/null 2>&1 &")
250 (stdin, stdout, stderr) = ssh.exec_command(command)
252 logger.debug("Waiting for %s..." % ips[0])
258 (stdin, stdout, stderr) = ssh.exec_command("ps lax | grep python")
259 if "vxlan_tool.py" in stdout.readlines()[0]:
260 logger.debug("HTTP firewall started")
263 logger.debug("HTTP firewall not started")
266 logger.error("vxlan_tool not started in SF1")
268 # SSH TO START THE VXLAN_TOOL ON SF2
270 ssh.connect(ips[1], username="root",
271 password="opnfv", timeout=2)
272 command = ("nohup python vxlan_tool.py -i eth0 "
273 "-d forward -v off -b 22 > /dev/null 2>&1 &")
274 (stdin, stdout, stderr) = ssh.exec_command(command)
276 logger.debug("Waiting for %s..." % ips[1])
282 (stdin, stdout, stderr) = ssh.exec_command("ps lax | grep python")
283 if "vxlan_tool.py" in stdout.readlines()[0]:
284 logger.debug("SSH firewall started")
287 logger.debug("SSH firewall not started")
290 logger.error("vxlan_tool not started in SF2")
292 # SSH to modify the classification flows in compute
294 contr_cmd3 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
295 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
296 logger.info("Executing script to modify the classi: '%s'" % contr_cmd3)
297 process = subprocess.Popen(contr_cmd3,
299 stdout=subprocess.PIPE)
301 # SSH TO EXECUTE cmd_client
303 logger.info("TEST STARTED")
305 ssh.connect(floatip_client, username="root",
306 password="opnfv", timeout=2)
307 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
308 (stdin, stdout, stderr) = ssh.exec_command(command)
310 logger.debug("Waiting for %s..." % floatip_client)
314 # WRITE THE CORRECT WAY TO DO LOGGING
316 if "timed out" in stdout.readlines()[0]:
317 logger.info('\033[92m' + "TEST 1 [PASSED] "
318 "==> SSH BLOCKED" + '\033[0m')
321 logger.error('\033[91m' + "TEST 1 [FAILED] "
322 "==> SSH NOT BLOCKED" + '\033[0m')
325 # SSH TO EXECUTE cmd_client
328 ssh.connect(floatip_client, username="root",
329 password="opnfv", timeout=2)
330 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
331 (stdin, stdout, stderr) = ssh.exec_command(command)
333 logger.debug("Waiting for %s..." % floatip_client)
337 if "succeeded" in stdout.readlines()[0]:
338 logger.info('\033[92m' + "TEST 2 [PASSED] "
339 "==> HTTP WORKS" + '\033[0m')
342 logger.error('\033[91m' + "TEST 2 [FAILED] "
343 "==> HTTP BLOCKED" + '\033[0m')
346 # CHANGE OF CLASSIFICATION #
347 logger.info("Changing the classification")
348 tacker_classi = "/home/opnfv/repos/functest/testcases/features/sfc/" + \
350 subprocess.call(tacker_classi, shell=True)
352 logger.info("Wait for ODL to update the classification rules in OVS")
355 # SSH to modify the classification flows in compute
357 contr_cmd4 = ("sshpass -p r00tme ssh " + ssh_options + " root@10.20.0.2"
358 " 'ssh " + ip_compute + " 'bash correct_classifier.bash''")
359 logger.info("Executing script to modify the classi: '%s'" % contr_cmd4)
360 process = subprocess.Popen(contr_cmd4,
362 stdout=subprocess.PIPE)
364 # SSH TO EXECUTE cmd_client
367 ssh.connect(floatip_client, username="root",
368 password="opnfv", timeout=2)
369 command = "nc -w 5 -zv " + instance_ip_2 + " 80 2>&1"
370 (stdin, stdout, stderr) = ssh.exec_command(command)
372 logger.debug("Waiting for %s..." % floatip_client)
376 if "timed out" in stdout.readlines()[0]:
377 logger.info('\033[92m' + "TEST 3 [WORKS] "
378 "==> HTTP BLOCKED" + '\033[0m')
381 logger.error('\033[91m' + "TEST 3 [FAILED] "
382 "==> HTTP NOT BLOCKED" + '\033[0m')
385 # SSH TO EXECUTE cmd_client
388 ssh.connect(floatip_client, username="root",
389 password="opnfv", timeout=2)
390 command = "nc -w 5 -zv " + instance_ip_2 + " 22 2>&1"
391 (stdin, stdout, stderr) = ssh.exec_command(command)
393 logger.debug("Waiting for %s..." % floatip_client)
397 if "succeeded" in stdout.readlines()[0]:
398 logger.info('\033[92m' + "TEST 4 [WORKS] "
399 "==> SSH WORKS" + '\033[0m')
402 logger.error('\033[91m' + "TEST 4 [FAILED] "
403 "==> SSH BLOCKED" + '\033[0m')
407 for x in range(0, 5):
408 logger.info('\033[92m' + "SFC TEST WORKED"
409 " :) \n" + '\033[0m')
413 if __name__ == '__main__':