blob: cc5d9d24fc3e460167ac6c46f01aa847ecd15791 [file] [log] [blame]
Steve Baker647b3452014-07-30 11:04:42 +12001# 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 json
14import logging
15
16from heat_integrationtests.common import exceptions
17from heat_integrationtests.common import test
18
19LOG = logging.getLogger(__name__)
20
21
22class CfnInitIntegrationTest(test.HeatIntegrationTest):
23
24 def setUp(self):
25 super(CfnInitIntegrationTest, self).setUp()
26 if not self.conf.image_ref:
27 raise self.skipException("No image configured to test")
Sergey Krayneva265c132015-02-13 03:51:03 -050028 self.assign_keypair()
Steve Baker647b3452014-07-30 11:04:42 +120029 self.client = self.orchestration_client
30 self.template_name = 'test_server_cfn_init.yaml'
Sergey Kraynevd6fa5c02015-02-13 03:03:55 -050031 self.sub_dir = 'templates'
Steve Baker647b3452014-07-30 11:04:42 +120032
Steve Baker647b3452014-07-30 11:04:42 +120033 def launch_stack(self):
34 net = self._get_default_network()
Sergey Kraynevef9d8422015-02-13 03:41:46 -050035 parameters = {
Steve Baker647b3452014-07-30 11:04:42 +120036 'key_name': self.keypair_name,
37 'flavor': self.conf.instance_type,
38 'image': self.conf.image_ref,
39 'timeout': self.conf.build_timeout,
Steve Baker0e7ad372015-02-24 16:35:04 +130040 'subnet': net['subnets'][0],
Steve Baker647b3452014-07-30 11:04:42 +120041 }
42
43 # create the stack
Sergey Kraynevef9d8422015-02-13 03:41:46 -050044 template = self._load_template(__file__, self.template_name,
45 self.sub_dir)
46 return self.stack_create(template=template,
47 parameters=parameters)
Steve Baker647b3452014-07-30 11:04:42 +120048
Sergey Kraynevef9d8422015-02-13 03:41:46 -050049 def check_stack(self, sid):
Steve Baker647b3452014-07-30 11:04:42 +120050 self._wait_for_resource_status(
51 sid, 'WaitHandle', 'CREATE_COMPLETE')
52 self._wait_for_resource_status(
53 sid, 'SmokeSecurityGroup', 'CREATE_COMPLETE')
54 self._wait_for_resource_status(
55 sid, 'SmokeKeys', 'CREATE_COMPLETE')
56 self._wait_for_resource_status(
57 sid, 'CfnUser', 'CREATE_COMPLETE')
58 self._wait_for_resource_status(
59 sid, 'SmokeServer', 'CREATE_COMPLETE')
60
61 server_resource = self.client.resources.get(sid, 'SmokeServer')
62 server_id = server_resource.physical_resource_id
63 server = self.compute_client.servers.get(server_id)
64 server_ip = server.networks[self.conf.network_for_ssh][0]
65
66 if not self._ping_ip_address(server_ip):
67 self._log_console_output(servers=[server])
68 self.fail(
69 "Timed out waiting for %s to become reachable" % server_ip)
70
71 try:
72 self._wait_for_resource_status(
73 sid, 'WaitCondition', 'CREATE_COMPLETE')
74 except (exceptions.StackResourceBuildErrorException,
75 exceptions.TimeoutException) as e:
76 raise e
77 finally:
78 # attempt to log the server console regardless of WaitCondition
79 # going to complete. This allows successful and failed cloud-init
80 # logs to be compared
81 self._log_console_output(servers=[server])
82
83 self._wait_for_stack_status(sid, 'CREATE_COMPLETE')
84
85 stack = self.client.stacks.get(sid)
86
87 # This is an assert of great significance, as it means the following
88 # has happened:
89 # - cfn-init read the provided metadata and wrote out a file
90 # - a user was created and credentials written to the server
91 # - a cfn-signal was built which was signed with provided credentials
92 # - the wait condition was fulfilled and the stack has changed state
93 wait_status = json.loads(
94 self._stack_output(stack, 'WaitConditionStatus'))
95 self.assertEqual('smoke test complete', wait_status['smoke_status'])
96
97 if self.keypair:
98 # Check that the user can authenticate with the generated
99 # keypair
100 try:
101 linux_client = self.get_remote_client(
102 server_ip, username='ec2-user')
103 linux_client.validate_authentication()
104 except (exceptions.ServerUnreachable,
105 exceptions.SSHTimeout) as e:
106 self._log_console_output(servers=[server])
107 raise e
108
109 def test_server_cfn_init(self):
Sergey Kraynevef9d8422015-02-13 03:41:46 -0500110 sid = self.launch_stack()
111 self.check_stack(sid)