b587a50afca1b9d6f9d4bf83027ffe1d69509815
[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 os
16 import uuid
17 import unittest
18
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
22
23 __author__ = 'spisarski'
24
25
26 class KeypairSettingsUnitTests(unittest.TestCase):
27     """
28     Tests the construction of the KeypairSettings class
29     """
30
31     def test_no_params(self):
32         with self.assertRaises(Exception):
33             KeypairSettings()
34
35     def test_empty_config(self):
36         with self.assertRaises(Exception):
37             KeypairSettings(config=dict())
38
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)
44
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)
50
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)
56
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)
62
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)
68
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)
74
75     def test_all(self):
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)
80
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)
87
88
89 class CreateKeypairsTests(OSIntegrationTestCase):
90     """
91     Tests for the OpenStackKeypair class
92     """
93
94     def setUp(self):
95         super(self.__class__, self).__start__()
96
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
102
103         self.keypair_creator = None
104
105     def tearDown(self):
106         """
107         Cleanup of created keypair
108         """
109         if self.keypair_creator:
110             self.keypair_creator.clean()
111
112         try:
113             os.remove(self.pub_file_path)
114         except:
115             pass
116
117         try:
118             os.remove(self.priv_file_path)
119         except:
120             pass
121
122         super(self.__class__, self).__clean__()
123
124     def test_create_keypair_only(self):
125         """
126         Tests the creation of a generated keypair without saving to file
127         :return:
128         """
129         self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(name=self.keypair_name))
130         self.keypair_creator.create()
131
132         keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
133         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
134
135     def test_create_delete_keypair(self):
136         """
137         Tests the creation then deletion of an OpenStack keypair to ensure clean() does not raise an Exception.
138         """
139         # Create Image
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)
143
144         # Delete Image manually
145         nova_utils.delete_keypair(self.nova, created_keypair)
146
147         self.assertIsNone(nova_utils.get_keypair_by_name(self.nova, self.keypair_name))
148
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())
152
153     def test_create_keypair_save_pub_only(self):
154         """
155         Tests the creation of a generated keypair and saves the public key only
156         :return:
157         """
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()
161
162         keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
163         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
164
165         file_key = open(os.path.expanduser(self.pub_file_path)).read()
166         self.assertEqual(self.keypair_creator.get_keypair().public_key, file_key)
167
168     def test_create_keypair_save_both(self):
169         """
170         Tests the creation of a generated keypair and saves both private and public key files[
171         :return:
172         """
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()
177
178         keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
179         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
180
181         file_key = open(os.path.expanduser(self.pub_file_path)).read()
182         self.assertEqual(self.keypair_creator.get_keypair().public_key, file_key)
183
184         self.assertTrue(os.path.isfile(self.priv_file_path))
185
186     def test_create_keypair_from_file(self):
187         """
188         Tests the creation of an existing public keypair from a file
189         :return:
190         """
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()
196
197         keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
198         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
199
200         file_key = open(os.path.expanduser(self.pub_file_path)).read()
201         self.assertEqual(self.keypair_creator.get_keypair().public_key, file_key)