blob: 2df815bc56215ecde9a820ee522dff3bcd5a409d [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()
47 if self.conf.skip_software_config_tests:
48 self.skipTest('Testing software config disabled in conf, '
49 'skipping')
Steve Bakerf6c8f122015-02-10 13:54:46 +130050 self.stack_name = self._stack_rand_name()
Steve Bakerf6c8f122015-02-10 13:54:46 +130051
Steve Baker8c9aee12015-03-10 16:17:45 +130052 def check_stack(self):
53 sid = self.stack_identifier
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040054 # Check that all stack resources were created
Steve Bakerf6c8f122015-02-10 13:54:46 +130055 for res in ('cfg2a', 'cfg2b', 'cfg1', 'cfg3', 'server'):
56 self._wait_for_resource_status(
57 sid, res, 'CREATE_COMPLETE')
58
59 server_resource = self.client.resources.get(sid, 'server')
60 server_id = server_resource.physical_resource_id
61 server = self.compute_client.servers.get(server_id)
62
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040063 # Waiting for each deployment to contribute their
64 # config to resource
Steve Bakerf6c8f122015-02-10 13:54:46 +130065 try:
Steve Bakerf6c8f122015-02-10 13:54:46 +130066 for res in ('dep2b', 'dep1', 'dep3'):
67 self._wait_for_resource_status(
68 sid, res, 'CREATE_IN_PROGRESS')
69
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040070 server_metadata = self.client.resources.metadata(
71 sid, 'server')
Steve Bakerf6c8f122015-02-10 13:54:46 +130072 deployments = dict((d['name'], d) for d in
73 server_metadata['deployments'])
74
75 for res in ('dep2a', 'dep2b', 'dep1', 'dep3'):
76 self._wait_for_resource_status(
77 sid, res, 'CREATE_COMPLETE')
78 except (exceptions.StackResourceBuildErrorException,
79 exceptions.TimeoutException) as e:
80 self._log_console_output(servers=[server])
81 raise e
82
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040083 # Check that stack was fully created
Steve Bakerf6c8f122015-02-10 13:54:46 +130084 self._wait_for_stack_status(sid, 'CREATE_COMPLETE')
85
86 complete_server_metadata = self.client.resources.metadata(
87 sid, 'server')
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040088
89 # Ensure any previously available deployments haven't changed so
Steve Bakerf6c8f122015-02-10 13:54:46 +130090 # config isn't re-triggered
91 complete_deployments = dict((d['name'], d) for d in
92 complete_server_metadata['deployments'])
93 for k, v in six.iteritems(deployments):
94 self.assertEqual(v, complete_deployments[k])
95
96 stack = self.client.stacks.get(sid)
97
98 res1 = self._stack_output(stack, 'res1')
99 self.assertEqual(
100 'The file %s contains %s for server %s during %s' % (
101 '/tmp/baaaaa', 'fooooo', server_id, 'CREATE'),
102 res1['result'])
103 self.assertEqual(0, res1['status_code'])
104 self.assertEqual('Output to stderr\n', res1['stderr'])
105 self.assertTrue(len(res1['stdout']) > 0)
106
107 res2 = self._stack_output(stack, 'res2')
108 self.assertEqual(
109 'The file %s contains %s for server %s during %s' % (
110 '/tmp/cfn-init-foo', 'barrr', server_id, 'CREATE'),
111 res2['result'])
112 self.assertEqual(0, res2['status_code'])
113 self.assertEqual('', res2['stderr'])
114 self.assertEqual('', res2['stdout'])
115
116 res3 = self._stack_output(stack, 'res3')
117 self.assertEqual(
118 'The file %s contains %s for server %s during %s' % (
119 '/tmp/ba', 'fo', server_id, 'CREATE'),
120 res3['result'])
121 self.assertEqual(0, res3['status_code'])
122 self.assertEqual('', res3['stderr'])
123 self.assertTrue(len(res1['stdout']) > 0)
124
125 dep1_resource = self.client.resources.get(sid, 'dep1')
126 dep1_id = dep1_resource.physical_resource_id
127 dep1_dep = self.client.software_deployments.get(dep1_id)
Steve Baker8c9aee12015-03-10 16:17:45 +1300128 if hasattr(dep1_dep, 'updated_time'):
129 # Only check updated_time if the attribute exists.
130 # This allows latest heat agent code to be tested with
131 # Juno heat (which doesn't expose updated_time)
132 self.assertIsNotNone(dep1_dep.updated_time)
133 self.assertNotEqual(
134 dep1_dep.updated_time,
135 dep1_dep.creation_time)
Steve Bakerf6c8f122015-02-10 13:54:46 +1300136
137 def test_server_software_config(self):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400138 """
139 Check that passed files with scripts are executed on created server.
140
141 The alternative scenario is the following:
142 1. Create a stack and pass files with scripts.
143 2. Check that all stack resources are created successfully.
144 3. Wait for all deployments.
145 4. Check that stack was created.
146 5. Check stack outputs.
147 """
148
149 parameters = {
150 'key_name': self.keypair_name,
151 'flavor': self.conf.instance_type,
152 'image': self.conf.image_ref,
153 'network': self.net['id']
154 }
155
156 files = {
157 'cfg1.sh': CFG1_SH,
158 'cfg3.pp': CFG3_PP
159 }
160
Steve Baker0b679bb2015-03-11 13:46:42 +1300161 env_files, env = template_utils.process_environment_and_files(
162 self.conf.boot_config_env)
163
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400164 # Launch stack
Steve Baker8c9aee12015-03-10 16:17:45 +1300165 self.stack_identifier = self.launch_stack(
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400166 stack_name=self.stack_name,
167 template_name='test_server_software_config.yaml',
168 parameters=parameters,
Steve Baker0b679bb2015-03-11 13:46:42 +1300169 files=dict(list(files.items()) + list(env_files.items())),
170 expected_status=None,
171 environment=env
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400172 )
173
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400174 # Check stack
Steve Baker8c9aee12015-03-10 16:17:45 +1300175 self.check_stack()