blob: 1f12e716afe525300d810d263c23a8919970fae8 [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
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040017from heat_integrationtests.scenario import scenario_base
Steve Baker647b3452014-07-30 11:04:42 +120018
19LOG = logging.getLogger(__name__)
20
21
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040022class CfnInitIntegrationTest(scenario_base.ScenarioTestsBase):
23 """
24 The class is responsible for testing cfn-init and cfn-signal workability
25 """
Steve Baker647b3452014-07-30 11:04:42 +120026
27 def setUp(self):
28 super(CfnInitIntegrationTest, self).setUp()
Steve Baker647b3452014-07-30 11:04:42 +120029
Sergey Kraynevef9d8422015-02-13 03:41:46 -050030 def check_stack(self, sid):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040031 # Check status of all resources
32 for res in ('WaitHandle', 'SmokeSecurityGroup', 'SmokeKeys',
33 'CfnUser', 'SmokeServer'):
34 self._wait_for_resource_status(
35 sid, res, 'CREATE_COMPLETE')
Steve Baker647b3452014-07-30 11:04:42 +120036
37 server_resource = self.client.resources.get(sid, 'SmokeServer')
38 server_id = server_resource.physical_resource_id
39 server = self.compute_client.servers.get(server_id)
40 server_ip = server.networks[self.conf.network_for_ssh][0]
41
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040042 # Check that created server is reachable
Steve Baker647b3452014-07-30 11:04:42 +120043 if not self._ping_ip_address(server_ip):
44 self._log_console_output(servers=[server])
45 self.fail(
46 "Timed out waiting for %s to become reachable" % server_ip)
47
48 try:
49 self._wait_for_resource_status(
50 sid, 'WaitCondition', 'CREATE_COMPLETE')
51 except (exceptions.StackResourceBuildErrorException,
52 exceptions.TimeoutException) as e:
53 raise e
54 finally:
55 # attempt to log the server console regardless of WaitCondition
56 # going to complete. This allows successful and failed cloud-init
57 # logs to be compared
58 self._log_console_output(servers=[server])
59
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040060 # Check stack status
Steve Baker647b3452014-07-30 11:04:42 +120061 self._wait_for_stack_status(sid, 'CREATE_COMPLETE')
62
63 stack = self.client.stacks.get(sid)
64
65 # This is an assert of great significance, as it means the following
66 # has happened:
67 # - cfn-init read the provided metadata and wrote out a file
68 # - a user was created and credentials written to the server
69 # - a cfn-signal was built which was signed with provided credentials
70 # - the wait condition was fulfilled and the stack has changed state
71 wait_status = json.loads(
72 self._stack_output(stack, 'WaitConditionStatus'))
73 self.assertEqual('smoke test complete', wait_status['smoke_status'])
74
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040075 # Check that the user can authenticate with the generated keypair
Steve Baker647b3452014-07-30 11:04:42 +120076 if self.keypair:
Steve Baker647b3452014-07-30 11:04:42 +120077 try:
78 linux_client = self.get_remote_client(
79 server_ip, username='ec2-user')
80 linux_client.validate_authentication()
81 except (exceptions.ServerUnreachable,
82 exceptions.SSHTimeout) as e:
83 self._log_console_output(servers=[server])
84 raise e
85
86 def test_server_cfn_init(self):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040087 """
88 Check cfn-init and cfn-signal availability on the created server.
89
90 The alternative scenario is the following:
91 1. Create a stack with a server and configured security group.
92 2. Check that all stack resources were created.
93 3. Check that created server is reachable.
94 4. Check that stack was created successfully.
95 5. Check that is it possible to connect to server
96 via generated keypair.
97 """
98 parameters = {
99 "key_name": self.keypair_name,
100 "flavor": self.conf.instance_type,
101 "image": self.conf.image_ref,
102 "timeout": self.conf.build_timeout,
103 "subnet": self.net["subnets"][0],
104 }
105
106 # Launch stack
107 stack_id = self.launch_stack(
108 template_name="test_server_cfn_init.yaml",
109 parameters=parameters
110 )
111
112 # Check stack
113 self.check_stack(stack_id)