blob: 75de02ee1398e364d614d59ed89a0b8b865fd44d [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",
Oleg Khavroshin6c7211c2016-01-12 16:38:13 +030040}
41'''
Steve Bakerf6c8f122015-02-10 13:54:46 +130042
43
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040044class SoftwareConfigIntegrationTest(scenario_base.ScenarioTestsBase):
Steve Bakerf6c8f122015-02-10 13:54:46 +130045
Steve Baker8c9aee12015-03-10 16:17:45 +130046 def check_stack(self):
47 sid = self.stack_identifier
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040048 # Check that all stack resources were created
Steve Bakerf6c8f122015-02-10 13:54:46 +130049 for res in ('cfg2a', 'cfg2b', 'cfg1', 'cfg3', 'server'):
50 self._wait_for_resource_status(
51 sid, res, 'CREATE_COMPLETE')
52
53 server_resource = self.client.resources.get(sid, 'server')
54 server_id = server_resource.physical_resource_id
55 server = self.compute_client.servers.get(server_id)
56
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040057 # Waiting for each deployment to contribute their
58 # config to resource
Steve Bakerf6c8f122015-02-10 13:54:46 +130059 try:
Steve Bakerf6c8f122015-02-10 13:54:46 +130060 for res in ('dep2b', 'dep1', 'dep3'):
61 self._wait_for_resource_status(
62 sid, res, 'CREATE_IN_PROGRESS')
63
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040064 server_metadata = self.client.resources.metadata(
65 sid, 'server')
Steve Bakerf6c8f122015-02-10 13:54:46 +130066 deployments = dict((d['name'], d) for d in
67 server_metadata['deployments'])
68
69 for res in ('dep2a', 'dep2b', 'dep1', 'dep3'):
70 self._wait_for_resource_status(
71 sid, res, 'CREATE_COMPLETE')
72 except (exceptions.StackResourceBuildErrorException,
73 exceptions.TimeoutException) as e:
Steve Bakerf6c8f122015-02-10 13:54:46 +130074 raise e
Steve Baker803f1502015-03-11 13:47:08 +130075 finally:
76 # attempt to log the server console regardless of deployments
77 # going to complete. This allows successful and failed boot
78 # logs to be compared
79 self._log_console_output(servers=[server])
Steve Bakerf6c8f122015-02-10 13:54:46 +130080
Steve Bakerf6c8f122015-02-10 13:54:46 +130081 complete_server_metadata = self.client.resources.metadata(
82 sid, 'server')
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040083
84 # Ensure any previously available deployments haven't changed so
Steve Bakerf6c8f122015-02-10 13:54:46 +130085 # config isn't re-triggered
86 complete_deployments = dict((d['name'], d) for d in
87 complete_server_metadata['deployments'])
88 for k, v in six.iteritems(deployments):
89 self.assertEqual(v, complete_deployments[k])
90
91 stack = self.client.stacks.get(sid)
92
93 res1 = self._stack_output(stack, 'res1')
94 self.assertEqual(
95 'The file %s contains %s for server %s during %s' % (
96 '/tmp/baaaaa', 'fooooo', server_id, 'CREATE'),
97 res1['result'])
98 self.assertEqual(0, res1['status_code'])
99 self.assertEqual('Output to stderr\n', res1['stderr'])
100 self.assertTrue(len(res1['stdout']) > 0)
101
102 res2 = self._stack_output(stack, 'res2')
103 self.assertEqual(
104 'The file %s contains %s for server %s during %s' % (
105 '/tmp/cfn-init-foo', 'barrr', server_id, 'CREATE'),
106 res2['result'])
107 self.assertEqual(0, res2['status_code'])
108 self.assertEqual('', res2['stderr'])
109 self.assertEqual('', res2['stdout'])
110
111 res3 = self._stack_output(stack, 'res3')
112 self.assertEqual(
113 'The file %s contains %s for server %s during %s' % (
114 '/tmp/ba', 'fo', server_id, 'CREATE'),
115 res3['result'])
116 self.assertEqual(0, res3['status_code'])
117 self.assertEqual('', res3['stderr'])
118 self.assertTrue(len(res1['stdout']) > 0)
119
120 dep1_resource = self.client.resources.get(sid, 'dep1')
121 dep1_id = dep1_resource.physical_resource_id
122 dep1_dep = self.client.software_deployments.get(dep1_id)
Steve Baker8c9aee12015-03-10 16:17:45 +1300123 if hasattr(dep1_dep, 'updated_time'):
124 # Only check updated_time if the attribute exists.
125 # This allows latest heat agent code to be tested with
126 # Juno heat (which doesn't expose updated_time)
127 self.assertIsNotNone(dep1_dep.updated_time)
128 self.assertNotEqual(
129 dep1_dep.updated_time,
130 dep1_dep.creation_time)
Steve Bakerf6c8f122015-02-10 13:54:46 +1300131
132 def test_server_software_config(self):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +0300133 """Check that passed files with scripts are executed on created server.
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400134
135 The alternative scenario is the following:
136 1. Create a stack and pass files with scripts.
137 2. Check that all stack resources are created successfully.
138 3. Wait for all deployments.
139 4. Check that stack was created.
140 5. Check stack outputs.
141 """
142
143 parameters = {
144 'key_name': self.keypair_name,
145 'flavor': self.conf.instance_type,
146 'image': self.conf.image_ref,
147 'network': self.net['id']
148 }
149
150 files = {
151 'cfg1.sh': CFG1_SH,
152 'cfg3.pp': CFG3_PP
153 }
154
Steve Baker0b679bb2015-03-11 13:46:42 +1300155 env_files, env = template_utils.process_environment_and_files(
156 self.conf.boot_config_env)
157
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400158 # Launch stack
Steve Baker8c9aee12015-03-10 16:17:45 +1300159 self.stack_identifier = self.launch_stack(
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400160 template_name='test_server_software_config.yaml',
161 parameters=parameters,
Steve Baker0b679bb2015-03-11 13:46:42 +1300162 files=dict(list(files.items()) + list(env_files.items())),
163 expected_status=None,
164 environment=env
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400165 )
166
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400167 # Check stack
Steve Baker8c9aee12015-03-10 16:17:45 +1300168 self.check_stack()