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
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("create_instance_and_ip").getLogger()
23 REPO_PATH = os.environ['repos_dir'] + '/functest/'
24 HOME = os.environ['HOME'] + "/"
27 INSTANCE_NAME = "client"
29 IMAGE_NAME = "sf_summit2016"
30 IMAGE_FILENAME = "sf_summit2016.qcow2"
31 IMAGE_FORMAT = "qcow2"
32 IMAGE_PATH = "/home/opnfv/functest/data" + "/" + IMAGE_FILENAME
34 # NEUTRON Private Network parameters
36 NET_NAME = "example-net"
37 SUBNET_NAME = "example-subnet"
38 SUBNET_CIDR = "11.0.0.0/24"
39 ROUTER_NAME = "example-router"
41 SECGROUP_NAME = "example-sg"
42 SECGROUP_DESCR = "Example Security group"
44 INSTANCE_NAME_2 = "server"
46 # TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
48 PRE_SETUP_SCRIPT = 'sfc_pre_setup.bash'
49 TACKER_SCRIPT = 'sfc_tacker.bash'
50 TEARDOWN_SCRIPT = "sfc_teardown.bash"
51 TACKER_CHANGECLASSI = "sfc_change_classi.bash"
56 nova_client = os_utils.get_nova_client()
57 neutron_client = os_utils.get_neutron_client()
58 glance_client = os_utils.get_glance_client()
62 if not os.path.isfile(IMAGE_PATH):
63 logger.info("Downloading image")
64 ft_utils.download_url(
65 "http://artifacts.opnfv.org/sfc/demo/sf_summit2016.qcow2",
66 "/home/opnfv/functest/data/")
68 logger.info("Using old image")
70 # Allow any port so that tacker commands reaches the server.
71 # CHECK IF THIS STILL MAKES SENSE WHEN TACKER IS INCLUDED IN OPNFV INSTALATION
73 controller_command = ("sshpass -p r00tme ssh root@10.20.0.2"
74 " 'fuel node'|grep controller|awk '{print $10}'")
75 logger.info("Executing tacker script: '%s'" % controller_command)
76 process = subprocess.Popen(controller_command,
78 stdout=subprocess.PIPE)
79 ip = process.stdout.readline()
81 iptable_command1 = ("sshpass -p r00tme ssh root@10.20.0.2 ssh"
82 " " + ip + " iptables -P INPUT ACCEPT ")
83 iptable_command2 = ("sshpass -p r00tme ssh root@10.20.0.2 ssh"
84 " " + ip + "iptables -t nat -P INPUT ACCEPT ")
86 subprocess.call(iptable_command1, shell=True)
87 subprocess.call(iptable_command2, shell=True)
89 # Create glance image and the neutron network
91 image_id = os_utils.create_glance_image(glance_client,
98 network_dic = os_utils.create_network_full(neutron_client,
105 "There has been a problem when creating the neutron network")
108 network_id = network_dic["net_id"]
110 sg_id = os_utils.create_security_group_full(neutron_client,
111 SECGROUP_NAME, SECGROUP_DESCR)
114 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
116 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
117 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
118 instance = os_utils.create_instance_and_wait_for_active(FLAVOR,
124 logger.error("Error while booting instance.")
126 # Retrieve IP of INSTANCE
127 instance_ip = instance.networks.get(NET_NAME)[0]
128 logger.debug("Instance '%s' got private ip '%s'." %
129 (INSTANCE_NAME, instance_ip))
131 logger.info("Adding '%s' to security group '%s'..."
132 % (INSTANCE_NAME, SECGROUP_NAME))
133 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
135 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
136 floatip_dic = os_utils.create_floating_ip(neutron_client)
137 floatip_client = floatip_dic['fip_addr']
138 # floatip_id = floatip_dic['fip_id']
140 if floatip_client is None:
141 logger.error("Cannot create floating IP.")
143 logger.info("Floating IP created: '%s'" % floatip_client)
145 logger.info("Associating floating ip: '%s' to VM '%s' "
146 % (floatip_client, INSTANCE_NAME))
147 if not os_utils.add_floating_ip(nova_client, instance.id, floatip_client):
148 logger.error("Cannot associate floating IP to VM.")
151 # STARTING SECOND VM (server) ###
154 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
156 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
157 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
158 instance_2 = os_utils.create_instance_and_wait_for_active(FLAVOR,
163 if instance_2 is None:
164 logger.error("Error while booting instance.")
166 # Retrieve IP of INSTANCE
167 instance_ip_2 = instance_2.networks.get(NET_NAME)[0]
168 logger.debug("Instance '%s' got private ip '%s'." %
169 (INSTANCE_NAME_2, instance_ip_2))
171 logger.info("Adding '%s' to security group '%s'..."
172 % (INSTANCE_NAME_2, SECGROUP_NAME))
173 os_utils.add_secgroup_to_instance(nova_client, instance_2.id, sg_id)
175 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME_2)
176 floatip_dic = os_utils.create_floating_ip(neutron_client)
177 floatip_server = floatip_dic['fip_addr']
178 # floatip_id = floatip_dic['fip_id']
180 if floatip_server is None:
181 logger.error("Cannot create floating IP.")
183 logger.info("Floating IP created: '%s'" % floatip_server)
185 logger.info("Associating floating ip: '%s' to VM '%s' "
186 % (floatip_server, INSTANCE_NAME_2))
188 if not os_utils.add_floating_ip(nova_client,
191 logger.error("Cannot associate floating IP to VM.")
194 # CREATION OF THE 2 SF ####
196 tacker_script = "/home/opnfv/repos/functest/testcases/features/sfc/" + \
198 logger.info("Executing tacker script: '%s'" % tacker_script)
199 subprocess.call(tacker_script, shell=True)
201 # SSH CALL TO START HTTP SERVER
202 ssh = paramiko.SSHClient()
203 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
206 ssh.connect(floatip_server, username="root",
207 password="opnfv", timeout=2)
208 command = "python -m SimpleHTTPServer 80 > /dev/null 2>&1 &"
209 logger.info("Starting HTTP server")
210 (stdin, stdout, stderr) = ssh.exec_command(command)
212 logger.debug("Waiting for %s..." % floatip_server)
216 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
219 for instance in instances:
220 if "server" not in instance.name:
221 if "client" not in instance.name:
223 "This is the instance name: %s " % instance.name)
224 floatip_dic = os_utils.create_floating_ip(neutron_client)
225 floatip = floatip_dic['fip_addr']
227 instance.add_floating_ip(floatip)
229 logger.debug("Problems assigning floating IP to SFs")
231 logger.debug("Floating IPs for SFs: %s..." % ips)
232 # SSH TO START THE VXLAN_TOOL ON SF1
233 logger.info("Configuring the SFs")
235 ssh.connect(ips[0], username="root",
236 password="opnfv", timeout=2)
237 command = ("nohup python vxlan_tool.py -i eth0 "
238 "-d forward -v off -f -b 80 &")
239 (stdin, stdout, stderr) = ssh.exec_command(command)
241 logger.debug("Waiting for %s..." % ips[0])
245 # SSH TO START THE VXLAN_TOOL ON SF2
247 ssh.connect(ips[1], username="root",
248 password="opnfv", timeout=2)
249 command = ("nohup python vxlan_tool.py -i eth0 "
250 "-d forward -v off -f -b 22 &")
251 (stdin, stdout, stderr) = ssh.exec_command(command)
253 logger.debug("Waiting for %s..." % ips[1])
257 # SSH TO EXECUTE cmd_client
259 logger.info("TEST STARTED")
261 ssh.connect(floatip_client, username="root",
262 password="opnfv", timeout=2)
263 command = "nc -w 5 -zv " + floatip_server + " 22 2>&1"
264 (stdin, stdout, stderr) = ssh.exec_command(command)
266 logger.debug("Waiting for %s..." % floatip_client)
270 # WRITE THE CORRECT WAY TO DO LOGGING
272 logger.info("First output: %s" % stdout.readlines())
273 if "timed out" in stdout.readlines()[0]:
274 logger.info('\033[92m' + "TEST 1 [PASSED] "
275 "==> SSH BLOCKED" + '\033[0m')
278 logger.debug('\033[91m' + "TEST 1 [FAILED] "
279 "==> SSH NOT BLOCKED" + '\033[0m')
282 # SSH TO EXECUTE cmd_client
285 ssh.connect(floatip_client, username="root",
286 password="opnfv", timeout=2)
287 command = "nc -w 5 -zv " + floatip_server + " 80 2>&1"
288 (stdin, stdout, stderr) = ssh.exec_command(command)
290 logger.debug("Waiting for %s..." % floatip_client)
294 if "succeeded" in stdout.readlines()[0]:
295 logger.info('\033[92m' + "TEST 2 [PASSED] "
296 "==> HTTP WORKS" + '\033[0m')
299 logger.debug('\033[91m' + "TEST 2 [FAILED] "
300 "==> HTTP BLOCKED" + '\033[0m')
303 # CHANGE OF CLASSIFICATION #
304 logger.info("Changing the classification")
305 tacker_classi = "/home/opnfv/repos/functest/testcases/features/sfc/" + \
307 subprocess.call(tacker_classi, shell=True)
309 # SSH TO EXECUTE cmd_client
312 ssh.connect(floatip_client, username="root",
313 password="opnfv", timeout=2)
314 command = "nc -w 5 -zv " + floatip_server + " 80 2>&1"
315 (stdin, stdout, stderr) = ssh.exec_command(command)
317 logger.debug("Waiting for %s..." % floatip_client)
321 if "timed out" in stdout.readlines()[0]:
322 logger.info('\033[92m' + "TEST 3 [WORKS] "
323 "==> HTTP BLOCKED" + '\033[0m')
326 logger.debug('\033[91m' + "TEST 3 [FAILED] "
327 "==> HTTP NOT BLOCKED" + '\033[0m')
330 # SSH TO EXECUTE cmd_client
333 ssh.connect(floatip_client, username="root",
334 password="opnfv", timeout=2)
335 command = "nc -w 5 -zv " + floatip_server + " 22 2>&1"
336 (stdin, stdout, stderr) = ssh.exec_command(command)
338 logger.debug("Waiting for %s..." % floatip_client)
342 if "succeeded" in stdout.readlines()[0]:
343 logger.info('\033[92m' + "TEST 4 [WORKS] "
344 "==> SSH WORKS" + '\033[0m')
347 logger.debug('\033[91m' + "TEST 4 [FAILED] "
348 "==> SSH BLOCKED" + '\033[0m')
352 for x in range(0, 5):
353 logger.info('\033[92m' + "SFC TEST WORKED"
354 " :) \n" + '\033[0m')
358 if __name__ == '__main__':