Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 1 | # Copyright 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # 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, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 15 | from oslo_config import cfg |
andreaf | b8a5228 | 2015-03-19 22:21:54 +0000 | [diff] [blame] | 16 | from tempest_lib import auth |
| 17 | from tempest_lib import exceptions as lib_exc |
| 18 | from tempest_lib.services.identity.v2 import token_client as v2_client |
| 19 | from tempest_lib.services.identity.v3 import token_client as v3_client |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 20 | |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 21 | from tempest.common import credentials_factory as common_creds |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 22 | from tempest.common import tempest_fixtures as fixtures |
Andrea Frittoli (andreaf) | abf0d6f | 2015-03-23 15:14:02 +0000 | [diff] [blame] | 23 | from tempest import config |
andreaf | b8a5228 | 2015-03-19 22:21:54 +0000 | [diff] [blame] | 24 | from tempest.tests import base |
Andrea Frittoli (andreaf) | abf0d6f | 2015-03-23 15:14:02 +0000 | [diff] [blame] | 25 | from tempest.tests import fake_config |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 26 | from tempest.tests import fake_identity |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 27 | |
| 28 | |
andreaf | b8a5228 | 2015-03-19 22:21:54 +0000 | [diff] [blame] | 29 | class ConfiguredV2CredentialsTests(base.TestCase): |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 30 | attributes = { |
| 31 | 'username': 'fake_username', |
| 32 | 'password': 'fake_password', |
| 33 | 'tenant_name': 'fake_tenant_name' |
| 34 | } |
| 35 | |
| 36 | identity_response = fake_identity._fake_v2_response |
| 37 | credentials_class = auth.KeystoneV2Credentials |
Ken'ichi Ohmichi | 96e7279 | 2015-09-09 04:05:41 +0000 | [diff] [blame] | 38 | tokenclient_class = v2_client.TokenClient |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 39 | identity_version = 'v2' |
| 40 | |
| 41 | def setUp(self): |
| 42 | super(ConfiguredV2CredentialsTests, self).setUp() |
Andrea Frittoli (andreaf) | abf0d6f | 2015-03-23 15:14:02 +0000 | [diff] [blame] | 43 | self.useFixture(fake_config.ConfigFixture()) |
| 44 | self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 45 | self.stubs.Set(self.tokenclient_class, 'raw_request', |
| 46 | self.identity_response) |
| 47 | |
andreaf | b8a5228 | 2015-03-19 22:21:54 +0000 | [diff] [blame] | 48 | def _get_credentials(self, attributes=None): |
| 49 | if attributes is None: |
| 50 | attributes = self.attributes |
| 51 | return self.credentials_class(**attributes) |
| 52 | |
| 53 | def _check(self, credentials, credentials_class, filled): |
| 54 | # Check the right version of credentials has been returned |
| 55 | self.assertIsInstance(credentials, credentials_class) |
| 56 | # Check the id attributes are filled in |
| 57 | attributes = [x for x in credentials.ATTRIBUTES if ( |
| 58 | '_id' in x and x != 'domain_id')] |
| 59 | for attr in attributes: |
| 60 | if filled: |
| 61 | self.assertIsNotNone(getattr(credentials, attr)) |
| 62 | else: |
| 63 | self.assertIsNone(getattr(credentials, attr)) |
| 64 | |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 65 | def _verify_credentials(self, credentials_class, filled=True, |
| 66 | identity_version=None): |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 67 | for ctype in common_creds.CREDENTIAL_TYPES: |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 68 | if identity_version is None: |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 69 | creds = common_creds.get_configured_credentials( |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 70 | credential_type=ctype, fill_in=filled) |
| 71 | else: |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 72 | creds = common_creds.get_configured_credentials( |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 73 | credential_type=ctype, fill_in=filled, |
| 74 | identity_version=identity_version) |
| 75 | self._check(creds, credentials_class, filled) |
| 76 | |
andreaf | b8a5228 | 2015-03-19 22:21:54 +0000 | [diff] [blame] | 77 | def test_create(self): |
| 78 | creds = self._get_credentials() |
| 79 | self.assertEqual(self.attributes, creds._initial) |
| 80 | |
| 81 | def test_create_invalid_attr(self): |
| 82 | self.assertRaises(lib_exc.InvalidCredentials, |
| 83 | self._get_credentials, |
| 84 | attributes=dict(invalid='fake')) |
| 85 | |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 86 | def test_get_configured_credentials(self): |
| 87 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 88 | self._verify_credentials(credentials_class=self.credentials_class) |
| 89 | |
| 90 | def test_get_configured_credentials_unfilled(self): |
| 91 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 92 | self._verify_credentials(credentials_class=self.credentials_class, |
| 93 | filled=False) |
| 94 | |
| 95 | def test_get_configured_credentials_version(self): |
| 96 | # version specified and not loaded from config |
| 97 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 98 | self._verify_credentials(credentials_class=self.credentials_class, |
| 99 | identity_version=self.identity_version) |
| 100 | |
| 101 | def test_is_valid(self): |
| 102 | creds = self._get_credentials() |
| 103 | self.assertTrue(creds.is_valid()) |
| 104 | |
| 105 | |
| 106 | class ConfiguredV3CredentialsTests(ConfiguredV2CredentialsTests): |
| 107 | attributes = { |
| 108 | 'username': 'fake_username', |
| 109 | 'password': 'fake_password', |
| 110 | 'project_name': 'fake_project_name', |
| 111 | 'user_domain_name': 'fake_domain_name' |
| 112 | } |
| 113 | |
| 114 | credentials_class = auth.KeystoneV3Credentials |
| 115 | identity_response = fake_identity._fake_v3_response |
Ken'ichi Ohmichi | 96e7279 | 2015-09-09 04:05:41 +0000 | [diff] [blame] | 116 | tokenclient_class = v3_client.V3TokenClient |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 117 | identity_version = 'v3' |
| 118 | |
| 119 | def setUp(self): |
| 120 | super(ConfiguredV3CredentialsTests, self).setUp() |
| 121 | # Additional config items reset by cfg fixture after each test |
| 122 | cfg.CONF.set_default('auth_version', 'v3', group='identity') |
| 123 | # Identity group items |
| 124 | for prefix in ['', 'alt_', 'admin_']: |
Matthew Treinish | 16cf1e5 | 2015-08-11 10:39:23 -0400 | [diff] [blame] | 125 | if prefix == 'admin_': |
| 126 | group = 'auth' |
| 127 | else: |
| 128 | group = 'identity' |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 129 | cfg.CONF.set_default(prefix + 'domain_name', 'fake_domain_name', |
Matthew Treinish | 16cf1e5 | 2015-08-11 10:39:23 -0400 | [diff] [blame] | 130 | group=group) |