summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/tests/create_keypairs_tests.py
blob: aeeefafcf57fe8dcb05a81a5a57020965e0d6cf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import uuid

import os
from snaps.openstack.create_keypairs import (
    KeypairSettings, OpenStackKeypair, KeypairSettingsError)
from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
from snaps.openstack.utils import nova_utils

__author__ = 'spisarski'


class KeypairSettingsUnitTests(unittest.TestCase):
    """
    Tests the construction of the KeypairSettings class
    """

    def test_no_params(self):
        with self.assertRaises(KeypairSettingsError):
            KeypairSettings()

    def test_empty_config(self):
        with self.assertRaises(KeypairSettingsError):
            KeypairSettings(**dict())

    def test_name_only(self):
        settings = KeypairSettings(name='foo')
        self.assertEqual('foo', settings.name)
        self.assertIsNone(settings.public_filepath)
        self.assertIsNone(settings.private_filepath)

    def test_config_with_name_only(self):
        settings = KeypairSettings(**{'name': 'foo'})
        self.assertEqual('foo', settings.name)
        self.assertIsNone(settings.public_filepath)
        self.assertIsNone(settings.private_filepath)

    def test_name_pub_only(self):
        settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub')
        self.assertEqual('foo', settings.name)
        self.assertEqual('/foo/bar.pub', settings.public_filepath)
        self.assertIsNone(settings.private_filepath)

    def test_config_with_name_pub_only(self):
        settings = KeypairSettings(
            **{'name': 'foo', 'public_filepath': '/foo/bar.pub'})
        self.assertEqual('foo', settings.name)
        self.assertEqual('/foo/bar.pub', settings.public_filepath)
        self.assertIsNone(settings.private_filepath)

    def test_name_priv_only(self):
        settings = KeypairSettings(name='foo', private_filepath='/foo/bar')
        self.assertEqual('foo', settings.name)
        self.assertIsNone(settings.public_filepath)
        self.assertEqual('/foo/bar', settings.private_filepath)

    def test_config_with_name_priv_only(self):
        settings = KeypairSettings(
            **{'name': 'foo', 'private_filepath': '/foo/bar'})
        self.assertEqual('foo', settings.name)
        self.assertIsNone(settings.public_filepath)
        self.assertEqual('/foo/bar', settings.private_filepath)

    def test_all(self):
        settings = KeypairSettings(name='foo', public_filepath='/foo/bar.pub',
                                   private_filepath='/foo/bar')
        self.assertEqual('foo', settings.name)
        self.assertEqual('/foo/bar.pub', settings.public_filepath)
        self.assertEqual('/foo/bar', settings.private_filepath)

    def test_config_all(self):
        settings = KeypairSettings(
            **{'name': 'foo', 'public_filepath': '/foo/bar.pub',
               'private_filepath': '/foo/bar'})
        self.assertEqual('foo', settings.name)
        self.assertEqual('/foo/bar.pub', settings.public_filepath)
        self.assertEqual('/foo/bar', settings.private_filepath)


class CreateKeypairsTests(OSIntegrationTestCase):
    """
    Tests for the OpenStackKeypair class
    """

    def setUp(self):
        super(self.__class__, self).__start__()

        guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.priv_file_path = 'tmp/' + guid
        self.pub_file_path = self.priv_file_path + '.pub'
        self.nova = nova_utils.nova_client(self.os_creds)
        self.keypair_name = guid

        self.keypair_creator = None

    def tearDown(self):
        """
        Cleanup of created keypair
        """
        if self.keypair_creator:
            self.keypair_creator.clean()

        try:
            os.remove(self.pub_file_path)
        except:
            pass

        try:
            os.remove(self.priv_file_path)
        except:
            pass

        super(self.__class__, self).__clean__()

    def test_create_keypair_only(self):
        """
        Tests the creation of a generated keypair without saving to file
        :return:
        """
        self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(
            name=self.keypair_name))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

    def test_create_delete_keypair(self):
        """
        Tests the creation then deletion of an OpenStack keypair to ensure
        clean() does not raise an Exception.
        """
        # Create Image
        self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings(
            name=self.keypair_name))
        created_keypair = self.keypair_creator.create()
        self.assertIsNotNone(created_keypair)

        # Delete Image manually
        nova_utils.delete_keypair(self.nova, created_keypair)

        self.assertIsNone(
            nova_utils.get_keypair_by_name(self.nova, self.keypair_name))

        # Must not throw an exception when attempting to cleanup non-existent
        # image
        self.keypair_creator.clean()
        self.assertIsNone(self.keypair_creator.get_keypair())

    def test_create_keypair_save_pub_only(self):
        """
        Tests the creation of a generated keypair and saves the public key only
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairSettings(name=self.keypair_name,
                                           public_filepath=self.pub_file_path))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

        file_key = open(os.path.expanduser(self.pub_file_path)).read()
        self.assertEqual(self.keypair_creator.get_keypair().public_key,
                         file_key)

    def test_create_keypair_save_both(self):
        """
        Tests the creation of a generated keypair and saves both private and
        public key files[
        :return:
        """
        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairSettings(
                name=self.keypair_name, public_filepath=self.pub_file_path,
                private_filepath=self.priv_file_path))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

        file_key = open(os.path.expanduser(self.pub_file_path)).read()
        self.assertEqual(self.keypair_creator.get_keypair().public_key,
                         file_key)

        self.assertTrue(os.path.isfile(self.priv_file_path))

    def test_create_keypair_from_file(self):
        """
        Tests the creation of an existing public keypair from a file
        :return:
        """
        keys = nova_utils.create_keys()
        nova_utils.save_keys_to_files(keys=keys,
                                      pub_file_path=self.pub_file_path)
        self.keypair_creator = OpenStackKeypair(
            self.os_creds, KeypairSettings(name=self.keypair_name,
                                           public_filepath=self.pub_file_path))
        self.keypair_creator.create()

        keypair = nova_utils.keypair_exists(self.nova,
                                            self.keypair_creator.get_keypair())
        self.assertEqual(self.keypair_creator.get_keypair(), keypair)

        file_key = open(os.path.expanduser(self.pub_file_path)).read()
        self.assertEqual(self.keypair_creator.get_keypair().public_key,
                         file_key)