| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2017 Red Hat, Inc. |
| 4 |
# All Rights Reserved. |
| 5 |
# |
| 6 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 |
# not use this file except in compliance with the License. You may obtain |
| 8 |
# 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, WITHOUT |
| 14 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 |
# License for the specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
import re |
| 19 |
from unittest import mock |
| 20 |
|
| 21 |
from fixtures import MonkeyPatch |
| 22 |
|
| 23 |
from config_tempest.clients import ClientManager |
| 24 |
from config_tempest import main as tool |
| 25 |
from config_tempest import tempest_conf |
| 26 |
from config_tempest.tests.base import BaseConfigTempestTest |
| 27 |
|
| 28 |
|
| 29 |
class TestOsClientConfigSupport(BaseConfigTempestTest): |
| 30 |
|
| 31 |
def setUp(self): |
| 32 |
super(TestOsClientConfigSupport, self).setUp() |
| 33 |
self.conf = self._get_conf("v2.0", "v3") |
| 34 |
|
| 35 |
def _check_credentials(self, manager, username, password, project_name): |
| 36 |
exp_user = manager.auth_provider.credentials._initial['username'] |
| 37 |
exp_pass = manager.auth_provider.credentials._initial['password'] |
| 38 |
creds = manager.auth_provider.credentials |
| 39 |
exp_project = creds._initial['project_name'] |
| 40 |
self.assertEqual(exp_user, username) |
| 41 |
self.assertEqual(exp_pass, password) |
| 42 |
self.assertEqual(exp_project, project_name) |
| 43 |
|
| 44 |
def _override_setup(self): |
| 45 |
mock_function = mock.Mock(return_value={"id": "my_fake_id"}) |
| 46 |
func2mock = ('config_tempest.clients.ProjectsClient.' |
| 47 |
'get_project_by_name') |
| 48 |
self.useFixture(MonkeyPatch(func2mock, mock_function)) |
| 49 |
|
| 50 |
def _obtain_client_config_data(self, non_admin=True, region_name=None): |
| 51 |
cloud_args = { |
| 52 |
'username': 'cloud_user', |
| 53 |
'password': 'cloud_pass', |
| 54 |
'project_name': 'cloud_project', |
| 55 |
'auth_url': 'http://auth.url.com/', |
| 56 |
'user_domain_name': 'cloud_domain', |
| 57 |
'project_domain_name': 'cloud_domain', |
| 58 |
} |
| 59 |
if region_name: |
| 60 |
cloud_args.update(region_name=region_name) |
| 61 |
# create an empty conf |
| 62 |
conf = tempest_conf.TempestConf() |
| 63 |
conf.set('identity', 'uri', cloud_args['auth_url'], priority=True) |
| 64 |
# call the function and check if data were obtained properly |
| 65 |
tool.set_cloud_config_values(non_admin, cloud_args, conf) |
| 66 |
if non_admin: |
| 67 |
self.assertEqual(cloud_args['username'], |
| 68 |
conf.get('identity', 'username')) |
| 69 |
self.assertEqual(cloud_args['password'], |
| 70 |
conf.get('identity', 'password')) |
| 71 |
self.assertEqual(cloud_args['project_name'], |
| 72 |
conf.get('identity', 'project_name')) |
| 73 |
else: |
| 74 |
self.assertEqual(cloud_args['username'], |
| 75 |
conf.get('auth', 'admin_username')) |
| 76 |
self.assertEqual(cloud_args['password'], |
| 77 |
conf.get('auth', 'admin_password')) |
| 78 |
self.assertEqual(cloud_args['project_name'], |
| 79 |
conf.get('auth', 'admin_project_name')) |
| 80 |
self.assertEqual(cloud_args['project_domain_name'], |
| 81 |
conf.get('auth', 'admin_project_domain_name')) |
| 82 |
self.assertEqual(cloud_args['user_domain_name'], |
| 83 |
conf.get('auth', 'admin_user_domain_name')) |
| 84 |
if region_name: |
| 85 |
self.assertEqual(cloud_args['region_name'], |
| 86 |
conf.get('identity', 'region')) |
| 87 |
|
| 88 |
def test_init_manager_client_config(self): |
| 89 |
self._obtain_client_config_data() |
| 90 |
|
| 91 |
def test_init_manager_client_config_as_admin(self): |
| 92 |
self._obtain_client_config_data(non_admin=False) |
| 93 |
|
| 94 |
def test_init_manager_client_config_region_name(self): |
| 95 |
self._obtain_client_config_data(region_name='regionOne') |
| 96 |
|
| 97 |
def test_init_manager_client_config_get_default(self): |
| 98 |
manager = ClientManager(self.conf, self._get_creds(self.conf)) |
| 99 |
# cloud_args is empty => check if default credentials were used |
| 100 |
self._check_credentials(manager, |
| 101 |
self.conf.get('identity', 'username'), |
| 102 |
self.conf.get('identity', 'password'), |
| 103 |
self.conf.get('identity', 'project_name')) |
| 104 |
|
| 105 |
def test_init_manager_client_config_override(self): |
| 106 |
self._override_setup() |
| 107 |
manager = ClientManager(self.conf, self._get_creds(self.conf)) |
| 108 |
# check if cloud_args credentials were overrided by the ones set in CLI |
| 109 |
self._check_credentials(manager, |
| 110 |
self.conf.get('identity', 'username'), |
| 111 |
self.conf.get('identity', 'password'), |
| 112 |
self.conf.get('identity', 'project_name')) |
| 113 |
|
| 114 |
def test_init_manager_client_config_admin_override(self): |
| 115 |
self._override_setup() |
| 116 |
creds = self._get_creds(self.conf, admin=True) |
| 117 |
manager = ClientManager(self.conf, creds) |
| 118 |
# check if cloud_args credentials were overrided by admin ones |
| 119 |
self._check_credentials(manager, |
| 120 |
self.conf.get('auth', 'admin_username'), |
| 121 |
self.conf.get('auth', 'admin_password'), |
| 122 |
self.conf.get('auth', 'admin_project_name')) |
| 123 |
|
| 124 |
|
| 125 |
class TestGeneratePassword(BaseConfigTempestTest): |
| 126 |
|
| 127 |
def test_generate_password_success_easy(self): |
| 128 |
regex = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$' |
| 129 |
length = 12 |
| 130 |
password = tool.generate_password_by_regex(regex, length) |
| 131 |
|
| 132 |
self.assertIsNotNone(password) |
| 133 |
self.assertEqual(length, len(password)) |
| 134 |
self.assertTrue(re.fullmatch(regex, password)) |
| 135 |
|
| 136 |
def test_generate_password_success_hard(self): |
| 137 |
regex = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*_+-=]).{8}$' |
| 138 |
length = 8 |
| 139 |
password = tool.generate_password_by_regex(regex, length) |
| 140 |
|
| 141 |
self.assertIsNotNone(password) |
| 142 |
self.assertEqual(length, len(password)) |
| 143 |
self.assertTrue(re.fullmatch(regex, password)) |
| 144 |
|
| 145 |
def test_generate_password_invalid_regex(self): |
| 146 |
invalid_regex = r'[' |
| 147 |
exc = Exception |
| 148 |
self.assertRaises(exc, |
| 149 |
tool.generate_password_by_regex, |
| 150 |
invalid_regex, 8) |
| 151 |
|
| 152 |
def test_generate_password_impossible(self): |
| 153 |
impossible_regex = r'^[a-z]{10}$' |
| 154 |
exc = Exception |
| 155 |
self.assertRaises(exc, |
| 156 |
tool.generate_password_by_regex, |
| 157 |
impossible_regex, 8) |