blob: 8739a994c206099877beca700abb6ace0308e70d [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
Steve Baker0b679bb2015-03-11 13:46:42 +130013from heatclient.common import template_utils
Steve Bakerf6c8f122015-02-10 13:54:46 +130014import six
15
16from heat_integrationtests.common import exceptions
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040017from heat_integrationtests.scenario import scenario_base
Steve Bakerf6c8f122015-02-10 13:54:46 +130018
19CFG1_SH = '''#!/bin/sh
20echo "Writing to /tmp/$bar"
21echo $foo > /tmp/$bar
22echo -n "The file /tmp/$bar contains `cat /tmp/$bar` for server \
23$deploy_server_id during $deploy_action" > $heat_outputs_path.result
24echo "Written to /tmp/$bar"
25echo "Output to stderr" 1>&2
26'''
27
28CFG3_PP = '''file {'barfile':
29 ensure => file,
30 mode => 0644,
31 path => "/tmp/$::bar",
32 content => "$::foo",
33}
34file {'output_result':
35 ensure => file,
36 path => "$::heat_outputs_path.result",
37 mode => 0644,
38 content => "The file /tmp/$::bar contains $::foo for server \
39$::deploy_server_id during $::deploy_action",
40}'''
41
42
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040043class SoftwareConfigIntegrationTest(scenario_base.ScenarioTestsBase):
Steve Bakerf6c8f122015-02-10 13:54:46 +130044
45 def setUp(self):
46 super(SoftwareConfigIntegrationTest, self).setUp()
Steve Bakerf6c8f122015-02-10 13:54:46 +130047 self.stack_name = self._stack_rand_name()
Steve Bakerf6c8f122015-02-10 13:54:46 +130048
Steve Baker8c9aee12015-03-10 16:17:45 +130049 def check_stack(self):
50 sid = self.stack_identifier
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040051 # Check that all stack resources were created
Steve Bakerf6c8f122015-02-10 13:54:46 +130052 for res in ('cfg2a', 'cfg2b', 'cfg1', 'cfg3', 'server'):
53 self._wait_for_resource_status(
54 sid, res, 'CREATE_COMPLETE')
55
56 server_resource = self.client.resources.get(sid, 'server')
57 server_id = server_resource.physical_resource_id
58 server = self.compute_client.servers.get(server_id)
59
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040060 # Waiting for each deployment to contribute their
61 # config to resource
Steve Bakerf6c8f122015-02-10 13:54:46 +130062 try:
Steve Bakerf6c8f122015-02-10 13:54:46 +130063 for res in ('dep2b', 'dep1', 'dep3'):
64 self._wait_for_resource_status(
65 sid, res, 'CREATE_IN_PROGRESS')
66
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040067 server_metadata = self.client.resources.metadata(
68 sid, 'server')
Steve Bakerf6c8f122015-02-10 13:54:46 +130069 deployments = dict((d['name'], d) for d in
70 server_metadata['deployments'])
71
72 for res in ('dep2a', 'dep2b', 'dep1', 'dep3'):
73 self._wait_for_resource_status(
74 sid, res, 'CREATE_COMPLETE')
75 except (exceptions.StackResourceBuildErrorException,
76 exceptions.TimeoutException) as e:
Steve Bakerf6c8f122015-02-10 13:54:46 +130077 raise e
Steve Baker803f1502015-03-11 13:47:08 +130078 finally:
79 # attempt to log the server console regardless of deployments
80 # going to complete. This allows successful and failed boot
81 # logs to be compared
82 self._log_console_output(servers=[server])
Steve Bakerf6c8f122015-02-10 13:54:46 +130083
Steve Bakerf6c8f122015-02-10 13:54:46 +130084 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)
Steve Baker8c9aee12015-03-10 16:17:45 +1300126 if hasattr(dep1_dep, 'updated_time'):
127 # Only check updated_time if the attribute exists.
128 # This allows latest heat agent code to be tested with
129 # Juno heat (which doesn't expose updated_time)
130 self.assertIsNotNone(dep1_dep.updated_time)
131 self.assertNotEqual(
132 dep1_dep.updated_time,
133 dep1_dep.creation_time)
Steve Bakerf6c8f122015-02-10 13:54:46 +1300134
135 def test_server_software_config(self):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400136 """
137 Check that passed files with scripts are executed on created server.
138
139 The alternative scenario is the following:
140 1. Create a stack and pass files with scripts.
141 2. Check that all stack resources are created successfully.
142 3. Wait for all deployments.
143 4. Check that stack was created.
144 5. Check stack outputs.
145 """
146
147 parameters = {
148 'key_name': self.keypair_name,
149 'flavor': self.conf.instance_type,
150 'image': self.conf.image_ref,
151 'network': self.net['id']
152 }
153
154 files = {
155 'cfg1.sh': CFG1_SH,
156 'cfg3.pp': CFG3_PP
157 }
158
Steve Baker0b679bb2015-03-11 13:46:42 +1300159 env_files, env = template_utils.process_environment_and_files(
160 self.conf.boot_config_env)
161
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400162 # Launch stack
Steve Baker8c9aee12015-03-10 16:17:45 +1300163 self.stack_identifier = self.launch_stack(
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400164 stack_name=self.stack_name,
165 template_name='test_server_software_config.yaml',
166 parameters=parameters,
Steve Baker0b679bb2015-03-11 13:46:42 +1300167 files=dict(list(files.items()) + list(env_files.items())),
168 expected_status=None,
169 environment=env
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400170 )
171
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400172 # Check stack
Steve Baker8c9aee12015-03-10 16:17:45 +1300173 self.check_stack()