Merge "Set controllerType to uppercase for LCM"
[so.git] / aria / multivim-plugin / src / main / python / multivim-plugin / nova_plugin / tests / test_validation.py
1 #########
2 # Copyright (c) 2016 GigaSpaces Technologies Ltd. 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
16 import os
17 from os import path
18 import tempfile
19 import shutil
20
21 import unittest
22 import mock
23
24 from cloudify.test_utils import workflow_test
25 from nova_plugin.keypair import creation_validation
26 from cloudify.exceptions import NonRecoverableError
27
28 PRIVATE_KEY_NAME = 'private_key'
29
30
31 class TestValidation(unittest.TestCase):
32
33     blueprint_path = path.join('resources',
34                                'test-keypair-validation-blueprint.yaml')
35
36     def setUp(self):
37         _, fp = tempfile.mkstemp()
38         self.private_key = fp
39         _, fp = tempfile.mkstemp()
40         self.not_readable_private_key = fp
41         os.chmod(self.not_readable_private_key, 0o200)
42         self.temp_dir = tempfile.mkdtemp()
43         self.not_writable_temp_dir_r = tempfile.mkdtemp()
44         os.chmod(self.not_writable_temp_dir_r, 0o400)
45         self.not_writable_temp_dir_rx = tempfile.mkdtemp()
46         os.chmod(self.not_writable_temp_dir_rx, 0o500)
47         self.not_writable_temp_dir_rw = tempfile.mkdtemp()
48         os.chmod(self.not_writable_temp_dir_rw, 0o600)
49
50     def tearDown(self):
51         if self.private_key:
52             os.remove(self.private_key)
53
54         if self.not_readable_private_key:
55             os.remove(self.not_readable_private_key)
56
57         shutil.rmtree(self.not_writable_temp_dir_r, ignore_errors=True)
58         shutil.rmtree(self.not_writable_temp_dir_rx, ignore_errors=True)
59         shutil.rmtree(self.not_writable_temp_dir_rw, ignore_errors=True)
60         shutil.rmtree(self.temp_dir, ignore_errors=True)
61
62     def new_keypair_create(self, *args, **kwargs):
63         creation_validation(*args, **kwargs)
64
65     def new_keypair_create_with_exception(self, *args, **kwargs):
66         self.assertRaises(NonRecoverableError, creation_validation,
67                           *args, **kwargs)
68
69     def get_keypair_inputs_private_key(self, is_external, **kwargs):
70         return {
71             'private_key': self.private_key,
72             'is_keypair_external': is_external
73         }
74
75     def get_keypair_inputs_not_readable_private_key(self,
76                                                     is_external, **kwargs):
77         return {
78             'private_key': self.not_readable_private_key,
79             'is_keypair_external': is_external
80         }
81
82     def get_keypair_inputs_not_writable_dir_r(self, is_external, **kwargs):
83         return {
84             'private_key': path.join(self.not_writable_temp_dir_r,
85                                      PRIVATE_KEY_NAME),
86             'is_keypair_external': is_external
87         }
88
89     def get_keypair_inputs_not_writable_dir_rx(self, is_external, **kwargs):
90         return {
91             'private_key': path.join(self.not_writable_temp_dir_rx,
92                                      PRIVATE_KEY_NAME),
93             'is_keypair_external': is_external
94         }
95
96     def get_keypair_inputs_not_writable_dir_rw(self, is_external, **kwargs):
97         return {
98             'private_key': path.join(self.not_writable_temp_dir_rw,
99                                      PRIVATE_KEY_NAME),
100             'is_keypair_external': is_external
101         }
102
103     def get_keypair_inputs_temp_dir(self, is_external, **kwargs):
104         return {
105             'private_key': path.join(self.temp_dir, PRIVATE_KEY_NAME),
106             'is_keypair_external': is_external
107         }
108
109     @workflow_test(blueprint_path, inputs={
110         'private_key': '',
111         'is_keypair_external': False
112     })
113     @mock.patch('nova_plugin.keypair.validate_resource')
114     def test_keypair_valid_config(self, cfy_local, *args):
115
116         with mock.patch('nova_plugin.keypair.create',
117                         new=self.new_keypair_create):
118             cfy_local.execute('install', task_retries=0)
119
120     @workflow_test(blueprint_path, inputs='get_keypair_inputs_private_key',
121                    input_func_kwargs={'is_external': True})
122     @mock.patch('nova_plugin.keypair.validate_resource')
123     def test_keypair_valid_config_external(self, cfy_local, *args):
124
125         with mock.patch('nova_plugin.keypair.create',
126                         new=self.new_keypair_create):
127             cfy_local.execute('install', task_retries=0)
128
129     @workflow_test(blueprint_path, inputs='get_keypair_inputs_temp_dir',
130                    input_func_kwargs={'is_external': True})
131     @mock.patch('nova_plugin.keypair.validate_resource')
132     def test_keypair_no_private_key(self, cfy_local, *args):
133
134         with mock.patch('nova_plugin.keypair.create',
135                         new=self.new_keypair_create_with_exception):
136             cfy_local.execute('install', task_retries=0)
137
138     @workflow_test(blueprint_path, inputs='get_keypair_inputs_private_key',
139                    input_func_kwargs={'is_external': False})
140     @mock.patch('nova_plugin.keypair.validate_resource')
141     def test_keypair_local_and_exists(self, cfy_local, *args):
142
143         with mock.patch('nova_plugin.keypair.create',
144                         new=self.new_keypair_create_with_exception):
145             cfy_local.execute('install', task_retries=0)
146
147     @workflow_test(blueprint_path, inputs='get_keypair_inputs_temp_dir',
148                    input_func_kwargs={'is_external': False})
149     @mock.patch('nova_plugin.keypair.validate_resource')
150     def test_keypair_local_temp_dir(self, cfy_local, *args):
151
152         with mock.patch('nova_plugin.keypair.create',
153                         new=self.new_keypair_create):
154             cfy_local.execute('install', task_retries=0)
155
156     @workflow_test(blueprint_path,
157                    inputs='get_keypair_inputs_not_writable_dir_r',
158                    input_func_kwargs={'is_external': False})
159     @mock.patch('nova_plugin.keypair.validate_resource')
160     def test_keypair_local_non_writable_dir_r(self, cfy_local, *args):
161
162         with mock.patch('nova_plugin.keypair.create',
163                         new=self.new_keypair_create_with_exception):
164             cfy_local.execute('install', task_retries=0)
165
166     @workflow_test(blueprint_path,
167                    inputs='get_keypair_inputs_not_writable_dir_rx',
168                    input_func_kwargs={'is_external': False})
169     @mock.patch('nova_plugin.keypair.validate_resource')
170     def test_keypair_local_non_writable_dir_rx(self, cfy_local, *args):
171
172         with mock.patch('nova_plugin.keypair.create',
173                         new=self.new_keypair_create_with_exception):
174             cfy_local.execute('install', task_retries=0)
175
176     @workflow_test(blueprint_path,
177                    inputs='get_keypair_inputs_not_writable_dir_rw',
178                    input_func_kwargs={'is_external': False})
179     @mock.patch('nova_plugin.keypair.validate_resource')
180     def test_keypair_local_non_writable_dir_rw(self, cfy_local, *args):
181
182         with mock.patch('nova_plugin.keypair.create',
183                         new=self.new_keypair_create_with_exception):
184             cfy_local.execute('install', task_retries=0)
185
186     @workflow_test(blueprint_path,
187                    inputs='get_keypair_inputs_not_readable_private_key',
188                    input_func_kwargs={'is_external': True})
189     @mock.patch('nova_plugin.keypair.validate_resource')
190     def test_keypair_not_readable_private_key(self, cfy_local, *args):
191
192         with mock.patch('nova_plugin.keypair.create',
193                         new=self.new_keypair_create_with_exception):
194             cfy_local.execute('install', task_retries=0)