blob: d3ee7ee9bb08b2859c1a9390641be936f7953a43 [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
Steve Baker647b3452014-07-30 11:04:42 +120014
15from heat_integrationtests.common import exceptions
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040016from heat_integrationtests.scenario import scenario_base
Steve Baker647b3452014-07-30 11:04:42 +120017
Steve Baker647b3452014-07-30 11:04:42 +120018
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040019class CfnInitIntegrationTest(scenario_base.ScenarioTestsBase):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030020 """Testing cfn-init and cfn-signal workability."""
Steve Baker647b3452014-07-30 11:04:42 +120021
22 def setUp(self):
23 super(CfnInitIntegrationTest, self).setUp()
Steve Baker647b3452014-07-30 11:04:42 +120024
Sergey Kraynevef9d8422015-02-13 03:41:46 -050025 def check_stack(self, sid):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040026 # Check status of all resources
27 for res in ('WaitHandle', 'SmokeSecurityGroup', 'SmokeKeys',
Rakesh H Seecee652015-07-15 17:44:41 +053028 'CfnUser', 'SmokeServer', 'SmokeServerElasticIp'):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040029 self._wait_for_resource_status(
30 sid, res, 'CREATE_COMPLETE')
Steve Baker647b3452014-07-30 11:04:42 +120031
32 server_resource = self.client.resources.get(sid, 'SmokeServer')
33 server_id = server_resource.physical_resource_id
34 server = self.compute_client.servers.get(server_id)
Steve Baker647b3452014-07-30 11:04:42 +120035
36 try:
37 self._wait_for_resource_status(
38 sid, 'WaitCondition', 'CREATE_COMPLETE')
39 except (exceptions.StackResourceBuildErrorException,
40 exceptions.TimeoutException) as e:
41 raise e
42 finally:
43 # attempt to log the server console regardless of WaitCondition
44 # going to complete. This allows successful and failed cloud-init
45 # logs to be compared
46 self._log_console_output(servers=[server])
47
Steve Baker647b3452014-07-30 11:04:42 +120048 stack = self.client.stacks.get(sid)
49
50 # This is an assert of great significance, as it means the following
51 # has happened:
52 # - cfn-init read the provided metadata and wrote out a file
53 # - a user was created and credentials written to the server
54 # - a cfn-signal was built which was signed with provided credentials
55 # - the wait condition was fulfilled and the stack has changed state
56 wait_status = json.loads(
57 self._stack_output(stack, 'WaitConditionStatus'))
58 self.assertEqual('smoke test complete', wait_status['smoke_status'])
59
Rakesh H Seecee652015-07-15 17:44:41 +053060 # Check EIP attributes.
61 server_floatingip_id = self._stack_output(stack,
62 'ElasticIp_Id')
63 self.assertIsNotNone(server_floatingip_id)
64
65 # Fetch EIP details.
66 net_show = self.network_client.show_floatingip(
67 floatingip=server_floatingip_id)
68 floating_ip = net_show['floatingip']['floating_ip_address']
69 port_id = net_show['floatingip']['port_id']
70
71 # Ensure that EIP was assigned to server.
72 port_show = self.network_client.show_port(port=port_id)
73 self.assertEqual(server.id, port_show['port']['device_id'])
74 server_ip = self._stack_output(stack, 'SmokeServerElasticIp')
75 self.assertEqual(server_ip, floating_ip)
Steve Bakerd6de8a22015-03-12 14:42:51 +130076
77 # Check that created server is reachable
78 if not self._ping_ip_address(server_ip):
79 self._log_console_output(servers=[server])
80 self.fail(
81 "Timed out waiting for %s to become reachable" % server_ip)
82
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040083 # Check that the user can authenticate with the generated keypair
Steve Baker647b3452014-07-30 11:04:42 +120084 if self.keypair:
Steve Baker647b3452014-07-30 11:04:42 +120085 try:
86 linux_client = self.get_remote_client(
87 server_ip, username='ec2-user')
88 linux_client.validate_authentication()
89 except (exceptions.ServerUnreachable,
90 exceptions.SSHTimeout) as e:
91 self._log_console_output(servers=[server])
92 raise e
93
94 def test_server_cfn_init(self):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030095 """Check cfn-init and cfn-signal availability on the created server.
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040096
97 The alternative scenario is the following:
98 1. Create a stack with a server and configured security group.
99 2. Check that all stack resources were created.
100 3. Check that created server is reachable.
101 4. Check that stack was created successfully.
102 5. Check that is it possible to connect to server
103 via generated keypair.
104 """
105 parameters = {
Sergey Kraynev83ef84d2015-04-29 05:31:54 -0400106 'key_name': self.keypair_name,
107 'flavor': self.conf.instance_type,
108 'image': self.conf.image_ref,
109 'timeout': self.conf.build_timeout,
Rabi Mishraec4b03b2015-05-23 02:20:47 +0530110 'subnet': self.net['subnets'][0],
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400111 }
112
113 # Launch stack
114 stack_id = self.launch_stack(
115 template_name="test_server_cfn_init.yaml",
Steve Bakerd6de8a22015-03-12 14:42:51 +1300116 parameters=parameters,
117 expected_status=None
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400118 )
119
120 # Check stack
121 self.check_stack(stack_id)