Created domain classes for security groups.
[snaps.git] / snaps / openstack / tests / create_project_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.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
25
26 __author__ = 'spisarski'
27
28
29 class ProjectSettingsUnitTests(unittest.TestCase):
30     """
31     Tests the construction of the ProjectSettings class
32     """
33
34     def test_no_params(self):
35         with self.assertRaises(Exception):
36             ProjectSettings()
37
38     def test_empty_config(self):
39         with self.assertRaises(Exception):
40             ProjectSettings(**dict())
41
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)
48
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)
55
56     def test_all(self):
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)
63
64     def test_config_all(self):
65         settings = ProjectSettings(
66             **{'name': 'foo', 'domain': 'bar', 'description': 'foobar',
67                'enabled': False})
68         self.assertEqual('foo', settings.name)
69         self.assertEqual('bar', settings.domain)
70         self.assertEqual('foobar', settings.description)
71         self.assertFalse(settings.enabled)
72
73
74 class CreateProjectSuccessTests(OSComponentTestCase):
75     """
76     Test for the CreateImage class defined in create_image.py
77     """
78
79     def setUp(self):
80         """
81         Instantiates the CreateImage object that is responsible for downloading
82         and creating an OS image file within OpenStack
83         """
84         guid = str(uuid.uuid4())[:-19]
85         guid = self.__class__.__name__ + '-' + guid
86         self.project_settings = ProjectSettings(name=guid + '-name')
87
88         self.keystone = keystone_utils.keystone_client(self.os_creds)
89
90         # Initialize for cleanup
91         self.project_creator = None
92
93     def tearDown(self):
94         """
95         Cleans the image and downloaded image file
96         """
97         if self.project_creator:
98             self.project_creator.clean()
99
100     def test_create_project(self):
101         """
102         Tests the creation of an OpenStack project.
103         """
104         self.project_creator = OpenStackProject(self.os_creds,
105                                                 self.project_settings)
106         created_project = self.project_creator.create()
107         self.assertIsNotNone(created_project)
108
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)
113
114     def test_create_project_2x(self):
115         """
116         Tests the creation of an OpenStack project twice to ensure it only
117         creates one.
118         """
119         self.project_creator = OpenStackProject(self.os_creds,
120                                                 self.project_settings)
121         created_project = self.project_creator.create()
122         self.assertIsNotNone(created_project)
123
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)
128
129         project2 = OpenStackProject(self.os_creds,
130                                     self.project_settings).create()
131         self.assertEqual(retrieved_project, project2)
132
133     def test_create_delete_project(self):
134         """
135         Tests the creation of an OpenStack project, it's deletion, then cleanup
136         """
137         # Create Image
138         self.project_creator = OpenStackProject(self.os_creds,
139                                                 self.project_settings)
140         created_project = self.project_creator.create()
141         self.assertIsNotNone(created_project)
142
143         keystone_utils.delete_project(self.keystone, created_project)
144
145         self.project_creator.clean()
146
147         self.assertIsNone(self.project_creator.get_project())
148
149         # TODO - Expand tests
150
151
152 class CreateProjectUserTests(OSComponentTestCase):
153     """
154     Test for the CreateImage class defined in create_image.py
155     """
156
157     def setUp(self):
158         """
159         Instantiates the CreateImage object that is responsible for downloading
160         and creating an OS image file within OpenStack
161         """
162         guid = str(uuid.uuid4())[:-19]
163         self.guid = self.__class__.__name__ + '-' + guid
164         self.project_settings = ProjectSettings(name=self.guid + '-name')
165
166         self.keystone = keystone_utils.keystone_client(self.os_creds)
167
168         # Initialize for cleanup
169         self.project_creator = None
170         self.user_creators = list()
171
172         self.sec_grp_creators = list()
173
174     def tearDown(self):
175         """
176         Cleans the image and downloaded image file
177         """
178         for sec_grp_creator in self.sec_grp_creators:
179             sec_grp_creator.clean()
180
181         for user_creator in self.user_creators:
182             user_creator.clean()
183
184         if self.project_creator:
185             self.project_creator.clean()
186
187     def test_create_project_sec_grp_one_user(self):
188         """
189         Tests the creation of an OpenStack object to a project with a new users
190         and to create a security group
191         """
192         self.project_creator = OpenStackProject(self.os_creds,
193                                                 self.project_settings)
194         created_project = self.project_creator.create()
195         self.assertIsNotNone(created_project)
196
197         user_creator = OpenStackUser(self.os_creds,
198                                      UserSettings(name=self.guid + '-user',
199                                                   password=self.guid))
200         self.project_creator.assoc_user(user_creator.create())
201         self.user_creators.append(user_creator)
202
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)
211
212         self.assertEqual(self.project_creator.get_project().id,
213                          sec_grp.project_id)
214
215     def test_create_project_sec_grp_two_users(self):
216         """
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
219         """
220         self.project_creator = OpenStackProject(self.os_creds,
221                                                 self.project_settings)
222         created_project = self.project_creator.create()
223         self.assertIsNotNone(created_project)
224
225         user_creator_1 = OpenStackUser(self.os_creds,
226                                        UserSettings(name=self.guid + '-user1',
227                                                     password=self.guid))
228         self.project_creator.assoc_user(user_creator_1.create())
229         self.user_creators.append(user_creator_1)
230
231         user_creator_2 = OpenStackUser(self.os_creds,
232                                        UserSettings(name=self.guid + '-user2',
233                                                     password=self.guid))
234         self.project_creator.assoc_user(user_creator_2.create())
235         self.user_creators.append(user_creator_2)
236
237         ctr = 0
238         for user_creator in self.user_creators:
239             ctr += 1
240             sec_grp_os_creds = user_creator.get_os_creds(
241                 self.project_creator.get_project().name)
242
243             sec_grp_creator = OpenStackSecurityGroup(
244                 sec_grp_os_creds,
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)
250
251             self.assertEqual(self.project_creator.get_project().id,
252                              sec_grp.project_id)