1 # Copyright (c) 2016 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 Crypto.PublicKey import RSA
21 from snaps.openstack.create_keypairs import KeypairSettings, OpenStackKeypair
22 from snaps.openstack.utils import nova_utils
23 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
25 __author__ = 'spisarski'
28 class KeypairSettingsUnitTests(unittest.TestCase):
30 Tests the construction of the KeypairSettings class
33 def test_no_params(self):
34 with self.assertRaises(Exception):
37 def test_empty_config(self):
38 with self.assertRaises(Exception):
39 KeypairSettings(config=dict())
41 def test_name_only(self):
42 settings = KeypairSettings(name='foo')
43 self.assertEquals('foo', settings.name)
44 self.assertIsNone(settings.public_filepath)
45 self.assertIsNone(settings.private_filepath)
47 def test_config_with_name_only(self):
48 settings = KeypairSettings(config={'name': 'foo'})
49 self.assertEquals('foo', settings.name)
50 self.assertIsNone(settings.public_filepath)
51 self.assertIsNone(settings.private_filepath)
53 def test_name_pub_only(self):
54 settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub')
55 self.assertEquals('foo', settings.name)
56 self.assertEquals('/foo/bar.pub', settings.public_filepath)
57 self.assertIsNone(settings.private_filepath)
59 def test_config_with_name_pub_only(self):
60 settings = KeypairSettings(config={'name': 'foo', 'public_filepath': '/foo/bar.pub'})
61 self.assertEquals('foo', settings.name)
62 self.assertEquals('/foo/bar.pub', settings.public_filepath)
63 self.assertIsNone(settings.private_filepath)
65 def test_name_priv_only(self):
66 settings = KeypairSettings(name='foo', private_filepath='/foo/bar')
67 self.assertEquals('foo', settings.name)
68 self.assertIsNone(settings.public_filepath)
69 self.assertEquals('/foo/bar', settings.private_filepath)
71 def test_config_with_name_priv_only(self):
72 settings = KeypairSettings(config={'name': 'foo', 'private_filepath': '/foo/bar'})
73 self.assertEquals('foo', settings.name)
74 self.assertIsNone(settings.public_filepath)
75 self.assertEquals('/foo/bar', settings.private_filepath)
78 settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub', private_filepath='/foo/bar')
79 self.assertEquals('foo', settings.name)
80 self.assertEquals('/foo/bar.pub', settings.public_filepath)
81 self.assertEquals('/foo/bar', settings.private_filepath)
83 def test_config_all(self):
84 settings = KeypairSettings(config={'name': 'foo', 'public_filepath': '/foo/bar.pub',
85 'private_filepath': '/foo/bar'})
86 self.assertEquals('foo', settings.name)
87 self.assertEquals('/foo/bar.pub', settings.public_filepath)
88 self.assertEquals('/foo/bar', settings.private_filepath)
91 class CreateKeypairsTests(OSIntegrationTestCase):
93 Tests for the OpenStackKeypair class
97 super(self.__class__, self).__start__()
99 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
100 self.priv_file_path = 'tmp/' + guid
101 self.pub_file_path = self.priv_file_path + '.pub'
102 self.nova = nova_utils.nova_client(self.os_creds)
103 self.keypair_name = guid
105 self.keypair_creator = None
109 Cleanup of created keypair
111 if self.keypair_creator:
112 self.keypair_creator.clean()
115 os.remove(self.pub_file_path)
120 os.remove(self.priv_file_path)
124 super(self.__class__, self).__clean__()
126 def test_create_keypair_only(self):
128 Tests the creation of a generated keypair without saving to file
131 self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(name=self.keypair_name))
132 self.keypair_creator.create()
134 keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
135 self.assertEquals(self.keypair_creator.get_keypair(), keypair)
137 def test_create_delete_keypair(self):
139 Tests the creation then deletion of an OpenStack keypair to ensure clean() does not raise an Exception.
142 self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(name=self.keypair_name))
143 created_keypair = self.keypair_creator.create()
144 self.assertIsNotNone(created_keypair)
146 # Delete Image manually
147 nova_utils.delete_keypair(self.nova, created_keypair)
149 self.assertIsNone(nova_utils.get_keypair_by_name(self.nova, self.keypair_name))
151 # Must not throw an exception when attempting to cleanup non-existent image
152 self.keypair_creator.clean()
153 self.assertIsNone(self.keypair_creator.get_keypair())
155 def test_create_keypair_save_pub_only(self):
157 Tests the creation of a generated keypair and saves the public key only
160 self.keypair_creator = OpenStackKeypair(
161 self.os_creds, KeypairSettings(name=self.keypair_name, public_filepath=self.pub_file_path))
162 self.keypair_creator.create()
164 keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
165 self.assertEquals(self.keypair_creator.get_keypair(), keypair)
167 file_key = open(os.path.expanduser(self.pub_file_path)).read()
168 self.assertEquals(self.keypair_creator.get_keypair().public_key, file_key)
170 def test_create_keypair_save_both(self):
172 Tests the creation of a generated keypair and saves both private and public key files[
175 self.keypair_creator = OpenStackKeypair(
176 self.os_creds, KeypairSettings(name=self.keypair_name, public_filepath=self.pub_file_path,
177 private_filepath=self.priv_file_path))
178 self.keypair_creator.create()
180 keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
181 self.assertEquals(self.keypair_creator.get_keypair(), keypair)
183 file_key = open(os.path.expanduser(self.pub_file_path)).read()
184 self.assertEquals(self.keypair_creator.get_keypair().public_key, file_key)
186 self.assertTrue(os.path.isfile(self.priv_file_path))
188 def test_create_keypair_from_file(self):
190 Tests the creation of an existing public keypair from a file
193 keys = RSA.generate(1024)
194 nova_utils.save_keys_to_files(keys=keys, pub_file_path=self.pub_file_path)
195 self.keypair_creator = OpenStackKeypair(
196 self.os_creds, KeypairSettings(name=self.keypair_name, public_filepath=self.pub_file_path))
197 self.keypair_creator.create()
199 keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
200 self.assertEquals(self.keypair_creator.get_keypair(), keypair)
202 file_key = open(os.path.expanduser(self.pub_file_path)).read()
203 self.assertEquals(self.keypair_creator.get_keypair().public_key, file_key)