Merge "Changes to RouterSettings constructors to use kwargs."
[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 unittest
16 import uuid
17
18 import os
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
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(**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(**{'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(
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)
63
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)
69
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)
76
77     def test_all(self):
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)
83
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)
91
92
93 class CreateKeypairsTests(OSIntegrationTestCase):
94     """
95     Tests for the OpenStackKeypair class
96     """
97
98     def setUp(self):
99         super(self.__class__, self).__start__()
100
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
106
107         self.keypair_creator = None
108
109     def tearDown(self):
110         """
111         Cleanup of created keypair
112         """
113         if self.keypair_creator:
114             self.keypair_creator.clean()
115
116         try:
117             os.remove(self.pub_file_path)
118         except:
119             pass
120
121         try:
122             os.remove(self.priv_file_path)
123         except:
124             pass
125
126         super(self.__class__, self).__clean__()
127
128     def test_create_keypair_only(self):
129         """
130         Tests the creation of a generated keypair without saving to file
131         :return:
132         """
133         self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(
134             name=self.keypair_name))
135         self.keypair_creator.create()
136
137         keypair = nova_utils.keypair_exists(self.nova,
138                                             self.keypair_creator.get_keypair())
139         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
140
141     def test_create_delete_keypair(self):
142         """
143         Tests the creation then deletion of an OpenStack keypair to ensure
144         clean() does not raise an Exception.
145         """
146         # Create Image
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)
151
152         # Delete Image manually
153         nova_utils.delete_keypair(self.nova, created_keypair)
154
155         self.assertIsNone(
156             nova_utils.get_keypair_by_name(self.nova, self.keypair_name))
157
158         # Must not throw an exception when attempting to cleanup non-existent
159         # image
160         self.keypair_creator.clean()
161         self.assertIsNone(self.keypair_creator.get_keypair())
162
163     def test_create_keypair_save_pub_only(self):
164         """
165         Tests the creation of a generated keypair and saves the public key only
166         :return:
167         """
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()
172
173         keypair = nova_utils.keypair_exists(self.nova,
174                                             self.keypair_creator.get_keypair())
175         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
176
177         file_key = open(os.path.expanduser(self.pub_file_path)).read()
178         self.assertEqual(self.keypair_creator.get_keypair().public_key,
179                          file_key)
180
181     def test_create_keypair_save_both(self):
182         """
183         Tests the creation of a generated keypair and saves both private and
184         public key files[
185         :return:
186         """
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()
192
193         keypair = nova_utils.keypair_exists(self.nova,
194                                             self.keypair_creator.get_keypair())
195         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
196
197         file_key = open(os.path.expanduser(self.pub_file_path)).read()
198         self.assertEqual(self.keypair_creator.get_keypair().public_key,
199                          file_key)
200
201         self.assertTrue(os.path.isfile(self.priv_file_path))
202
203     def test_create_keypair_from_file(self):
204         """
205         Tests the creation of an existing public keypair from a file
206         :return:
207         """
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()
215
216         keypair = nova_utils.keypair_exists(self.nova,
217                                             self.keypair_creator.get_keypair())
218         self.assertEqual(self.keypair_creator.get_keypair(), keypair)
219
220         file_key = open(os.path.expanduser(self.pub_file_path)).read()
221         self.assertEqual(self.keypair_creator.get_keypair().public_key,
222                          file_key)