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