update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / ra / pytest / ns / ha / test_ha_operations.py
1 #!/usr/bin/env python3
2 """
3 #
4 # Copyright 2017 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 """
19
20 import gi
21 import pytest
22 import random
23 import time
24
25 import rift.auto.mano as mano
26 import rift.auto.descriptor
27 from gi.repository.RwKeyspec import quoted_key
28
29 from gi.repository import (
30 RwProjectNsdYang,
31 RwNsrYang,
32 RwVnfrYang,
33 RwVlrYang,
34 RwProjectVnfdYang,
35 RwCloudYang
36 )
37
38
39 @pytest.mark.setup('active_configuration')
40 @pytest.mark.incremental
41 class TestActiveLpConfiguration(object):
42 """Setting up the configuration."""
43
44 def collect_active_lp_data(
45 self, active_lp_node_obj, active_confd_host,
46 standby_confd_host, logger):
47 """Collect active lp data."""
48 mano.verify_hagr_endpoints(active_confd_host, standby_confd_host)
49 active_lp_node_obj.collect_data()
50
51 def wait_for_standby_to_comeup(
52 self, standby_mgmt_session, active_confd_host, standby_confd_host):
53 """Wait for the standby to come up.
54
55 Wait for endpoint 'ha/geographic/active' to return 200
56 """
57 mano.wait_for_standby_to_become_active(standby_mgmt_session)
58 # mano.verify_hagr_endpoints(
59 # active_host=standby_confd_host, standby_host=active_confd_host)
60
61 def collect_standby_lp_data(
62 self, standby_lp_node_obj, standby_mgmt_session, cloud_account,
63 fmt_cloud_xpath, projects, fmt_nsd_catalog_xpath):
64 """Collect standby lp data."""
65 time.sleep(180)
66 rw_new_active_cloud_pxy = standby_mgmt_session.proxy(RwCloudYang)
67 nsd_pxy = standby_mgmt_session.proxy(RwProjectNsdYang)
68 rwnsr_proxy = standby_mgmt_session.proxy(RwNsrYang)
69
70 for project_name in projects:
71 rw_new_active_cloud_pxy.wait_for(
72 fmt_cloud_xpath.format(
73 project=quoted_key(project_name),
74 account_name=quoted_key(cloud_account.name)) +
75 '/connection-status/status', 'success',
76 timeout=60, fail_on=['failure'])
77
78 # nsd_catalog = nsd_pxy.get_config(
79 # fmt_nsd_catalog_xpath.format(project=quoted_key(project_name)))
80 # assert nsd_catalog
81
82 if pytest.config.getoption("--nsr-test"):
83 nsr_opdata = rwnsr_proxy.get(
84 '/rw-project:project[rw-project:name={project}]' +
85 '/ns-instance-opdata'.format(
86 project=quoted_key(project_name))
87 )
88
89 assert nsr_opdata
90 nsrs = nsr_opdata.nsr
91
92 for nsr in nsrs:
93 xpath = (
94 '/rw-project:project[rw-project:name={project}]' +
95 '/ns-instance-opdata/nsr[ns-instance-config-ref=' +
96 '{config_ref}]/config-status'.format(
97 project=quoted_key(project_name),
98 config_ref=quoted_key(nsr.ns_instance_config_ref))
99 )
100
101 rwnsr_proxy.wait_for(
102 xpath, "configured", fail_on=['failed'], timeout=400)
103
104 standby_lp_node_obj.collect_data()
105
106 def attempt_indirect_failover(
107 self, revertive_pref_host, active_confd_host, standby_confd_host,
108 active_site_name, standby_site_name, logger):
109 """Try indirect failover."""
110 time.sleep(5)
111 logger.debug(
112 'Attempting first failover. Host {} will be new active'.format(
113 standby_confd_host))
114
115 mano.indirect_failover(
116 revertive_pref_host, new_active_ip=standby_confd_host,
117 new_active_site=standby_site_name,
118 new_standby_ip=active_confd_host,
119 new_standby_site=active_site_name)
120
121 def match_active_standby(self, active_lp_node_obj, standby_lp_node_obj):
122 """Compare active standby."""
123 active_lp_node_obj.compare(standby_lp_node_obj)
124
125 def test_create_project_users_cloud_acc(
126 self, rbac_user_passwd, user_domain, rw_active_user_proxy, logger,
127 rw_active_project_proxy, rw_active_rbac_int_proxy, cloud_account,
128 rw_active_conman_proxy, rw_active_cloud_pxy, user_roles,
129 fmt_prefixed_cloud_xpath, fmt_cloud_xpath, descriptors,
130 active_mgmt_session, fmt_nsd_catalog_xpath, active_lp_node_obj,
131 standby_lp_node_obj, active_confd_host, standby_confd_host,
132 revertive_pref_host, active_site_name, standby_site_name,
133 standby_mgmt_session):
134 """Create 3 of users, projects, cloud accounts, decriptors & nsrs."""
135 def failover_and_match():
136 """Try an indirect failover.
137
138 Match active and standby data
139 """
140 self.collect_active_lp_data(
141 active_lp_node_obj, active_confd_host,
142 standby_confd_host, logger)
143 self.attempt_indirect_failover(
144 revertive_pref_host, active_confd_host, standby_confd_host,
145 active_site_name, standby_site_name, logger)
146 self.wait_for_standby_to_comeup(
147 standby_mgmt_session, active_confd_host, standby_confd_host)
148 self.collect_standby_lp_data(
149 standby_lp_node_obj, standby_mgmt_session, cloud_account,
150 fmt_cloud_xpath, projects, fmt_nsd_catalog_xpath)
151 self.match_active_standby(active_lp_node_obj, standby_lp_node_obj)
152
153 def delete_data_set(idx):
154
155 rift.auto.descriptor.terminate_nsr(
156 rwvnfr_pxy, rwnsr_pxy, rwvlr_pxy, logger,
157 project=projects[idx])
158
159 rift.auto.descriptor.delete_descriptors(
160 active_mgmt_session, project_name)
161
162 rw_active_cloud_pxy.delete_config(
163 fmt_prefixed_cloud_xpath.format(
164 project=quoted_key(projects[idx]),
165 account_name=quoted_key(cloud_account.name)
166 )
167 )
168 response = rw_active_cloud_pxy.get(
169 fmt_cloud_xpath.format(
170 project=quoted_key(projects[idx]),
171 account_name=quoted_key(cloud_account.name)
172 )
173 )
174 assert response is None
175
176 mano.delete_project(rw_active_conman_proxy, projects[idx])
177 projects.pop()
178 mano.delete_user(rw_active_user_proxy, users[idx], user_domain)
179 users.pop()
180
181 # Create test users
182 user_name_pfx = 'user_ha_'
183 users = []
184 for idx in range(1, 4):
185 users.append(user_name_pfx + str(idx))
186
187 mano.create_user(
188 rw_active_user_proxy, user_name_pfx + str(idx),
189 rbac_user_passwd, user_domain)
190
191 # Create projects and assign roles to users
192 prj_name_pfx = 'prj_ha_'
193 projects = []
194 for idx in range(1, 4):
195 project_name = prj_name_pfx + str(idx)
196 projects.append(project_name)
197 mano.create_project(
198 rw_active_conman_proxy, project_name)
199
200 for idx in range(0, 3):
201 project_name = projects[idx]
202 role = random.choice(user_roles)
203 user = users[idx]
204 logger.debug(
205 'Assinging role {} to user {} in project {}'.format(
206 role, user, project_name))
207
208 mano.assign_project_role_to_user(
209 rw_active_project_proxy, role, user, project_name,
210 user_domain, rw_active_rbac_int_proxy)
211
212 logger.debug(
213 'Creating cloud account {} for project {}'.format(
214 cloud_account.name, project_name))
215
216 xpath = fmt_prefixed_cloud_xpath.format(
217 project=quoted_key(project_name),
218 account_name=quoted_key(cloud_account.name))
219
220 rw_active_cloud_pxy.replace_config(xpath, cloud_account)
221
222 xpath_no_pfx = fmt_cloud_xpath.format(
223 project=quoted_key(project_name),
224 account_name=quoted_key(cloud_account.name))
225
226 response = rw_active_cloud_pxy.get(xpath_no_pfx)
227 assert response.name == cloud_account.name
228 assert response.account_type == cloud_account.account_type
229
230 rw_active_cloud_pxy.wait_for(
231 fmt_cloud_xpath.format(
232 project=quoted_key(project_name),
233 account_name=quoted_key(cloud_account.name)) +
234 '/connection-status/status', 'success', timeout=30,
235 fail_on=['failure'])
236
237 # Uploads the descriptors
238 for descriptor in descriptors:
239 rift.auto.descriptor.onboard(
240 active_mgmt_session, descriptor, project=project_name)
241
242 # Verify whether the descriptors uploaded successfully
243 logger.debug(
244 'Onboarding descriptors for project {}'.format(project_name))
245
246 nsd_pxy = active_mgmt_session.proxy(RwProjectNsdYang)
247 rwnsr_pxy = active_mgmt_session.proxy(RwNsrYang)
248 rwvnfr_pxy = active_mgmt_session.proxy(RwVnfrYang)
249 rwvlr_pxy = active_mgmt_session.proxy(RwVlrYang)
250
251 nsd_xpath = fmt_nsd_catalog_xpath.format(
252 project=quoted_key(project_name))
253 nsd_catalog = nsd_pxy.get_config(nsd_xpath)
254 assert nsd_catalog
255
256 nsd_xpath = fmt_nsd_catalog_xpath.format(
257 project=quoted_key(project_name))
258 nsd_catalog = nsd_pxy.get_config(nsd_xpath)
259 assert nsd_catalog
260 nsd = nsd_catalog.nsd[0]
261 nsr = rift.auto.descriptor.create_nsr(
262 cloud_account.name, nsd.name, nsd)
263
264 logger.debug(
265 'Instantiating NS for project {}'.format(project_name))
266 rift.auto.descriptor.instantiate_nsr(
267 nsr, rwnsr_pxy, logger, project=project_name)
268
269 delete_data_set(2)
270 failover_and_match()
271 delete_data_set(1)
272 failover_and_match()
273
274