2 # Copyright (c) 2016 GigaSpaces Technologies Ltd. All rights reserved
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
24 from cloudify.test_utils import workflow_test
25 from nova_plugin.keypair import creation_validation
26 from cloudify.exceptions import NonRecoverableError
28 PRIVATE_KEY_NAME = 'private_key'
31 class TestValidation(unittest.TestCase):
33 blueprint_path = path.join('resources',
34 'test-keypair-validation-blueprint.yaml')
37 _, fp = tempfile.mkstemp()
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)
52 os.remove(self.private_key)
54 if self.not_readable_private_key:
55 os.remove(self.not_readable_private_key)
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)
62 def new_keypair_create(self, *args, **kwargs):
63 creation_validation(*args, **kwargs)
65 def new_keypair_create_with_exception(self, *args, **kwargs):
66 self.assertRaises(NonRecoverableError, creation_validation,
69 def get_keypair_inputs_private_key(self, is_external, **kwargs):
71 'private_key': self.private_key,
72 'is_keypair_external': is_external
75 def get_keypair_inputs_not_readable_private_key(self,
76 is_external, **kwargs):
78 'private_key': self.not_readable_private_key,
79 'is_keypair_external': is_external
82 def get_keypair_inputs_not_writable_dir_r(self, is_external, **kwargs):
84 'private_key': path.join(self.not_writable_temp_dir_r,
86 'is_keypair_external': is_external
89 def get_keypair_inputs_not_writable_dir_rx(self, is_external, **kwargs):
91 'private_key': path.join(self.not_writable_temp_dir_rx,
93 'is_keypair_external': is_external
96 def get_keypair_inputs_not_writable_dir_rw(self, is_external, **kwargs):
98 'private_key': path.join(self.not_writable_temp_dir_rw,
100 'is_keypair_external': is_external
103 def get_keypair_inputs_temp_dir(self, is_external, **kwargs):
105 'private_key': path.join(self.temp_dir, PRIVATE_KEY_NAME),
106 'is_keypair_external': is_external
109 @workflow_test(blueprint_path, inputs={
111 'is_keypair_external': False
113 @mock.patch('nova_plugin.keypair.validate_resource')
114 def test_keypair_valid_config(self, cfy_local, *args):
116 with mock.patch('nova_plugin.keypair.create',
117 new=self.new_keypair_create):
118 cfy_local.execute('install', task_retries=0)
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):
125 with mock.patch('nova_plugin.keypair.create',
126 new=self.new_keypair_create):
127 cfy_local.execute('install', task_retries=0)
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):
134 with mock.patch('nova_plugin.keypair.create',
135 new=self.new_keypair_create_with_exception):
136 cfy_local.execute('install', task_retries=0)
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):
143 with mock.patch('nova_plugin.keypair.create',
144 new=self.new_keypair_create_with_exception):
145 cfy_local.execute('install', task_retries=0)
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):
152 with mock.patch('nova_plugin.keypair.create',
153 new=self.new_keypair_create):
154 cfy_local.execute('install', task_retries=0)
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):
162 with mock.patch('nova_plugin.keypair.create',
163 new=self.new_keypair_create_with_exception):
164 cfy_local.execute('install', task_retries=0)
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):
172 with mock.patch('nova_plugin.keypair.create',
173 new=self.new_keypair_create_with_exception):
174 cfy_local.execute('install', task_retries=0)
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):
182 with mock.patch('nova_plugin.keypair.create',
183 new=self.new_keypair_create_with_exception):
184 cfy_local.execute('install', task_retries=0)
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):
192 with mock.patch('nova_plugin.keypair.create',
193 new=self.new_keypair_create_with_exception):
194 cfy_local.execute('install', task_retries=0)