blob: c7d84e3bf6610f803a80df4bef3de15f4a23cbe9 [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()
rabic333f762016-09-29 08:34:03 +053024 if not self.conf.image_ref:
25 raise self.skipException("No image configured to test")
26 if not self.conf.instance_type:
27 raise self.skipException("No flavor configured to test")
Steve Baker647b3452014-07-30 11:04:42 +120028
Sergey Kraynevef9d8422015-02-13 03:41:46 -050029 def check_stack(self, sid):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040030 # Check status of all resources
31 for res in ('WaitHandle', 'SmokeSecurityGroup', 'SmokeKeys',
Rakesh H Seecee652015-07-15 17:44:41 +053032 'CfnUser', 'SmokeServer', 'SmokeServerElasticIp'):
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040033 self._wait_for_resource_status(
34 sid, res, 'CREATE_COMPLETE')
Steve Baker647b3452014-07-30 11:04:42 +120035
36 server_resource = self.client.resources.get(sid, 'SmokeServer')
37 server_id = server_resource.physical_resource_id
38 server = self.compute_client.servers.get(server_id)
Steve Baker647b3452014-07-30 11:04:42 +120039
40 try:
41 self._wait_for_resource_status(
42 sid, 'WaitCondition', 'CREATE_COMPLETE')
Steve Baker647b3452014-07-30 11:04:42 +120043 finally:
44 # attempt to log the server console regardless of WaitCondition
45 # going to complete. This allows successful and failed cloud-init
46 # logs to be compared
47 self._log_console_output(servers=[server])
48
Steve Baker647b3452014-07-30 11:04:42 +120049 stack = self.client.stacks.get(sid)
50
51 # This is an assert of great significance, as it means the following
52 # has happened:
53 # - cfn-init read the provided metadata and wrote out a file
54 # - a user was created and credentials written to the server
55 # - a cfn-signal was built which was signed with provided credentials
56 # - the wait condition was fulfilled and the stack has changed state
57 wait_status = json.loads(
58 self._stack_output(stack, 'WaitConditionStatus'))
59 self.assertEqual('smoke test complete', wait_status['smoke_status'])
60
Rakesh H Seecee652015-07-15 17:44:41 +053061 # Check EIP attributes.
62 server_floatingip_id = self._stack_output(stack,
63 'ElasticIp_Id')
64 self.assertIsNotNone(server_floatingip_id)
65
66 # Fetch EIP details.
67 net_show = self.network_client.show_floatingip(
68 floatingip=server_floatingip_id)
69 floating_ip = net_show['floatingip']['floating_ip_address']
70 port_id = net_show['floatingip']['port_id']
71
72 # Ensure that EIP was assigned to server.
73 port_show = self.network_client.show_port(port=port_id)
74 self.assertEqual(server.id, port_show['port']['device_id'])
75 server_ip = self._stack_output(stack, 'SmokeServerElasticIp')
76 self.assertEqual(server_ip, floating_ip)
Steve Bakerd6de8a22015-03-12 14:42:51 +130077
78 # Check that created server is reachable
79 if not self._ping_ip_address(server_ip):
80 self._log_console_output(servers=[server])
81 self.fail(
82 "Timed out waiting for %s to become reachable" % server_ip)
83
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040084 # Check that the user can authenticate with the generated keypair
Steve Baker647b3452014-07-30 11:04:42 +120085 if self.keypair:
Steve Baker647b3452014-07-30 11:04:42 +120086 try:
87 linux_client = self.get_remote_client(
88 server_ip, username='ec2-user')
89 linux_client.validate_authentication()
90 except (exceptions.ServerUnreachable,
Bin Zhou80e0ad62016-07-06 16:33:45 +080091 exceptions.SSHTimeout):
Steve Baker647b3452014-07-30 11:04:42 +120092 self._log_console_output(servers=[server])
Bin Zhou80e0ad62016-07-06 16:33:45 +080093 raise
Steve Baker647b3452014-07-30 11:04:42 +120094
95 def test_server_cfn_init(self):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030096 """Check cfn-init and cfn-signal availability on the created server.
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +040097
98 The alternative scenario is the following:
99 1. Create a stack with a server and configured security group.
100 2. Check that all stack resources were created.
101 3. Check that created server is reachable.
102 4. Check that stack was created successfully.
103 5. Check that is it possible to connect to server
104 via generated keypair.
105 """
106 parameters = {
Sergey Kraynev83ef84d2015-04-29 05:31:54 -0400107 'key_name': self.keypair_name,
108 'flavor': self.conf.instance_type,
109 'image': self.conf.image_ref,
110 'timeout': self.conf.build_timeout,
Rabi Mishraec4b03b2015-05-23 02:20:47 +0530111 'subnet': self.net['subnets'][0],
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400112 }
113
114 # Launch stack
115 stack_id = self.launch_stack(
116 template_name="test_server_cfn_init.yaml",
Steve Bakerd6de8a22015-03-12 14:42:51 +1300117 parameters=parameters,
118 expected_status=None
Anastasia Kuznetsovae45bfff2015-02-25 12:50:34 +0400119 )
120
121 # Check stack
122 self.check_stack(stack_id)