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.tests.os_source_file_test import OSIntegrationTestCase
21 from snaps.openstack.utils import nova_utils
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(**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(**{'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(
59 **{'name': 'foo', 'public_filepath': '/foo/bar.pub'})
60 self.assertEqual('foo', settings.name)
61 self.assertEqual('/foo/bar.pub', settings.public_filepath)
62 self.assertIsNone(settings.private_filepath)
64 def test_name_priv_only(self):
65 settings = KeypairSettings(name='foo', private_filepath='/foo/bar')
66 self.assertEqual('foo', settings.name)
67 self.assertIsNone(settings.public_filepath)
68 self.assertEqual('/foo/bar', settings.private_filepath)
70 def test_config_with_name_priv_only(self):
71 settings = KeypairSettings(
72 **{'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)
78 settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub',
79 private_filepath='/foo/bar')
80 self.assertEqual('foo', settings.name)
81 self.assertEqual('/foo/bar.pub', settings.public_filepath)
82 self.assertEqual('/foo/bar', settings.private_filepath)
84 def test_config_all(self):
85 settings = KeypairSettings(
86 **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
87 'private_filepath': '/foo/bar'})
88 self.assertEqual('foo', settings.name)
89 self.assertEqual('/foo/bar.pub', settings.public_filepath)
90 self.assertEqual('/foo/bar', settings.private_filepath)
93 class CreateKeypairsTests(OSIntegrationTestCase):
95 Tests for the OpenStackKeypair class
99 super(self.__class__, self).__start__()
101 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
102 self.priv_file_path = 'tmp/' + guid
103 self.pub_file_path = self.priv_file_path + '.pub'
104 self.nova = nova_utils.nova_client(self.os_creds)
105 self.keypair_name = guid
107 self.keypair_creator = None
111 Cleanup of created keypair
113 if self.keypair_creator:
114 self.keypair_creator.clean()
117 os.remove(self.pub_file_path)
122 os.remove(self.priv_file_path)
126 super(self.__class__, self).__clean__()
128 def test_create_keypair_only(self):
130 Tests the creation of a generated keypair without saving to file
133 self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(
134 name=self.keypair_name))
135 self.keypair_creator.create()
137 keypair = nova_utils.keypair_exists(self.nova,
138 self.keypair_creator.get_keypair())
139 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
141 def test_create_delete_keypair(self):
143 Tests the creation then deletion of an OpenStack keypair to ensure
144 clean() does not raise an Exception.
147 self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(
148 name=self.keypair_name))
149 created_keypair = self.keypair_creator.create()
150 self.assertIsNotNone(created_keypair)
152 # Delete Image manually
153 nova_utils.delete_keypair(self.nova, created_keypair)
156 nova_utils.get_keypair_by_name(self.nova, self.keypair_name))
158 # Must not throw an exception when attempting to cleanup non-existent
160 self.keypair_creator.clean()
161 self.assertIsNone(self.keypair_creator.get_keypair())
163 def test_create_keypair_save_pub_only(self):
165 Tests the creation of a generated keypair and saves the public key only
168 self.keypair_creator = OpenStackKeypair(
169 self.os_creds, KeypairSettings(name=self.keypair_name,
170 public_filepath=self.pub_file_path))
171 self.keypair_creator.create()
173 keypair = nova_utils.keypair_exists(self.nova,
174 self.keypair_creator.get_keypair())
175 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
177 file_key = open(os.path.expanduser(self.pub_file_path)).read()
178 self.assertEqual(self.keypair_creator.get_keypair().public_key,
181 def test_create_keypair_save_both(self):
183 Tests the creation of a generated keypair and saves both private and
187 self.keypair_creator = OpenStackKeypair(
188 self.os_creds, KeypairSettings(
189 name=self.keypair_name, public_filepath=self.pub_file_path,
190 private_filepath=self.priv_file_path))
191 self.keypair_creator.create()
193 keypair = nova_utils.keypair_exists(self.nova,
194 self.keypair_creator.get_keypair())
195 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
197 file_key = open(os.path.expanduser(self.pub_file_path)).read()
198 self.assertEqual(self.keypair_creator.get_keypair().public_key,
201 self.assertTrue(os.path.isfile(self.priv_file_path))
203 def test_create_keypair_from_file(self):
205 Tests the creation of an existing public keypair from a file
208 keys = nova_utils.create_keys()
209 nova_utils.save_keys_to_files(keys=keys,
210 pub_file_path=self.pub_file_path)
211 self.keypair_creator = OpenStackKeypair(
212 self.os_creds, KeypairSettings(name=self.keypair_name,
213 public_filepath=self.pub_file_path))
214 self.keypair_creator.create()
216 keypair = nova_utils.keypair_exists(self.nova,
217 self.keypair_creator.get_keypair())
218 self.assertEqual(self.keypair_creator.get_keypair(), keypair)
220 file_key = open(os.path.expanduser(self.pub_file_path)).read()
221 self.assertEqual(self.keypair_creator.get_keypair().public_key,