Support network configuration overridden
[snaps.git] / snaps / openstack / tests / create_user_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 unittest
16 import uuid
17
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
22
23 __author__ = 'spisarski'
24
25
26 class UserSettingsUnitTests(unittest.TestCase):
27     """
28     Tests the construction of the UserSettings class
29     """
30
31     def test_no_params(self):
32         with self.assertRaises(Exception):
33             UserSettings()
34
35     def test_empty_config(self):
36         with self.assertRaises(Exception):
37             UserSettings(**dict())
38
39     def test_name_only(self):
40         with self.assertRaises(Exception):
41             UserSettings(name='foo')
42
43     def test_config_with_name_only(self):
44         with self.assertRaises(Exception):
45             UserSettings(**{'name': 'foo'})
46
47     def test_name_pass_enabled_str(self):
48         with self.assertRaises(Exception):
49             UserSettings(name='foo', password='bar', enabled='true')
50
51     def test_config_with_name_pass_enabled_str(self):
52         with self.assertRaises(Exception):
53             UserSettings(
54                 **{'name': 'foo', 'password': 'bar', 'enabled': 'true'})
55
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)
63
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)
71
72     def test_all(self):
73         settings = UserSettings(name='foo', password='bar',
74                                 project_name='proj-foo', email='foo@bar.com',
75                                 enabled=False)
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)
81
82     def test_config_all(self):
83         settings = UserSettings(**{'name': 'foo', 'password': 'bar',
84                                    'project_name': 'proj-foo',
85                                    'email': 'foo@bar.com',
86                                    'enabled': False})
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)
92
93
94 class CreateUserSuccessTests(OSComponentTestCase):
95     """
96     Test for the CreateImage class defined in create_image.py
97     """
98
99     def setUp(self):
100         """
101         Instantiates the CreateImage object that is responsible for downloading
102         and creating an OS image file within OpenStack
103         """
104         guid = str(uuid.uuid4())[:-19]
105         guid = self.__class__.__name__ + '-' + guid
106         self.user_settings = UserConfig(
107             name=guid + '-name',
108             password=guid + '-password',
109             roles={'admin': self.os_creds.project_name},
110             domain_name=self.os_creds.user_domain_name)
111
112         self.keystone = keystone_utils.keystone_client(self.os_creds)
113
114         # Initialize for cleanup
115         self.user_creator = None
116
117     def tearDown(self):
118         """
119         Cleans the image and downloaded image file
120         """
121         if self.user_creator:
122             self.user_creator.clean()
123
124     def test_create_user(self):
125         """
126         Tests the creation of an OpenStack user.
127         """
128         self.user_creator = OpenStackUser(self.os_creds, self.user_settings)
129         created_user = self.user_creator.create()
130         self.assertIsNotNone(created_user)
131
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)
136
137     def test_create_user_2x(self):
138         """
139         Tests the creation of an OpenStack user twice to ensure it only creates
140         one.
141         """
142         self.user_creator = OpenStackUser(self.os_creds, self.user_settings)
143         created_user = self.user_creator.create()
144         self.assertIsNotNone(created_user)
145
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)
150
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)
154
155     def test_create_delete_user(self):
156         """
157         Tests the creation of an OpenStack user then delete.
158         """
159         # Create Image
160         self.user_creator = OpenStackUser(self.os_creds, self.user_settings)
161         created_user = self.user_creator.create()
162         self.assertIsNotNone(created_user)
163
164         keystone_utils.delete_user(self.keystone, created_user)
165
166         # Delete user
167         self.user_creator.clean()
168         self.assertIsNone(self.user_creator.get_user())
169
170     def test_create_admin_user(self):
171         """
172         Tests the creation of an OpenStack user.
173         """
174         self.user_creator = OpenStackUser(self.os_creds, self.user_settings)
175         created_user = self.user_creator.create()
176         self.assertIsNotNone(created_user)
177
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)
182
183         role = keystone_utils.get_role_by_name(self.keystone, 'admin')
184         self.assertIsNotNone(role)
185
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)