dfilppi | 9981f55 | 2017-08-07 20:10:53 +0000 | [diff] [blame] | 1 | ######### |
| 2 | # Copyright (c) 2015 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 unittest |
| 17 | |
| 18 | import mock |
| 19 | |
| 20 | from cloudify.mocks import MockCloudifyContext |
| 21 | |
| 22 | from nova_plugin import userdata |
| 23 | |
| 24 | |
| 25 | def ctx_mock(): |
| 26 | result = MockCloudifyContext( |
| 27 | node_id='d', |
| 28 | properties={}) |
| 29 | result.node.type_hierarchy = ['cloudify.nodes.Compute'] |
| 30 | return result |
| 31 | |
| 32 | |
| 33 | class TestServerUserdataHandling(unittest.TestCase): |
| 34 | |
| 35 | @mock.patch('nova_plugin.userdata.ctx', ctx_mock()) |
| 36 | def test_no_userdata(self): |
| 37 | server_conf = {} |
| 38 | userdata.handle_userdata(server_conf) |
| 39 | self.assertEqual(server_conf, {}) |
| 40 | |
| 41 | def test_agent_installation_userdata(self): |
| 42 | ctx = ctx_mock() |
| 43 | ctx.agent.init_script = lambda: 'SCRIPT' |
| 44 | with mock.patch('nova_plugin.userdata.ctx', ctx): |
| 45 | server_conf = {} |
| 46 | userdata.handle_userdata(server_conf) |
| 47 | self.assertEqual(server_conf, {'userdata': 'SCRIPT'}) |
| 48 | |
| 49 | @mock.patch('nova_plugin.userdata.ctx', ctx_mock()) |
| 50 | def test_existing_userdata(self): |
| 51 | server_conf = {'userdata': 'EXISTING'} |
| 52 | server_conf_copy = server_conf.copy() |
| 53 | userdata.handle_userdata(server_conf) |
| 54 | self.assertEqual(server_conf, server_conf_copy) |
| 55 | |
| 56 | def test_existing_and_agent_installation_userdata(self): |
| 57 | ctx = ctx_mock() |
| 58 | ctx.agent.init_script = lambda: '#! SCRIPT' |
| 59 | with mock.patch('nova_plugin.userdata.ctx', ctx): |
| 60 | server_conf = {'userdata': '#! EXISTING'} |
| 61 | userdata.handle_userdata(server_conf) |
| 62 | self.assertTrue(server_conf['userdata'].startswith( |
| 63 | 'Content-Type: multi')) |