blob: cf74bd826492e3d93cdf5cbd494800c811824a1a [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',
Steve Bakerd6de8a22015-03-12 14:42:51 +130033 'CfnUser', 'SmokeServer', 'IPAddress'):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040034 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)
Steve Baker647b3452014-07-30 11:04:42 +120040
41 try:
42 self._wait_for_resource_status(
43 sid, 'WaitCondition', 'CREATE_COMPLETE')
44 except (exceptions.StackResourceBuildErrorException,
45 exceptions.TimeoutException) as e:
46 raise e
47 finally:
48 # attempt to log the server console regardless of WaitCondition
49 # going to complete. This allows successful and failed cloud-init
50 # logs to be compared
51 self._log_console_output(servers=[server])
52
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040053 # Check stack status
Steve Baker647b3452014-07-30 11:04:42 +120054 self._wait_for_stack_status(sid, 'CREATE_COMPLETE')
55
56 stack = self.client.stacks.get(sid)
57
58 # This is an assert of great significance, as it means the following
59 # has happened:
60 # - cfn-init read the provided metadata and wrote out a file
61 # - a user was created and credentials written to the server
62 # - a cfn-signal was built which was signed with provided credentials
63 # - the wait condition was fulfilled and the stack has changed state
64 wait_status = json.loads(
65 self._stack_output(stack, 'WaitConditionStatus'))
66 self.assertEqual('smoke test complete', wait_status['smoke_status'])
67
Steve Bakerd6de8a22015-03-12 14:42:51 +130068 server_ip = self._stack_output(stack, 'SmokeServerIp')
69
70 # Check that created server is reachable
71 if not self._ping_ip_address(server_ip):
72 self._log_console_output(servers=[server])
73 self.fail(
74 "Timed out waiting for %s to become reachable" % server_ip)
75
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040076 # Check that the user can authenticate with the generated keypair
Steve Baker647b3452014-07-30 11:04:42 +120077 if self.keypair:
Steve Baker647b3452014-07-30 11:04:42 +120078 try:
79 linux_client = self.get_remote_client(
80 server_ip, username='ec2-user')
81 linux_client.validate_authentication()
82 except (exceptions.ServerUnreachable,
83 exceptions.SSHTimeout) as e:
84 self._log_console_output(servers=[server])
85 raise e
86
87 def test_server_cfn_init(self):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040088 """
89 Check cfn-init and cfn-signal availability on the created server.
90
91 The alternative scenario is the following:
92 1. Create a stack with a server and configured security group.
93 2. Check that all stack resources were created.
94 3. Check that created server is reachable.
95 4. Check that stack was created successfully.
96 5. Check that is it possible to connect to server
97 via generated keypair.
98 """
99 parameters = {
100 "key_name": self.keypair_name,
101 "flavor": self.conf.instance_type,
102 "image": self.conf.image_ref,
103 "timeout": self.conf.build_timeout,
104 "subnet": self.net["subnets"][0],
105 }
106
107 # Launch stack
108 stack_id = self.launch_stack(
109 template_name="test_server_cfn_init.yaml",
Steve Bakerd6de8a22015-03-12 14:42:51 +1300110 parameters=parameters,
111 expected_status=None
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400112 )
113
114 # Check stack
115 self.check_stack(stack_id)