973f447d08979e76801d3f50e0eae446073e51c3
[osm/SO.git] / rwlaunchpad / ra / pytest / ns / ha / conftest.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 pytest
21 import subprocess
22 import os
23 import time
24
25 import rift.vcs.vcs
26 import rift.auto.mano as mano
27
28 from gi.repository import (
29 RwConmanYang,
30 RwUserYang,
31 RwProjectYang,
32 RwRbacInternalYang,
33 RwRbacPlatformYang,
34 RwCloudYang,
35 )
36
37 @pytest.fixture(scope='session')
38 def ha_mgmt_sessions(sut_host_addrs, session_type):
39 """Fixture that returns mgmt sessions for active, standby LPs"""
40 sessions = {}
41 for name,addr in sut_host_addrs.items():
42 if session_type == 'netconf':
43 mgmt_session = rift.auto.session.NetconfSession(host=addr)
44 elif session_type == 'restconf':
45 mgmt_session = rift.auto.session.RestconfSession(host=addr)
46
47 if 'standby' in name:
48 sessions['standby'] = mgmt_session
49 elif 'active' in name:
50 sessions['active'] = mgmt_session
51 mgmt_session.connect()
52 rift.vcs.vcs.wait_until_system_started(mgmt_session)
53
54 return sessions
55
56 @pytest.fixture(scope='session')
57 def active_mgmt_session(ha_mgmt_sessions):
58 """Fixture that returns mgmt sessions for active LP"""
59 return ha_mgmt_sessions['active']
60
61 @pytest.fixture(scope='session')
62 def standby_mgmt_session(ha_mgmt_sessions):
63 """Fixture that returns mgmt sessions for standby LP"""
64 return ha_mgmt_sessions['standby']
65
66 @pytest.fixture(scope='session')
67 def active_confd_host(active_mgmt_session):
68 """Fixture that returns mgmt sessions for active LP"""
69 return active_mgmt_session.host
70
71 @pytest.fixture(scope='session')
72 def standby_confd_host(standby_mgmt_session):
73 """Fixture that returns mgmt sessions for standby LP"""
74 return standby_mgmt_session.host
75
76 @pytest.fixture(scope='session')
77 def revertive_pref_host(active_mgmt_session):
78 """Fixture that returns mgmt sessions for active LP"""
79 return active_mgmt_session.host
80
81 @pytest.fixture(scope='session')
82 def active_site_name(active_mgmt_session):
83 """Fixture that returns mgmt sessions for active LP"""
84 return 'site-a'
85
86 @pytest.fixture(scope='session')
87 def standby_site_name(standby_mgmt_session):
88 """Fixture that returns mgmt sessions for standby LP"""
89 return 'site-b'
90
91 @pytest.fixture(scope='session', autouse=True)
92 def redundancy_config_setup(logger, active_confd_host, standby_confd_host, active_mgmt_session):
93 """Fixture that prepares the rw-redundancy-config.xml file and copies it to RVR of active, standby systems;
94 starts the mock dns script in the revertive-preference host.
95 It assumes system-tests are running containers where launchpad runs in production mode"""
96
97 # Starts the mock dns script in revertive-preference host which is the active system.
98 ssh_mock_dns_cmd = 'ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no {revertive_pref_host} -- "python3 /usr/rift/usr/rift/systemtest/util/test_mock_dns.py --active-site site-a {active_host} --standby-site site-b {standby_host}"'.format(
99 revertive_pref_host=active_confd_host, active_host=active_confd_host, standby_host=standby_confd_host)
100 logger.debug('Running mock dns script in host {host}; cmd: {ssh_cmd}'.format(host=active_confd_host,
101 ssh_cmd=ssh_mock_dns_cmd))
102 subprocess.Popen(ssh_mock_dns_cmd, shell=True)
103 # Have to check if the script ran fine
104
105 # Prepares the rw-redundancy-config.xml file
106 redundancy_cfg_file_path = os.path.join(os.getenv('RIFT_INSTALL'),
107 'usr/rift/systemtest/config/rw-redundancy-config.xml')
108 with open(redundancy_cfg_file_path) as f:
109 file_content = f.read()
110
111 with open(redundancy_cfg_file_path+'.auto', 'w') as f:
112 new_content = file_content.replace('1.1.1.1', active_confd_host).replace('2.2.2.2', standby_confd_host)
113 logger.debug('redundancy config file content: {}'.format(new_content))
114 f.write(new_content)
115
116 # Copies the redundancy config file to active, standby systems
117 for host_addr in (active_confd_host, standby_confd_host):
118 scp_cmd = 'scp -o StrictHostkeyChecking=no {file_path} {host}:/usr/rift/var/rift/rw-redundancy-config.xml'.format(
119 file_path=redundancy_cfg_file_path+'.auto', host=host_addr)
120 logger.debug(
121 'Copying redundancy config xml to host {host}; scp cmd: {scp_cmd}'.format(host=host_addr, scp_cmd=scp_cmd))
122 assert os.system(scp_cmd) == 0
123
124 # Restart the launchpad service in active, standby systems
125 for host_addr in (active_confd_host, standby_confd_host):
126 ssh_launchpad_restart_cmd = 'ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no {host} -- "sudo pkill rwmain"'.format(
127 host=host_addr)
128 logger.debug('Restarting launchpad service in host {host}. cmd: {ssh_cmd}'.format(host=host_addr,
129 ssh_cmd=ssh_launchpad_restart_cmd))
130 assert os.system(ssh_launchpad_restart_cmd.format(host=host_addr)) == 0
131 time.sleep(30)
132
133 active_mgmt_session.connect()
134 rift.vcs.vcs.wait_until_system_started(active_mgmt_session)
135 mano.verify_ha_redundancy_state(active_mgmt_session)
136
137 @pytest.fixture(scope='session')
138 def ha_lp_nodes(sut_host_addrs, session_type):
139 """Fixture that returns rift.auto.mano.LpNode objects for active, standby LPs"""
140 lp_nodes = {}
141 for name,addr in sut_host_addrs.items():
142 lp_node = mano.LpNode(host=addr, session_type=session_type, connect=False)
143 if 'standby' in name:
144 lp_nodes['standby'] = lp_node
145 elif 'active' in name:
146 lp_nodes['active'] = lp_node
147
148 return lp_nodes
149
150 @pytest.fixture(scope='session')
151 def active_lp_node_obj(ha_lp_nodes):
152 """Fixture that returns rift.auto.mano.LpNode object for active LP"""
153 return ha_lp_nodes['active']
154
155 @pytest.fixture(scope='session')
156 def standby_lp_node_obj(ha_lp_nodes):
157 """Fixture that returns rift.auto.mano.LpNode object for standby LP"""
158 return ha_lp_nodes['standby']
159
160 @pytest.fixture(scope='session')
161 def rw_active_user_proxy(active_mgmt_session):
162 return active_mgmt_session.proxy(RwUserYang)
163
164 @pytest.fixture(scope='session')
165 def rw_active_project_proxy(active_mgmt_session):
166 return active_mgmt_session.proxy(RwProjectYang)
167
168 @pytest.fixture(scope='session')
169 def rw_active_rbac_int_proxy(active_mgmt_session):
170 return active_mgmt_session.proxy(RwRbacInternalYang)
171
172 @pytest.fixture(scope='session')
173 def rw_active_conman_proxy(active_mgmt_session):
174 return active_mgmt_session.proxy(RwConmanYang)
175
176 @pytest.fixture(scope='session')
177 def rbac_active_platform_proxy(active_mgmt_session):
178 return active_mgmt_session.proxy(RwRbacPlatformYang)
179
180 @pytest.fixture(scope='session')
181 def rw_active_cloud_pxy(active_mgmt_session):
182 return active_mgmt_session.proxy(RwCloudYang)