1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from snaps.config.user import UserConfig
19 from snaps.openstack.create_user import OpenStackUser, UserSettings
20 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
21 from snaps.openstack.utils import keystone_utils
23 __author__ = 'spisarski'
26 class UserSettingsUnitTests(unittest.TestCase):
28 Tests the construction of the UserSettings class
31 def test_no_params(self):
32 with self.assertRaises(Exception):
35 def test_empty_config(self):
36 with self.assertRaises(Exception):
37 UserSettings(**dict())
39 def test_name_only(self):
40 with self.assertRaises(Exception):
41 UserSettings(name='foo')
43 def test_config_with_name_only(self):
44 with self.assertRaises(Exception):
45 UserSettings(**{'name': 'foo'})
47 def test_name_pass_enabled_str(self):
48 with self.assertRaises(Exception):
49 UserSettings(name='foo', password='bar', enabled='true')
51 def test_config_with_name_pass_enabled_str(self):
52 with self.assertRaises(Exception):
54 **{'name': 'foo', 'password': 'bar', 'enabled': 'true'})
56 def test_name_pass_only(self):
57 settings = UserSettings(name='foo', password='bar')
58 self.assertEqual('foo', settings.name)
59 self.assertEqual('bar', settings.password)
60 self.assertIsNone(settings.project_name)
61 self.assertIsNone(settings.email)
62 self.assertTrue(settings.enabled)
64 def test_config_with_name_pass_only(self):
65 settings = UserSettings(**{'name': 'foo', 'password': 'bar'})
66 self.assertEqual('foo', settings.name)
67 self.assertEqual('bar', settings.password)
68 self.assertIsNone(settings.project_name)
69 self.assertIsNone(settings.email)
70 self.assertTrue(settings.enabled)
73 settings = UserSettings(name='foo', password='bar',
74 project_name='proj-foo', email='foo@bar.com',
76 self.assertEqual('foo', settings.name)
77 self.assertEqual('bar', settings.password)
78 self.assertEqual('proj-foo', settings.project_name)
79 self.assertEqual('foo@bar.com', settings.email)
80 self.assertFalse(settings.enabled)
82 def test_config_all(self):
83 settings = UserSettings(**{'name': 'foo', 'password': 'bar',
84 'project_name': 'proj-foo',
85 'email': 'foo@bar.com',
87 self.assertEqual('foo', settings.name)
88 self.assertEqual('bar', settings.password)
89 self.assertEqual('proj-foo', settings.project_name)
90 self.assertEqual('foo@bar.com', settings.email)
91 self.assertFalse(settings.enabled)
94 class CreateUserSuccessTests(OSComponentTestCase):
96 Test for the CreateImage class defined in create_image.py
101 Instantiates the CreateImage object that is responsible for downloading
102 and creating an OS image file within OpenStack
104 guid = str(uuid.uuid4())[:-19]
105 guid = self.__class__.__name__ + '-' + guid
106 self.user_settings = UserConfig(
108 password=guid + '-password',
109 roles={'admin': self.os_creds.project_name},
110 domain_name=self.os_creds.user_domain_name)
112 self.keystone = keystone_utils.keystone_client(self.os_creds)
114 # Initialize for cleanup
115 self.user_creator = None
119 Cleans the image and downloaded image file
121 if self.user_creator:
122 self.user_creator.clean()
124 def test_create_user(self):
126 Tests the creation of an OpenStack user.
128 self.user_creator = OpenStackUser(self.os_creds, self.user_settings)
129 created_user = self.user_creator.create()
130 self.assertIsNotNone(created_user)
132 retrieved_user = keystone_utils.get_user(self.keystone,
133 self.user_settings.name)
134 self.assertIsNotNone(retrieved_user)
135 self.assertEqual(created_user, retrieved_user)
137 def test_create_user_2x(self):
139 Tests the creation of an OpenStack user twice to ensure it only creates
142 self.user_creator = OpenStackUser(self.os_creds, self.user_settings)
143 created_user = self.user_creator.create()
144 self.assertIsNotNone(created_user)
146 retrieved_user = keystone_utils.get_user(self.keystone,
147 self.user_settings.name)
148 self.assertIsNotNone(retrieved_user)
149 self.assertEqual(created_user, retrieved_user)
151 # Create user for the second time to ensure it is the same
152 user2 = OpenStackUser(self.os_creds, self.user_settings).create()
153 self.assertEqual(retrieved_user, user2)
155 def test_create_delete_user(self):
157 Tests the creation of an OpenStack user then delete.
160 self.user_creator = OpenStackUser(self.os_creds, self.user_settings)
161 created_user = self.user_creator.create()
162 self.assertIsNotNone(created_user)
164 keystone_utils.delete_user(self.keystone, created_user)
167 self.user_creator.clean()
168 self.assertIsNone(self.user_creator.get_user())
170 def test_create_admin_user(self):
172 Tests the creation of an OpenStack user.
174 self.user_creator = OpenStackUser(self.os_creds, self.user_settings)
175 created_user = self.user_creator.create()
176 self.assertIsNotNone(created_user)
178 retrieved_user = keystone_utils.get_user(self.keystone,
179 self.user_settings.name)
180 self.assertIsNotNone(retrieved_user)
181 self.assertEqual(created_user, retrieved_user)
183 role = keystone_utils.get_role_by_name(self.keystone, 'admin')
184 self.assertIsNotNone(role)
186 os_proj = keystone_utils.get_project(
187 keystone=self.keystone, project_name=self.os_creds.project_name)
188 user_roles = keystone_utils.get_roles_by_user(
189 self.keystone, retrieved_user, os_proj)
190 self.assertIsNotNone(user_roles)
191 self.assertEqual(1, len(user_roles))
192 self.assertEqual(role.id, user_roles[0].id)