Debian Codegraph @

keystoneclient/tests/functional/base.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from openstack import config as occ
14 import openstack.exceptions
15 import testtools
16
17 from keystoneclient import client
18
19 IDENTITY_CLIENT = 'identity'
20 OPENSTACK_CLOUDS = ('functional_admin', 'devstack-admin', 'envvars')
21
22
23 def get_client(version):
24 """Create a keystoneclient instance to run functional tests.
25
26 The client is instantiated either based on a clouds.yaml config file or
27 from the environment variables.
28
29 First, look for a 'functional_admin' cloud, as this is a cloud that the
30 user may have defined for functional testing with admin credentials. If
31 that is not found, check for the 'devstack-admin' cloud. Finally, fall
32 back to looking for environment variables.
33
34 """
35 for cloud in OPENSTACK_CLOUDS:
36 try:
37 cloud_config = occ.OpenStackConfig().get_one(
38 cloud=cloud, identity_api_version=version)
39 endpoint = cloud_config.get_session_endpoint(IDENTITY_CLIENT)
40 return client.Client(
41 version=version,
42 session=cloud_config.get_session(),
43 endpoint=endpoint)
44 except openstack.exceptions.ConfigException:
45 pass
46
47 raise Exception("Could not find any cloud definition for clouds named"
48 " functional_admin or devstack-admin. Check your"
49 " clouds.yaml file or your envvars and try again.")
50
51
52 class ClientTestCase(testtools.TestCase):
53
54 def setUp(self):
55 super(ClientTestCase, self).setUp()
56
57 if not self.auth_ref.project_scoped:
58 raise Exception("Could not run functional tests, which are "
59 "run based on the scope provided for "
60 "authentication. Please provide a project "
61 "scope information.")
62
63 @property
64 def client(self):
65 if not hasattr(self, '_client'):
66 self._client = get_client(self.version)
67
68 return self._client
69
70 @property
71 def auth_ref(self):
72 return self.client.session.auth.get_auth_ref(self.client.session)
73
74 @property
75 def project_domain_id(self):
76 return self.auth_ref.project_domain_id
77
78 @property
79 def project_id(self):
80 return self.client.session.get_project_id()
81
82 @property
83 def user_id(self):
84 return self.client.session.get_user_id()
85
86
87 class V3ClientTestCase(ClientTestCase):
88 version = '3'