Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 1 | # Copyright 2014 Hewlett-Packard Development Company, L.P. |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Andrea Frittoli | 2095d24 | 2014-03-20 08:36:23 +0000 | [diff] [blame] | 16 | import copy |
| 17 | |
Andrea Frittoli | b1b04bb | 2014-04-06 11:57:07 +0100 | [diff] [blame] | 18 | from oslo.config import cfg |
| 19 | |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 20 | from tempest import auth |
| 21 | from tempest.common import http |
| 22 | from tempest.common import tempest_fixtures as fixtures |
| 23 | from tempest import config |
| 24 | from tempest import exceptions |
| 25 | from tempest.tests import base |
| 26 | from tempest.tests import fake_config |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 27 | from tempest.tests import fake_identity |
| 28 | |
| 29 | |
| 30 | class CredentialsTests(base.TestCase): |
| 31 | attributes = {} |
| 32 | credentials_class = auth.Credentials |
| 33 | |
| 34 | def _get_credentials(self, attributes=None): |
| 35 | if attributes is None: |
| 36 | attributes = self.attributes |
| 37 | return self.credentials_class(**attributes) |
| 38 | |
| 39 | def setUp(self): |
| 40 | super(CredentialsTests, self).setUp() |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 41 | self.useFixture(fake_config.ConfigFixture()) |
| 42 | self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) |
| 43 | |
Andrea Frittoli | 2095d24 | 2014-03-20 08:36:23 +0000 | [diff] [blame] | 44 | def test_create(self): |
| 45 | creds = self._get_credentials() |
| 46 | self.assertEqual(self.attributes, creds._initial) |
| 47 | |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 48 | def test_create_invalid_attr(self): |
| 49 | self.assertRaises(exceptions.InvalidCredentials, |
| 50 | self._get_credentials, |
| 51 | attributes=dict(invalid='fake')) |
| 52 | |
| 53 | def test_default(self): |
| 54 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 55 | for ctype in self.credentials_class.TYPES: |
| 56 | self.assertRaises(NotImplementedError, |
| 57 | self.credentials_class.get_default, |
| 58 | credentials_type=ctype) |
| 59 | |
| 60 | def test_invalid_default(self): |
| 61 | self.assertRaises(exceptions.InvalidCredentials, |
| 62 | auth.Credentials.get_default, |
| 63 | credentials_type='invalid_type') |
| 64 | |
| 65 | def test_is_valid(self): |
| 66 | creds = self._get_credentials() |
| 67 | self.assertRaises(NotImplementedError, creds.is_valid) |
| 68 | |
| 69 | |
| 70 | class KeystoneV2CredentialsTests(CredentialsTests): |
| 71 | attributes = { |
| 72 | 'username': 'fake_username', |
| 73 | 'password': 'fake_password', |
| 74 | 'tenant_name': 'fake_tenant_name' |
| 75 | } |
| 76 | |
| 77 | identity_response = fake_identity._fake_v2_response |
| 78 | credentials_class = auth.KeystoneV2Credentials |
| 79 | |
| 80 | def setUp(self): |
| 81 | super(KeystoneV2CredentialsTests, self).setUp() |
| 82 | self.stubs.Set(http.ClosingHttp, 'request', self.identity_response) |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 83 | |
Andrea Frittoli | 2095d24 | 2014-03-20 08:36:23 +0000 | [diff] [blame] | 84 | def _verify_credentials(self, credentials_class, filled=True, |
| 85 | creds_dict=None): |
| 86 | |
| 87 | def _check(credentials): |
| 88 | # Check the right version of credentials has been returned |
| 89 | self.assertIsInstance(credentials, credentials_class) |
| 90 | # Check the id attributes are filled in |
| 91 | attributes = [x for x in credentials.ATTRIBUTES if ( |
| 92 | '_id' in x and x != 'domain_id')] |
| 93 | for attr in attributes: |
| 94 | if filled: |
| 95 | self.assertIsNotNone(getattr(credentials, attr)) |
| 96 | else: |
| 97 | self.assertIsNone(getattr(credentials, attr)) |
| 98 | |
| 99 | if creds_dict is None: |
| 100 | for ctype in auth.Credentials.TYPES: |
| 101 | creds = auth.get_default_credentials(credential_type=ctype, |
| 102 | fill_in=filled) |
| 103 | _check(creds) |
| 104 | else: |
| 105 | creds = auth.get_credentials(fill_in=filled, **creds_dict) |
| 106 | _check(creds) |
| 107 | |
| 108 | def test_get_default_credentials(self): |
| 109 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 110 | self._verify_credentials(credentials_class=self.credentials_class) |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 111 | |
| 112 | def test_get_credentials(self): |
| 113 | self.useFixture(fixtures.LockFixture('auth_version')) |
Andrea Frittoli | 2095d24 | 2014-03-20 08:36:23 +0000 | [diff] [blame] | 114 | self._verify_credentials(credentials_class=self.credentials_class, |
| 115 | creds_dict=self.attributes) |
| 116 | |
| 117 | def test_get_credentials_not_filled(self): |
| 118 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 119 | self._verify_credentials(credentials_class=self.credentials_class, |
| 120 | filled=False, |
| 121 | creds_dict=self.attributes) |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 122 | |
| 123 | def test_is_valid(self): |
| 124 | creds = self._get_credentials() |
| 125 | self.assertTrue(creds.is_valid()) |
| 126 | |
Matthew Treinish | 6bbc874 | 2014-08-25 18:28:15 -0400 | [diff] [blame] | 127 | def _test_is_not_valid(self, ignore_key): |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 128 | creds = self._get_credentials() |
| 129 | for attr in self.attributes.keys(): |
Matthew Treinish | 6bbc874 | 2014-08-25 18:28:15 -0400 | [diff] [blame] | 130 | if attr == ignore_key: |
| 131 | continue |
| 132 | temp_attr = getattr(creds, attr) |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 133 | delattr(creds, attr) |
| 134 | self.assertFalse(creds.is_valid(), |
| 135 | "Credentials should be invalid without %s" % attr) |
Matthew Treinish | 6bbc874 | 2014-08-25 18:28:15 -0400 | [diff] [blame] | 136 | setattr(creds, attr, temp_attr) |
| 137 | |
| 138 | def test_is_not_valid(self): |
| 139 | # NOTE(mtreinish): A KeystoneV2 credential object is valid without |
| 140 | # a tenant_name. So skip that check. See tempest.auth for the valid |
| 141 | # credential requirements |
| 142 | self._test_is_not_valid('tenant_name') |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 143 | |
| 144 | def test_default(self): |
| 145 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 146 | for ctype in self.credentials_class.TYPES: |
| 147 | creds = self.credentials_class.get_default(credentials_type=ctype) |
| 148 | for attr in self.attributes.keys(): |
| 149 | # Default configuration values related to credentials |
| 150 | # are defined as fake_* in fake_config.py |
| 151 | self.assertEqual(getattr(creds, attr), 'fake_' + attr) |
Andrea Frittoli | b1b04bb | 2014-04-06 11:57:07 +0100 | [diff] [blame] | 152 | |
Andrea Frittoli | 2095d24 | 2014-03-20 08:36:23 +0000 | [diff] [blame] | 153 | def test_reset_all_attributes(self): |
| 154 | creds = self._get_credentials() |
| 155 | initial_creds = copy.deepcopy(creds) |
| 156 | set_attr = creds.__dict__.keys() |
| 157 | missing_attr = set(creds.ATTRIBUTES).difference(set_attr) |
| 158 | # Set all unset attributes, then reset |
| 159 | for attr in missing_attr: |
| 160 | setattr(creds, attr, 'fake' + attr) |
| 161 | creds.reset() |
| 162 | # Check reset credentials are same as initial ones |
| 163 | self.assertEqual(creds, initial_creds) |
| 164 | |
| 165 | def test_reset_single_attribute(self): |
| 166 | creds = self._get_credentials() |
| 167 | initial_creds = copy.deepcopy(creds) |
| 168 | set_attr = creds.__dict__.keys() |
| 169 | missing_attr = set(creds.ATTRIBUTES).difference(set_attr) |
| 170 | # Set one unset attributes, then reset |
| 171 | for attr in missing_attr: |
| 172 | setattr(creds, attr, 'fake' + attr) |
| 173 | creds.reset() |
| 174 | # Check reset credentials are same as initial ones |
| 175 | self.assertEqual(creds, initial_creds) |
| 176 | |
Andrea Frittoli | b1b04bb | 2014-04-06 11:57:07 +0100 | [diff] [blame] | 177 | |
| 178 | class KeystoneV3CredentialsTests(KeystoneV2CredentialsTests): |
| 179 | attributes = { |
| 180 | 'username': 'fake_username', |
| 181 | 'password': 'fake_password', |
| 182 | 'project_name': 'fake_project_name', |
| 183 | 'user_domain_name': 'fake_domain_name' |
| 184 | } |
| 185 | |
| 186 | credentials_class = auth.KeystoneV3Credentials |
| 187 | identity_response = fake_identity._fake_v3_response |
| 188 | |
| 189 | def setUp(self): |
| 190 | super(KeystoneV3CredentialsTests, self).setUp() |
| 191 | # Additional config items reset by cfg fixture after each test |
| 192 | cfg.CONF.set_default('auth_version', 'v3', group='identity') |
| 193 | # Identity group items |
| 194 | for prefix in ['', 'alt_', 'admin_']: |
| 195 | cfg.CONF.set_default(prefix + 'domain_name', 'fake_domain_name', |
| 196 | group='identity') |
| 197 | # Compute Admin group items |
| 198 | cfg.CONF.set_default('domain_name', 'fake_domain_name', |
| 199 | group='compute-admin') |
| 200 | |
| 201 | def test_default(self): |
| 202 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 203 | for ctype in self.credentials_class.TYPES: |
| 204 | creds = self.credentials_class.get_default(credentials_type=ctype) |
| 205 | for attr in self.attributes.keys(): |
| 206 | if attr == 'project_name': |
| 207 | config_value = 'fake_tenant_name' |
| 208 | elif attr == 'user_domain_name': |
| 209 | config_value = 'fake_domain_name' |
| 210 | else: |
| 211 | config_value = 'fake_' + attr |
| 212 | self.assertEqual(getattr(creds, attr), config_value) |
| 213 | |
Matthew Treinish | 6bbc874 | 2014-08-25 18:28:15 -0400 | [diff] [blame] | 214 | def test_is_not_valid(self): |
| 215 | # NOTE(mtreinish) For a Keystone V3 credential object a project name |
| 216 | # is not required to be valid, so we skip that check. See tempest.auth |
| 217 | # for the valid credential requirements |
| 218 | self._test_is_not_valid('project_name') |
| 219 | |
Andrea Frittoli | b1b04bb | 2014-04-06 11:57:07 +0100 | [diff] [blame] | 220 | def test_synced_attributes(self): |
| 221 | attributes = self.attributes |
| 222 | # Create V3 credentials with tenant instead of project, and user_domain |
| 223 | for attr in ['project_id', 'user_domain_id']: |
| 224 | attributes[attr] = 'fake_' + attr |
| 225 | creds = self._get_credentials(attributes) |
| 226 | self.assertEqual(creds.project_name, creds.tenant_name) |
| 227 | self.assertEqual(creds.project_id, creds.tenant_id) |
| 228 | self.assertEqual(creds.user_domain_name, creds.project_domain_name) |
| 229 | self.assertEqual(creds.user_domain_id, creds.project_domain_id) |
| 230 | # Replace user_domain with project_domain |
| 231 | del attributes['user_domain_name'] |
| 232 | del attributes['user_domain_id'] |
| 233 | del attributes['project_name'] |
| 234 | del attributes['project_id'] |
| 235 | for attr in ['project_domain_name', 'project_domain_id', |
| 236 | 'tenant_name', 'tenant_id']: |
| 237 | attributes[attr] = 'fake_' + attr |
| 238 | self.assertEqual(creds.tenant_name, creds.project_name) |
| 239 | self.assertEqual(creds.tenant_id, creds.project_id) |
| 240 | self.assertEqual(creds.project_domain_name, creds.user_domain_name) |
| 241 | self.assertEqual(creds.project_domain_id, creds.user_domain_id) |