Add cloudtaf framework
[ta/cloudtaf.git] / libraries / cluster / envcreatorverifier.py
1 # Copyright 2019 Nokia
2 #
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import os
16 import mock
17 from crl.remotesession.remotesession import RemoteSession
18 from crl.interactivesessions.remoterunner import RunResult
19 from .usermanager import (
20     UserManager,
21     UserRecord)
22 from .envcreator import EnvCreator
23
24
25 UPDATE_ENV_DICT = {
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'}
32
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'})
38
39 ROLES = ['role1', 'role2']
40
41 USERRECORD = UserRecord(uuid='uuid',
42                         username='username',
43                         password='password',
44                         roles=ROLES)
45
46 TARGET = 'target'
47
48
49 THISDIR = os.path.dirname(__file__)
50 USER_CONFIG_PATH = os.path.join(THISDIR, 'testutils', 'user_config.yaml')
51
52
53 class EnvCreatorVerifier(object):
54     def __init__(self):
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)
60         self._setup_mocks()
61
62     @property
63     def _envname(self):
64         return ', '.join(ROLES)
65
66     def _setup_mocks(self):
67         self._setup_mock_remotesession()
68         self._setup_mock_usermanager()
69
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
73
74     @property
75     def _get_source_update_env_dict(self):
76         return self._mock_remotesession_factory.return_value.get_source_update_env_dict
77
78     @property
79     def _execute_command_in_target(self):
80         return self._mock_remotesession_factory.return_value.execute_command_in_target
81
82     @staticmethod
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='')
87
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(
93                     expected=ROLES,
94                     actual=roles_list))
95             return USERRECORD
96
97         self._mock_usermanager.create_user_with_roles.side_effect = mock_create_user_with_roles
98
99     def verify_create(self):
100         self._assert_target_dict(self._envcreator.create(target=TARGET,
101                                                          envname=self._envname))
102         self._verify_mock_calls()
103
104     def verify_multiple_creates(self):
105         self.verify_create()
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)
110
111     def verify_create_admin(self):
112         assert self._envcreator.create(target=TARGET, envname='admin') == UPDATE_ENV_DICT
113         self._verify_mock_calls()
114
115     def verify_create_um_admin(self):
116         assert self._envcreator.create(target=TARGET, envname='um_admin') == UM_ADMIN_ENV_DICT
117
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,
122                 actual=target_dict))
123
124     @property
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'})
131         return d
132
133     def _verify_mock_calls(self):
134         self._get_source_update_env_dict.assert_called_once_with(
135             self._openrc, target=TARGET)
136
137     @property
138     def _openrc(self):
139         return 'openrc'