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.
19 from snaps.openstack.create_keypairs import KeypairSettings, OpenStackKeypair
20 from snaps.openstack.utils import nova_utils
21 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
23 __author__ = 'spisarski'
26 class KeypairSettingsUnitTests(unittest.TestCase):
28 Tests the construction of the KeypairSettings class
31 def test_no_params(self):
32 with self.assertRaises(Exception):
35 def test_empty_config(self):
36 with self.assertRaises(Exception):
37 KeypairSettings(config=dict())
39 def test_name_only(self):
40 settings = KeypairSettings(name='foo')
41 self.assertEqual('foo', settings.name)
42 self.assertIsNone(settings.public_filepath)
43 self.assertIsNone(settings.private_filepath)
45 def test_config_with_name_only(self):
46 settings = KeypairSettings(config={'name': 'foo'})
47 self.assertEqual('foo', settings.name)
48 self.assertIsNone(settings.public_filepath)
49 self.assertIsNone(settings.private_filepath)
51 def test_name_pub_only(self):
52 settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub')
53 self.assertEqual('foo', settings.name)
54 self.assertEqual('/foo/bar.pub', settings.public_filepath)
55 self.assertIsNone(settings.private_filepath)
57 def test_config_with_name_pub_only(self):
58 settings = KeypairSettings(config={'name': 'foo', 'public_filepath': '/foo/bar.pub'})
59 self.assertEqual('foo', settings.name)
60 self.assertEqual('/foo/bar.pub', settings.public_filepath)
61 self.assertIsNone(settings.private_filepath)
63 def test_name_priv_only(self):
64 settings = KeypairSettings(name='foo', private_filepath='/foo/bar')
65 self.assertEqual('foo', settings.name)
66 self.assertIsNone(settings.public_filepath)
67 self.assertEqual('/foo/bar', settings.private_filepath)
69 def test_config_with_name_priv_only(self):
70 settings = KeypairSettings(config={'name': 'foo', 'private_filepath': '/foo/bar'})
71 self.assertEqual('foo', settings.name)
72 self.assertIsNone(settings.public_filepath)
73 self.assertEqual('/foo/bar', settings.private_filepath)
76 settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub', private_filepath='/foo/bar')
77 self.assertEqual('foo', settings.name)
78 self.assertEqual('/foo/bar.pub', settings.public_filepath)
79 self.assertEqual('/foo/bar', settings.private_filepath)
81 def test_config_all(self):
82 settings = KeypairSettings(config={'name': 'foo', 'public_filepath': '/foo/bar.pub',
83 'private_filepath': '/foo/bar'})
84 self.assertEqual('foo', settings.name)
85 self.assertEqual('/foo/bar.pub', settings.public_filepath)
86 self.assertEqual('/foo/bar', settings.private_filepath)
89 class CreateKeypairsTests(OSIntegrationTestCase):
91 Tests for the OpenStackKeypair class
95 super(self.__class__, self).__start__()
97 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
98 self.priv_file_path = 'tmp/' + guid
99 self.pub_file_path = self.priv_file_path + '.pub'
100 self.nova = nova_utils.nova_client(self.os_creds)
101 self.keypair_name = guid
103 self.keypair_creator = None
107 Cleanup of created keypair
109 if self.keypair_creator:
110 self.keypair_creator.clean()
113 os.remove(self.pub_file_path)
118 os.remove(self.priv_file_path)
122 super(self.__class__, self).__clean__()
124 def test_create_keypair_only(self):
126 Tests the creation of a generated keypair without saving to file
129 self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(name=self.keypair_name))
130 self.keypair_creator.create()
132 keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
133 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
135 def test_create_delete_keypair(self):
137 Tests the creation then deletion of an OpenStack keypair to ensure clean() does not raise an Exception.
140 self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(name=self.keypair_name))
141 created_keypair = self.keypair_creator.create()
142 self.assertIsNotNone(created_keypair)
144 # Delete Image manually
145 nova_utils.delete_keypair(self.nova, created_keypair)
147 self.assertIsNone(nova_utils.get_keypair_by_name(self.nova, self.keypair_name))
149 # Must not throw an exception when attempting to cleanup non-existent image
150 self.keypair_creator.clean()
151 self.assertIsNone(self.keypair_creator.get_keypair())
153 def test_create_keypair_save_pub_only(self):
155 Tests the creation of a generated keypair and saves the public key only
158 self.keypair_creator = OpenStackKeypair(
159 self.os_creds, KeypairSettings(name=self.keypair_name, public_filepath=self.pub_file_path))
160 self.keypair_creator.create()
162 keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
163 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
165 file_key = open(os.path.expanduser(self.pub_file_path)).read()
166 self.assertEqual(self.keypair_creator.get_keypair().public_key, file_key)
168 def test_create_keypair_save_both(self):
170 Tests the creation of a generated keypair and saves both private and public key files[
173 self.keypair_creator = OpenStackKeypair(
174 self.os_creds, KeypairSettings(name=self.keypair_name, public_filepath=self.pub_file_path,
175 private_filepath=self.priv_file_path))
176 self.keypair_creator.create()
178 keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
179 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
181 file_key = open(os.path.expanduser(self.pub_file_path)).read()
182 self.assertEqual(self.keypair_creator.get_keypair().public_key, file_key)
184 self.assertTrue(os.path.isfile(self.priv_file_path))
186 def test_create_keypair_from_file(self):
188 Tests the creation of an existing public keypair from a file
191 keys = nova_utils.create_keys()
192 nova_utils.save_keys_to_files(keys=keys, pub_file_path=self.pub_file_path)
193 self.keypair_creator = OpenStackKeypair(
194 self.os_creds, KeypairSettings(name=self.keypair_name, public_filepath=self.pub_file_path))
195 self.keypair_creator.create()
197 keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
198 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
200 file_key = open(os.path.expanduser(self.pub_file_path)).read()
201 self.assertEqual(self.keypair_creator.get_keypair().public_key, file_key)