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 | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 18 | from tempest import auth |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 19 | from tempest.common import tempest_fixtures as fixtures |
| 20 | from tempest import config |
| 21 | from tempest import exceptions |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 22 | from tempest.services.identity.json import token_client as v2_client |
| 23 | from tempest.services.identity.v3.json import token_client as v3_client |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 24 | from tempest.tests import base |
| 25 | from tempest.tests import fake_config |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 26 | from tempest.tests import fake_identity |
| 27 | |
| 28 | |
| 29 | class CredentialsTests(base.TestCase): |
| 30 | attributes = {} |
| 31 | credentials_class = auth.Credentials |
| 32 | |
| 33 | def _get_credentials(self, attributes=None): |
| 34 | if attributes is None: |
| 35 | attributes = self.attributes |
| 36 | return self.credentials_class(**attributes) |
| 37 | |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 38 | def _check(self, credentials, credentials_class, filled): |
| 39 | # Check the right version of credentials has been returned |
| 40 | self.assertIsInstance(credentials, credentials_class) |
| 41 | # Check the id attributes are filled in |
| 42 | attributes = [x for x in credentials.ATTRIBUTES if ( |
| 43 | '_id' in x and x != 'domain_id')] |
| 44 | for attr in attributes: |
| 45 | if filled: |
| 46 | self.assertIsNotNone(getattr(credentials, attr)) |
| 47 | else: |
| 48 | self.assertIsNone(getattr(credentials, attr)) |
| 49 | |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 50 | def setUp(self): |
| 51 | super(CredentialsTests, self).setUp() |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 52 | self.useFixture(fake_config.ConfigFixture()) |
| 53 | self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate) |
| 54 | |
Andrea Frittoli | 2095d24 | 2014-03-20 08:36:23 +0000 | [diff] [blame] | 55 | def test_create(self): |
| 56 | creds = self._get_credentials() |
| 57 | self.assertEqual(self.attributes, creds._initial) |
| 58 | |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 59 | def test_create_invalid_attr(self): |
| 60 | self.assertRaises(exceptions.InvalidCredentials, |
| 61 | self._get_credentials, |
| 62 | attributes=dict(invalid='fake')) |
| 63 | |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 64 | def test_is_valid(self): |
| 65 | creds = self._get_credentials() |
| 66 | self.assertRaises(NotImplementedError, creds.is_valid) |
| 67 | |
| 68 | |
| 69 | class KeystoneV2CredentialsTests(CredentialsTests): |
| 70 | attributes = { |
| 71 | 'username': 'fake_username', |
| 72 | 'password': 'fake_password', |
| 73 | 'tenant_name': 'fake_tenant_name' |
| 74 | } |
| 75 | |
| 76 | identity_response = fake_identity._fake_v2_response |
| 77 | credentials_class = auth.KeystoneV2Credentials |
Ken'ichi Ohmichi | 03010dc | 2015-01-08 08:16:25 +0000 | [diff] [blame] | 78 | tokenclient_class = v2_client.TokenClientJSON |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 79 | identity_version = 'v2' |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 80 | |
| 81 | def setUp(self): |
| 82 | super(KeystoneV2CredentialsTests, self).setUp() |
Ken'ichi Ohmichi | 03010dc | 2015-01-08 08:16:25 +0000 | [diff] [blame] | 83 | self.stubs.Set(self.tokenclient_class, 'raw_request', |
| 84 | self.identity_response) |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 85 | |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 86 | def _verify_credentials(self, credentials_class, creds_dict, filled=True): |
ghanshyam | 5ff763f | 2015-02-18 16:15:58 +0900 | [diff] [blame] | 87 | creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL, |
| 88 | fill_in=filled, |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 89 | identity_version=self.identity_version, |
| 90 | **creds_dict) |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 91 | self._check(creds, credentials_class, filled) |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 92 | |
| 93 | def test_get_credentials(self): |
| 94 | self.useFixture(fixtures.LockFixture('auth_version')) |
Andrea Frittoli | 2095d24 | 2014-03-20 08:36:23 +0000 | [diff] [blame] | 95 | self._verify_credentials(credentials_class=self.credentials_class, |
| 96 | creds_dict=self.attributes) |
| 97 | |
| 98 | def test_get_credentials_not_filled(self): |
| 99 | self.useFixture(fixtures.LockFixture('auth_version')) |
| 100 | self._verify_credentials(credentials_class=self.credentials_class, |
Andrea Frittoli | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 101 | creds_dict=self.attributes, |
| 102 | filled=False) |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 103 | |
| 104 | def test_is_valid(self): |
| 105 | creds = self._get_credentials() |
| 106 | self.assertTrue(creds.is_valid()) |
| 107 | |
Matthew Treinish | 6bbc874 | 2014-08-25 18:28:15 -0400 | [diff] [blame] | 108 | def _test_is_not_valid(self, ignore_key): |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 109 | creds = self._get_credentials() |
| 110 | for attr in self.attributes.keys(): |
Matthew Treinish | 6bbc874 | 2014-08-25 18:28:15 -0400 | [diff] [blame] | 111 | if attr == ignore_key: |
| 112 | continue |
| 113 | temp_attr = getattr(creds, attr) |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 114 | delattr(creds, attr) |
| 115 | self.assertFalse(creds.is_valid(), |
| 116 | "Credentials should be invalid without %s" % attr) |
Matthew Treinish | 6bbc874 | 2014-08-25 18:28:15 -0400 | [diff] [blame] | 117 | setattr(creds, attr, temp_attr) |
| 118 | |
| 119 | def test_is_not_valid(self): |
| 120 | # NOTE(mtreinish): A KeystoneV2 credential object is valid without |
| 121 | # a tenant_name. So skip that check. See tempest.auth for the valid |
| 122 | # credential requirements |
| 123 | self._test_is_not_valid('tenant_name') |
Andrea Frittoli | 7d707a5 | 2014-04-06 11:46:32 +0100 | [diff] [blame] | 124 | |
Andrea Frittoli | 2095d24 | 2014-03-20 08:36:23 +0000 | [diff] [blame] | 125 | def test_reset_all_attributes(self): |
| 126 | creds = self._get_credentials() |
| 127 | initial_creds = copy.deepcopy(creds) |
| 128 | set_attr = creds.__dict__.keys() |
| 129 | missing_attr = set(creds.ATTRIBUTES).difference(set_attr) |
| 130 | # Set all unset attributes, then reset |
| 131 | for attr in missing_attr: |
| 132 | setattr(creds, attr, 'fake' + attr) |
| 133 | creds.reset() |
| 134 | # Check reset credentials are same as initial ones |
| 135 | self.assertEqual(creds, initial_creds) |
| 136 | |
| 137 | def test_reset_single_attribute(self): |
| 138 | creds = self._get_credentials() |
| 139 | initial_creds = copy.deepcopy(creds) |
| 140 | set_attr = creds.__dict__.keys() |
| 141 | missing_attr = set(creds.ATTRIBUTES).difference(set_attr) |
| 142 | # Set one unset attributes, then reset |
| 143 | for attr in missing_attr: |
| 144 | setattr(creds, attr, 'fake' + attr) |
| 145 | creds.reset() |
| 146 | # Check reset credentials are same as initial ones |
| 147 | self.assertEqual(creds, initial_creds) |
| 148 | |
Andrea Frittoli | b1b04bb | 2014-04-06 11:57:07 +0100 | [diff] [blame] | 149 | |
| 150 | class KeystoneV3CredentialsTests(KeystoneV2CredentialsTests): |
| 151 | attributes = { |
| 152 | 'username': 'fake_username', |
| 153 | 'password': 'fake_password', |
| 154 | 'project_name': 'fake_project_name', |
| 155 | 'user_domain_name': 'fake_domain_name' |
| 156 | } |
| 157 | |
| 158 | credentials_class = auth.KeystoneV3Credentials |
| 159 | identity_response = fake_identity._fake_v3_response |
Ken'ichi Ohmichi | 03010dc | 2015-01-08 08:16:25 +0000 | [diff] [blame] | 160 | tokenclient_class = v3_client.V3TokenClientJSON |
Andrea Frittoli | 878d5ab | 2015-01-30 13:22:50 +0000 | [diff] [blame] | 161 | identity_version = 'v3' |
Andrea Frittoli | b1b04bb | 2014-04-06 11:57:07 +0100 | [diff] [blame] | 162 | |
Matthew Treinish | 6bbc874 | 2014-08-25 18:28:15 -0400 | [diff] [blame] | 163 | def test_is_not_valid(self): |
| 164 | # NOTE(mtreinish) For a Keystone V3 credential object a project name |
| 165 | # is not required to be valid, so we skip that check. See tempest.auth |
| 166 | # for the valid credential requirements |
| 167 | self._test_is_not_valid('project_name') |
| 168 | |
Andrea Frittoli | b1b04bb | 2014-04-06 11:57:07 +0100 | [diff] [blame] | 169 | def test_synced_attributes(self): |
| 170 | attributes = self.attributes |
| 171 | # Create V3 credentials with tenant instead of project, and user_domain |
| 172 | for attr in ['project_id', 'user_domain_id']: |
| 173 | attributes[attr] = 'fake_' + attr |
| 174 | creds = self._get_credentials(attributes) |
| 175 | self.assertEqual(creds.project_name, creds.tenant_name) |
| 176 | self.assertEqual(creds.project_id, creds.tenant_id) |
| 177 | self.assertEqual(creds.user_domain_name, creds.project_domain_name) |
| 178 | self.assertEqual(creds.user_domain_id, creds.project_domain_id) |
| 179 | # Replace user_domain with project_domain |
| 180 | del attributes['user_domain_name'] |
| 181 | del attributes['user_domain_id'] |
| 182 | del attributes['project_name'] |
| 183 | del attributes['project_id'] |
| 184 | for attr in ['project_domain_name', 'project_domain_id', |
| 185 | 'tenant_name', 'tenant_id']: |
| 186 | attributes[attr] = 'fake_' + attr |
| 187 | self.assertEqual(creds.tenant_name, creds.project_name) |
| 188 | self.assertEqual(creds.tenant_id, creds.project_id) |
| 189 | self.assertEqual(creds.project_domain_name, creds.user_domain_name) |
| 190 | self.assertEqual(creds.project_domain_id, creds.user_domain_id) |