Removed TODO from comment about the physical network value.
[snaps.git] / snaps / openstack / tests / create_keypairs_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 import os
19
20 from snaps import file_utils
21 from snaps.openstack.create_keypairs import (
22     KeypairSettings, OpenStackKeypair, KeypairSettingsError)
23 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
24 from snaps.openstack.utils import nova_utils
25
26 __author__ = 'spisarski'
27
28
29 class KeypairSettingsUnitTests(unittest.TestCase):
30     """
31     Tests the construction of the KeypairSettings class
32     """
33
34     def test_no_params(self):
35         with self.assertRaises(KeypairSettingsError):
36             KeypairSettings()
37
38     def test_empty_config(self):
39         with self.assertRaises(KeypairSettingsError):
40             KeypairSettings(**dict())
41
42     def test_name_only(self):
43         settings = KeypairSettings(name='foo')
44         self.assertEqual('foo', settings.name)
45         self.assertIsNone(settings.public_filepath)
46         self.assertIsNone(settings.private_filepath)
47         self.assertIsNone(settings.delete_on_clean)
48
49     def test_config_with_name_only(self):
50         settings = KeypairSettings(**{'name': 'foo'})
51         self.assertEqual('foo', settings.name)
52         self.assertIsNone(settings.public_filepath)
53         self.assertIsNone(settings.private_filepath)
54         self.assertIsNone(settings.delete_on_clean)
55
56     def test_name_pub_only(self):
57         settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub')
58         self.assertEqual('foo', settings.name)
59         self.assertEqual('/foo/bar.pub', settings.public_filepath)
60         self.assertIsNone(settings.private_filepath)
61         self.assertIsNone(settings.delete_on_clean)
62
63     def test_config_with_name_pub_only(self):
64         settings = KeypairSettings(
65             **{'name': 'foo', 'public_filepath': '/foo/bar.pub'})
66         self.assertEqual('foo', settings.name)
67         self.assertEqual('/foo/bar.pub', settings.public_filepath)
68         self.assertIsNone(settings.private_filepath)
69         self.assertIsNone(settings.delete_on_clean)
70
71     def test_name_priv_only(self):
72         settings = KeypairSettings(name='foo', private_filepath='/foo/bar')
73         self.assertEqual('foo', settings.name)
74         self.assertIsNone(settings.public_filepath)
75         self.assertEqual('/foo/bar', settings.private_filepath)
76         self.assertIsNone(settings.delete_on_clean)
77
78     def test_config_with_name_priv_only(self):
79         settings = KeypairSettings(
80             **{'name': 'foo', 'private_filepath': '/foo/bar'})
81         self.assertEqual('foo', settings.name)
82         self.assertIsNone(settings.public_filepath)
83         self.assertEqual('/foo/bar', settings.private_filepath)
84         self.assertIsNone(settings.delete_on_clean)
85
86     def test_all_delete_bool(self):
87         settings = KeypairSettings(
88             name='foo', public_filepath='/foo/bar.pub',
89             private_filepath='/foo/bar', delete_on_clean=True)
90         self.assertEqual('foo', settings.name)
91         self.assertEqual('/foo/bar.pub', settings.public_filepath)
92         self.assertEqual('/foo/bar', settings.private_filepath)
93         self.assertTrue(settings.delete_on_clean)
94
95     def test_all_delete_str_true_cap(self):
96         settings = KeypairSettings(
97             name='foo', public_filepath='/foo/bar.pub',
98             private_filepath='/foo/bar', delete_on_clean='True')
99         self.assertEqual('foo', settings.name)
100         self.assertEqual('/foo/bar.pub', settings.public_filepath)
101         self.assertEqual('/foo/bar', settings.private_filepath)
102         self.assertTrue(settings.delete_on_clean)
103
104     def test_all_delete_str_true_lc(self):
105         settings = KeypairSettings(
106             name='foo', public_filepath='/foo/bar.pub',
107             private_filepath='/foo/bar', delete_on_clean='true')
108         self.assertEqual('foo', settings.name)
109         self.assertEqual('/foo/bar.pub', settings.public_filepath)
110         self.assertEqual('/foo/bar', settings.private_filepath)
111         self.assertTrue(settings.delete_on_clean)
112
113     def test_all_delete_str_false_cap(self):
114         settings = KeypairSettings(
115             name='foo', public_filepath='/foo/bar.pub',
116             private_filepath='/foo/bar', delete_on_clean='False')
117         self.assertEqual('foo', settings.name)
118         self.assertEqual('/foo/bar.pub', settings.public_filepath)
119         self.assertEqual('/foo/bar', settings.private_filepath)
120         self.assertFalse(settings.delete_on_clean)
121
122     def test_all_delete_str_false_lc(self):
123         settings = KeypairSettings(
124             name='foo', public_filepath='/foo/bar.pub',
125             private_filepath='/foo/bar', delete_on_clean='false')
126         self.assertEqual('foo', settings.name)
127         self.assertEqual('/foo/bar.pub', settings.public_filepath)
128         self.assertEqual('/foo/bar', settings.private_filepath)
129         self.assertFalse(settings.delete_on_clean)
130
131     def test_config_all_delete_false_bool(self):
132         settings = KeypairSettings(
133             **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
134                'private_filepath': '/foo/bar', 'delete_on_clean': False})
135         self.assertEqual('foo', settings.name)
136         self.assertEqual('/foo/bar.pub', settings.public_filepath)
137         self.assertEqual('/foo/bar', settings.private_filepath)
138         self.assertFalse(settings.delete_on_clean)
139
140     def test_config_all_delete_false_str_cap(self):
141         settings = KeypairSettings(
142             **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
143                'private_filepath': '/foo/bar', 'delete_on_clean': 'False'})
144         self.assertEqual('foo', settings.name)
145         self.assertEqual('/foo/bar.pub', settings.public_filepath)
146         self.assertEqual('/foo/bar', settings.private_filepath)
147         self.assertFalse(settings.delete_on_clean)
148
149     def test_config_all_delete_false_str_lc(self):
150         settings = KeypairSettings(
151             **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
152                'private_filepath': '/foo/bar', 'delete_on_clean': 'false'})
153         self.assertEqual('foo', settings.name)
154         self.assertEqual('/foo/bar.pub', settings.public_filepath)
155         self.assertEqual('/foo/bar', settings.private_filepath)
156         self.assertFalse(settings.delete_on_clean)
157
158     def test_config_all_delete_false_str_foo(self):
159         settings = KeypairSettings(
160             **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
161                'private_filepath': '/foo/bar', 'delete_on_clean': 'foo'})
162         self.assertEqual('foo', settings.name)
163         self.assertEqual('/foo/bar.pub', settings.public_filepath)
164         self.assertEqual('/foo/bar', settings.private_filepath)
165         self.assertFalse(settings.delete_on_clean)
166
167
168 class CreateKeypairsTests(OSIntegrationTestCase):
169     """
170     Tests for the OpenStackKeypair class
171     """
172
173     def setUp(self):
174         super(self.__class__, self).__start__()
175
176         guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
177         self.priv_file_path = 'tmp/' + guid
178         self.pub_file_path = self.priv_file_path + '.pub'
179         self.nova = nova_utils.nova_client(self.os_creds)
180         self.keypair_name = guid
181
182         self.keypair_creator = None
183
184     def tearDown(self):
185         """
186         Cleanup of created keypair
187         """
188         if self.keypair_creator:
189             self.keypair_creator.clean()
190
191         try:
192             os.remove(self.pub_file_path)
193         except:
194             pass
195
196         try:
197             os.remove(self.priv_file_path)
198         except:
199             pass
200
201         super(self.__class__, self).__clean__()
202
203     def test_create_keypair_only(self):
204         """
205         Tests the creation of a generated keypair without saving to file
206         :return:
207         """
208         self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(
209             name=self.keypair_name))
210         self.keypair_creator.create()
211
212         keypair = nova_utils.keypair_exists(self.nova,
213                                             self.keypair_creator.get_keypair())
214         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
215
216     def test_create_delete_keypair(self):
217         """
218         Tests the creation then deletion of an OpenStack keypair to ensure
219         clean() does not raise an Exception.
220         """
221         # Create Image
222         self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(
223             name=self.keypair_name))
224         created_keypair = self.keypair_creator.create()
225         self.assertIsNotNone(created_keypair)
226
227         # Delete Image manually
228         nova_utils.delete_keypair(self.nova, created_keypair)
229
230         self.assertIsNone(
231             nova_utils.get_keypair_by_name(self.nova, self.keypair_name))
232
233         # Must not throw an exception when attempting to cleanup non-existent
234         # image
235         self.keypair_creator.clean()
236         self.assertIsNone(self.keypair_creator.get_keypair())
237
238     def test_create_keypair_save_pub_only(self):
239         """
240         Tests the creation of a generated keypair and saves the public key only
241         :return:
242         """
243         self.keypair_creator = OpenStackKeypair(
244             self.os_creds, KeypairSettings(name=self.keypair_name,
245                                            public_filepath=self.pub_file_path))
246         self.keypair_creator.create()
247
248         keypair = nova_utils.keypair_exists(self.nova,
249                                             self.keypair_creator.get_keypair())
250         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
251
252         file_key = open(os.path.expanduser(self.pub_file_path)).read()
253         self.assertEqual(self.keypair_creator.get_keypair().public_key,
254                          file_key)
255
256     def test_create_keypair_save_both(self):
257         """
258         Tests the creation of a generated keypair and saves both private and
259         public key files[
260         :return:
261         """
262         self.keypair_creator = OpenStackKeypair(
263             self.os_creds, KeypairSettings(
264                 name=self.keypair_name, public_filepath=self.pub_file_path,
265                 private_filepath=self.priv_file_path))
266         self.keypair_creator.create()
267
268         keypair = nova_utils.keypair_exists(self.nova,
269                                             self.keypair_creator.get_keypair())
270         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
271
272         file_key = open(os.path.expanduser(self.pub_file_path)).read()
273         self.assertEqual(self.keypair_creator.get_keypair().public_key,
274                          file_key)
275
276         self.assertTrue(os.path.isfile(self.priv_file_path))
277
278     def test_create_keypair_from_file(self):
279         """
280         Tests the creation of an existing public keypair from a file
281         :return:
282         """
283         keys = nova_utils.create_keys()
284         nova_utils.save_keys_to_files(keys=keys,
285                                       pub_file_path=self.pub_file_path)
286         self.keypair_creator = OpenStackKeypair(
287             self.os_creds, KeypairSettings(name=self.keypair_name,
288                                            public_filepath=self.pub_file_path))
289         self.keypair_creator.create()
290
291         keypair = nova_utils.keypair_exists(self.nova,
292                                             self.keypair_creator.get_keypair())
293         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
294
295         file_key = open(os.path.expanduser(self.pub_file_path)).read()
296         self.assertEqual(self.keypair_creator.get_keypair().public_key,
297                          file_key)
298
299
300 class CreateKeypairsCleanupTests(OSIntegrationTestCase):
301     """
302     Tests for the OpenStackKeypair#clean method to ensure key files are deleted
303     when required
304     """
305
306     def setUp(self):
307         super(self.__class__, self).__start__()
308
309         guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
310         self.priv_file_path = 'tmp/' + guid
311         self.pub_file_path = self.priv_file_path + '.pub'
312         self.nova = nova_utils.nova_client(self.os_creds)
313         self.keypair_name = guid
314
315         self.keypair_creator = None
316
317     def tearDown(self):
318         """
319         Cleanup of created keypair
320         """
321         if self.keypair_creator:
322             self.keypair_creator.clean()
323
324         try:
325             os.remove(self.pub_file_path)
326         except:
327             pass
328
329         try:
330             os.remove(self.priv_file_path)
331         except:
332             pass
333
334         super(self.__class__, self).__clean__()
335
336     def test_create_keypair_gen_files_delete_1(self):
337         """
338         Tests the creation of a generated keypair and ensures that the files
339         are deleted on clean()
340         :return:
341         """
342         self.keypair_creator = OpenStackKeypair(
343             self.os_creds, KeypairSettings(
344                 name=self.keypair_name, public_filepath=self.pub_file_path,
345                 private_filepath=self.priv_file_path))
346         self.keypair_creator.create()
347         self.keypair_creator.clean()
348
349         self.assertFalse(file_utils.file_exists(self.pub_file_path))
350         self.assertFalse(file_utils.file_exists(self.priv_file_path))
351
352     def test_create_keypair_gen_files_delete_2(self):
353         """
354         Tests the creation of a generated keypair and ensures that the files
355         are deleted on clean()
356         :return:
357         """
358         self.keypair_creator = OpenStackKeypair(
359             self.os_creds, KeypairSettings(
360                 name=self.keypair_name, public_filepath=self.pub_file_path,
361                 private_filepath=self.priv_file_path, delete_on_clean=True))
362         self.keypair_creator.create()
363         self.keypair_creator.clean()
364
365         self.assertFalse(file_utils.file_exists(self.pub_file_path))
366         self.assertFalse(file_utils.file_exists(self.priv_file_path))
367
368     def test_create_keypair_gen_files_keep(self):
369         """
370         Tests the creation of a generated keypair and ensures that the files
371         are not deleted on clean()
372         :return:
373         """
374         self.keypair_creator = OpenStackKeypair(
375             self.os_creds, KeypairSettings(
376                 name=self.keypair_name, public_filepath=self.pub_file_path,
377                 private_filepath=self.priv_file_path, delete_on_clean=False))
378         self.keypair_creator.create()
379         self.keypair_creator.clean()
380
381         self.assertTrue(file_utils.file_exists(self.pub_file_path))
382         self.assertTrue(file_utils.file_exists(self.priv_file_path))
383
384     def test_create_keypair_exist_files_keep(self):
385         """
386         Tests the creation of an existing public keypair and ensures the files
387         are not deleted on clean
388         :return:
389         """
390         keys = nova_utils.create_keys()
391         nova_utils.save_keys_to_files(
392             keys=keys, pub_file_path=self.pub_file_path,
393             priv_file_path=self.priv_file_path)
394         self.keypair_creator = OpenStackKeypair(
395             self.os_creds, KeypairSettings(
396                 name=self.keypair_name, public_filepath=self.pub_file_path,
397                 private_filepath=self.priv_file_path, delete_on_clean=False))
398         self.keypair_creator.create()
399         self.keypair_creator.clean()
400
401         self.assertTrue(file_utils.file_exists(self.pub_file_path))
402         self.assertTrue(file_utils.file_exists(self.priv_file_path))
403
404     def test_create_keypair_exist_files_delete(self):
405         """
406         Tests the creation of an existing public keypair and ensures the files
407         are deleted on clean
408         :return:
409         """
410         keys = nova_utils.create_keys()
411         nova_utils.save_keys_to_files(
412             keys=keys, pub_file_path=self.pub_file_path,
413             priv_file_path=self.priv_file_path)
414         self.keypair_creator = OpenStackKeypair(
415             self.os_creds, KeypairSettings(
416                 name=self.keypair_name, public_filepath=self.pub_file_path,
417                 private_filepath=self.priv_file_path, delete_on_clean=True))
418         self.keypair_creator.create()
419         self.keypair_creator.clean()
420
421         self.assertFalse(file_utils.file_exists(self.pub_file_path))
422         self.assertFalse(file_utils.file_exists(self.priv_file_path))