Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import logging |
| 19 | import time |
| 20 | |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 21 | import nose.plugins.attrib |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 22 | import testresources |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 23 | import testtools |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 24 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 25 | from tempest import config |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 26 | from tempest import manager |
| 27 | |
| 28 | LOG = logging.getLogger(__name__) |
| 29 | |
| 30 | |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 31 | def attr(*args, **kwargs): |
| 32 | """A decorator which applies the nose and testtools attr decorator |
| 33 | |
| 34 | This decorator applies the nose attr decorator as well as the |
| 35 | the testtools.testcase.attr if it is in the list of attributes |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 36 | to testtools we want to apply. |
| 37 | """ |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 38 | |
| 39 | def decorator(f): |
| 40 | testtool_attributes = ('smoke') |
| 41 | |
| 42 | if 'type' in kwargs and kwargs['type'] in testtool_attributes: |
| 43 | return nose.plugins.attrib.attr(*args, **kwargs)( |
| 44 | testtools.testcase.attr(kwargs['type'])(f)) |
| 45 | else: |
| 46 | return nose.plugins.attrib.attr(*args, **kwargs)(f) |
| 47 | |
| 48 | return decorator |
| 49 | |
| 50 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 51 | class BaseTestCase(testtools.TestCase, |
| 52 | testtools.testcase.WithAttributes, |
| 53 | testresources.ResourcedTestCase): |
Attila Fazekas | c43fec8 | 2013-04-09 23:17:52 +0200 | [diff] [blame^] | 54 | |
| 55 | config = config.TempestConfig() |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 56 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 57 | @classmethod |
| 58 | def setUpClass(cls): |
| 59 | if hasattr(super(BaseTestCase, cls), 'setUpClass'): |
| 60 | super(BaseTestCase, cls).setUpClass() |
| 61 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 62 | |
| 63 | class TestCase(BaseTestCase): |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 64 | """Base test case class for all Tempest tests |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 65 | |
| 66 | Contains basic setup and convenience methods |
| 67 | """ |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 68 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 69 | manager_class = None |
| 70 | |
| 71 | @classmethod |
| 72 | def setUpClass(cls): |
| 73 | cls.manager = cls.manager_class() |
Maru Newby | dec13ec | 2012-08-30 11:19:17 -0700 | [diff] [blame] | 74 | for attr_name in cls.manager.client_attr_names: |
| 75 | # Ensure that pre-existing class attributes won't be |
| 76 | # accidentally overriden. |
| 77 | assert not hasattr(cls, attr_name) |
| 78 | client = getattr(cls.manager, attr_name) |
| 79 | setattr(cls, attr_name, client) |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 80 | cls.resource_keys = {} |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 81 | cls.os_resources = [] |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 82 | |
| 83 | def set_resource(self, key, thing): |
| 84 | LOG.debug("Adding %r to shared resources of %s" % |
| 85 | (thing, self.__class__.__name__)) |
| 86 | self.resource_keys[key] = thing |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 87 | self.os_resources.append(thing) |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 88 | |
| 89 | def get_resource(self, key): |
| 90 | return self.resource_keys[key] |
| 91 | |
| 92 | def remove_resource(self, key): |
| 93 | thing = self.resource_keys[key] |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 94 | self.os_resources.remove(thing) |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 95 | del self.resource_keys[key] |
| 96 | |
| 97 | |
Maru Newby | d3bf0aa | 2012-09-05 20:01:43 -0700 | [diff] [blame] | 98 | def call_until_true(func, duration, sleep_for): |
| 99 | """ |
| 100 | Call the given function until it returns True (and return True) or |
| 101 | until the specified duration (in seconds) elapses (and return |
| 102 | False). |
| 103 | |
| 104 | :param func: A zero argument callable that returns True on success. |
| 105 | :param duration: The number of seconds for which to attempt a successful |
| 106 | call of the function. |
| 107 | :param sleep_for: The number of seconds to sleep after an unsuccessful |
| 108 | invocation of the function. |
| 109 | """ |
| 110 | now = time.time() |
| 111 | timeout = now + duration |
| 112 | while now < timeout: |
| 113 | if func(): |
| 114 | return True |
| 115 | LOG.debug("Sleeping for %d seconds", sleep_for) |
| 116 | time.sleep(sleep_for) |
| 117 | now = time.time() |
| 118 | return False |
| 119 | |
| 120 | |
Pavel Sedlák | 51ed356 | 2013-04-26 12:36:08 +0200 | [diff] [blame] | 121 | def status_timeout(testcase, things, thing_id, expected_status): |
Matthew Treinish | fbf40fd | 2013-04-09 11:10:58 -0400 | [diff] [blame] | 122 | """ |
| 123 | Given a thing and an expected status, do a loop, sleeping |
| 124 | for a configurable amount of time, checking for the |
| 125 | expected status to show. At any time, if the returned |
| 126 | status of the thing is ERROR, fail out. |
| 127 | """ |
| 128 | def check_status(): |
| 129 | # python-novaclient has resources available to its client |
| 130 | # that all implement a get() method taking an identifier |
| 131 | # for the singular resource to retrieve. |
| 132 | thing = things.get(thing_id) |
| 133 | new_status = thing.status |
| 134 | if new_status == 'ERROR': |
Pavel Sedlák | 51ed356 | 2013-04-26 12:36:08 +0200 | [diff] [blame] | 135 | testcase.fail("%s failed to get to expected status." |
| 136 | "In ERROR state." |
| 137 | % thing) |
Matthew Treinish | fbf40fd | 2013-04-09 11:10:58 -0400 | [diff] [blame] | 138 | elif new_status == expected_status: |
| 139 | return True # All good. |
| 140 | LOG.debug("Waiting for %s to get to %s status. " |
| 141 | "Currently in %s status", |
| 142 | thing, expected_status, new_status) |
| 143 | conf = config.TempestConfig() |
| 144 | if not call_until_true(check_status, |
| 145 | conf.compute.build_timeout, |
| 146 | conf.compute.build_interval): |
Pavel Sedlák | 51ed356 | 2013-04-26 12:36:08 +0200 | [diff] [blame] | 147 | testcase.fail("Timed out waiting for thing %s to become %s" |
| 148 | % (thing_id, expected_status)) |
Matthew Treinish | fbf40fd | 2013-04-09 11:10:58 -0400 | [diff] [blame] | 149 | |
| 150 | |
| 151 | class DefaultClientSmokeTest(TestCase): |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 152 | |
| 153 | """ |
Matthew Treinish | fbf40fd | 2013-04-09 11:10:58 -0400 | [diff] [blame] | 154 | Base smoke test case class that provides the default clients to |
| 155 | access the various OpenStack APIs. |
| 156 | |
| 157 | Smoke tests are tests that have the following characteristics: |
| 158 | |
| 159 | * Test basic operations of an API, typically in an order that |
| 160 | a regular user would perform those operations |
| 161 | * Test only the correct inputs and action paths -- no fuzz or |
| 162 | random input data is sent, only valid inputs. |
| 163 | * Use only the default client tool for calling an API |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 164 | """ |
| 165 | |
Maru Newby | dec13ec | 2012-08-30 11:19:17 -0700 | [diff] [blame] | 166 | manager_class = manager.DefaultClientManager |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 167 | |
Matthew Treinish | fbf40fd | 2013-04-09 11:10:58 -0400 | [diff] [blame] | 168 | @classmethod |
| 169 | def tearDownClass(cls): |
| 170 | # NOTE(jaypipes): Because smoke tests are typically run in a specific |
| 171 | # order, and because test methods in smoke tests generally create |
| 172 | # resources in a particular order, we destroy resources in the reverse |
| 173 | # order in which resources are added to the smoke test class object |
| 174 | while cls.os_resources: |
| 175 | thing = cls.os_resources.pop() |
| 176 | LOG.debug("Deleting %r from shared resources of %s" % |
| 177 | (thing, cls.__name__)) |
| 178 | |
| 179 | try: |
| 180 | # OpenStack resources are assumed to have a delete() |
| 181 | # method which destroys the resource... |
| 182 | thing.delete() |
| 183 | except Exception as e: |
| 184 | # If the resource is already missing, mission accomplished. |
| 185 | if e.__class__.__name__ == 'NotFound': |
| 186 | continue |
| 187 | raise |
| 188 | |
| 189 | def is_deletion_complete(): |
| 190 | # Deletion testing is only required for objects whose |
| 191 | # existence cannot be checked via retrieval. |
| 192 | if isinstance(thing, dict): |
| 193 | return True |
| 194 | try: |
| 195 | thing.get() |
| 196 | except Exception as e: |
| 197 | # Clients are expected to return an exception |
| 198 | # called 'NotFound' if retrieval fails. |
| 199 | if e.__class__.__name__ == 'NotFound': |
| 200 | return True |
| 201 | raise |
| 202 | return False |
| 203 | |
| 204 | # Block until resource deletion has completed or timed-out |
| 205 | call_until_true(is_deletion_complete, 10, 1) |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 206 | |
| 207 | |
| 208 | class ComputeFuzzClientTest(TestCase): |
| 209 | |
| 210 | """ |
| 211 | Base test case class for OpenStack Compute API (Nova) |
| 212 | that uses the Tempest REST fuzz client libs for calling the API. |
| 213 | """ |
| 214 | |
| 215 | manager_class = manager.ComputeFuzzClientManager |