blob: d404660753226c1c8c892655ba61c989c5d34331 [file] [log] [blame]
Andrea Frittoli9efbe952015-01-29 12:43:09 +00001# 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 Hellmann583ce2c2015-03-11 14:55:46 +000015from oslo_config import cfg
andreafb8a52282015-03-19 22:21:54 +000016from tempest_lib import auth
17from tempest_lib import exceptions as lib_exc
18from tempest_lib.services.identity.v2 import token_client as v2_client
19from tempest_lib.services.identity.v3 import token_client as v3_client
Andrea Frittoli9efbe952015-01-29 12:43:09 +000020
andreafb8a52282015-03-19 22:21:54 +000021
Andrea Frittoli9efbe952015-01-29 12:43:09 +000022from tempest.common import cred_provider
23from tempest.common import tempest_fixtures as fixtures
Andrea Frittoli (andreaf)abf0d6f2015-03-23 15:14:02 +000024from tempest import config
andreafb8a52282015-03-19 22:21:54 +000025from tempest.tests import base
Andrea Frittoli (andreaf)abf0d6f2015-03-23 15:14:02 +000026from tempest.tests import fake_config
Andrea Frittoli9efbe952015-01-29 12:43:09 +000027from tempest.tests import fake_identity
Andrea Frittoli9efbe952015-01-29 12:43:09 +000028
29
andreafb8a52282015-03-19 22:21:54 +000030class ConfiguredV2CredentialsTests(base.TestCase):
Andrea Frittoli9efbe952015-01-29 12:43:09 +000031 attributes = {
32 'username': 'fake_username',
33 'password': 'fake_password',
34 'tenant_name': 'fake_tenant_name'
35 }
36
37 identity_response = fake_identity._fake_v2_response
38 credentials_class = auth.KeystoneV2Credentials
Ken'ichi Ohmichi96e72792015-09-09 04:05:41 +000039 tokenclient_class = v2_client.TokenClient
Andrea Frittoli9efbe952015-01-29 12:43:09 +000040 identity_version = 'v2'
41
42 def setUp(self):
43 super(ConfiguredV2CredentialsTests, self).setUp()
Andrea Frittoli (andreaf)abf0d6f2015-03-23 15:14:02 +000044 self.useFixture(fake_config.ConfigFixture())
45 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Andrea Frittoli9efbe952015-01-29 12:43:09 +000046 self.stubs.Set(self.tokenclient_class, 'raw_request',
47 self.identity_response)
48
andreafb8a52282015-03-19 22:21:54 +000049 def _get_credentials(self, attributes=None):
50 if attributes is None:
51 attributes = self.attributes
52 return self.credentials_class(**attributes)
53
54 def _check(self, credentials, credentials_class, filled):
55 # Check the right version of credentials has been returned
56 self.assertIsInstance(credentials, credentials_class)
57 # Check the id attributes are filled in
58 attributes = [x for x in credentials.ATTRIBUTES if (
59 '_id' in x and x != 'domain_id')]
60 for attr in attributes:
61 if filled:
62 self.assertIsNotNone(getattr(credentials, attr))
63 else:
64 self.assertIsNone(getattr(credentials, attr))
65
Andrea Frittoli9efbe952015-01-29 12:43:09 +000066 def _verify_credentials(self, credentials_class, filled=True,
67 identity_version=None):
68 for ctype in cred_provider.CREDENTIAL_TYPES:
69 if identity_version is None:
70 creds = cred_provider.get_configured_credentials(
71 credential_type=ctype, fill_in=filled)
72 else:
73 creds = cred_provider.get_configured_credentials(
74 credential_type=ctype, fill_in=filled,
75 identity_version=identity_version)
76 self._check(creds, credentials_class, filled)
77
andreafb8a52282015-03-19 22:21:54 +000078 def test_create(self):
79 creds = self._get_credentials()
80 self.assertEqual(self.attributes, creds._initial)
81
82 def test_create_invalid_attr(self):
83 self.assertRaises(lib_exc.InvalidCredentials,
84 self._get_credentials,
85 attributes=dict(invalid='fake'))
86
Andrea Frittoli9efbe952015-01-29 12:43:09 +000087 def test_get_configured_credentials(self):
88 self.useFixture(fixtures.LockFixture('auth_version'))
89 self._verify_credentials(credentials_class=self.credentials_class)
90
91 def test_get_configured_credentials_unfilled(self):
92 self.useFixture(fixtures.LockFixture('auth_version'))
93 self._verify_credentials(credentials_class=self.credentials_class,
94 filled=False)
95
96 def test_get_configured_credentials_version(self):
97 # version specified and not loaded from config
98 self.useFixture(fixtures.LockFixture('auth_version'))
99 self._verify_credentials(credentials_class=self.credentials_class,
100 identity_version=self.identity_version)
101
102 def test_is_valid(self):
103 creds = self._get_credentials()
104 self.assertTrue(creds.is_valid())
105
106
107class ConfiguredV3CredentialsTests(ConfiguredV2CredentialsTests):
108 attributes = {
109 'username': 'fake_username',
110 'password': 'fake_password',
111 'project_name': 'fake_project_name',
112 'user_domain_name': 'fake_domain_name'
113 }
114
115 credentials_class = auth.KeystoneV3Credentials
116 identity_response = fake_identity._fake_v3_response
Ken'ichi Ohmichi96e72792015-09-09 04:05:41 +0000117 tokenclient_class = v3_client.V3TokenClient
Andrea Frittoli9efbe952015-01-29 12:43:09 +0000118 identity_version = 'v3'
119
120 def setUp(self):
121 super(ConfiguredV3CredentialsTests, self).setUp()
122 # Additional config items reset by cfg fixture after each test
123 cfg.CONF.set_default('auth_version', 'v3', group='identity')
124 # Identity group items
125 for prefix in ['', 'alt_', 'admin_']:
Matthew Treinish16cf1e52015-08-11 10:39:23 -0400126 if prefix == 'admin_':
127 group = 'auth'
128 else:
129 group = 'identity'
Andrea Frittoli9efbe952015-01-29 12:43:09 +0000130 cfg.CONF.set_default(prefix + 'domain_name', 'fake_domain_name',
Matthew Treinish16cf1e52015-08-11 10:39:23 -0400131 group=group)