blob: 7ed6082584502308b951e20634660ad8e7b71f04 [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')
Steve Baker647b3452014-07-30 11:04:42 +120039 finally:
40 # attempt to log the server console regardless of WaitCondition
41 # going to complete. This allows successful and failed cloud-init
42 # logs to be compared
43 self._log_console_output(servers=[server])
44
Steve Baker647b3452014-07-30 11:04:42 +120045 stack = self.client.stacks.get(sid)
46
47 # This is an assert of great significance, as it means the following
48 # has happened:
49 # - cfn-init read the provided metadata and wrote out a file
50 # - a user was created and credentials written to the server
51 # - a cfn-signal was built which was signed with provided credentials
52 # - the wait condition was fulfilled and the stack has changed state
53 wait_status = json.loads(
54 self._stack_output(stack, 'WaitConditionStatus'))
55 self.assertEqual('smoke test complete', wait_status['smoke_status'])
56
Rakesh H Seecee652015-07-15 17:44:41 +053057 # Check EIP attributes.
58 server_floatingip_id = self._stack_output(stack,
59 'ElasticIp_Id')
60 self.assertIsNotNone(server_floatingip_id)
61
62 # Fetch EIP details.
63 net_show = self.network_client.show_floatingip(
64 floatingip=server_floatingip_id)
65 floating_ip = net_show['floatingip']['floating_ip_address']
66 port_id = net_show['floatingip']['port_id']
67
68 # Ensure that EIP was assigned to server.
69 port_show = self.network_client.show_port(port=port_id)
70 self.assertEqual(server.id, port_show['port']['device_id'])
71 server_ip = self._stack_output(stack, 'SmokeServerElasticIp')
72 self.assertEqual(server_ip, floating_ip)
Steve Bakerd6de8a22015-03-12 14:42:51 +130073
74 # Check that created server is reachable
75 if not self._ping_ip_address(server_ip):
76 self._log_console_output(servers=[server])
77 self.fail(
78 "Timed out waiting for %s to become reachable" % server_ip)
79
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040080 # Check that the user can authenticate with the generated keypair
Steve Baker647b3452014-07-30 11:04:42 +120081 if self.keypair:
Steve Baker647b3452014-07-30 11:04:42 +120082 try:
83 linux_client = self.get_remote_client(
84 server_ip, username='ec2-user')
85 linux_client.validate_authentication()
86 except (exceptions.ServerUnreachable,
Bin Zhou80e0ad62016-07-06 16:33:45 +080087 exceptions.SSHTimeout):
Steve Baker647b3452014-07-30 11:04:42 +120088 self._log_console_output(servers=[server])
Bin Zhou80e0ad62016-07-06 16:33:45 +080089 raise
Steve Baker647b3452014-07-30 11:04:42 +120090
91 def test_server_cfn_init(self):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030092 """Check cfn-init and cfn-signal availability on the created server.
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040093
94 The alternative scenario is the following:
95 1. Create a stack with a server and configured security group.
96 2. Check that all stack resources were created.
97 3. Check that created server is reachable.
98 4. Check that stack was created successfully.
99 5. Check that is it possible to connect to server
100 via generated keypair.
101 """
102 parameters = {
Sergey Kraynev83ef84d2015-04-29 05:31:54 -0400103 'key_name': self.keypair_name,
104 'flavor': self.conf.instance_type,
105 'image': self.conf.image_ref,
106 'timeout': self.conf.build_timeout,
Rabi Mishraec4b03b2015-05-23 02:20:47 +0530107 'subnet': self.net['subnets'][0],
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400108 }
109
110 # Launch stack
111 stack_id = self.launch_stack(
112 template_name="test_server_cfn_init.yaml",
Steve Bakerd6de8a22015-03-12 14:42:51 +1300113 parameters=parameters,
114 expected_status=None
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400115 )
116
117 # Check stack
118 self.check_stack(stack_id)