Making test user password dynamic for testing against secure pods.
[snaps.git] / snaps / openstack / utils / tests / keystone_utils_tests.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain 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,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import uuid
16
17 from snaps.openstack.create_project import ProjectSettings
18 from snaps.openstack.create_user import UserSettings
19 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
20 from snaps.openstack.utils import keystone_utils
21
22 __author__ = 'spisarski'
23
24
25 class KeystoneSmokeTests(OSComponentTestCase):
26     """
27     Tests to ensure that the neutron client can communicate with the cloud
28     """
29
30     def test_keystone_connect_success(self):
31         """
32         Tests to ensure that the proper credentials can connect.
33         """
34         keystone = keystone_utils.keystone_client(self.os_creds)
35
36         users = keystone.users.list()
37         self.assertIsNotNone(users)
38
39     def test_keystone_connect_fail(self):
40         """
41         Tests to ensure that the improper credentials cannot connect.
42         """
43         from snaps.openstack.os_credentials import OSCreds
44
45         with self.assertRaises(Exception):
46             keystone = keystone_utils.keystone_client(OSCreds(
47                 username='user', password='pass', auth_url='url',
48                 project_name='project'))
49             keystone.users.list()
50
51
52 class KeystoneUtilsTests(OSComponentTestCase):
53     """
54     Test for the CreateImage class defined in create_image.py
55     """
56
57     def setUp(self):
58         """
59         Instantiates the CreateImage object that is responsible for downloading
60         and creating an OS image file within OpenStack
61         """
62         guid = uuid.uuid4()
63         self.username = self.__class__.__name__ + '-' + str(guid)
64         self.user = None
65
66         self.project_name = self.__class__.__name__ + '-' + str(guid)
67         self.project = None
68         self.keystone = keystone_utils.keystone_client(self.os_creds)
69
70     def tearDown(self):
71         """
72         Cleans the remote OpenStack objects
73         """
74         if self.project:
75                 keystone_utils.delete_project(self.keystone, self.project)
76
77         if self.user:
78             keystone_utils.delete_user(self.keystone, self.user)
79
80     def test_create_user_minimal(self):
81         """
82         Tests the keystone_utils.create_user() function
83         """
84         user_settings = UserSettings(name=self.username,
85                                      password=str(uuid.uuid4()))
86         self.user = keystone_utils.create_user(self.keystone, user_settings)
87         self.assertEqual(self.username, self.user.name)
88
89         user = keystone_utils.get_user(self.keystone, self.username)
90         self.assertIsNotNone(user)
91         self.assertEqual(self.user, user)
92
93     def test_create_project_minimal(self):
94         """
95         Tests the keyston_utils.create_project() funtion
96         """
97         project_settings = ProjectSettings(name=self.project_name)
98         self.project = keystone_utils.create_project(self.keystone,
99                                                      project_settings)
100         self.assertEqual(self.project_name, self.project.name)
101
102         project = keystone_utils.get_project(
103             keystone=self.keystone, project_name=project_settings.name)
104         self.assertIsNotNone(project)
105         self.assertEqual(self.project_name, self.project.name)
106
107     def test_get_endpoint_success(self):
108         """
109         Tests to ensure that proper credentials and proper service type can
110         succeed.
111         """
112         endpoint = keystone_utils.get_endpoint(self.os_creds,
113                                                service_type="identity")
114         self.assertIsNotNone(endpoint)
115
116     def test_get_endpoint_fail_without_proper_service(self):
117         """
118         Tests to ensure that proper credentials and improper service type
119         cannot succeed.
120         """
121         with self.assertRaises(Exception):
122             keystone_utils.get_endpoint(self.os_creds, service_type="glance")
123
124     def test_get_endpoint_fail_without_proper_credentials(self):
125         """
126         Tests to ensure that improper credentials and proper service type
127         cannot succeed.
128         """
129         from snaps.openstack.os_credentials import OSCreds
130
131         with self.assertRaises(Exception):
132             keystone_utils.get_endpoint(
133                 OSCreds(username='user', password='pass', auth_url='url',
134                         project_name='project'),
135                 service_type="image")