Merge "Fixed two tests that never had their networks removed on tearDown()"
[snaps.git] / snaps / openstack / tests / create_keypairs_tests.py
1 # Copyright (c) 2016 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 Crypto.PublicKey import RSA
20
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
24
25 __author__ = 'spisarski'
26
27
28 class KeypairSettingsUnitTests(unittest.TestCase):
29     """
30     Tests the construction of the KeypairSettings class
31     """
32
33     def test_no_params(self):
34         with self.assertRaises(Exception):
35             KeypairSettings()
36
37     def test_empty_config(self):
38         with self.assertRaises(Exception):
39             KeypairSettings(config=dict())
40
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)
46
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)
52
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)
58
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)
64
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)
70
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)
76
77     def test_all(self):
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)
82
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)
89
90
91 class CreateKeypairsTests(OSIntegrationTestCase):
92     """
93     Tests for the OpenStackKeypair class
94     """
95
96     def setUp(self):
97         super(self.__class__, self).__start__()
98
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
104
105         self.keypair_creator = None
106
107     def tearDown(self):
108         """
109         Cleanup of created keypair
110         """
111         if self.keypair_creator:
112             self.keypair_creator.clean()
113
114         try:
115             os.remove(self.pub_file_path)
116         except:
117             pass
118
119         try:
120             os.remove(self.priv_file_path)
121         except:
122             pass
123
124         super(self.__class__, self).__clean__()
125
126     def test_create_keypair_only(self):
127         """
128         Tests the creation of a generated keypair without saving to file
129         :return:
130         """
131         self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(name=self.keypair_name))
132         self.keypair_creator.create()
133
134         keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
135         self.assertEquals(self.keypair_creator.get_keypair(), keypair)
136
137     def test_create_delete_keypair(self):
138         """
139         Tests the creation then deletion of an OpenStack keypair to ensure clean() does not raise an Exception.
140         """
141         # Create Image
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)
145
146         # Delete Image manually
147         nova_utils.delete_keypair(self.nova, created_keypair)
148
149         self.assertIsNone(nova_utils.get_keypair_by_name(self.nova, self.keypair_name))
150
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())
154
155     def test_create_keypair_save_pub_only(self):
156         """
157         Tests the creation of a generated keypair and saves the public key only
158         :return:
159         """
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()
163
164         keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
165         self.assertEquals(self.keypair_creator.get_keypair(), keypair)
166
167         file_key = open(os.path.expanduser(self.pub_file_path)).read()
168         self.assertEquals(self.keypair_creator.get_keypair().public_key, file_key)
169
170     def test_create_keypair_save_both(self):
171         """
172         Tests the creation of a generated keypair and saves both private and public key files[
173         :return:
174         """
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()
179
180         keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
181         self.assertEquals(self.keypair_creator.get_keypair(), keypair)
182
183         file_key = open(os.path.expanduser(self.pub_file_path)).read()
184         self.assertEquals(self.keypair_creator.get_keypair().public_key, file_key)
185
186         self.assertTrue(os.path.isfile(self.priv_file_path))
187
188     def test_create_keypair_from_file(self):
189         """
190         Tests the creation of an existing public keypair from a file
191         :return:
192         """
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()
198
199         keypair = nova_utils.keypair_exists(self.nova, self.keypair_creator.get_keypair())
200         self.assertEquals(self.keypair_creator.get_keypair(), keypair)
201
202         file_key = open(os.path.expanduser(self.pub_file_path)).read()
203         self.assertEquals(self.keypair_creator.get_keypair().public_key, file_key)