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 |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 17 | from heat_integrationtests.scenario import scenario_base |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 18 | |
| 19 | LOG = logging.getLogger(__name__) |
| 20 | |
| 21 | |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 22 | class CfnInitIntegrationTest(scenario_base.ScenarioTestsBase): |
| 23 | """ |
| 24 | The class is responsible for testing cfn-init and cfn-signal workability |
| 25 | """ |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 26 | |
| 27 | def setUp(self): |
| 28 | super(CfnInitIntegrationTest, self).setUp() |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 29 | |
Sergey Kraynev | ef9d842 | 2015-02-13 03:41:46 -0500 | [diff] [blame] | 30 | def check_stack(self, sid): |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 31 | # Check status of all resources |
| 32 | for res in ('WaitHandle', 'SmokeSecurityGroup', 'SmokeKeys', |
Steve Baker | d6de8a2 | 2015-03-12 14:42:51 +1300 | [diff] [blame^] | 33 | 'CfnUser', 'SmokeServer', 'IPAddress'): |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 34 | self._wait_for_resource_status( |
| 35 | sid, res, 'CREATE_COMPLETE') |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 36 | |
| 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 Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 40 | |
| 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 Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 53 | # Check stack status |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 54 | 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 Baker | d6de8a2 | 2015-03-12 14:42:51 +1300 | [diff] [blame^] | 68 | 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 Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 76 | # Check that the user can authenticate with the generated keypair |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 77 | if self.keypair: |
Steve Baker | 647b345 | 2014-07-30 11:04:42 +1200 | [diff] [blame] | 78 | 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 Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 88 | """ |
| 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 Baker | d6de8a2 | 2015-03-12 14:42:51 +1300 | [diff] [blame^] | 110 | parameters=parameters, |
| 111 | expected_status=None |
Anastasia Kuznetsova | e45bfff | 2015-02-25 12:50:34 +0400 | [diff] [blame] | 112 | ) |
| 113 | |
| 114 | # Check stack |
| 115 | self.check_stack(stack_id) |