blob: 64b2a3573dd96db454aeed34f621d5d5a88b9c3b [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'
30
31 def assign_keypair(self):
32 self.stack_name = self._stack_rand_name()
33 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()
42 self.parameters = {
43 '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
51 self.template = self._load_template(__file__, self.template_name)
52 self.client.stacks.create(
53 stack_name=self.stack_name,
54 template=self.template,
55 parameters=self.parameters)
56
57 self.stack = self.client.stacks.get(self.stack_name)
58 self.stack_identifier = '%s/%s' % (self.stack_name, self.stack.id)
59 self.addCleanup(self._stack_delete, self.stack_identifier)
60
61 def check_stack(self):
62 sid = self.stack_identifier
63 self._wait_for_resource_status(
64 sid, 'WaitHandle', 'CREATE_COMPLETE')
65 self._wait_for_resource_status(
66 sid, 'SmokeSecurityGroup', 'CREATE_COMPLETE')
67 self._wait_for_resource_status(
68 sid, 'SmokeKeys', 'CREATE_COMPLETE')
69 self._wait_for_resource_status(
70 sid, 'CfnUser', 'CREATE_COMPLETE')
71 self._wait_for_resource_status(
72 sid, 'SmokeServer', 'CREATE_COMPLETE')
73
74 server_resource = self.client.resources.get(sid, 'SmokeServer')
75 server_id = server_resource.physical_resource_id
76 server = self.compute_client.servers.get(server_id)
77 server_ip = server.networks[self.conf.network_for_ssh][0]
78
79 if not self._ping_ip_address(server_ip):
80 self._log_console_output(servers=[server])
81 self.fail(
82 "Timed out waiting for %s to become reachable" % server_ip)
83
84 try:
85 self._wait_for_resource_status(
86 sid, 'WaitCondition', 'CREATE_COMPLETE')
87 except (exceptions.StackResourceBuildErrorException,
88 exceptions.TimeoutException) as e:
89 raise e
90 finally:
91 # attempt to log the server console regardless of WaitCondition
92 # going to complete. This allows successful and failed cloud-init
93 # logs to be compared
94 self._log_console_output(servers=[server])
95
96 self._wait_for_stack_status(sid, 'CREATE_COMPLETE')
97
98 stack = self.client.stacks.get(sid)
99
100 # This is an assert of great significance, as it means the following
101 # has happened:
102 # - cfn-init read the provided metadata and wrote out a file
103 # - a user was created and credentials written to the server
104 # - a cfn-signal was built which was signed with provided credentials
105 # - the wait condition was fulfilled and the stack has changed state
106 wait_status = json.loads(
107 self._stack_output(stack, 'WaitConditionStatus'))
108 self.assertEqual('smoke test complete', wait_status['smoke_status'])
109
110 if self.keypair:
111 # Check that the user can authenticate with the generated
112 # keypair
113 try:
114 linux_client = self.get_remote_client(
115 server_ip, username='ec2-user')
116 linux_client.validate_authentication()
117 except (exceptions.ServerUnreachable,
118 exceptions.SSHTimeout) as e:
119 self._log_console_output(servers=[server])
120 raise e
121
122 def test_server_cfn_init(self):
123 self.assign_keypair()
124 self.launch_stack()
125 self.check_stack()