562d410b8adeec2d3cbfc1a9fcf7be301c138255
[functest.git] / testcases / features / sfc / sfc.py
1 import argparse
2 import os
3 import subprocess
4 import sys
5 import time
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
9 import paramiko
10
11
12 parser = argparse.ArgumentParser()
13
14 parser.add_argument("-r", "--report",
15                     help="Create json result file",
16                     action="store_true")
17
18 args = parser.parse_args()
19
20 """ logging configuration """
21 logger = ft_logger.Logger("ODL_SFC").getLogger()
22
23 REPO_PATH = os.environ['repos_dir'] + '/functest/'
24 HOME = os.environ['HOME'] + "/"
25
26 VM_BOOT_TIMEOUT = 180
27 INSTANCE_NAME = "client"
28 FLAVOR = "m1.small"
29 IMAGE_NAME = "sf_summit2016"
30 IMAGE_FILENAME = "sf_summit2016.qcow2"
31 IMAGE_FORMAT = "qcow2"
32 IMAGE_PATH = "/home/opnfv/functest/data" + "/" + IMAGE_FILENAME
33
34 # NEUTRON Private Network parameters
35
36 NET_NAME = "example-net"
37 SUBNET_NAME = "example-subnet"
38 SUBNET_CIDR = "11.0.0.0/24"
39 ROUTER_NAME = "example-router"
40
41 SECGROUP_NAME = "example-sg"
42 SECGROUP_DESCR = "Example Security group"
43
44 INSTANCE_NAME_2 = "server"
45
46 # TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
47
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"
52
53
54 def main():
55
56     nova_client = os_utils.get_nova_client()
57     neutron_client = os_utils.get_neutron_client()
58     glance_client = os_utils.get_glance_client()
59
60 # Download the image
61
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/")
67     else:
68         logger.info("Using old image")
69
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
72
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,
77                                shell=True,
78                                stdout=subprocess.PIPE)
79     ip = process.stdout.readline()
80
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 ")
85
86     subprocess.call(iptable_command1, shell=True)
87     subprocess.call(iptable_command2, shell=True)
88
89 # Create glance image and the neutron network
90
91     image_id = os_utils.create_glance_image(glance_client,
92                                             IMAGE_NAME,
93                                             IMAGE_PATH,
94                                             disk=IMAGE_FORMAT,
95                                             container="bare",
96                                             public=True)
97
98     network_dic = os_utils.create_network_full(neutron_client,
99                                                NET_NAME,
100                                                SUBNET_NAME,
101                                                ROUTER_NAME,
102                                                SUBNET_CIDR)
103     if not network_dic:
104         logger.error(
105             "There has been a problem when creating the neutron network")
106         sys.exit(-1)
107
108     network_id = network_dic["net_id"]
109
110     sg_id = os_utils.create_security_group_full(neutron_client,
111                                                 SECGROUP_NAME, SECGROUP_DESCR)
112
113     # boot INTANCE
114     logger.info("Creating instance '%s'..." % INSTANCE_NAME)
115     logger.debug(
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,
119                                                             image_id,
120                                                             network_id,
121                                                             INSTANCE_NAME)
122
123     if instance is None:
124         logger.error("Error while booting instance.")
125         sys.exit(-1)
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))
130
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)
134
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']
139
140     if floatip_client is None:
141         logger.error("Cannot create floating IP.")
142         sys.exit(-1)
143     logger.info("Floating IP created: '%s'" % floatip_client)
144
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.")
149         sys.exit(-1)
150
151 # STARTING SECOND VM (server) ###
152
153     # boot INTANCE
154     logger.info("Creating instance '%s'..." % INSTANCE_NAME)
155     logger.debug(
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,
159                                                               image_id,
160                                                               network_id,
161                                                               INSTANCE_NAME_2)
162
163     if instance_2 is None:
164         logger.error("Error while booting instance.")
165         sys.exit(-1)
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))
170
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)
174
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']
179
180     if floatip_server is None:
181         logger.error("Cannot create floating IP.")
182         sys.exit(-1)
183     logger.info("Floating IP created: '%s'" % floatip_server)
184
185     logger.info("Associating floating ip: '%s' to VM '%s' "
186                 % (floatip_server, INSTANCE_NAME_2))
187
188     if not os_utils.add_floating_ip(nova_client,
189                                     instance_2.id,
190                                     floatip_server):
191         logger.error("Cannot associate floating IP to VM.")
192         sys.exit(-1)
193
194     # CREATION OF THE 2 SF ####
195
196     tacker_script = "/home/opnfv/repos/functest/testcases/features/sfc/" + \
197         TACKER_SCRIPT
198     logger.info("Executing tacker script: '%s'" % tacker_script)
199     subprocess.call(tacker_script, shell=True)
200
201     # SSH CALL TO START HTTP SERVER
202     ssh = paramiko.SSHClient()
203     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
204
205     try:
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)
211     except:
212         logger.debug("Waiting for %s..." % floatip_server)
213         time.sleep(6)
214         # timeout -= 1
215
216     instances = nova_client.servers.list(search_opts={'all_tenants': 1})
217     ips = []
218     try:
219         for instance in instances:
220             if "server" not in instance.name:
221                 if "client" not in instance.name:
222                     logger.debug(
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']
226                     ips.append(floatip)
227                     instance.add_floating_ip(floatip)
228     except:
229         logger.debug("Problems assigning floating IP to SFs")
230
231     logger.debug("Floating IPs for SFs: %s..." % ips)
232     # SSH TO START THE VXLAN_TOOL ON SF1
233     logger.info("Configuring the SFs")
234     try:
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)
240     except:
241         logger.debug("Waiting for %s..." % ips[0])
242         time.sleep(6)
243         # timeout -= 1
244
245     # SSH TO START THE VXLAN_TOOL ON SF2
246     try:
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)
252     except:
253         logger.debug("Waiting for %s..." % ips[1])
254         time.sleep(6)
255         # timeout -= 1
256
257     # SSH TO EXECUTE cmd_client
258
259     logger.info("TEST STARTED")
260     try:
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)
265     except:
266         logger.debug("Waiting for %s..." % floatip_client)
267         time.sleep(6)
268         # timeout -= 1
269
270     # WRITE THE CORRECT WAY TO DO LOGGING
271     i = 0
272     if "timed out" in stdout.readlines()[0]:
273         logger.info('\033[92m' + "TEST 1 [PASSED] "
274                     "==> SSH BLOCKED" + '\033[0m')
275         i = i + 1
276     else:
277         logger.error('\033[91m' + "TEST 1 [FAILED] "
278                      "==> SSH NOT BLOCKED" + '\033[0m')
279         return
280
281     # SSH TO EXECUTE cmd_client
282
283     try:
284         ssh.connect(floatip_client, username="root",
285                     password="opnfv", timeout=2)
286         command = "nc -w 5 -zv " + floatip_server + " 80 2>&1"
287         (stdin, stdout, stderr) = ssh.exec_command(command)
288     except:
289         logger.debug("Waiting for %s..." % floatip_client)
290         time.sleep(6)
291         # timeout -= 1
292
293     if "succeeded" in stdout.readlines()[0]:
294         logger.info('\033[92m' + "TEST 2 [PASSED] "
295                     "==> HTTP WORKS" + '\033[0m')
296         i = i + 1
297     else:
298         logger.error('\033[91m' + "TEST 2 [FAILED] "
299                      "==> HTTP BLOCKED" + '\033[0m')
300         return
301
302     # CHANGE OF CLASSIFICATION #
303     logger.info("Changing the classification")
304     tacker_classi = "/home/opnfv/repos/functest/testcases/features/sfc/" + \
305         TACKER_CHANGECLASSI
306     subprocess.call(tacker_classi, shell=True)
307
308     # SSH TO EXECUTE cmd_client
309
310     try:
311         ssh.connect(floatip_client, username="root",
312                     password="opnfv", timeout=2)
313         command = "nc -w 5 -zv " + floatip_server + " 80 2>&1"
314         (stdin, stdout, stderr) = ssh.exec_command(command)
315     except:
316         logger.debug("Waiting for %s..." % floatip_client)
317         time.sleep(6)
318         # timeout -= 1
319
320     if "timed out" in stdout.readlines()[0]:
321         logger.info('\033[92m' + "TEST 3 [WORKS] "
322                     "==> HTTP BLOCKED" + '\033[0m')
323         i = i + 1
324     else:
325         logger.error('\033[91m' + "TEST 3 [FAILED] "
326                      "==> HTTP NOT BLOCKED" + '\033[0m')
327         return
328
329     # SSH TO EXECUTE cmd_client
330
331     try:
332         ssh.connect(floatip_client, username="root",
333                     password="opnfv", timeout=2)
334         command = "nc -w 5 -zv " + floatip_server + " 22 2>&1"
335         (stdin, stdout, stderr) = ssh.exec_command(command)
336     except:
337         logger.debug("Waiting for %s..." % floatip_client)
338         time.sleep(6)
339         # timeout -= 1
340
341     if "succeeded" in stdout.readlines()[0]:
342         logger.info('\033[92m' + "TEST 4 [WORKS] "
343                     "==> SSH WORKS" + '\033[0m')
344         i = i + 1
345     else:
346         logger.error('\033[91m' + "TEST 4 [FAILED] "
347                      "==> SSH BLOCKED" + '\033[0m')
348         return
349
350     if i == 4:
351         for x in range(0, 5):
352             logger.info('\033[92m' + "SFC TEST WORKED"
353                         " :) \n" + '\033[0m')
354
355     sys.exit(0)
356
357 if __name__ == '__main__':
358     main()