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.openstack.create_project import OpenStackProject, ProjectSettings
19 from snaps.openstack.create_security_group import OpenStackSecurityGroup
20 from snaps.openstack.create_security_group import SecurityGroupSettings
21 from snaps.openstack.create_user import OpenStackUser
22 from snaps.openstack.create_user import UserSettings
23 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
24 from snaps.openstack.utils import keystone_utils
26 __author__ = 'spisarski'
29 class ProjectSettingsUnitTests(unittest.TestCase):
31 Tests the construction of the ProjectSettings class
34 def test_no_params(self):
35 with self.assertRaises(Exception):
38 def test_empty_config(self):
39 with self.assertRaises(Exception):
40 ProjectSettings(**dict())
42 def test_name_only(self):
43 settings = ProjectSettings(name='foo')
44 self.assertEqual('foo', settings.name)
45 self.assertEqual('default', settings.domain)
46 self.assertIsNone(settings.description)
47 self.assertTrue(settings.enabled)
49 def test_config_with_name_only(self):
50 settings = ProjectSettings(**{'name': 'foo'})
51 self.assertEqual('foo', settings.name)
52 self.assertEqual('default', settings.domain)
53 self.assertIsNone(settings.description)
54 self.assertTrue(settings.enabled)
57 settings = ProjectSettings(name='foo', domain='bar',
58 description='foobar', enabled=False)
59 self.assertEqual('foo', settings.name)
60 self.assertEqual('bar', settings.domain)
61 self.assertEqual('foobar', settings.description)
62 self.assertFalse(settings.enabled)
64 def test_config_all(self):
65 settings = ProjectSettings(
66 **{'name': 'foo', 'domain': 'bar', 'description': 'foobar',
68 self.assertEqual('foo', settings.name)
69 self.assertEqual('bar', settings.domain)
70 self.assertEqual('foobar', settings.description)
71 self.assertFalse(settings.enabled)
74 class CreateProjectSuccessTests(OSComponentTestCase):
76 Test for the CreateImage class defined in create_image.py
81 Instantiates the CreateImage object that is responsible for downloading
82 and creating an OS image file within OpenStack
84 guid = str(uuid.uuid4())[:-19]
85 guid = self.__class__.__name__ + '-' + guid
86 self.project_settings = ProjectSettings(name=guid + '-name')
88 self.keystone = keystone_utils.keystone_client(self.os_creds)
90 # Initialize for cleanup
91 self.project_creator = None
95 Cleans the image and downloaded image file
97 if self.project_creator:
98 self.project_creator.clean()
100 def test_create_project(self):
102 Tests the creation of an OpenStack project.
104 self.project_creator = OpenStackProject(self.os_creds,
105 self.project_settings)
106 created_project = self.project_creator.create()
107 self.assertIsNotNone(created_project)
109 retrieved_project = keystone_utils.get_project(
110 keystone=self.keystone, project_name=self.project_settings.name)
111 self.assertIsNotNone(retrieved_project)
112 self.assertEqual(created_project, retrieved_project)
114 def test_create_project_2x(self):
116 Tests the creation of an OpenStack project twice to ensure it only
119 self.project_creator = OpenStackProject(self.os_creds,
120 self.project_settings)
121 created_project = self.project_creator.create()
122 self.assertIsNotNone(created_project)
124 retrieved_project = keystone_utils.get_project(
125 keystone=self.keystone, project_name=self.project_settings.name)
126 self.assertIsNotNone(retrieved_project)
127 self.assertEqual(created_project, retrieved_project)
129 project2 = OpenStackProject(self.os_creds,
130 self.project_settings).create()
131 self.assertEqual(retrieved_project, project2)
133 def test_create_delete_project(self):
135 Tests the creation of an OpenStack project, it's deletion, then cleanup
138 self.project_creator = OpenStackProject(self.os_creds,
139 self.project_settings)
140 created_project = self.project_creator.create()
141 self.assertIsNotNone(created_project)
143 keystone_utils.delete_project(self.keystone, created_project)
145 self.project_creator.clean()
147 self.assertIsNone(self.project_creator.get_project())
149 # TODO - Expand tests
152 class CreateProjectUserTests(OSComponentTestCase):
154 Test for the CreateImage class defined in create_image.py
159 Instantiates the CreateImage object that is responsible for downloading
160 and creating an OS image file within OpenStack
162 guid = str(uuid.uuid4())[:-19]
163 self.guid = self.__class__.__name__ + '-' + guid
164 self.project_settings = ProjectSettings(name=self.guid + '-name')
166 self.keystone = keystone_utils.keystone_client(self.os_creds)
168 # Initialize for cleanup
169 self.project_creator = None
170 self.user_creators = list()
172 self.sec_grp_creators = list()
176 Cleans the image and downloaded image file
178 for sec_grp_creator in self.sec_grp_creators:
179 sec_grp_creator.clean()
181 for user_creator in self.user_creators:
184 if self.project_creator:
185 self.project_creator.clean()
187 def test_create_project_sec_grp_one_user(self):
189 Tests the creation of an OpenStack object to a project with a new users
190 and to create a security group
192 self.project_creator = OpenStackProject(self.os_creds,
193 self.project_settings)
194 created_project = self.project_creator.create()
195 self.assertIsNotNone(created_project)
197 user_creator = OpenStackUser(self.os_creds,
198 UserSettings(name=self.guid + '-user',
200 self.project_creator.assoc_user(user_creator.create())
201 self.user_creators.append(user_creator)
203 sec_grp_os_creds = user_creator.get_os_creds(
204 self.project_creator.get_project().name)
205 sec_grp_creator = OpenStackSecurityGroup(
206 sec_grp_os_creds, SecurityGroupSettings(name=self.guid + '-name',
207 description='hello group'))
208 sec_grp = sec_grp_creator.create()
209 self.assertIsNotNone(sec_grp)
210 self.sec_grp_creators.append(sec_grp_creator)
212 self.assertEqual(self.project_creator.get_project().id,
215 def test_create_project_sec_grp_two_users(self):
217 Tests the creation of an OpenStack object to a project with two new
218 users and use each user to create a security group
220 self.project_creator = OpenStackProject(self.os_creds,
221 self.project_settings)
222 created_project = self.project_creator.create()
223 self.assertIsNotNone(created_project)
225 user_creator_1 = OpenStackUser(self.os_creds,
226 UserSettings(name=self.guid + '-user1',
228 self.project_creator.assoc_user(user_creator_1.create())
229 self.user_creators.append(user_creator_1)
231 user_creator_2 = OpenStackUser(self.os_creds,
232 UserSettings(name=self.guid + '-user2',
234 self.project_creator.assoc_user(user_creator_2.create())
235 self.user_creators.append(user_creator_2)
238 for user_creator in self.user_creators:
240 sec_grp_os_creds = user_creator.get_os_creds(
241 self.project_creator.get_project().name)
243 sec_grp_creator = OpenStackSecurityGroup(
245 SecurityGroupSettings(name=self.guid + '-name',
246 description='hello group'))
247 sec_grp = sec_grp_creator.create()
248 self.assertIsNotNone(sec_grp)
249 self.sec_grp_creators.append(sec_grp_creator)
251 self.assertEqual(self.project_creator.get_project().id,