f33d247f525f7a6e17a65d5b0c374afb82b4e262
[sdnvpn.git] / sdnvpn / test / functest / testcase_1bis.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2018 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 import logging
12 import sys
13 import pkg_resources
14
15 from random import randint
16 from sdnvpn.lib import config as sdnvpn_config
17 from sdnvpn.lib import openstack_utils as os_utils
18 from sdnvpn.lib import utils as test_utils
19 from sdnvpn.lib.results import Results
20
21 logger = logging.getLogger(__name__)
22
23 COMMON_CONFIG = sdnvpn_config.CommonConfig()
24 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
25     'sdnvpn.test.functest.testcase_1bis')
26
27
28 def main():
29     conn = os_utils.get_os_connection()
30     results = Results(COMMON_CONFIG.line_length, conn)
31
32     results.add_to_summary(0, "=")
33     results.add_to_summary(2, "STATUS", "SUBTEST")
34     results.add_to_summary(0, "=")
35
36     heat_client = os_utils.get_heat_client()
37     # neutron client is needed as long as bgpvpn heat module
38     # is not yet installed by default in apex (APEX-618)
39     neutron_client = os_utils.get_neutron_client()
40
41     image_ids = []
42     bgpvpn_ids = []
43
44     try:
45         # image created outside HOT (OS::Glance::Image deprecated since ocata)
46         image_id = os_utils.create_glance_image(
47             conn, TESTCASE_CONFIG.image_name,
48             COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
49             container="bare", public='public')
50         image_ids = [image_id]
51
52         compute_nodes = test_utils.assert_and_get_compute_nodes(conn)
53         az_1 = "nova:" + compute_nodes[0]
54         az_2 = "nova:" + compute_nodes[1]
55
56         file_path = pkg_resources.resource_filename(
57                     'sdnvpn', TESTCASE_CONFIG.hot_file_name)
58         templ = open(file_path, 'r').read()
59         logger.debug("Template is read: '%s'" % templ)
60         env = test_utils.get_heat_environment(TESTCASE_CONFIG, COMMON_CONFIG)
61         logger.debug("Environment is read: '%s'" % env)
62
63         kwargs = {
64             "stack_name": TESTCASE_CONFIG.stack_name,
65             "template": templ,
66             "environment": env,
67             "parameters": {
68                 "image_n": TESTCASE_CONFIG.image_name,
69                 "av_zone_1": az_1,
70                 "av_zone_2": az_2
71             }
72         }
73         stack_id = os_utils.create_stack(heat_client, **kwargs)
74         if stack_id is None:
75             logger.error("Stack create start failed")
76             raise SystemError("Stack create start failed")
77
78         test_utils.wait_stack_for_status(heat_client,
79                                          stack_id, 'CREATE_COMPLETE')
80
81         net_1_output = os_utils.get_output(heat_client, stack_id, 'net_1_o')
82         network_1_id = net_1_output['output']['output_value']
83         net_2_output = os_utils.get_output(heat_client, stack_id, 'net_2_o')
84         network_2_id = net_2_output['output']['output_value']
85
86         vm_stack_output_keys = ['vm1_o', 'vm2_o', 'vm3_o', 'vm4_o', 'vm5_o']
87         vms = test_utils.get_vms_from_stack_outputs(heat_client,
88                                                     conn,
89                                                     stack_id,
90                                                     vm_stack_output_keys)
91
92         logger.debug("Entering base test case with stack '%s'" % stack_id)
93
94         msg = ("Create VPN with eRT<>iRT")
95         results.record_action(msg)
96         vpn_name = "sdnvpn-" + str(randint(100000, 999999))
97         kwargs = {
98             "import_targets": TESTCASE_CONFIG.targets1,
99             "export_targets": TESTCASE_CONFIG.targets2,
100             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
101             "name": vpn_name
102         }
103         bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
104         bgpvpn_id = bgpvpn['bgpvpn']['id']
105         logger.debug("VPN created details: %s" % bgpvpn)
106         bgpvpn_ids.append(bgpvpn_id)
107
108         msg = ("Associate network '%s' to the VPN." %
109                TESTCASE_CONFIG.heat_parameters['net_1_name'])
110         results.record_action(msg)
111         results.add_to_summary(0, "-")
112
113         test_utils.create_network_association(
114             neutron_client, bgpvpn_id, network_1_id)
115
116         # Remember: vms[X] is former vm_X+1
117
118         results.get_ping_status(vms[0], vms[1], expected="PASS", timeout=200)
119         results.get_ping_status(vms[0], vms[2], expected="PASS", timeout=30)
120         results.get_ping_status(vms[0], vms[3], expected="FAIL", timeout=30)
121
122         msg = ("Associate network '%s' to the VPN." %
123                TESTCASE_CONFIG.heat_parameters['net_2_name'])
124         results.add_to_summary(0, "-")
125         results.record_action(msg)
126         results.add_to_summary(0, "-")
127
128         test_utils.create_network_association(
129             neutron_client, bgpvpn_id, network_2_id)
130
131         test_utils.wait_for_bgp_net_assocs(neutron_client,
132                                            bgpvpn_id,
133                                            network_1_id,
134                                            network_2_id)
135
136         logger.info("Waiting for the VMs to connect to each other using the"
137                     " updated network configuration")
138         test_utils.wait_before_subtest()
139
140         results.get_ping_status(vms[3], vms[4], expected="PASS", timeout=30)
141         # TODO enable again when isolation in VPN with iRT != eRT works
142         # results.get_ping_status(vms[0], vms[3], expected="FAIL", timeout=30)
143         # results.get_ping_status(vms[0], vms[4], expected="FAIL", timeout=30)
144
145         msg = ("Update VPN with eRT=iRT ...")
146         results.add_to_summary(0, "-")
147         results.record_action(msg)
148         results.add_to_summary(0, "-")
149
150         # use bgpvpn-create instead of update till NETVIRT-1067 bug is fixed
151         # kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
152         #           "export_targets": TESTCASE_CONFIG.targets1,
153         #           "name": vpn_name}
154         # bgpvpn = test_utils.update_bgpvpn(neutron_client,
155         #                                   bgpvpn_id, **kwargs)
156
157         test_utils.delete_bgpvpn(neutron_client, bgpvpn_id)
158         bgpvpn_ids.remove(bgpvpn_id)
159         kwargs = {
160             "import_targets": TESTCASE_CONFIG.targets1,
161             "export_targets": TESTCASE_CONFIG.targets1,
162             "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
163             "name": vpn_name
164         }
165
166         test_utils.wait_before_subtest()
167
168         bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
169         bgpvpn_id = bgpvpn['bgpvpn']['id']
170         logger.debug("VPN re-created details: %s" % bgpvpn)
171         bgpvpn_ids.append(bgpvpn_id)
172
173         msg = ("Associate network '%s' to the VPN." %
174                TESTCASE_CONFIG.heat_parameters['net_1_name'])
175         results.record_action(msg)
176         results.add_to_summary(0, "-")
177
178         test_utils.create_network_association(
179             neutron_client, bgpvpn_id, network_1_id)
180
181         test_utils.create_network_association(
182             neutron_client, bgpvpn_id, network_2_id)
183
184         test_utils.wait_for_bgp_net_assocs(neutron_client,
185                                            bgpvpn_id,
186                                            network_1_id,
187                                            network_2_id)
188         # The above code has to be removed after re-enabling bgpvpn-update
189
190         logger.info("Waiting for the VMs to connect to each other using the"
191                     " updated network configuration")
192         test_utils.wait_before_subtest()
193
194         results.get_ping_status(vms[0], vms[3], expected="PASS", timeout=30)
195         results.get_ping_status(vms[0], vms[4], expected="PASS", timeout=30)
196
197     except Exception as e:
198         logger.error("exception occurred while executing testcase_1bis: %s", e)
199         raise
200     finally:
201         test_utils.cleanup_glance(conn, image_ids)
202         test_utils.cleanup_neutron(conn, neutron_client, [], bgpvpn_ids,
203                                    [], [], [], [])
204
205         try:
206             test_utils.delete_stack_and_wait(heat_client, stack_id)
207         except Exception as e:
208             logger.error(
209                    "exception occurred while executing testcase_1bis: %s", e)
210
211     return results.compile_summary()
212
213
214 if __name__ == '__main__':
215     sys.exit(main())