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.
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
26 __author__ = 'spisarski'
29 class KeypairSettingsUnitTests(unittest.TestCase):
31 Tests the construction of the KeypairSettings class
34 def test_no_params(self):
35 with self.assertRaises(KeypairSettingsError):
38 def test_empty_config(self):
39 with self.assertRaises(KeypairSettingsError):
40 KeypairSettings(**dict())
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
168 class CreateKeypairsTests(OSIntegrationTestCase):
170 Tests for the OpenStackKeypair class
174 super(self.__class__, self).__start__()
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
182 self.keypair_creator = None
186 Cleanup of created keypair
188 if self.keypair_creator:
189 self.keypair_creator.clean()
192 os.remove(self.pub_file_path)
197 os.remove(self.priv_file_path)
201 super(self.__class__, self).__clean__()
203 def test_create_keypair_only(self):
205 Tests the creation of a generated keypair without saving to file
208 self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(
209 name=self.keypair_name))
210 self.keypair_creator.create()
212 keypair = nova_utils.keypair_exists(self.nova,
213 self.keypair_creator.get_keypair())
214 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
216 def test_create_delete_keypair(self):
218 Tests the creation then deletion of an OpenStack keypair to ensure
219 clean() does not raise an Exception.
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)
227 # Delete Image manually
228 nova_utils.delete_keypair(self.nova, created_keypair)
231 nova_utils.get_keypair_by_name(self.nova, self.keypair_name))
233 # Must not throw an exception when attempting to cleanup non-existent
235 self.keypair_creator.clean()
236 self.assertIsNone(self.keypair_creator.get_keypair())
238 def test_create_keypair_save_pub_only(self):
240 Tests the creation of a generated keypair and saves the public key only
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()
248 keypair = nova_utils.keypair_exists(self.nova,
249 self.keypair_creator.get_keypair())
250 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
252 file_key = open(os.path.expanduser(self.pub_file_path)).read()
253 self.assertEqual(self.keypair_creator.get_keypair().public_key,
256 def test_create_keypair_save_both(self):
258 Tests the creation of a generated keypair and saves both private and
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()
268 keypair = nova_utils.keypair_exists(self.nova,
269 self.keypair_creator.get_keypair())
270 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
272 file_key = open(os.path.expanduser(self.pub_file_path)).read()
273 self.assertEqual(self.keypair_creator.get_keypair().public_key,
276 self.assertTrue(os.path.isfile(self.priv_file_path))
278 def test_create_keypair_from_file(self):
280 Tests the creation of an existing public keypair from a file
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()
291 keypair = nova_utils.keypair_exists(self.nova,
292 self.keypair_creator.get_keypair())
293 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
295 file_key = open(os.path.expanduser(self.pub_file_path)).read()
296 self.assertEqual(self.keypair_creator.get_keypair().public_key,
300 class CreateKeypairsCleanupTests(OSIntegrationTestCase):
302 Tests for the OpenStackKeypair#clean method to ensure key files are deleted
307 super(self.__class__, self).__start__()
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
315 self.keypair_creator = None
319 Cleanup of created keypair
321 if self.keypair_creator:
322 self.keypair_creator.clean()
325 os.remove(self.pub_file_path)
330 os.remove(self.priv_file_path)
334 super(self.__class__, self).__clean__()
336 def test_create_keypair_gen_files_delete_1(self):
338 Tests the creation of a generated keypair and ensures that the files
339 are deleted on clean()
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()
349 self.assertFalse(file_utils.file_exists(self.pub_file_path))
350 self.assertFalse(file_utils.file_exists(self.priv_file_path))
352 def test_create_keypair_gen_files_delete_2(self):
354 Tests the creation of a generated keypair and ensures that the files
355 are deleted on clean()
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()
365 self.assertFalse(file_utils.file_exists(self.pub_file_path))
366 self.assertFalse(file_utils.file_exists(self.priv_file_path))
368 def test_create_keypair_gen_files_keep(self):
370 Tests the creation of a generated keypair and ensures that the files
371 are not deleted on clean()
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()
381 self.assertTrue(file_utils.file_exists(self.pub_file_path))
382 self.assertTrue(file_utils.file_exists(self.priv_file_path))
384 def test_create_keypair_exist_files_keep(self):
386 Tests the creation of an existing public keypair and ensures the files
387 are not deleted on clean
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()
401 self.assertTrue(file_utils.file_exists(self.pub_file_path))
402 self.assertTrue(file_utils.file_exists(self.priv_file_path))
404 def test_create_keypair_exist_files_delete(self):
406 Tests the creation of an existing public keypair and ensures the files
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()
421 self.assertFalse(file_utils.file_exists(self.pub_file_path))
422 self.assertFalse(file_utils.file_exists(self.priv_file_path))