def test_delete(self): if do_not_delete_instance(): CONFIG.get_report().log("TESTS_DO_NOT_DELETE_INSTANCE=True was " "specified, skipping delete...") raise SkipTest("TESTS_DO_NOT_DELETE_INSTANCE was specified.") global dbaas if not hasattr(instance_info, "initial_result"): raise SkipTest("Instance was never created, skipping test...") # Update the report so the logs inside the instance will be saved. CONFIG.get_report().update() dbaas.instances.delete(instance_info.id) instance_info.deleted_at = timeutils.utcnow().isoformat() attempts = 0 try: time.sleep(1) result = True while result is not None: attempts += 1 time.sleep(1) result = dbaas.instances.get(instance_info.id) assert_equal(200, dbaas.last_http_code) assert_equal("SHUTDOWN", result.status) except exceptions.NotFound: pass except Exception as ex: fail("A failure occured when trying to GET instance %s for the %d" " time: %s" % (str(instance_info.id), attempts, str(ex)))
def resize_should_not_delete_users(self): """Resize should not delete users.""" # Resize has an incredibly weird bug where users are deleted after # a resize. The code below is an attempt to catch this while proceeding # with the rest of the test (note the use of runs_after). if USE_IP: self.connection.connect() if not self.connection.is_connected(): # Ok, this is def. a failure, but before we toss up an error # lets recreate to see how far we can get. CONFIG.get_report().log("Having to recreate the test_user! Resizing killed it!") self.log_current_users() self.create_user() fail("Somehow, the resize made the test user disappear.")
def test_create(self): databases = [] databases.append({"name": "firstdb", "character_set": "latin2", "collate": "latin2_general_ci"}) databases.append({"name": "db2"}) instance_info.databases = databases users = [] users.append({"name": "lite", "password": "******", "databases": [{"name": "firstdb"}]}) instance_info.users = users if VOLUME_SUPPORT: instance_info.volume = {'size': 1} else: instance_info.volume = None if create_new_instance(): instance_info.initial_result = dbaas.instances.create( instance_info.name, instance_info.dbaas_flavor_href, instance_info.volume, databases, users, availability_zone="nova") assert_equal(200, dbaas.last_http_code) else: id = existing_instance() instance_info.initial_result = dbaas.instances.get(id) result = instance_info.initial_result instance_info.id = result.id report = CONFIG.get_report() report.log("Instance UUID = %s" % instance_info.id) if create_new_instance(): assert_equal("BUILD", instance_info.initial_result.status) else: report.log("Test was invoked with TESTS_USE_INSTANCE_ID=%s, so no " "instance was actually created." % id) # Check these attrs only are returned in create response expected_attrs = ['created', 'flavor', 'addresses', 'id', 'links', 'name', 'status', 'updated'] if ROOT_ON_CREATE: expected_attrs.append('password') if VOLUME_SUPPORT: expected_attrs.append('volume') if CONFIG.trove_dns_support: expected_attrs.append('hostname') with CheckInstance(result._info) as check: if create_new_instance(): check.attrs_exist(result._info, expected_attrs, msg="Create response") # Don't CheckInstance if the instance already exists. check.flavor() check.links(result._info['links']) if VOLUME_SUPPORT: check.volume()
def __init__(self, sleep_time=10, timeout=1200): self.def_sleep_time = sleep_time self.def_timeout = timeout self.instance_info = instance_info self.auth_client = create_dbaas_client(self.instance_info.user) self.unauth_client = None self.report = CONFIG.get_report() self._test_helper = None
def test_assign_name_to_instance_using_patch(self): # test assigning a name to an instance new_name = 'new_name_1' report = CONFIG.get_report() report.log("instance_info.id: %s" % instance_info.id) report.log("instance name:%s" % instance_info.name) report.log("instance new name:%s" % new_name) instance_info.dbaas.instances.edit(instance_info.id, name=new_name) assert_equal(202, instance_info.dbaas.last_http_code) check = instance_info.dbaas.instances.get(instance_info.id) assert_equal(200, instance_info.dbaas.last_http_code) assert_equal(check.name, new_name) # Restore instance name instance_info.dbaas.instances.edit(instance_info.id, name=instance_info.name) assert_equal(202, instance_info.dbaas.last_http_code)
def test_assign_config_and_name_to_instance_using_patch(self): # test assigning a configuration and name to an instance new_name = 'new_name' report = CONFIG.get_report() report.log("instance_info.id: %s" % instance_info.id) report.log("configuration_info: %s" % configuration_info) report.log("configuration_info.id: %s" % configuration_info.id) report.log("instance name:%s" % instance_info.name) report.log("instance new name:%s" % new_name) saved_name = instance_info.name config_id = configuration_info.id instance_info.dbaas.instances.edit(instance_info.id, configuration=config_id, name=new_name) assert_equal(202, instance_info.dbaas.last_http_code) check = instance_info.dbaas.instances.get(instance_info.id) assert_equal(200, instance_info.dbaas.last_http_code) assert_equal(check.name, new_name) # restore instance name instance_info.dbaas.instances.edit(instance_info.id, name=saved_name) assert_equal(202, instance_info.dbaas.last_http_code) instance = instance_info.dbaas.instances.get(instance_info.id) assert_equal('RESTART_REQUIRED', instance.status) # restart to be sure configuration is applied instance_info.dbaas.instances.restart(instance_info.id) assert_equal(202, instance_info.dbaas.last_http_code) sleep(2) def result_is_active(): instance = instance_info.dbaas.instances.get( instance_info.id) if instance.status == "ACTIVE": return True else: assert_equal("REBOOT", instance.status) return False poll_until(result_is_active) # test assigning a configuration to an instance that # already has an assigned configuration with patch config_id = configuration_info.id assert_raises(exceptions.BadRequest, instance_info.dbaas.instances.edit, instance_info.id, configuration=config_id)
def test_assign_config_and_name_to_instance_using_patch(self): # test assigning a configuration and name to an instance new_name = 'new_name' report = CONFIG.get_report() report.log("instance_info.id: %s" % instance_info.id) report.log("configuration_info: %s" % configuration_info) report.log("configuration_info.id: %s" % configuration_info.id) report.log("instance name:%s" % instance_info.name) report.log("instance new name:%s" % new_name) saved_name = instance_info.name config_id = configuration_info.id instance_info.dbaas.instances.edit(instance_info.id, configuration=config_id, name=new_name) assert_equal(202, instance_info.dbaas.last_http_code) check = instance_info.dbaas.instances.get(instance_info.id) assert_equal(200, instance_info.dbaas.last_http_code) assert_equal(check.name, new_name) # restore instance name instance_info.dbaas.instances.edit(instance_info.id, name=saved_name) assert_equal(202, instance_info.dbaas.last_http_code) instance = instance_info.dbaas.instances.get(instance_info.id) assert_equal('RESTART_REQUIRED', instance.status) # restart to be sure configuration is applied instance_info.dbaas.instances.restart(instance_info.id) assert_equal(202, instance_info.dbaas.last_http_code) sleep(2) def result_is_active(): instance = instance_info.dbaas.instances.get(instance_info.id) if instance.status == "ACTIVE": return True else: assert_equal("REBOOT", instance.status) return False poll_until(result_is_active) # test assigning a configuration to an instance that # already has an assigned configuration with patch config_id = configuration_info.id assert_raises(exceptions.BadRequest, instance_info.dbaas.instances.edit, instance_info.id, configuration=config_id)
def test_instance_created(self): # This version just checks the REST API status. def result_is_active(): instance = dbaas.instances.get(instance_info.id) if instance.status == "ACTIVE": return True else: # If its not ACTIVE, anything but BUILD must be # an error. assert_equal("BUILD", instance.status) if instance_info.volume is not None: assert_equal(instance.volume.get('used', None), None) return False poll_until(result_is_active) dbaas.instances.get(instance_info.id) report = CONFIG.get_report() report.log("Created an instance, ID = %s." % instance_info.id) report.log("TIP:") report.log("Rerun the tests with TESTS_USE_INSTANCE_ID=%s " "to skip ahead to this point." % instance_info.id) report.log("Add TESTS_DO_NOT_DELETE_INSTANCE=True to avoid deleting " "the instance at the end of the tests.")
def test_instance_created(self): """Wait for normal instance to be created.""" def result_is_active(): instance = dbaas.instances.get(instance_info.id) if instance.status in CONFIG.running_status: return True else: # If its not ACTIVE, anything but BUILD must be # an error. assert_equal("BUILD", instance.status) if instance_info.volume is not None: assert_equal(instance.volume.get('used', None), None) return False poll_until(result_is_active, sleep_time=5) dbaas.instances.get(instance_info.id) report = CONFIG.get_report() report.log("Created an instance, ID = %s." % instance_info.id) report.log("TIP:") report.log("Rerun the tests with TESTS_USE_INSTANCE_ID=%s " "to skip ahead to this point." % instance_info.id) report.log("Add TESTS_DO_NOT_DELETE_INSTANCE=True to avoid deleting " "the instance at the end of the tests.")
def log_current_users(self): users = self.dbaas.users.list(self.instance_id) CONFIG.get_report().log("Current user count = %d" % len(users)) for user in users: CONFIG.get_report().log("\t" + str(user))
class TestRunner(object): """ Base class for all 'Runner' classes. The Runner classes are those that actually do the work. The 'Group' classes are set up with decorators that control how the tests flow, and are used to organized the tests - however they are typically set up to just call a corresponding method in a Runner class. A Runner class can be overridden if a particular set of tests needs to have DataStore specific coding. The corresponding Group class will try to first load a DataStore specific class, and then fall back to the generic one if need be. For example, the NegativeClusterActionsGroup class specifies a runner_base_name of NegativeClusterActionsRunner. If the manager of the default datastore is mongodb, then the MongodbNegativeClusterActionsRunner is used instead. The prefix is created by capitalizing the name of the manager - overriding classes *must* follow this naming convention to be automatically used. The main assumption made here is that if a manager is used for different datastore versions, then the overriding runner should also be valid for the same datastore versions. """ USE_INSTANCE_ID_FLAG = 'TESTS_USE_INSTANCE_ID' DO_NOT_DELETE_INSTANCE_FLAG = 'TESTS_DO_NOT_DELETE_INSTANCE' VOLUME_SUPPORT = CONFIG.get('trove_volume_support', True) EPHEMERAL_SUPPORT = not VOLUME_SUPPORT and CONFIG.get('device_path', None) ROOT_PARTITION = not (VOLUME_SUPPORT or CONFIG.get('device_path', None)) GUEST_CAST_WAIT_TIMEOUT_SEC = 60 # Here's where the info for the 'main' test instance goes instance_info = InstanceTestInfo() report = CONFIG.get_report() def __init__(self, sleep_time=10, timeout=1200): self.def_sleep_time = sleep_time self.def_timeout = timeout self.instance_info.name = "TEST_" + datetime.datetime.strftime( datetime.datetime.now(), '%Y_%m_%d__%H_%M_%S') self.instance_info.dbaas_datastore = CONFIG.dbaas_datastore self.instance_info.dbaas_datastore_version = ( CONFIG.dbaas_datastore_version) self.instance_info.user = CONFIG.users.find_user_by_name('alt_demo') if self.VOLUME_SUPPORT: self.instance_info.volume_size = CONFIG.get('trove_volume_size', 1) self.instance_info.volume = { 'size': self.instance_info.volume_size } else: self.instance_info.volume_size = None self.instance_info.volume = None self.instance_info.nics = None shared_network = CONFIG.get('shared_network', None) if shared_network: self.instance_info.nics = [{'net-id': shared_network}] self._auth_client = None self._unauth_client = None self._admin_client = None self._swift_client = None self._nova_client = None self._test_helper = None self._servers = {} # Attempt to register the main instance. If it doesn't # exist, this will still set the 'report' and 'client' objects # correctly in LogOnFail inst_ids = [] if hasattr(self.instance_info, 'id') and self.instance_info.id: inst_ids = [self.instance_info.id] self.register_debug_inst_ids(inst_ids) @classmethod def fail(cls, message): asserts.fail(message) @classmethod def assert_is_sublist(cls, sub_list, full_list, message=None): if not message: message = 'Unexpected sublist' try: message += ": sub_list '%s' (full_list '%s')." % (sub_list, full_list) except TypeError: pass return cls.assert_true(set(sub_list).issubset(full_list), message) @classmethod def assert_unique(cls, iterable, message=None): """Assert that a given iterable contains only unique elements. """ cls.assert_equal(len(iterable), len(set(iterable)), message) @classmethod def assert_true(cls, condition, message=None): asserts.assert_true(condition, message=message) @classmethod def assert_false(cls, condition, message=None): asserts.assert_false(condition, message=message) @classmethod def assert_is_none(cls, value, message=None): asserts.assert_is_none(value, message=message) @classmethod def assert_is_not_none(cls, value, message=None): asserts.assert_is_not_none(value, message=message) @classmethod def assert_list_elements_equal(cls, expected, actual, message=None): """Assert that two lists contain same elements (with same multiplicities) ignoring the element order. """ return cls.assert_equal(sorted(expected), sorted(actual), message) @classmethod def assert_equal(cls, expected, actual, message=None): if not message: message = 'Unexpected value' try: message += ": '%s' (expected '%s')." % (actual, expected) except TypeError: pass asserts.assert_equal(expected, actual, message=message) @classmethod def assert_not_equal(cls, expected, actual, message=None): if not message: message = 'Expected different value than' try: message += ": '%s'." % expected except TypeError: pass asserts.assert_not_equal(expected, actual, message=message) @property def test_helper(self): return self._test_helper @test_helper.setter def test_helper(self, test_helper): self._test_helper = test_helper @property def auth_client(self): return self._create_authorized_client() def _create_authorized_client(self): """Create a client from the normal 'authorized' user.""" return create_dbaas_client(self.instance_info.user) @property def unauth_client(self): return self._create_unauthorized_client() def _create_unauthorized_client(self): """Create a client from a different 'unauthorized' user to facilitate negative testing. """ requirements = Requirements(is_admin=False) other_user = CONFIG.users.find_user( requirements, black_list=[self.instance_info.user.auth_user]) return create_dbaas_client(other_user) @property def admin_client(self): return self._create_admin_client() def _create_admin_client(self): """Create a client from an admin user.""" requirements = Requirements(is_admin=True, services=["swift"]) admin_user = CONFIG.users.find_user(requirements) return create_dbaas_client(admin_user) @property def swift_client(self): return self._create_swift_client() def _create_swift_client(self): """Create a swift client from the admin user details.""" requirements = Requirements(is_admin=True, services=["swift"]) user = CONFIG.users.find_user(requirements) os_options = {'region_name': CONFIG.trove_client_region_name} return swiftclient.client.Connection( authurl=CONFIG.nova_client['auth_url'], user=user.auth_user, key=user.auth_key, tenant_name=user.tenant, auth_version='2.0', os_options=os_options) @property def nova_client(self): return create_nova_client(self.instance_info.user) def register_debug_inst_ids(self, inst_ids): """Method to 'register' an instance ID (or list of instance IDs) for debug purposes on failure. Note that values are only appended here, not overridden. The LogOnFail class will handle 'missing' IDs. """ LogOnFail.add_inst_ids(inst_ids) LogOnFail.set_client(self.admin_client) LogOnFail.set_report(self.report) def get_client_tenant(self, client): tenant_name = client.real_client.client.tenant service_url = client.real_client.client.service_url su_parts = service_url.split('/') tenant_id = su_parts[-1] return tenant_name, tenant_id def assert_raises(self, expected_exception, expected_http_code, client, client_cmd, *cmd_args, **cmd_kwargs): if client: # Make sure that the client_cmd comes from the same client that # was passed in, otherwise asserting the client code may fail. cmd_clz = client_cmd.im_self cmd_clz_name = cmd_clz.__class__.__name__ client_attrs = [ attr[0] for attr in inspect.getmembers(client.real_client) if '__' not in attr[0] ] match = [ getattr(client, a) for a in client_attrs if getattr(client, a).__class__.__name__ == cmd_clz_name ] self.assert_true( any(match), "Could not find method class in client: %s" % client_attrs) self.assert_equal( match[0], cmd_clz, "Test error: client_cmd must be from client obj") asserts.assert_raises(expected_exception, client_cmd, *cmd_args, **cmd_kwargs) self.assert_client_code(client, expected_http_code) def get_datastore_config_property(self, name, datastore=None): """Get a Trove configuration property for a given datastore. Use the current instance's datastore if None. """ try: datastore = datastore or self.instance_info.dbaas_datastore return CONF.get(datastore).get(name) except NoSuchOptError: return CONF.get(name) @property def is_using_existing_instance(self): return TestRunner.using_existing_instance() @staticmethod def using_existing_instance(): return TestRunner.has_env_flag(TestRunner.USE_INSTANCE_ID_FLAG) @staticmethod def has_env_flag(flag_name): """Return whether a given flag was set.""" return os.environ.get(flag_name, None) is not None def get_existing_instance(self): if self.is_using_existing_instance: instance_id = os.environ.get(self.USE_INSTANCE_ID_FLAG) return self.get_instance(instance_id) return None @property def has_do_not_delete_instance(self): return self.has_env_flag(self.DO_NOT_DELETE_INSTANCE_FLAG) def assert_instance_action(self, instance_ids, expected_states): if expected_states: self.assert_all_instance_states( instance_ids if utils.is_collection(instance_ids) else [instance_ids], expected_states) def assert_client_code(self, client, expected_http_code): if client and expected_http_code is not None: self.assert_equal(expected_http_code, client.last_http_code, "Unexpected client status code") def assert_all_instance_states(self, instance_ids, expected_states, fast_fail_status=None, require_all_states=False): self.report.log("Waiting for states (%s) for instances: %s" % (expected_states, instance_ids)) def _make_fn(inst_id): return lambda: self._assert_instance_states( inst_id, expected_states, fast_fail_status=fast_fail_status, require_all_states=require_all_states) tasks = [ build_polling_task(_make_fn(instance_id), sleep_time=self.def_sleep_time, time_out=self.def_timeout) for instance_id in instance_ids ] poll_until(lambda: all(poll_task.ready() for poll_task in tasks), sleep_time=self.def_sleep_time, time_out=self.def_timeout) for task in tasks: if task.has_result(): self.assert_true( task.poll_result(), "Some instances failed to acquire all expected states.") elif task.has_exception(): self.fail(str(task.poll_exception())) def _assert_instance_states(self, instance_id, expected_states, fast_fail_status=None, require_all_states=False): """Keep polling for the expected instance states until the instance acquires either the last or fast-fail state. If the instance state does not match the state expected at the time of polling (and 'require_all_states' is not set) the code assumes the instance had already acquired before and moves to the next expected state. """ self.report.log("Waiting for states (%s) for instance: %s" % (expected_states, instance_id)) if fast_fail_status is None: fast_fail_status = ['ERROR', 'FAILED'] found = False for status in expected_states: found_current = self._has_status(instance_id, status, fast_fail_status=fast_fail_status) if require_all_states or found or found_current: found = True start_time = timer.time() try: if not found_current: poll_until(lambda: self._has_status(instance_id, status, fast_fail_status= fast_fail_status), sleep_time=self.def_sleep_time, time_out=self.def_timeout) self.report.log( "Instance '%s' has gone '%s' in %s." % (instance_id, status, self._time_since(start_time))) except exception.PollTimeOut: self.report.log( "Status of instance '%s' did not change to '%s' " "after %s." % (instance_id, status, self._time_since(start_time))) return False else: self.report.log( "Instance state was not '%s', moving to the next expected " "state." % status) return found def _time_since(self, start_time): return '%.1fs' % (timer.time() - start_time) def assert_all_gone(self, instance_ids, expected_last_status): self._wait_all_deleted( instance_ids if utils.is_collection(instance_ids) else [instance_ids], expected_last_status) def assert_pagination_match(self, list_page, full_list, start_idx, end_idx): self.assert_equal( full_list[start_idx:end_idx], list(list_page), "List page does not match the expected full " "list section.") def _wait_all_deleted(self, instance_ids, expected_last_status): self.report.log("Waiting for instances to be gone: %s (status %s)" % (instance_ids, expected_last_status)) def _make_fn(inst_id): return lambda: self._wait_for_delete(inst_id, expected_last_status) tasks = [ build_polling_task(_make_fn(instance_id), sleep_time=self.def_sleep_time, time_out=self.def_timeout) for instance_id in instance_ids ] poll_until(lambda: all(poll_task.ready() for poll_task in tasks), sleep_time=self.def_sleep_time, time_out=self.def_timeout) for task in tasks: if task.has_result(): self.assert_true(task.poll_result(), "Some instances were not removed.") elif task.has_exception(): self.fail(str(task.poll_exception())) def _wait_for_delete(self, instance_id, expected_last_status): self.report.log("Waiting for instance to be gone: %s (status %s)" % (instance_id, expected_last_status)) start_time = timer.time() try: self._poll_while(instance_id, expected_last_status, sleep_time=self.def_sleep_time, time_out=self.def_timeout) except exceptions.NotFound: self.report.log("Instance was removed in %s." % self._time_since(start_time)) return True except exception.PollTimeOut: self.report.log("Instance '%s' still existed after %s." % (instance_id, self._time_since(start_time))) return False def _poll_while(self, instance_id, expected_status, sleep_time=1, time_out=None): poll_until(lambda: not self._has_status(instance_id, expected_status), sleep_time=sleep_time, time_out=time_out) def _has_status(self, instance_id, status, fast_fail_status=None): fast_fail_status = fast_fail_status or [] instance = self.get_instance(instance_id, self.admin_client) self.report.log("Polling instance '%s' for state '%s', was '%s'." % (instance_id, status, instance.status)) if instance.status in fast_fail_status: raise RuntimeError( "Instance '%s' acquired a fast-fail status: %s" % (instance_id, instance.status)) return instance.status == status def get_server(self, instance_id): server = None if instance_id in self._servers: server = self._servers[instance_id] else: instance = self.get_instance(instance_id) self.report.log("Getting server for instance: %s" % instance) for nova_server in self.nova_client.servers.list(): if str(nova_server.name) == instance.name: server = nova_server break if server: self._servers[instance_id] = server return server def assert_server_group_exists(self, instance_id): """Check that the Nova instance associated with instance_id belongs to a server group, and return the id. """ server = self.get_server(instance_id) self.assert_is_not_none( server, "Could not find Nova server for '%s'" % instance_id) server_group = None server_groups = self.nova_client.server_groups.list() for sg in server_groups: if server.id in sg.members: server_group = sg break if server_group is None: self.fail("Could not find server group for Nova instance %s" % server.id) return server_group.id def assert_server_group_gone(self, srv_grp_id): """Ensure that the server group is no longer present.""" server_group = None server_groups = self.nova_client.server_groups.list() for sg in server_groups: if sg.id == srv_grp_id: server_group = sg break if server_group: self.fail("Found left-over server group: %s" % server_group) def get_instance(self, instance_id, client=None): client = client or self.auth_client return client.instances.get(instance_id) def extract_ipv4s(self, ips): ipv4s = [str(ip) for ip in ips if netaddr.valid_ipv4(ip)] if not ipv4s: self.fail("No IPV4 ip found") return ipv4s def get_instance_host(self, instance_id=None): instance_id = instance_id or self.instance_info.id instance = self.get_instance(instance_id) host = self.extract_ipv4s(instance._info['ip'])[0] self.report.log("Found host %s for instance %s." % (host, instance_id)) return host def build_flavor(self, flavor_id=2, volume_size=1): return {"flavorRef": flavor_id, "volume": {"size": volume_size}} def get_flavor(self, flavor_name): flavors = self.auth_client.find_flavors_by_name(flavor_name) self.assert_equal( 1, len(flavors), "Unexpected number of flavors with name '%s' found." % flavor_name) flavor = flavors[0] self.assert_is_not_none(flavor, "Flavor '%s' not found." % flavor_name) return flavor def get_instance_flavor(self, fault_num=None): name_format = 'instance%s%s_flavor_name' default = 'm1.tiny' fault_str = '' eph_str = '' if fault_num: fault_str = '_fault_%d' % fault_num if self.EPHEMERAL_SUPPORT: eph_str = '_eph' default = 'eph.rd-tiny' name = name_format % (fault_str, eph_str) flavor_name = CONFIG.values.get(name, default) return self.get_flavor(flavor_name) def get_flavor_href(self, flavor): return self.auth_client.find_flavor_self_href(flavor) def copy_dict(self, d, ignored_keys=None): return { k: v for k, v in d.items() if not ignored_keys or k not in ignored_keys } def create_test_helper_on_instance(self, instance_id): """Here we add a helper user/database, if any, to a given instance via the Trove API. These are for internal use by the test framework and should not be changed by individual test-cases. """ database_def, user_def, root_def = self.build_helper_defs() client = self.auth_client if database_def: self.report.log("Creating a helper database '%s' on instance: %s" % (database_def['name'], instance_id)) client.databases.create(instance_id, [database_def]) self.wait_for_database_create(client, instance_id, [database_def]) if user_def: self.report.log( "Creating a helper user '%s:%s' on instance: %s" % (user_def['name'], user_def['password'], instance_id)) client.users.create(instance_id, [user_def]) self.wait_for_user_create(client, instance_id, [user_def]) if root_def: # Not enabling root on a single instance of the cluster here # because we want to test the cluster root enable instead. pass def build_helper_defs(self): """Build helper database and user JSON definitions if credentials are defined by the helper. """ database_def = None def _get_credentials(creds): if creds: username = creds.get('name') if username: password = creds.get('password', '') databases = [] if database_def: databases.append(database_def) return { 'name': username, 'password': password, 'databases': databases } return None credentials = self.test_helper.get_helper_credentials() if credentials: database = credentials.get('database') if database: database_def = {'name': database} credentials_root = self.test_helper.get_helper_credentials_root() return (database_def, _get_credentials(credentials), _get_credentials(credentials_root)) def wait_for_user_create(self, client, instance_id, expected_user_defs): expected_user_names = { user_def['name'] for user_def in expected_user_defs } self.report.log("Waiting for all created users to appear in the " "listing: %s" % expected_user_names) def _all_exist(): all_users = self.get_user_names(client, instance_id) return all(usr in all_users for usr in expected_user_names) try: poll_until(_all_exist, time_out=self.GUEST_CAST_WAIT_TIMEOUT_SEC) self.report.log("All users now exist on the instance.") except exception.PollTimeOut: self.fail("Some users were not created within the poll " "timeout: %ds" % self.GUEST_CAST_WAIT_TIMEOUT_SEC) def get_user_names(self, client, instance_id): full_list = client.users.list(instance_id) return {user.name: user for user in full_list} def wait_for_database_create(self, client, instance_id, expected_database_defs): expected_db_names = { db_def['name'] for db_def in expected_database_defs } self.report.log("Waiting for all created databases to appear in the " "listing: %s" % expected_db_names) def _all_exist(): all_dbs = self.get_db_names(client, instance_id) return all(db in all_dbs for db in expected_db_names) try: poll_until(_all_exist, time_out=self.GUEST_CAST_WAIT_TIMEOUT_SEC) self.report.log("All databases now exist on the instance.") except exception.PollTimeOut: self.fail("Some databases were not created within the poll " "timeout: %ds" % self.GUEST_CAST_WAIT_TIMEOUT_SEC) def get_db_names(self, client, instance_id): full_list = client.databases.list(instance_id) return {database.name: database for database in full_list} def create_initial_configuration(self, expected_http_code): client = self.auth_client dynamic_config = self.test_helper.get_dynamic_group() non_dynamic_config = self.test_helper.get_non_dynamic_group() values = dynamic_config or non_dynamic_config if values: json_def = json.dumps(values) result = client.configurations.create( 'initial_configuration_for_create_tests', json_def, "Configuration group used by create tests.", datastore=self.instance_info.dbaas_datastore, datastore_version=self.instance_info.dbaas_datastore_version) self.assert_client_code(client, expected_http_code) return (result.id, dynamic_config is None) return (None, False)
def setUp(self): self.instance = instance_info self.rd_client = create_dbaas_client(self.instance.user) self.report = CONFIG.get_report()
def test_create(self): databases = [] databases.append({"name": "firstdb", "character_set": "latin2", "collate": "latin2_general_ci"}) databases.append({"name": "db2"}) instance_info.databases = databases users = [] users.append({"name": "lite", "password": "******", "databases": [{"name": "firstdb"}]}) instance_info.users = users instance_info.dbaas_datastore = CONFIG.dbaas_datastore instance_info.dbaas_datastore_version = CONFIG.dbaas_datastore_version if VOLUME_SUPPORT: instance_info.volume = {'size': CONFIG.get('trove_volume_size', 2)} else: instance_info.volume = None if create_new_instance(): instance_info.initial_result = dbaas.instances.create( instance_info.name, instance_info.dbaas_flavor_href, instance_info.volume, databases, users, nics=instance_info.nics, availability_zone="nova", datastore=instance_info.dbaas_datastore, datastore_version=instance_info.dbaas_datastore_version) assert_equal(200, dbaas.last_http_code) else: id = existing_instance() instance_info.initial_result = dbaas.instances.get(id) result = instance_info.initial_result instance_info.id = result.id instance_info.dbaas_datastore_version = result.datastore['version'] report = CONFIG.get_report() report.log("Instance UUID = %s" % instance_info.id) if create_new_instance(): assert_equal("BUILD", instance_info.initial_result.status) else: report.log("Test was invoked with TESTS_USE_INSTANCE_ID=%s, so no " "instance was actually created." % id) # Check these attrs only are returned in create response allowed_attrs = ['created', 'flavor', 'addresses', 'id', 'links', 'name', 'status', 'updated', 'datastore', 'fault', 'region', 'service_status_updated', 'access', 'operating_status'] if ROOT_ON_CREATE: allowed_attrs.append('password') if VOLUME_SUPPORT: allowed_attrs.append('volume') if CONFIG.trove_dns_support: allowed_attrs.append('hostname') with CheckInstance(result._info) as check: if create_new_instance(): check.contains_allowed_attrs( result._info, allowed_attrs, msg="Create response") # Don't CheckInstance if the instance already exists. check.flavor() check.datastore() check.links(result._info['links']) if VOLUME_SUPPORT: check.volume()
class TestRunner(object): """ Base class for all 'Runner' classes. The Runner classes are those that actually do the work. The 'Group' classes are set up with decorators that control how the tests flow, and are used to organized the tests - however they are typically set up to just call a corresponding method in a Runner class. A Runner class can be overridden if a particular set of tests needs to have DataStore specific coding. The corresponding Group class will try to first load a DataStore specific class, and then fall back to the generic one if need be. For example, the NegativeClusterActionsGroup class specifies a runner_base_name of NegativeClusterActionsRunner. If the manager of the default datastore is mongodb, then the MongodbNegativeClusterActionsRunner is used instead. The prefix is created by capitalizing the name of the manager - overriding classes *must* follow this naming convention to be automatically used. The main assumption made here is that if a manager is used for different datastore versions, then the overriding runner should also be valid for the same datastore versions. """ USE_INSTANCE_ID_FLAG = 'TESTS_USE_INSTANCE_ID' DO_NOT_DELETE_INSTANCE_FLAG = 'TESTS_DO_NOT_DELETE_INSTANCE' VOLUME_SUPPORT = CONFIG.get('trove_volume_support', True) EPHEMERAL_SUPPORT = not VOLUME_SUPPORT and CONFIG.get('device_path', None) ROOT_PARTITION = not (VOLUME_SUPPORT or CONFIG.get('device_path', None)) report = CONFIG.get_report() def __init__(self, sleep_time=10, timeout=1200): self.def_sleep_time = sleep_time self.def_timeout = timeout self.instance_info = instance_info instance_info.dbaas_datastore = CONFIG.dbaas_datastore instance_info.dbaas_datastore_version = CONFIG.dbaas_datastore_version if self.VOLUME_SUPPORT: instance_info.volume = {'size': CONFIG.get('trove_volume_size', 1)} else: instance_info.volume = None self.auth_client = create_dbaas_client(self.instance_info.user) self._unauth_client = None self._admin_client = None self._swift_client = None self._nova_client = None self._test_helper = None self._servers = {} @classmethod def fail(cls, message): asserts.fail(message) @classmethod def assert_is_sublist(cls, sub_list, full_list, message=None): return cls.assert_true(set(sub_list).issubset(full_list), message) @classmethod def assert_unique(cls, iterable, message=None): """Assert that a given iterable contains only unique elements. """ cls.assert_equal(len(iterable), len(set(iterable)), message) @classmethod def assert_true(cls, condition, message=None): asserts.assert_true(condition, message=message) @classmethod def assert_false(cls, condition, message=None): asserts.assert_false(condition, message=message) @classmethod def assert_is_none(cls, value, message=None): asserts.assert_is_none(value, message=message) @classmethod def assert_is_not_none(cls, value, message=None): asserts.assert_is_not_none(value, message=message) @classmethod def assert_list_elements_equal(cls, expected, actual, message=None): """Assert that two lists contain same elements (with same multiplicities) ignoring the element order. """ return cls.assert_equal(sorted(expected), sorted(actual), message) @classmethod def assert_equal(cls, expected, actual, message=None): if not message: message = 'Unexpected value' try: message += ": '%s' (expected '%s')." % (actual, expected) except TypeError: pass asserts.assert_equal(expected, actual, message=message) @classmethod def assert_not_equal(cls, expected, actual, message=None): if not message: message = 'Expected different value than' try: message += ": '%s'." % expected except TypeError: pass asserts.assert_not_equal(expected, actual, message=message) @property def test_helper(self): return self._test_helper @test_helper.setter def test_helper(self, test_helper): self._test_helper = test_helper @property def unauth_client(self): if not self._unauth_client: self._unauth_client = self._create_unauthorized_client() return self._unauth_client def _create_unauthorized_client(self): """Create a client from a different 'unauthorized' user to facilitate negative testing. """ requirements = Requirements(is_admin=False) other_user = CONFIG.users.find_user( requirements, black_list=[self.instance_info.user.auth_user]) return create_dbaas_client(other_user) @property def nova_client(self): if not self._nova_client: self._nova_client = create_nova_client(self.instance_info.user) return self._nova_client @property def admin_client(self): if not self._admin_client: self._admin_client = self._create_admin_client() return self._admin_client def _create_admin_client(self): """Create a client from an admin user.""" requirements = Requirements(is_admin=True, services=["swift"]) admin_user = CONFIG.users.find_user(requirements) return create_dbaas_client(admin_user) @property def swift_client(self): if not self._swift_client: self._swift_client = self._create_swift_client() return self._swift_client def _create_swift_client(self): """Create a swift client from the admin user details.""" requirements = Requirements(is_admin=True, services=["swift"]) user = CONFIG.users.find_user(requirements) os_options = {'region_name': CONFIG.trove_client_region_name} return swiftclient.client.Connection( authurl=CONFIG.nova_client['auth_url'], user=user.auth_user, key=user.auth_key, tenant_name=user.tenant, auth_version='2.0', os_options=os_options) def get_client_tenant(self, client): tenant_name = client.real_client.client.tenant service_url = client.real_client.client.service_url su_parts = service_url.split('/') tenant_id = su_parts[-1] return tenant_name, tenant_id def assert_raises(self, expected_exception, expected_http_code, client_cmd, *cmd_args, **cmd_kwargs): asserts.assert_raises(expected_exception, client_cmd, *cmd_args, **cmd_kwargs) self.assert_client_code(expected_http_code) def get_datastore_config_property(self, name, datastore=None): """Get a Trove configuration property for a given datastore. Use the current instance's datastore if None. """ try: datastore = datastore or self.instance_info.dbaas_datastore return CONF.get(datastore).get(name) except NoSuchOptError: return CONF.get(name) @property def is_using_existing_instance(self): return self.has_env_flag(self.USE_INSTANCE_ID_FLAG) @staticmethod def has_env_flag(flag_name): """Return whether a given flag was set.""" return os.environ.get(flag_name, None) is not None def get_existing_instance(self): if self.is_using_existing_instance: instance_id = os.environ.get(self.USE_INSTANCE_ID_FLAG) return self.get_instance(instance_id) return None @property def has_do_not_delete_instance(self): return self.has_env_flag(self.DO_NOT_DELETE_INSTANCE_FLAG) def assert_instance_action( self, instance_ids, expected_states, expected_http_code): self.assert_client_code(expected_http_code) if expected_states: self.assert_all_instance_states( instance_ids if utils.is_collection(instance_ids) else [instance_ids], expected_states) def assert_client_code(self, expected_http_code, client=None): if expected_http_code is not None: client = client or self.auth_client self.assert_equal(expected_http_code, client.last_http_code, "Unexpected client status code") def assert_all_instance_states(self, instance_ids, expected_states): tasks = [build_polling_task( lambda: self._assert_instance_states(instance_id, expected_states), sleep_time=self.def_sleep_time, time_out=self.def_timeout) for instance_id in instance_ids] poll_until(lambda: all(poll_task.ready() for poll_task in tasks), sleep_time=self.def_sleep_time, time_out=self.def_timeout) for task in tasks: if task.has_result(): self.assert_true( task.poll_result(), "Some instances failed to acquire all expected states.") elif task.has_exception(): self.fail(str(task.poll_exception())) def _assert_instance_states(self, instance_id, expected_states, fast_fail_status=['ERROR', 'FAILED'], require_all_states=False): """Keep polling for the expected instance states until the instance acquires either the last or fast-fail state. If the instance state does not match the state expected at the time of polling (and 'require_all_states' is not set) the code assumes the instance had already acquired before and moves to the next expected state. """ found = False for status in expected_states: if require_all_states or found or self._has_status( instance_id, status, fast_fail_status=fast_fail_status): found = True start_time = timer.time() try: poll_until(lambda: self._has_status( instance_id, status, fast_fail_status=fast_fail_status), sleep_time=self.def_sleep_time, time_out=self.def_timeout) self.report.log("Instance has gone '%s' in %s." % (status, self._time_since(start_time))) except exception.PollTimeOut: self.report.log( "Status of instance '%s' did not change to '%s' " "after %s." % (instance_id, status, self._time_since(start_time))) return False else: self.report.log( "Instance state was not '%s', moving to the next expected " "state." % status) return found def _time_since(self, start_time): return '%.1fs' % (timer.time() - start_time) def assert_all_gone(self, instance_ids, expected_last_status): self._wait_all_deleted(instance_ids if utils.is_collection(instance_ids) else [instance_ids], expected_last_status) def assert_pagination_match( self, list_page, full_list, start_idx, end_idx): self.assert_equal(full_list[start_idx:end_idx], list(list_page), "List page does not match the expected full " "list section.") def _wait_all_deleted(self, instance_ids, expected_last_status): tasks = [build_polling_task( lambda: self._wait_for_delete(instance_id, expected_last_status), sleep_time=self.def_sleep_time, time_out=self.def_timeout) for instance_id in instance_ids] poll_until(lambda: all(poll_task.ready() for poll_task in tasks), sleep_time=self.def_sleep_time, time_out=self.def_timeout) for task in tasks: if task.has_result(): self.assert_true( task.poll_result(), "Some instances were not removed.") elif task.has_exception(): self.fail(str(task.poll_exception())) def _wait_for_delete(self, instance_id, expected_last_status): start_time = timer.time() try: self._poll_while(instance_id, expected_last_status, sleep_time=self.def_sleep_time, time_out=self.def_timeout) except exceptions.NotFound: self.assert_client_code(404) self.report.log("Instance was removed in %s." % self._time_since(start_time)) return True except exception.PollTimeOut: self.report.log( "Instance '%s' still existed after %s." % (instance_id, self._time_since(start_time))) return False def _poll_while(self, instance_id, expected_status, sleep_time=1, time_out=None): poll_until(lambda: not self._has_status(instance_id, expected_status), sleep_time=sleep_time, time_out=time_out) def _has_status(self, instance_id, status, fast_fail_status=None): fast_fail_status = fast_fail_status or [] instance = self.get_instance(instance_id) self.report.log("Polling instance '%s' for state '%s', was '%s'." % (instance_id, status, instance.status)) if instance.status in fast_fail_status: raise RuntimeError("Instance '%s' acquired a fast-fail status: %s" % (instance_id, instance.status)) return instance.status == status def get_server(self, instance_id): server = None if instance_id in self._servers: server = self._servers[instance_id] else: instance = self.get_instance(instance_id) self.report.log("Getting server for instance: %s" % instance) for nova_server in self.nova_client.servers.list(): if str(nova_server.name) == instance.name: server = nova_server break if server: self._servers[instance_id] = server return server def assert_server_group(self, instance_id, should_exist): """Check that the Nova instance associated with instance_id belongs to a server group, based on the 'should_exist' flag. """ server = self.get_server(instance_id) self.assert_is_not_none(server, "Could not find Nova server for '%s'" % instance_id) server_group = None server_groups = self.nova_client.server_groups.list() for sg in server_groups: if server.id in sg.members: server_group = sg if should_exist and server_group is None: raise ("Could not find server group for Nova instance %s" % server.id) if server_group and not should_exist: raise ("Found left-over server group: %s" % server_group) def get_instance(self, instance_id): return self.auth_client.instances.get(instance_id) def get_instance_host(self, instance_id=None): instance_id = instance_id or self.instance_info.id instance = self.get_instance(instance_id) host = str(instance._info['ip'][0]) self.report.log("Found host %s for instance %s." % (host, instance_id)) return host def build_flavor(self, flavor_id=2, volume_size=1): return {"flavorRef": flavor_id, "volume": {"size": volume_size}} def get_flavor(self, flavor_name): flavors = self.auth_client.find_flavors_by_name(flavor_name) self.assert_equal( 1, len(flavors), "Unexpected number of flavors with name '%s' found." % flavor_name) flavor = flavors[0] self.assert_is_not_none(flavor, "Flavor '%s' not found." % flavor_name) return flavor def copy_dict(self, d, ignored_keys=None): return {k: v for k, v in d.items() if not ignored_keys or k not in ignored_keys} def create_test_helper_on_instance(self, instance_id): """Here we add a helper user/database, if any, to a given instance via the Trove API. These are for internal use by the test framework and should not be changed by individual test-cases. """ database_def, user_def, root_def = self.build_helper_defs() if database_def: self.report.log( "Creating a helper database '%s' on instance: %s" % (database_def['name'], instance_id)) self.auth_client.databases.create(instance_id, [database_def]) if user_def: self.report.log( "Creating a helper user '%s:%s' on instance: %s" % (user_def['name'], user_def['password'], instance_id)) self.auth_client.users.create(instance_id, [user_def]) if root_def: # Not enabling root on a single instance of the cluster here # because we want to test the cluster root enable instead. pass def build_helper_defs(self): """Build helper database and user JSON definitions if credentials are defined by the helper. """ database_def = None def _get_credentials(creds): if creds: username = creds.get('name') if username: password = creds.get('password', '') return {'name': username, 'password': password, 'databases': [{'name': database}]} return None credentials = self.test_helper.get_helper_credentials() if credentials: database = credentials.get('database') if database: database_def = {'name': database} credentials_root = self.test_helper.get_helper_credentials_root() return (database_def, _get_credentials(credentials), _get_credentials(credentials_root))
class FakeTestHugeBackupOnSmallInstance(BackupRestoreMixin): report = CONFIG.get_report() def tweak_fake_guest(self, size): from trove.tests.fakes import guestagent guestagent.BACKUP_SIZE = size @test def test_load_mysql_with_data(self): if not CONFIG.fake_mode: raise SkipTest("Must run in fake mode.") self.tweak_fake_guest(1.9) @test(depends_on=[test_load_mysql_with_data]) def test_create_huge_backup(self): if not CONFIG.fake_mode: raise SkipTest("Must run in fake mode.") self.new_backup = instance_info.dbaas.backups.create( BACKUP_NAME, instance_info.id, BACKUP_DESC) assert_equal(202, instance_info.dbaas.last_http_code) @test(depends_on=[test_create_huge_backup]) def test_verify_huge_backup_completed(self): if not CONFIG.fake_mode: raise SkipTest("Must run in fake mode.") self.verify_backup(self.new_backup.id) @test(depends_on=[test_verify_huge_backup_completed]) def test_try_to_restore_on_small_instance_with_volume(self): if not CONFIG.fake_mode: raise SkipTest("Must run in fake mode.") assert_raises( exceptions.Forbidden, instance_info.dbaas.instances.create, instance_info.name + "_restore", instance_info.dbaas_flavor_href, {'size': 1}, datastore=instance_info.dbaas_datastore, datastore_version=(instance_info.dbaas_datastore_version), restorePoint={"backupRef": self.new_backup.id}) assert_equal(403, instance_info.dbaas.last_http_code) @test(depends_on=[test_verify_huge_backup_completed]) def test_try_to_restore_on_small_instance_with_flavor_only(self): if not CONFIG.fake_mode: raise SkipTest("Must run in fake mode.") self.orig_conf_value = cfg.CONF.get( instance_info.dbaas_datastore).volume_support cfg.CONF.get(instance_info.dbaas_datastore).volume_support = False assert_raises( exceptions.Forbidden, instance_info.dbaas.instances.create, instance_info.name + "_restore", 11, datastore=instance_info.dbaas_datastore, datastore_version=(instance_info.dbaas_datastore_version), restorePoint={"backupRef": self.new_backup.id}) assert_equal(403, instance_info.dbaas.last_http_code) cfg.CONF.get(instance_info.dbaas_datastore ).volume_support = self.orig_conf_value