blob: a70c32f37883f0ad2011de32acbaf8ecf1f4245e [file] [log] [blame]
Steve Bakerf6c8f122015-02-10 13:54:46 +13001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import six
14
15from heat_integrationtests.common import exceptions
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040016from heat_integrationtests.scenario import scenario_base
Steve Bakerf6c8f122015-02-10 13:54:46 +130017
18CFG1_SH = '''#!/bin/sh
19echo "Writing to /tmp/$bar"
20echo $foo > /tmp/$bar
21echo -n "The file /tmp/$bar contains `cat /tmp/$bar` for server \
22$deploy_server_id during $deploy_action" > $heat_outputs_path.result
23echo "Written to /tmp/$bar"
24echo "Output to stderr" 1>&2
25'''
26
27CFG3_PP = '''file {'barfile':
28 ensure => file,
29 mode => 0644,
30 path => "/tmp/$::bar",
31 content => "$::foo",
32}
33file {'output_result':
34 ensure => file,
35 path => "$::heat_outputs_path.result",
36 mode => 0644,
37 content => "The file /tmp/$::bar contains $::foo for server \
38$::deploy_server_id during $::deploy_action",
39}'''
40
41
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040042class SoftwareConfigIntegrationTest(scenario_base.ScenarioTestsBase):
Steve Bakerf6c8f122015-02-10 13:54:46 +130043
44 def setUp(self):
45 super(SoftwareConfigIntegrationTest, self).setUp()
46 if self.conf.skip_software_config_tests:
47 self.skipTest('Testing software config disabled in conf, '
48 'skipping')
Steve Bakerf6c8f122015-02-10 13:54:46 +130049 self.stack_name = self._stack_rand_name()
Steve Bakerf6c8f122015-02-10 13:54:46 +130050
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040051 def check_stack(self, sid):
52 # Check that all stack resources were created
Steve Bakerf6c8f122015-02-10 13:54:46 +130053 for res in ('cfg2a', 'cfg2b', 'cfg1', 'cfg3', 'server'):
54 self._wait_for_resource_status(
55 sid, res, 'CREATE_COMPLETE')
56
57 server_resource = self.client.resources.get(sid, 'server')
58 server_id = server_resource.physical_resource_id
59 server = self.compute_client.servers.get(server_id)
60
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040061 # Waiting for each deployment to contribute their
62 # config to resource
Steve Bakerf6c8f122015-02-10 13:54:46 +130063 try:
Steve Bakerf6c8f122015-02-10 13:54:46 +130064 for res in ('dep2b', 'dep1', 'dep3'):
65 self._wait_for_resource_status(
66 sid, res, 'CREATE_IN_PROGRESS')
67
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040068 server_metadata = self.client.resources.metadata(
69 sid, 'server')
Steve Bakerf6c8f122015-02-10 13:54:46 +130070 deployments = dict((d['name'], d) for d in
71 server_metadata['deployments'])
72
73 for res in ('dep2a', 'dep2b', 'dep1', 'dep3'):
74 self._wait_for_resource_status(
75 sid, res, 'CREATE_COMPLETE')
76 except (exceptions.StackResourceBuildErrorException,
77 exceptions.TimeoutException) as e:
78 self._log_console_output(servers=[server])
79 raise e
80
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040081 # Check that stack was fully created
Steve Bakerf6c8f122015-02-10 13:54:46 +130082 self._wait_for_stack_status(sid, 'CREATE_COMPLETE')
83
84 complete_server_metadata = self.client.resources.metadata(
85 sid, 'server')
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040086
87 # Ensure any previously available deployments haven't changed so
Steve Bakerf6c8f122015-02-10 13:54:46 +130088 # config isn't re-triggered
89 complete_deployments = dict((d['name'], d) for d in
90 complete_server_metadata['deployments'])
91 for k, v in six.iteritems(deployments):
92 self.assertEqual(v, complete_deployments[k])
93
94 stack = self.client.stacks.get(sid)
95
96 res1 = self._stack_output(stack, 'res1')
97 self.assertEqual(
98 'The file %s contains %s for server %s during %s' % (
99 '/tmp/baaaaa', 'fooooo', server_id, 'CREATE'),
100 res1['result'])
101 self.assertEqual(0, res1['status_code'])
102 self.assertEqual('Output to stderr\n', res1['stderr'])
103 self.assertTrue(len(res1['stdout']) > 0)
104
105 res2 = self._stack_output(stack, 'res2')
106 self.assertEqual(
107 'The file %s contains %s for server %s during %s' % (
108 '/tmp/cfn-init-foo', 'barrr', server_id, 'CREATE'),
109 res2['result'])
110 self.assertEqual(0, res2['status_code'])
111 self.assertEqual('', res2['stderr'])
112 self.assertEqual('', res2['stdout'])
113
114 res3 = self._stack_output(stack, 'res3')
115 self.assertEqual(
116 'The file %s contains %s for server %s during %s' % (
117 '/tmp/ba', 'fo', server_id, 'CREATE'),
118 res3['result'])
119 self.assertEqual(0, res3['status_code'])
120 self.assertEqual('', res3['stderr'])
121 self.assertTrue(len(res1['stdout']) > 0)
122
123 dep1_resource = self.client.resources.get(sid, 'dep1')
124 dep1_id = dep1_resource.physical_resource_id
125 dep1_dep = self.client.software_deployments.get(dep1_id)
126 self.assertIsNotNone(dep1_dep.updated_time)
127 self.assertNotEqual(dep1_dep.updated_time, dep1_dep.creation_time)
128
129 def test_server_software_config(self):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400130 """
131 Check that passed files with scripts are executed on created server.
132
133 The alternative scenario is the following:
134 1. Create a stack and pass files with scripts.
135 2. Check that all stack resources are created successfully.
136 3. Wait for all deployments.
137 4. Check that stack was created.
138 5. Check stack outputs.
139 """
140
141 parameters = {
142 'key_name': self.keypair_name,
143 'flavor': self.conf.instance_type,
144 'image': self.conf.image_ref,
145 'network': self.net['id']
146 }
147
148 files = {
149 'cfg1.sh': CFG1_SH,
150 'cfg3.pp': CFG3_PP
151 }
152
153 # Launch stack
154 stack_id = self.launch_stack(
155 stack_name=self.stack_name,
156 template_name='test_server_software_config.yaml',
157 parameters=parameters,
158 files=files,
159 expected_status=None
160 )
161
162 self.stack_identifier = '%s/%s' % (self.stack_name, stack_id)
163
164 # Check stack
165 self.check_stack(self.stack_identifier)