Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 1 | # 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 | |
| 13 | import json |
| 14 | import logging |
| 15 | |
| 16 | from heat_integrationtests.common import exceptions |
| 17 | from heat_integrationtests.common import test |
| 18 | |
| 19 | LOG = logging.getLogger(__name__) |
| 20 | |
| 21 | |
| 22 | class CfnInitIntegrationTest(test.HeatIntegrationTest): |
| 23 | |
| 24 | def setUp(self): |
| 25 | super(CfnInitIntegrationTest, self).setUp() |
| 26 | if not self.conf.image_ref: |
| 27 | raise self.skipException("No image configured to test") |
Sergey Kraynev | a265c13 | 2015-02-13 03:51:03 -0500 | [diff] [blame] | 28 | self.assign_keypair() |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 29 | self.client = self.orchestration_client |
| 30 | self.template_name = 'test_server_cfn_init.yaml' |
Sergey Kraynev | d6fa5c0 | 2015-02-13 03:03:55 -0500 | [diff] [blame] | 31 | self.sub_dir = 'templates' |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 32 | |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 33 | def launch_stack(self): |
| 34 | net = self._get_default_network() |
Sergey Kraynev | ef9d842 | 2015-02-13 03:41:46 -0500 | [diff] [blame] | 35 | parameters = { |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 36 | 'key_name': self.keypair_name, |
| 37 | 'flavor': self.conf.instance_type, |
| 38 | 'image': self.conf.image_ref, |
| 39 | 'timeout': self.conf.build_timeout, |
Steve Baker | 0e7ad37 | 2015-02-24 16:35:04 +1300 | [diff] [blame] | 40 | 'subnet': net['subnets'][0], |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | # create the stack |
Sergey Kraynev | ef9d842 | 2015-02-13 03:41:46 -0500 | [diff] [blame] | 44 | template = self._load_template(__file__, self.template_name, |
| 45 | self.sub_dir) |
| 46 | return self.stack_create(template=template, |
| 47 | parameters=parameters) |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 48 | |
Sergey Kraynev | ef9d842 | 2015-02-13 03:41:46 -0500 | [diff] [blame] | 49 | def check_stack(self, sid): |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 50 | self._wait_for_resource_status( |
| 51 | sid, 'WaitHandle', 'CREATE_COMPLETE') |
| 52 | self._wait_for_resource_status( |
| 53 | sid, 'SmokeSecurityGroup', 'CREATE_COMPLETE') |
| 54 | self._wait_for_resource_status( |
| 55 | sid, 'SmokeKeys', 'CREATE_COMPLETE') |
| 56 | self._wait_for_resource_status( |
| 57 | sid, 'CfnUser', 'CREATE_COMPLETE') |
| 58 | self._wait_for_resource_status( |
| 59 | sid, 'SmokeServer', 'CREATE_COMPLETE') |
| 60 | |
| 61 | server_resource = self.client.resources.get(sid, 'SmokeServer') |
| 62 | server_id = server_resource.physical_resource_id |
| 63 | server = self.compute_client.servers.get(server_id) |
| 64 | server_ip = server.networks[self.conf.network_for_ssh][0] |
| 65 | |
| 66 | if not self._ping_ip_address(server_ip): |
| 67 | self._log_console_output(servers=[server]) |
| 68 | self.fail( |
| 69 | "Timed out waiting for %s to become reachable" % server_ip) |
| 70 | |
| 71 | try: |
| 72 | self._wait_for_resource_status( |
| 73 | sid, 'WaitCondition', 'CREATE_COMPLETE') |
| 74 | except (exceptions.StackResourceBuildErrorException, |
| 75 | exceptions.TimeoutException) as e: |
| 76 | raise e |
| 77 | finally: |
| 78 | # attempt to log the server console regardless of WaitCondition |
| 79 | # going to complete. This allows successful and failed cloud-init |
| 80 | # logs to be compared |
| 81 | self._log_console_output(servers=[server]) |
| 82 | |
| 83 | self._wait_for_stack_status(sid, 'CREATE_COMPLETE') |
| 84 | |
| 85 | stack = self.client.stacks.get(sid) |
| 86 | |
| 87 | # This is an assert of great significance, as it means the following |
| 88 | # has happened: |
| 89 | # - cfn-init read the provided metadata and wrote out a file |
| 90 | # - a user was created and credentials written to the server |
| 91 | # - a cfn-signal was built which was signed with provided credentials |
| 92 | # - the wait condition was fulfilled and the stack has changed state |
| 93 | wait_status = json.loads( |
| 94 | self._stack_output(stack, 'WaitConditionStatus')) |
| 95 | self.assertEqual('smoke test complete', wait_status['smoke_status']) |
| 96 | |
| 97 | if self.keypair: |
| 98 | # Check that the user can authenticate with the generated |
| 99 | # keypair |
| 100 | try: |
| 101 | linux_client = self.get_remote_client( |
| 102 | server_ip, username='ec2-user') |
| 103 | linux_client.validate_authentication() |
| 104 | except (exceptions.ServerUnreachable, |
| 105 | exceptions.SSHTimeout) as e: |
| 106 | self._log_console_output(servers=[server]) |
| 107 | raise e |
| 108 | |
| 109 | def test_server_cfn_init(self): |
Sergey Kraynev | ef9d842 | 2015-02-13 03:41:46 -0500 | [diff] [blame] | 110 | sid = self.launch_stack() |
| 111 | self.check_stack(sid) |