blob: 96b75fdf5f56462938e818dd09353169559b41a7 [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
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010021from tempest.common import credentials_factory as common_creds
Andrea Frittoli9efbe952015-01-29 12:43:09 +000022from tempest.common import tempest_fixtures as fixtures
Andrea Frittoli (andreaf)abf0d6f2015-03-23 15:14:02 +000023from tempest import config
andreafb8a52282015-03-19 22:21:54 +000024from tempest.tests import base
Andrea Frittoli (andreaf)abf0d6f2015-03-23 15:14:02 +000025from tempest.tests import fake_config
Andrea Frittoli9efbe952015-01-29 12:43:09 +000026from tempest.tests import fake_identity
Andrea Frittoli9efbe952015-01-29 12:43:09 +000027
28
andreafb8a52282015-03-19 22:21:54 +000029class ConfiguredV2CredentialsTests(base.TestCase):
Andrea Frittoli9efbe952015-01-29 12:43:09 +000030 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 Ohmichi96e72792015-09-09 04:05:41 +000038 tokenclient_class = v2_client.TokenClient
Andrea Frittoli9efbe952015-01-29 12:43:09 +000039 identity_version = 'v2'
40
41 def setUp(self):
42 super(ConfiguredV2CredentialsTests, self).setUp()
Andrea Frittoli (andreaf)abf0d6f2015-03-23 15:14:02 +000043 self.useFixture(fake_config.ConfigFixture())
44 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Andrea Frittoli9efbe952015-01-29 12:43:09 +000045 self.stubs.Set(self.tokenclient_class, 'raw_request',
46 self.identity_response)
47
andreafb8a52282015-03-19 22:21:54 +000048 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 Frittoli9efbe952015-01-29 12:43:09 +000065 def _verify_credentials(self, credentials_class, filled=True,
66 identity_version=None):
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010067 for ctype in common_creds.CREDENTIAL_TYPES:
Andrea Frittoli9efbe952015-01-29 12:43:09 +000068 if identity_version is None:
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010069 creds = common_creds.get_configured_credentials(
Andrea Frittoli9efbe952015-01-29 12:43:09 +000070 credential_type=ctype, fill_in=filled)
71 else:
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010072 creds = common_creds.get_configured_credentials(
Andrea Frittoli9efbe952015-01-29 12:43:09 +000073 credential_type=ctype, fill_in=filled,
74 identity_version=identity_version)
75 self._check(creds, credentials_class, filled)
76
andreafb8a52282015-03-19 22:21:54 +000077 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 Frittoli9efbe952015-01-29 12:43:09 +000086 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
106class 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 Ohmichi96e72792015-09-09 04:05:41 +0000116 tokenclient_class = v3_client.V3TokenClient
Andrea Frittoli9efbe952015-01-29 12:43:09 +0000117 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 Treinish16cf1e52015-08-11 10:39:23 -0400125 if prefix == 'admin_':
126 group = 'auth'
127 else:
128 group = 'identity'
Andrea Frittoli9efbe952015-01-29 12:43:09 +0000129 cfg.CONF.set_default(prefix + 'domain_name', 'fake_domain_name',
Matthew Treinish16cf1e52015-08-11 10:39:23 -0400130 group=group)