blob: 9d8512055da3639fa4ece6c39965563d418c1c86 [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")
28 self.client = self.orchestration_client
29 self.template_name = 'test_server_cfn_init.yaml'
Sergey Kraynevd6fa5c02015-02-13 03:03:55 -050030 self.sub_dir = 'templates'
Steve Baker647b3452014-07-30 11:04:42 +120031
32 def assign_keypair(self):
Steve Baker647b3452014-07-30 11:04:42 +120033 if self.conf.keypair_name:
34 self.keypair = None
35 self.keypair_name = self.conf.keypair_name
36 else:
37 self.keypair = self.create_keypair()
38 self.keypair_name = self.keypair.id
39
40 def launch_stack(self):
41 net = self._get_default_network()
Sergey Kraynevef9d8422015-02-13 03:41:46 -050042 parameters = {
Steve Baker647b3452014-07-30 11:04:42 +120043 'key_name': self.keypair_name,
44 'flavor': self.conf.instance_type,
45 'image': self.conf.image_ref,
46 'timeout': self.conf.build_timeout,
47 'network': net['id'],
48 }
49
50 # create the stack
Sergey Kraynevef9d8422015-02-13 03:41:46 -050051 template = self._load_template(__file__, self.template_name,
52 self.sub_dir)
53 return self.stack_create(template=template,
54 parameters=parameters)
Steve Baker647b3452014-07-30 11:04:42 +120055
Sergey Kraynevef9d8422015-02-13 03:41:46 -050056 def check_stack(self, sid):
Steve Baker647b3452014-07-30 11:04:42 +120057 self._wait_for_resource_status(
58 sid, 'WaitHandle', 'CREATE_COMPLETE')
59 self._wait_for_resource_status(
60 sid, 'SmokeSecurityGroup', 'CREATE_COMPLETE')
61 self._wait_for_resource_status(
62 sid, 'SmokeKeys', 'CREATE_COMPLETE')
63 self._wait_for_resource_status(
64 sid, 'CfnUser', 'CREATE_COMPLETE')
65 self._wait_for_resource_status(
66 sid, 'SmokeServer', 'CREATE_COMPLETE')
67
68 server_resource = self.client.resources.get(sid, 'SmokeServer')
69 server_id = server_resource.physical_resource_id
70 server = self.compute_client.servers.get(server_id)
71 server_ip = server.networks[self.conf.network_for_ssh][0]
72
73 if not self._ping_ip_address(server_ip):
74 self._log_console_output(servers=[server])
75 self.fail(
76 "Timed out waiting for %s to become reachable" % server_ip)
77
78 try:
79 self._wait_for_resource_status(
80 sid, 'WaitCondition', 'CREATE_COMPLETE')
81 except (exceptions.StackResourceBuildErrorException,
82 exceptions.TimeoutException) as e:
83 raise e
84 finally:
85 # attempt to log the server console regardless of WaitCondition
86 # going to complete. This allows successful and failed cloud-init
87 # logs to be compared
88 self._log_console_output(servers=[server])
89
90 self._wait_for_stack_status(sid, 'CREATE_COMPLETE')
91
92 stack = self.client.stacks.get(sid)
93
94 # This is an assert of great significance, as it means the following
95 # has happened:
96 # - cfn-init read the provided metadata and wrote out a file
97 # - a user was created and credentials written to the server
98 # - a cfn-signal was built which was signed with provided credentials
99 # - the wait condition was fulfilled and the stack has changed state
100 wait_status = json.loads(
101 self._stack_output(stack, 'WaitConditionStatus'))
102 self.assertEqual('smoke test complete', wait_status['smoke_status'])
103
104 if self.keypair:
105 # Check that the user can authenticate with the generated
106 # keypair
107 try:
108 linux_client = self.get_remote_client(
109 server_ip, username='ec2-user')
110 linux_client.validate_authentication()
111 except (exceptions.ServerUnreachable,
112 exceptions.SSHTimeout) as e:
113 self._log_console_output(servers=[server])
114 raise e
115
116 def test_server_cfn_init(self):
117 self.assign_keypair()
Sergey Kraynevef9d8422015-02-13 03:41:46 -0500118 sid = self.launch_stack()
119 self.check_stack(sid)