3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from crl.remotesession.remotesession import RemoteSession
18 from crl.interactivesessions.remoterunner import RunResult
19 from .usermanager import (
22 from .envcreator import EnvCreator
26 'OS_ENDPOINT_TYPE': 'internalURL',
27 'OS_USERNAME': 'admin',
28 'OS_PASSWORD': 'password',
29 'OS_TENANT_NAME': 'admin',
30 'OS_PROJECT_NAME': 'admin',
31 'OS_AUTH_URL': '192.168.1.2:5000/v3'}
33 UM_ADMIN_ENV_DICT = UPDATE_ENV_DICT.copy()
34 UM_ADMIN_ENV_DICT.update({'OS_USERNAME': 'um_admin_username',
35 'OS_PASSWORD': 'um_admin_password',
36 'OS_TENANT_NAME': 'infrastructure',
37 'OS_PROJECT_NAME': 'infrastructure'})
39 ROLES = ['role1', 'role2']
41 USERRECORD = UserRecord(uuid='uuid',
49 THISDIR = os.path.dirname(__file__)
50 USER_CONFIG_PATH = os.path.join(THISDIR, 'testutils', 'user_config.yaml')
53 class EnvCreatorVerifier(object):
55 self._mock_remotesession_factory = mock.create_autospec(RemoteSession)
56 self._mock_usermanager = mock.create_autospec(UserManager)
57 self._envcreator = EnvCreator(
58 remotesession_factory=self._mock_remotesession_factory,
59 usermanager=self._mock_usermanager)
64 return ', '.join(ROLES)
66 def _setup_mocks(self):
67 self._setup_mock_remotesession()
68 self._setup_mock_usermanager()
70 def _setup_mock_remotesession(self):
71 self._get_source_update_env_dict.return_value = UPDATE_ENV_DICT
72 self._execute_command_in_target.side_effect = self._execute_side_effect
75 def _get_source_update_env_dict(self):
76 return self._mock_remotesession_factory.return_value.get_source_update_env_dict
79 def _execute_command_in_target(self):
80 return self._mock_remotesession_factory.return_value.execute_command_in_target
83 def _execute_side_effect(command):
84 assert command == 'cat /etc/userconfig/user_config.yaml'
85 with open(USER_CONFIG_PATH) as f:
86 return RunResult(status=0, stdout=f.read(), stderr='')
88 def _setup_mock_usermanager(self):
89 def mock_create_user_with_roles(*roles):
90 roles_list = list(roles)
91 assert list(roles_list) == ROLES, (
92 'Expected {expected!r}, actual {actual!r}'.format(
97 self._mock_usermanager.create_user_with_roles.side_effect = mock_create_user_with_roles
99 def verify_create(self):
100 self._assert_target_dict(self._envcreator.create(target=TARGET,
101 envname=self._envname))
102 self._verify_mock_calls()
104 def verify_multiple_creates(self):
106 self._assert_target_dict(self._envcreator.create(target='target2',
107 envname=self._envname))
108 self._get_source_update_env_dict.assert_called_once_with(
109 self._openrc, target=TARGET)
111 def verify_create_admin(self):
112 assert self._envcreator.create(target=TARGET, envname='admin') == UPDATE_ENV_DICT
113 self._verify_mock_calls()
115 def verify_create_um_admin(self):
116 assert self._envcreator.create(target=TARGET, envname='um_admin') == UM_ADMIN_ENV_DICT
118 def _assert_target_dict(self, target_dict):
119 assert target_dict == self._expected_target_dict, (
120 'Expcted: {expected}, actual: {actual}'.format(
121 expected=self._expected_target_dict,
125 def _expected_target_dict(self):
126 d = UPDATE_ENV_DICT.copy()
127 d.update({'OS_USERNAME': USERRECORD.username,
128 'OS_PASSWORD': USERRECORD.password,
129 'OS_TENANT_NAME': 'infrastructure',
130 'OS_PROJECT_NAME': 'infrastructure'})
133 def _verify_mock_calls(self):
134 self._get_source_update_env_dict.assert_called_once_with(
135 self._openrc, target=TARGET)