def expected_default_datastore_configs(): """Returns the expected test configurations for the default datastore defined in the Test Config as dbaas_datastore. """ default_datatstore = CONFIG.get('dbaas_datastore', None) datastore_test_configs = CONFIG.get(default_datatstore, {}) return datastore_test_configs.get("configurations", {})
def create_client_args(user): auth_strategy = None kwargs = {"service_type": "trove", "insecure": CONFIG.values["trove_client_insecure"]} def set_optional(kwargs_name, test_conf_name): value = CONFIG.values.get(test_conf_name, None) if value is not None: kwargs[kwargs_name] = value service_url = CONFIG.get("override_trove_api_url", None) if user.requirements.is_admin: service_url = CONFIG.get("override_admin_trove_api_url", service_url) if service_url: kwargs["service_url"] = service_url auth_strategy = None if user.requirements.is_admin: auth_strategy = CONFIG.get("admin_auth_strategy", CONFIG.auth_strategy) else: auth_strategy = CONFIG.auth_strategy set_optional("region_name", "trove_client_region_name") if CONFIG.values.get("override_trove_api_url_append_tenant", False): kwargs["service_url"] += "/" + user.tenant if auth_strategy == "fake": from troveclient.compat import auth class FakeAuth(auth.Authenticator): def authenticate(self): class FakeCatalog(object): def __init__(self, auth): self.auth = auth def get_public_url(self): return "%s/%s" % (CONFIG.dbaas_url, self.auth.tenant) def get_token(self): return self.auth.tenant return FakeCatalog(self) auth_strategy = FakeAuth if auth_strategy: kwargs["auth_strategy"] = auth_strategy if not user.requirements.is_admin: auth_url = CONFIG.trove_auth_url else: auth_url = CONFIG.values.get("trove_admin_auth_url", CONFIG.trove_auth_url) if CONFIG.values.get("trove_client_cls"): cls_name = CONFIG.trove_client_cls kwargs["client_cls"] = import_class(cls_name) kwargs["tenant"] = user.tenant kwargs["auth_url"] = auth_url return (user.auth_user, user.auth_key), kwargs
def run_initialized_instance_create( self, with_dbs=True, with_users=True, configuration_id=None, expected_states=['BUILD', 'ACTIVE'], expected_http_code=200): # TODO(pmalik): Instance create should return 202 Accepted (cast) # rather than 200 OK (call). name = self.instance_info.name flavor = self._get_instance_flavor() trove_volume_size = CONFIG.get('trove_volume_size', 1) self.init_inst_dbs = (self.test_helper.get_valid_database_definitions() if with_dbs else []) self.init_inst_users = (self.test_helper.get_valid_user_definitions() if with_users else []) if configuration_id: self.init_config_group_id = configuration_id if (self.init_inst_dbs or self.init_inst_users or self.init_config_group_id): info = self.assert_instance_create( name, flavor, trove_volume_size, self.init_inst_dbs, self.init_inst_users, self.init_config_group_id, None, CONFIG.dbaas_datastore, CONFIG.dbaas_datastore_version, expected_states, expected_http_code) self.init_inst_id = info.id else: # There is no need to run this test as it's effectively the same as # the empty instance test. raise SkipTest("No testable initial properties provided.")
def run_initialized_instance_create( self, with_dbs=True, with_users=True, configuration_id=None, expected_states=['BUILD', 'ACTIVE'], expected_http_code=200, create_helper_user=True): name = self.instance_info.name + '_init' flavor = self._get_instance_flavor() trove_volume_size = CONFIG.get('trove_volume_size', 1) self.init_inst_dbs = (self.test_helper.get_valid_database_definitions() if with_dbs else []) self.init_inst_users = (self.test_helper.get_valid_user_definitions() if with_users else []) if configuration_id: self.init_config_group_id = configuration_id if self.is_using_existing_instance: raise SkipTest("Using existing instance.") if (self.init_inst_dbs or self.init_inst_users or self.init_config_group_id): info = self.assert_instance_create( name, flavor, trove_volume_size, self.init_inst_dbs, self.init_inst_users, self.init_config_group_id, None, CONFIG.dbaas_datastore, CONFIG.dbaas_datastore_version, expected_states, expected_http_code, create_helper_user=create_helper_user) self.init_inst_id = info.id else: # There is no need to run this test as it's effectively the same as # the empty instance test. raise SkipTest("No testable initial properties provided.")
def expected_instance_datastore_configs(instance_id): """Given an instance retrieve the expected test configurations for instance's datastore. """ instance = instance_info.dbaas.instances.get(instance_id) datastore_type = instance.datastore['type'] datastore_test_configs = CONFIG.get(datastore_type, {}) return datastore_test_configs.get("configurations", {})
def load_config_file(): global conf if CONFIG.get("examples", None) is None: fail("Missing 'examples' config in test config.") conf = CONFIG.examples global normal_user normal_user = CONFIG.users.find_user_by_name(conf['normal_user_name']) global admin_user admin_user = CONFIG.users.find_user_by_name(conf['admin_user_name'])
def __init__(self, sleep_time=10, timeout=1800): self.def_sleep_time = sleep_time self.def_timeout = timeout self.instance_info.name = "TEST_" + datetime.datetime.strftime( timeutils.utcnow(), '%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") self.instance_info.admin_user = CONFIG.users.find_user( Requirements(is_admin=True)) 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._neutron_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) self.instance_info.flavors = self.nova_client.flavors.list()
def test_create_failure_with_empty_flavor(self): instance_name = "instance-failure-with-empty-flavor" databases = [] if VOLUME_SUPPORT: volume = {'size': CONFIG.get('trove_volume_size', 1)} else: volume = None assert_raises(exceptions.BadRequest, dbaas.instances.create, instance_name, '', volume, databases, nics=instance_info.nics) assert_equal(400, dbaas.last_http_code)
def test_create_failure_with_spaces_for_name(self): if VOLUME_SUPPORT: volume = {'size': CONFIG.get('trove_volume_size', 1)} else: volume = None instance_name = " " databases = [] assert_raises(exceptions.BadRequest, dbaas.instances.create, instance_name, instance_info.dbaas_flavor_href, volume, databases, nics=instance_info.nics) assert_equal(400, dbaas.last_http_code)
def call_xmllint(name, body): try: with open(CONFIG.xml_temp_file, "w") as file: file.write(body) # if CONFIG.get('xml_xsd', None): args = [CONFIG.xml_temp_file] if CONFIG.get("xml_xsd", None): args += ["--schema", CONFIG.xml_xsd] processutils.execute(CONFIG.xmllint_bin, *args, check_exit_code=0, shell=False) except processutils.ProcessExecutionError as pe: fail("Error validating XML! %s" % pe)
def call_xmllint(name, body): try: with open(CONFIG.xml_temp_file, 'w') as file: file.write(body) #if CONFIG.get('xml_xsd', None): args = [CONFIG.xml_temp_file] if CONFIG.get('xml_xsd', None): args += ["--schema", CONFIG.xml_xsd] processutils.execute(CONFIG.xmllint_bin, *args, check_exit_code=0, shell=False) except processutils.ProcessExecutionError as pe: fail("Error validating XML! %s" % pe)
def create_instance(self): volume = None if VOLUME_SUPPORT: volume = {'size': 1} nics = None shared_network = CONFIG.get('shared_network', None) if shared_network: nics = [{'net-id': shared_network}] initial = self.client.instances.create(self.name, self.flavor_id, volume, [], [], nics=nics) self.id = initial.id self._wait_for_active()
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)
def setUp(self): rd_user = test_config.users.find_user( Requirements(is_admin=False, services=["trove"])) self.rd_client = create_dbaas_client(rd_user) self.datastore = self.rd_client.datastores.get( test_config.dbaas_datastore) self.name1 = "test_instance1" self.name2 = "test_instance2" self.volume = {'size': 2} self.instance_id = None self.nics = None shared_network = CONFIG.get('shared_network', None) if shared_network: self.nics = [{'net-id': shared_network}]
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._test_helper = None
def test_create_with_port_id(self): instance_name = "instance-failure-with-port-id" if VOLUME_SUPPORT: volume = {'size': CONFIG.get('trove_volume_size', 1)} else: volume = None databases = [] bad_nic = [{"port-id": "1234"}] assert_raises( exceptions.BadRequest, dbaas.instances.create, instance_name, instance_info.dbaas_flavor_href, volume, databases, nics=bad_nic ) assert_equal(400, dbaas.last_http_code)
def test_create_with_multiple_net_id(self): instance_name = "instance_failure_with_multiple_net_id" volume = {'size': CONFIG.get('trove_volume_size', 1)} databases = [] multi_nics = [ {"net-id": str(uuid.uuid4())}, {"net-id": str(uuid.uuid4())} ] assert_raises( exceptions.BadRequest, dbaas.instances.create, instance_name, instance_info.dbaas_flavor_href, volume, databases, nics=multi_nics ) assert_equal(400, dbaas.last_http_code)
def test_create_failure_with_datastore_version_notfound(self): if VOLUME_SUPPORT: volume = {'size': CONFIG.get('trove_volume_size', 1)} else: volume = None instance_name = "datastore_version_notfound" databases = [] users = [] datastore = CONFIG.dbaas_datastore datastore_version = "nonexistent" assert_raises(exceptions.BadRequest, dbaas.instances.create, instance_name, instance_info.dbaas_flavor_href, volume, databases, users, datastore=datastore, datastore_version=datastore_version, nics=instance_info.nics)
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._nova_client = None self._test_helper = None self._servers = {}
def run_initialized_instance_create(self, with_dbs=True, with_users=True, configuration_id=None, expected_states=['BUILD', 'ACTIVE'], expected_http_code=200, create_helper_user=True): if self.is_using_existing_instance: # The user requested to run the tests using an existing instance. # We therefore skip any scenarios that involve creating new # test instances. raise SkipTest("Using an existing instance.") name = self.instance_info.name + '_init' flavor = self._get_instance_flavor() trove_volume_size = CONFIG.get('trove_volume_size', 1) self.init_inst_dbs = ( self.test_helper.get_valid_database_definitions() if with_dbs else []) self.init_inst_users = (self.test_helper.get_valid_user_definitions() if with_users else []) if configuration_id: self.init_config_group_id = configuration_id if (self.init_inst_dbs or self.init_inst_users or self.init_config_group_id): info = self.assert_instance_create( name, flavor, trove_volume_size, self.init_inst_dbs, self.init_inst_users, self.init_config_group_id, None, CONFIG.dbaas_datastore, CONFIG.dbaas_datastore_version, expected_states, expected_http_code, create_helper_user=create_helper_user) self.init_inst_id = info.id else: # There is no need to run this test as it's effectively the same as # the empty instance test. raise SkipTest("No testable initial properties provided.")
def test_create_with_bad_availability_zone(self): instance_name = "instance-failure-with-bad-az" if VOLUME_SUPPORT: volume = {'size': CONFIG.get('trove_volume_size', 1)} else: volume = None databases = [] result = dbaas.instances.create(instance_name, instance_info.dbaas_flavor_href, volume, databases, availability_zone="BAD_ZONE", nics=instance_info.nics) poll_until(self.instance_in_error(result.id), sleep_time=5, time_out=30) instance = dbaas.instances.get(result.id) assert_equal("ERROR", instance.status) self.delete_async(result.id)
def test_create_failure_with_datastore_notfound(self): if VOLUME_SUPPORT: volume = {'size': CONFIG.get('trove_volume_size', 1)} else: volume = None instance_name = "datastore_notfound" databases = [] users = [] datastore = "nonexistent" try: assert_raises(exceptions.NotFound, dbaas.instances.create, instance_name, instance_info.dbaas_flavor_href, volume, databases, users, datastore=datastore, nics=instance_info.nics) except exceptions.BadRequest as e: assert_equal(e.message, "Datastore '%s' cannot be found." % datastore)
def run_empty_instance_create( self, expected_states=['BUILD', 'ACTIVE'], expected_http_code=200): name = self.instance_info.name flavor = self._get_instance_flavor() trove_volume_size = CONFIG.get('trove_volume_size', 1) info = self.assert_instance_create( name, flavor, trove_volume_size, [], [], None, None, CONFIG.dbaas_datastore, CONFIG.dbaas_datastore_version, expected_states, expected_http_code, create_helper_user=True) # Update the shared instance info. self.instance_info.databases = info.databases self.instance_info.users = info.users self.instance_info.dbaas_datastore = info.dbaas_datastore self.instance_info.dbaas_datastore_version = (info. dbaas_datastore_version) self.instance_info.dbaas_flavor_href = info.dbaas_flavor_href self.instance_info.volume = info.volume self.instance_info.id = info.id
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)} else: self.instance_info.volume = None self._auth_client = None self._unauth_client = None self._admin_client = None self._swift_client = None self._test_helper = None
def set_up(self): """Create client for mgmt instance test (2).""" if not CONFIG.fake_mode: raise SkipTest("This test only works in fake mode.") self.client = create_client(is_admin=True) self.mgmt = self.client.management # Fake nova will fail a server ending with 'test_SERVER_ERROR'." # Fake volume will fail if the size is 13. # TODO(tim.simpson): This would be a lot nicer looking if we used a # traditional mock framework. datastore = {'type': 'mysql', 'version': '5.5'} body = {'datastore': datastore} vol_support = CONFIG.get(datastore['type'], 'mysql')['volume_support'] if vol_support: body.update({'size': 13}) response = self.client.instances.create( 'test_SERVER_ERROR', instance_info.dbaas_flavor_href, body, []) poll_until(lambda: self.client.instances.get(response.id), lambda instance: instance.status == 'ERROR', time_out=10) self.id = response.id
def run_empty_instance_create( self, expected_states=['BUILD', 'ACTIVE'], expected_http_code=200): # TODO(pmalik): Instance create should return 202 Accepted (cast) # rather than 200 OK (call). name = self.instance_info.name flavor = self._get_instance_flavor() trove_volume_size = CONFIG.get('trove_volume_size', 1) info = self.assert_instance_create( name, flavor, trove_volume_size, [], [], None, None, CONFIG.dbaas_datastore, CONFIG.dbaas_datastore_version, expected_states, expected_http_code, create_helper_user=True) # Update the shared instance info. self.instance_info.databases = info.databases self.instance_info.users = info.users self.instance_info.dbaas_datastore = info.dbaas_datastore self.instance_info.dbaas_datastore_version = (info. dbaas_datastore_version) self.instance_info.dbaas_flavor_href = info.dbaas_flavor_href self.instance_info.volume = info.volume self.instance_info.id = info.id
def run_empty_instance_create( self, expected_states=['BUILD', 'ACTIVE'], expected_http_code=200): # TODO(pmalik): Instance create should return 202 Accepted (cast) # rather than 200 OK (call). name = self.instance_info.name flavor = self._get_instance_flavor() trove_volume_size = CONFIG.get('trove_volume_size', 1) info = self.assert_instance_create( name, flavor, trove_volume_size, [], [], None, None, CONFIG.dbaas_datastore, CONFIG.dbaas_datastore_version, expected_states, expected_http_code) # Update the shared instance info. self.instance_info.databases = info.databases self.instance_info.users = info.users self.instance_info.dbaas_datastore = info.dbaas_datastore self.instance_info.dbaas_datastore_version = (info. dbaas_datastore_version) self.instance_info.dbaas_flavor_href = info.dbaas_flavor_href self.instance_info.volume = info.volume self.instance_info.id = info.id
def setUp(self): self.reqs = Requirements(is_admin=False) self.user = CONFIG.users.find_user(self.reqs) self.dbaas = create_dbaas_client(self.user) volume = None if VOLUME_SUPPORT: volume = {"size": 1} shared_network = CONFIG.get('shared_network', None) if shared_network: nics = [{'net-id': shared_network}] self.instance = self.dbaas.instances.create( name="qe_instance", flavor_id=instance_info.dbaas_flavor_href, datastore=instance_info.dbaas_datastore, datastore_version=instance_info.dbaas_datastore_version, volume=volume, databases=[{ "name": "firstdb", "character_set": "latin2", "collate": "latin2_general_ci" }], nics=nics)
def __init__(self): self.dbaas = None # The rich client instance used by these tests. self.dbaas_admin = None # The rich client with admin access. self.dbaas_flavor = None # The flavor object of the instance. self.dbaas_flavor_href = None # The flavor of the instance. self.dbaas_datastore = None # The datastore id self.dbaas_datastore_version = None # The datastore version id self.id = None # The ID of the instance in the database. self.local_id = None # The IP address of the database instance for the user. self.address = None # The management network IP address. self.mgmt_address = None self.nics = None # The dict of type/id for nics used on the instance. shared_network = CONFIG.get('shared_network', None) if shared_network: self.nics = [{'net-id': shared_network}] self.initial_result = None # The initial result from the create call. self.result = None # The instance info returned by the API self.nova_client = None # The instance of novaclient. self.volume_client = None # The instance of the volume client. self.name = None # Test name, generated each test run. self.pid = None # The process ID of the instance. self.user = None # The user instance who owns the instance. self.admin_user = None # The admin user for the management interfaces. self.volume = None # The volume the instance will have. self.volume_id = None # Id for the attached vo186lume self.storage = None # The storage device info for the volumes. self.databases = None # The databases created on the instance. self.host_info = None # Host Info before creating instances self.user_context = None # A regular user context self.users = None # The users created on the instance. self.consumer = create_usage_verifier() self.flavors = None # The cache of Nova flavors.
def create_dbaas_client(user): """Creates a rich client for the Trove API using the test config.""" auth_strategy = None kwargs = { 'service_type': 'database', 'insecure': test_config.values['trove_client_insecure'], } def set_optional(kwargs_name, test_conf_name): value = test_config.values.get(test_conf_name, None) if value is not None: kwargs[kwargs_name] = value force_url = 'override_trove_api_url' in test_config.values service_url = test_config.get('override_trove_api_url', None) if user.requirements.is_admin: service_url = test_config.get('override_admin_trove_api_url', service_url) if service_url: kwargs['service_url'] = service_url auth_strategy = None if user.requirements.is_admin: auth_strategy = test_config.get('admin_auth_strategy', test_config.auth_strategy) else: auth_strategy = test_config.auth_strategy set_optional('region_name', 'trove_client_region_name') if test_config.values.get('override_trove_api_url_append_tenant', False): kwargs['service_url'] += "/" + user.tenant if auth_strategy == 'fake': from troveclient.compat import auth class FakeAuth(auth.Authenticator): def authenticate(self): class FakeCatalog(object): def __init__(self, auth): self.auth = auth def get_public_url(self): return "%s/%s" % (test_config.dbaas_url, self.auth.tenant) def get_token(self): return self.auth.tenant return FakeCatalog(self) auth_strategy = FakeAuth if auth_strategy: kwargs['auth_strategy'] = auth_strategy if not user.requirements.is_admin: auth_url = test_config.trove_auth_url else: auth_url = test_config.values.get('trove_admin_auth_url', test_config.trove_auth_url) if test_config.values.get('trove_client_cls'): cls_name = test_config.trove_client_cls kwargs['client_cls'] = import_class(cls_name) dbaas = Dbaas(user.auth_user, user.auth_key, tenant=user.tenant, auth_url=auth_url, **kwargs) dbaas.authenticate() with Check() as check: check.is_not_none(dbaas.client.auth_token, "Auth token not set!") if not force_url and user.requirements.is_admin: expected_prefix = test_config.dbaas_url actual = dbaas.client.service_url msg = "Dbaas management url was expected to start with %s, but " \ "was %s." % (expected_prefix, actual) check.true(actual.startswith(expected_prefix), msg) return TestClient(dbaas)
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))
def create_client_args(user): auth_strategy = None kwargs = { 'service_type': 'trove', 'insecure': CONFIG.values['trove_client_insecure'], } def set_optional(kwargs_name, test_conf_name): value = CONFIG.values.get(test_conf_name, None) if value is not None: kwargs[kwargs_name] = value service_url = CONFIG.get('override_trove_api_url', None) if user.requirements.is_admin: service_url = CONFIG.get('override_admin_trove_api_url', service_url) if service_url: kwargs['service_url'] = service_url auth_strategy = None if user.requirements.is_admin: auth_strategy = CONFIG.get('admin_auth_strategy', CONFIG.auth_strategy) else: auth_strategy = CONFIG.auth_strategy set_optional('region_name', 'trove_client_region_name') if CONFIG.values.get('override_trove_api_url_append_tenant', False): kwargs['service_url'] += "/" + user.tenant if auth_strategy == 'fake': from troveclient.compat import auth class FakeAuth(auth.Authenticator): def authenticate(self): class FakeCatalog(object): def __init__(self, auth): self.auth = auth def get_public_url(self): return "%s/%s" % (CONFIG.dbaas_url, self.auth.tenant) def get_token(self): return self.auth.tenant return FakeCatalog(self) auth_strategy = FakeAuth if auth_strategy: kwargs['auth_strategy'] = auth_strategy if not user.requirements.is_admin: auth_url = CONFIG.trove_auth_url else: auth_url = CONFIG.values.get('trove_admin_auth_url', CONFIG.trove_auth_url) if CONFIG.values.get('trove_client_cls'): cls_name = CONFIG.trove_client_cls kwargs['client_cls'] = import_class(cls_name) kwargs['tenant'] = user.tenant kwargs['auth_url'] = auth_url return (user.auth_user, user.auth_key), kwargs
def get_address(self): result = self.dbaas_admin.mgmt.instances.show(self.id) return result.ip[0] def get_local_id(self): mgmt_instance = self.dbaas_admin.management.show(self.id) return mgmt_instance.server["local_id"] # The two variables are used below by tests which depend on an instance # existing. instance_info = InstanceTestInfo() dbaas = None # Rich client used throughout this test. dbaas_admin = None # Same as above, with admin privs. ROOT_ON_CREATE = CONFIG.get('root_on_create', False) VOLUME_SUPPORT = CONFIG.get('trove_volume_support', False) EPHEMERAL_SUPPORT = not VOLUME_SUPPORT and CONFIG.get('device_path', '/dev/vdb') is not None ROOT_PARTITION = not VOLUME_SUPPORT and CONFIG.get('device_path', None) is None # This is like a cheat code which allows the tests to skip creating a new # instance and use an old one. def existing_instance(): return os.environ.get("TESTS_USE_INSTANCE_ID", None) def do_not_delete_instance(): return os.environ.get("TESTS_DO_NOT_DELETE_INSTANCE", None) is not None
def mgmt_instance_get(): """Tests the mgmt instances index method.""" reqs = Requirements(is_admin=True) user = CONFIG.users.find_user(reqs) client = create_dbaas_client(user) mgmt = client.management # Grab the info.id created by the main instance test which is stored in # a global. id = instance_info.id api_instance = mgmt.show(id) datastore = getattr(api_instance, 'datastore') datastore_type = datastore.get('type') # Print out all fields for extra info if the test fails. for name in dir(api_instance): print(str(name) + "=" + str(getattr(api_instance, name))) with TypeCheck("instance", api_instance) as instance: instance.has_field('created', six.string_types) instance.has_field('deleted', bool) # If the instance hasn't been deleted, this should be false... but # lets avoid creating more ordering work. instance.has_field('deleted_at', (six.string_types, None)) instance.has_field('flavor', dict, flavor_check) instance.has_field('datastore', dict, datastore_check) instance.has_field('guest_status', dict, guest_status_check) instance.has_field('id', six.string_types) instance.has_field('links', list) instance.has_field('name', six.string_types) # instance.has_field('server_status', six.string_types) instance.has_field('status', six.string_types) instance.has_field('tenant_id', six.string_types) instance.has_field('updated', six.string_types) # Can be None if no volume is given on this instance. volume_support = CONFIG.get(datastore_type, 'mysql')['volume_support'] if volume_support: instance.has_field('volume', dict, volume_check) else: instance.has_field('volume', None) # TODO(tim-simpson): Validate additional fields, assert # no extra fields exist. if api_instance.server is not None: print("the real content of server: %s" % dir(api_instance.server)) print("the type of server: %s" % type(api_instance.server)) print("the real content of api_instance: %s" % dir(api_instance)) print("the type of api_instance: %s" % type(api_instance)) print(hasattr(api_instance, "server")) with CollectionCheck("server", api_instance.server) as server: server.has_element("addresses", dict) server.has_element("deleted", bool) server.has_element("deleted_at", (six.string_types, None)) server.has_element("host", six.string_types) server.has_element("id", six.string_types) server.has_element("local_id", int) server.has_element("name", six.string_types) server.has_element("status", six.string_types) server.has_element("tenant_id", six.string_types) if (volume_support and CONFIG.trove_main_instance_has_volume): with CollectionCheck("volume", api_instance.volume) as volume: volume.has_element("attachments", list) volume.has_element("availability_zone", six.string_types) volume.has_element("created_at", (six.string_types, None)) volume.has_element("id", six.string_types) volume.has_element("size", int) volume.has_element("status", six.string_types)
def get_local_id(self): mgmt_instance = self.dbaas_admin.management.show(self.id) return mgmt_instance.server["local_id"] def get_volume_filesystem_size(self): mgmt_instance = self.dbaas_admin.management.show(self.id) return mgmt_instance.volume["total"] # The two variables are used below by tests which depend on an instance # existing. instance_info = InstanceTestInfo() dbaas = None # Rich client used throughout this test. dbaas_admin = None # Same as above, with admin privs. ROOT_ON_CREATE = CONFIG.get('root_on_create', False) VOLUME_SUPPORT = CONFIG.get('trove_volume_support', False) EPHEMERAL_SUPPORT = not VOLUME_SUPPORT and CONFIG.get('device_path', '/dev/vdb') is not None ROOT_PARTITION = not VOLUME_SUPPORT and CONFIG.get('device_path', None) is None # This is like a cheat code which allows the tests to skip creating a new # instance and use an old one. def existing_instance(): return os.environ.get("TESTS_USE_INSTANCE_ID", None) def create_new_instance(): return existing_instance() is None
def assert_instance_create(self, name, flavor, trove_volume_size, database_definitions, user_definitions, configuration_id, root_password, datastore, datastore_version, expected_states, expected_http_code, create_helper_user=False): """This assert method executes a 'create' call and verifies the server response. It neither waits for the instance to become available nor it performs any other validations itself. It has been designed this way to increase test granularity (other tests may run while the instance is building) and also to allow its reuse in other runners . """ databases = database_definitions users = [{ 'name': item['name'], 'password': item['password'] } for item in user_definitions] # Here we add helper user/database if any. if create_helper_user: helper_db_def, helper_user_def, root_def = self.build_helper_defs() if helper_db_def: self.report.log( "Appending a helper database '%s' to the instance " "definition." % helper_db_def['name']) databases.append(helper_db_def) if helper_user_def: self.report.log( "Appending a helper user '%s:%s' to the instance " "definition." % (helper_user_def['name'], helper_user_def['password'])) users.append(helper_user_def) instance_info = InstanceTestInfo() instance_info.name = name instance_info.databases = databases instance_info.users = users instance_info.dbaas_datastore = CONFIG.dbaas_datastore instance_info.dbaas_datastore_version = CONFIG.dbaas_datastore_version instance_info.dbaas_flavor_href = self._get_flavor_href(flavor) if self.VOLUME_SUPPORT: instance_info.volume = {'size': trove_volume_size} else: instance_info.volume = None shared_network = CONFIG.get('shared_network', None) if shared_network: instance_info.nics = [{'net-id': shared_network}] self.report.log( "Testing create instance: %s" % { 'name': name, 'flavor': flavor.id, 'volume': trove_volume_size, 'nics': instance_info.nics, 'databases': databases, 'users': users, 'configuration': configuration_id, 'root password': root_password, 'datastore': datastore, 'datastore version': datastore_version }) instance = self.get_existing_instance() if instance: self.report.log("Using an existing instance: %s" % instance.id) self.assert_equal(expected_states[-1], instance.status, "Given instance is in a bad state.") else: self.report.log("Creating a new instance.") instance = self.auth_client.instances.create( instance_info.name, instance_info.dbaas_flavor_href, instance_info.volume, instance_info.databases, instance_info.users, nics=instance_info.nics, configuration=configuration_id, availability_zone="nova", datastore=instance_info.dbaas_datastore, datastore_version=instance_info.dbaas_datastore_version) self.assert_instance_action(instance.id, expected_states[0:1], expected_http_code) instance_info.id = instance.id with CheckInstance(instance._info) as check: check.flavor() check.datastore() check.links(instance._info['links']) if self.VOLUME_SUPPORT: check.volume() self.assert_equal(trove_volume_size, instance._info['volume']['size'], "Unexpected Trove volume size") self.assert_equal(instance_info.name, instance._info['name'], "Unexpected instance name") self.assert_equal(flavor.id, int(instance._info['flavor']['id']), "Unexpected instance flavor") self.assert_equal(instance_info.dbaas_datastore, instance._info['datastore']['type'], "Unexpected instance datastore version") self.assert_equal(instance_info.dbaas_datastore_version, instance._info['datastore']['version'], "Unexpected instance datastore version") self.assert_configuration_group(instance_info.id, configuration_id) return instance_info
def assert_instance_create( self, name, flavor, trove_volume_size, database_definitions, user_definitions, configuration_id, root_password, datastore, datastore_version, expected_states, expected_http_code, create_helper_user=False): """This assert method executes a 'create' call and verifies the server response. It neither waits for the instance to become available nor it performs any other validations itself. It has been designed this way to increase test granularity (other tests may run while the instance is building) and also to allow its reuse in other runners . """ databases = database_definitions users = [{'name': item['name'], 'password': item['password']} for item in user_definitions] # Here we add helper user/database if any. if create_helper_user: helper_db_def, helper_user_def = self.build_helper_defs() if helper_db_def: self.report.log( "Appending a helper database '%s' to the instance " "definition." % helper_db_def['name']) databases.append(helper_db_def) if helper_user_def: self.report.log( "Appending a helper user '%s:%s' to the instance " "definition." % (helper_user_def['name'], helper_user_def['password'])) users.append(helper_user_def) instance_info = InstanceTestInfo() instance_info.name = name instance_info.databases = databases instance_info.users = users instance_info.dbaas_datastore = CONFIG.dbaas_datastore instance_info.dbaas_datastore_version = CONFIG.dbaas_datastore_version instance_info.dbaas_flavor_href = self._get_flavor_href(flavor) if self.VOLUME_SUPPORT: instance_info.volume = {'size': trove_volume_size} else: instance_info.volume = None shared_network = CONFIG.get('shared_network', None) if shared_network: instance_info.nics = [{'net-id': shared_network}] self.report.log("Testing create instance: %s" % {'name': name, 'flavor': flavor.id, 'volume': trove_volume_size, 'nics': instance_info.nics, 'databases': databases, 'users': users, 'configuration': configuration_id, 'root password': root_password, 'datastore': datastore, 'datastore version': datastore_version}) instance = self.get_existing_instance() if instance: self.report.log("Using an existing instance: %s" % instance.id) self.assert_equal(expected_states[-1], instance.status, "Given instance is in a bad state.") else: self.report.log("Creating a new instance.") instance = self.auth_client.instances.create( instance_info.name, instance_info.dbaas_flavor_href, instance_info.volume, instance_info.databases, instance_info.users, nics=instance_info.nics, configuration=configuration_id, availability_zone="nova", datastore=instance_info.dbaas_datastore, datastore_version=instance_info.dbaas_datastore_version) self.assert_instance_action( instance.id, expected_states[0:1], expected_http_code) instance_info.id = instance.id with CheckInstance(instance._info) as check: check.flavor() check.datastore() check.links(instance._info['links']) if self.VOLUME_SUPPORT: check.volume() self.assert_equal(trove_volume_size, instance._info['volume']['size'], "Unexpected Trove volume size") self.assert_equal(instance_info.name, instance._info['name'], "Unexpected instance name") self.assert_equal(flavor.id, int(instance._info['flavor']['id']), "Unexpected instance flavor") self.assert_equal(instance_info.dbaas_datastore, instance._info['datastore']['type'], "Unexpected instance datastore version") self.assert_equal(instance_info.dbaas_datastore_version, instance._info['datastore']['version'], "Unexpected instance datastore version") self.assert_configuration_group(instance_info.id, configuration_id) return instance_info
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)) 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 @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 def get_unauth_client(self): if not self.unauth_client: self.unauth_client = self._create_unauthorized_client() return self.unauth_client def _create_unauthorized_client(self, force=False): """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) 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: return CONF.get(datastore or self.instance_info.dbaas_datastore).get(name) except NoSuchOptError: return CONF.get(name) @property def is_using_existing_instance(self): return os.environ.get(self.USE_INSTANCE_ID_FLAG, 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_info(instance_id) return None @property def has_do_not_delete_instance(self): return os.environ.get(self.DO_NOT_DELETE_INSTANCE_FLAG, None) is not None 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'): for status in expected_states: 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 return True 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): instance = self.get_instance(instance_id) self.report.log("Waiting for instance '%s' to become '%s': %s" % (instance_id, status, instance.status)) if fast_fail_status and instance.status == fast_fail_status: raise RuntimeError( "Instance '%s' acquired a fast-fail status: %s" % (instance_id, status)) return instance.status == status 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 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()
def setUp(self): self.info = instances.InstanceTestInfo() reqs = users_util.Requirements(is_admin=True) self.info.admin_user = CONFIG.users.find_user(reqs) self.info.dbaas_admin = self.dbaas_admin = util.create_dbaas_client( self.info.admin_user) reqs = users_util.Requirements(is_admin=False) self.info.user = CONFIG.users.find_user(reqs) self.info.dbaas = self.dbaas = util.create_dbaas_client(self.info.user) self.info.name = "TEST_%s" % self.__class__.__name__ flavor, flavor_href = self.info.find_default_flavor() self.info.dbaas_flavor = flavor self.info.dbaas_flavor_href = flavor_href databases = [] databases.append({ "name": "firstdb", "character_set": "latin2", "collate": "latin2_general_ci" }) databases.append({"name": "db2"}) self.info.databases = databases users = [] users.append({ "name": "lite", "password": "******", "databases": [{ "name": "firstdb" }] }) self.info.users = users self.info.dbaas_datastore = CONFIG.dbaas_datastore self.info.dbaas_datastore_version = CONFIG.dbaas_datastore_version self.info.volume = {'size': CONFIG.get('trove_volume_size', 2)} self.info.initial_result = self.dbaas.instances.create( self.info.name, self.info.dbaas_flavor_href, self.info.volume, databases, users, nics=self.info.nics, availability_zone="nova", datastore=self.info.dbaas_datastore, datastore_version=self.info.dbaas_datastore_version) assert_equal(200, self.dbaas.last_http_code) self.id = self.info.initial_result.id def result_is_active(): instance = self.dbaas.instances.get(self.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) return False utils.poll_until(result_is_active)
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)