blob: d7f056d72c733d5381e23448da721aaf7e7f393d [file] [log] [blame]
dfilppi9981f552017-08-07 20:10:53 +00001#########
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
16import unittest
17
18import mock
19
20from cloudify.mocks import MockCloudifyContext
21
22from nova_plugin import userdata
23
24
25def ctx_mock():
26 result = MockCloudifyContext(
27 node_id='d',
28 properties={})
29 result.node.type_hierarchy = ['cloudify.nodes.Compute']
30 return result
31
32
33class 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'))