def setUp(self): super(OrderingTests, self).setUp() util.init_db() now = utils.utcnow() self.context, self.instance_id = _prep_conf(now) info = { 'tenant_id': self.context.tenant, 'state': BACKUP_STATE, 'instance_id': self.instance_id, 'size': 2.0, 'deleted': False } four = now - datetime.timedelta(days=4) one = now - datetime.timedelta(days=1) three = now - datetime.timedelta(days=3) two = now - datetime.timedelta(days=2) # Create backups out of order, save/create set the 'updated' field, # so we need to use the db_api directly. models.DBBackup().db_api.save( models.DBBackup(name='four', updated=four, id=utils.generate_uuid(), **info)) models.DBBackup().db_api.save( models.DBBackup(name='one', updated=one, id=utils.generate_uuid(), **info)) models.DBBackup().db_api.save( models.DBBackup(name='three', updated=three, id=utils.generate_uuid(), **info)) models.DBBackup().db_api.save( models.DBBackup(name='two', updated=two, id=utils.generate_uuid(), **info))
def test_cluster_root_create(self, root_create, root_history_create): context = Mock() uuid = utils.generate_uuid() user = "******" password = "******" cluster_instances = [utils.generate_uuid(), utils.generate_uuid()] common_models.ClusterRoot.create(context, uuid, user, password, cluster_instances) root_create.assert_called_with(context, uuid, user, password, cluster_instances_list=None) self.assertEqual(2, root_history_create.call_count) calls = [call(context, cluster_instances[0], user), call(context, cluster_instances[1], user)] root_history_create.assert_has_calls(calls)
def _create_backup(self, name='fake backup'): new_id = utils.generate_uuid() backup = bkup_models.DBBackup.create( id=new_id, name=name, description='This is a fake backup object.', tenant_id=utils.generate_uuid(), state=state.BackupState.NEW, instance_id=self.instance_id) backup.save() return new_id
def test_get_cluster_instance_id(self, mock_find_all): tenant_id = Mock() cluster_id = Mock() db_inst_1 = Mock() db_inst_1.id.return_value = utils.generate_uuid() db_inst_2 = Mock() db_inst_2.id.return_value = utils.generate_uuid() cluster_instances = [db_inst_1, db_inst_2] mock_find_all.return_value.all.return_value = cluster_instances ret = self.controller._get_cluster_instance_id(tenant_id, cluster_id) self.assertTrue(db_inst_1.id, ret[0]) self.assertTrue(cluster_instances, ret[1])
def test_log_save_fresh(self): self.cleanup_swift() f = '/tmp/' + generate_uuid() + '.log' f2 = self.instances.log_save(instance_info.id, log='error', publish=True, filename=f) assert_equal(f, f2) copied = '/tmp/' + generate_uuid() + '.log' logfile = MySQLGuestLog.datastore_logs['error'][2] # TODO(atomic77) How to tell what user the tests should use on GA? self.server.scp(logfile, copied, user='******', dest_is_remote=False) self.compare_files(f, copied) utils.execute_with_timeout("rm", f) utils.execute_with_timeout("rm", copied)
def run_attach_non_dynamic_group_to_non_existent_inst( self, expected_exception=exceptions.NotFound, expected_http_code=404): if self.non_dynamic_group_id: self.assert_instance_modify_failure( generate_uuid(), self.non_dynamic_group_id, expected_exception, expected_http_code)
def run_backup_create_instance_not_found( self, expected_exception=exceptions.NotFound, expected_http_code=404): self.assert_raises( expected_exception, expected_http_code, self.auth_client.backups.create, self.BACKUP_NAME, generate_uuid(), self.BACKUP_DESC)
def test_root_index_instance(self, mock_instance_root_index): req = Mock() tenant_id = Mock() uuid = utils.generate_uuid() is_cluster = False self.controller.root_index(req, tenant_id, uuid, is_cluster) mock_instance_root_index.assert_called_with(req, tenant_id, uuid)
def test_instance_root_index(self, mock_cluster_root_load): req = Mock() req.environ = {'trove.context': self.context} tenant_id = Mock() instance_id = utils.generate_uuid() self.controller.instance_root_index(req, tenant_id, instance_id) mock_cluster_root_load.assert_called_with(self.context, instance_id)
def _create(self, req, body, tenant_id, instance_type): """return a SimpleInstance""" LOG.info(_("Creating database instance for tenant '%s'") % tenant_id) LOG.info(_("req : '%s'\n\n") % req) LOG.info(_("body : '%s'\n\n") % body) context, name, flavor_id,image_id, databases, users,\ service_type, volume_size,backup_id, instance_type,\ extend,master_id,template_config_id = self._get_create_param(req, body, instance_type) # @classmethod # def create(cls, context, name, flavor_id, image_id, databases, # users, service_type, volume_size, backup_id, instance_type, # extend=None, master_id=None): instance = pacth_inst_models.KSC_Instance.create(context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id, instance_type, template_config_id, extend=extend, master_id=master_id) if instance_type in [InstanceType.HA, InstanceType.SG, InstanceType.RR]: vinst_id = utils.generate_uuid() InstanceVip.fake_allocate(context, instance.id, attached_virtual_instid = vinst_id) return instance
def run_backup_create_instance_not_found( self, expected_exception=exceptions.NotFound, expected_http_code=404): self.assert_raises(expected_exception, expected_http_code, self.auth_client.backups.create, self.BACKUP_NAME, generate_uuid(), self.BACKUP_DESC)
def create_or_update_datastore_configuration_parameter(name, datastore_version_id, restart_required, data_type, max_size, min_size): get_db_api().configure_db(CONF) datastore_version = dstore_models.DatastoreVersion.load_by_uuid( datastore_version_id) try: config = DatastoreConfigurationParameters.load_parameter_by_name( datastore_version_id, name, show_deleted=True) config.restart_required = restart_required config.max_size = max_size config.min_size = min_size config.data_type = data_type get_db_api().save(config) except exception.NotFound: config = DBDatastoreConfigurationParameters( id=utils.generate_uuid(), name=name, datastore_version_id=datastore_version.id, restart_required=restart_required, data_type=data_type, max_size=max_size, min_size=min_size, deleted=False, ) get_db_api().save(config)
def _create_iss(self): new_id = utils.generate_uuid() iss = t_models.InstanceServiceStatus(id=new_id, instance_id=self.instance_id, status=ServiceStatuses.NEW) iss.save() return new_id
def setUp(self): # See LP bug #1255178 bkup_models.DBBackup.save = OLD_DBB_SAVE super(ConductorMethodTests, self).setUp() util.init_db() self.cond_mgr = conductor_manager.Manager() self.instance_id = utils.generate_uuid()
def test_cluster_root_create(self, root_create, root_history_create): context = Mock() uuid = utils.generate_uuid() user = "******" password = "******" cluster_instances = [utils.generate_uuid(), utils.generate_uuid()] common_models.ClusterRoot.create(context, uuid, user, password, cluster_instances) root_create.assert_called_with(context, uuid, user, password, cluster_instances_list=None) self.assertEqual(2, root_history_create.call_count) calls = [ call(context, cluster_instances[0], user), call(context, cluster_instances[1], user) ] root_history_create.assert_has_calls(calls)
def _create_shard_instances(self, instances, replica_set_name=None, key=None): """Create the instances for a new shard in the cluster.""" shard_id = utils.generate_uuid() if not replica_set_name: replica_set_name = self._gen_replica_set_name() cluster_config = {"shard_id": shard_id, "instance_type": "member", "replica_set_name": replica_set_name} return self._create_instances(instances, cluster_config, replica_set_name, key=key)
def run_attach_non_existent_group(self, expected_exception=exceptions.NotFound, expected_http_code=404): self.assert_instance_modify_failure(self.instance_info.id, generate_uuid(), expected_exception, expected_http_code)
def add_shard(self): if self.db_info.task_status != ClusterTasks.NONE: current_task = self.db_info.task_status.name msg = _("This action cannot be performed on the cluster while " "the current cluster task is '%s'.") % current_task LOG.error(msg) raise exception.UnprocessableEntity(msg) db_insts = inst_models.DBInstance.find_all(cluster_id=self.id, type='member').all() num_unique_shards = len(set([db_inst.shard_id for db_inst in db_insts])) if num_unique_shards == 0: msg = _("This action cannot be performed on the cluster as no " "reference shard exists.") LOG.error(msg) raise exception.UnprocessableEntity(msg) arbitrary_shard_id = db_insts[0].shard_id members_in_shard = [db_inst for db_inst in db_insts if db_inst.shard_id == arbitrary_shard_id] num_members_per_shard = len(members_in_shard) a_member = inst_models.load_any_instance(self.context, members_in_shard[0].id) deltas = {'instances': num_members_per_shard} volume_size = a_member.volume_size if volume_size: deltas['volumes'] = volume_size * num_members_per_shard check_quotas(self.context.tenant, deltas) new_replica_set_name = "rs" + str(num_unique_shards + 1) new_shard_id = utils.generate_uuid() dsv_manager = (datastore_models.DatastoreVersion. load_by_uuid(db_insts[0].datastore_version_id).manager) manager = task_api.load(self.context, dsv_manager) key = manager.get_key(a_member) member_config = {"id": self.id, "shard_id": new_shard_id, "instance_type": "member", "replica_set_name": new_replica_set_name, "key": key} for i in range(1, num_members_per_shard + 1): instance_name = "%s-%s-%s" % (self.name, new_replica_set_name, str(i)) inst_models.Instance.create(self.context, instance_name, a_member.flavor_id, a_member.datastore_version.image_id, [], [], a_member.datastore, a_member.datastore_version, volume_size, None, availability_zone=None, nics=None, configuration_id=None, cluster_config=member_config) self.update_db(task_status=ClusterTasks.ADDING_SHARD) manager.mongodb_add_shard_cluster( self.id, new_shard_id, new_replica_set_name)
def update_datastore_version(datastore, name, manager, image_id, image_tags, packages, active, version=None, new_name=None): """Create or update datastore version.""" version = version or name db_api.configure_db(CONF) datastore = Datastore.load(datastore) try: ds_version = DBDatastoreVersion.find_by(datastore_id=datastore.id, name=name, version=version) except exception.ModelNotFoundError: # Create a new one ds_version = DBDatastoreVersion() ds_version.id = utils.generate_uuid() ds_version.version = version ds_version.datastore_id = datastore.id ds_version.name = new_name or name ds_version.manager = manager ds_version.image_id = image_id ds_version.image_tags = (",".join(image_tags) if type(image_tags) is list else image_tags) ds_version.packages = packages ds_version.active = active db_api.save(ds_version)
def test_backup_instance_id_nomatch(self): new_iid = utils.generate_uuid() bkup_id = self._create_backup('nomatch') old_name = self._get_backup(bkup_id).name self.cond_mgr.update_backup(None, new_iid, bkup_id, name="remains unchanged") bkup = self._get_backup(bkup_id) self.assertEqual(old_name, bkup.name)
def test_root_create_cluster(self, mock_cluster_root_create): req = Mock() body = Mock() tenant_id = Mock() uuid = utils.generate_uuid() is_cluster = True self.controller.root_create(req, body, tenant_id, uuid, is_cluster) mock_cluster_root_create.assert_called_with(req, body, tenant_id, uuid)
def _create_iss(self): new_id = utils.generate_uuid() iss = t_models.InstanceServiceStatus( id=new_id, instance_id=self.instance_id, status=ServiceStatuses.NEW) iss.save() return new_id
def test_report_root_double_create(self): uuid = utils.generate_uuid() history = mysql_models.RootHistory(uuid, "root").save() mysql_models.RootHistory.load = Mock(return_value=history) report = mysql_models.RootHistory.create(None, uuid, "root") self.assertTrue(mysql_models.RootHistory.load.called) self.assertEqual(history.user, report.user) self.assertEqual(history.id, report.id)
def test_instance_root_index(self, mock_cluster_root_load): context = Mock() req = Mock() req.environ = Mock() req.environ.__getitem__ = Mock(return_value=context) tenant_id = Mock() instance_id = utils.generate_uuid() self.controller.instance_root_index(req, tenant_id, instance_id) mock_cluster_root_load.assert_called_with(context, instance_id)
def test_root_create_instance(self, mock_instance_root_create, mock_check): req = Mock() body = Mock() tenant_id = Mock() uuid = utils.generate_uuid() is_cluster = False self.controller.root_create(req, body, tenant_id, uuid, is_cluster) mock_check.assert_called_with(uuid) mock_instance_root_create.assert_called_with(req, body, uuid)
def __init__(self, tenant_id, resource, hard_limit, id=utils.generate_uuid(), created=timeutils.utcnow(), update=timeutils.utcnow()): self.tenant_id = tenant_id self.resource = resource self.hard_limit = hard_limit self.id = id self.created = created self.update = update
def __init__(self, tenant_id, resource, hard_limit, id=utils.generate_uuid(), created=utils.utcnow(), update=utils.utcnow()): self.tenant_id = tenant_id self.resource = resource self.hard_limit = hard_limit self.id = id self.created = created self.update = update
def test_root_index_with_cluster(self): req = Mock() tenant_id = Mock() uuid = utils.generate_uuid() is_cluster = True self.assertRaises( exception.ClusterOperationNotSupported, self.controller.root_index, req, tenant_id, uuid, is_cluster)
def test_instance_root_index_exception(self, mock_cluster_root_load): req = Mock() req.environ = {'trove.context': self.context} tenant_id = Mock() instance_id = utils.generate_uuid() self.assertRaises(exception.UnprocessableEntity, self.controller.instance_root_index, req, tenant_id, instance_id) mock_cluster_root_load.assert_called_with(self.context, instance_id)
def create(cls, **values): if 'id' not in values: values['id'] = utils.generate_uuid() if hasattr(cls, 'deleted') and 'deleted' not in values: values['deleted'] = False values['created'] = utils.utcnow() instance = cls(**values).save() if not instance.is_valid(): raise exception.InvalidModelError(errors=instance.errors) return instance
def test_report_root_double_create(self): uuid = utils.generate_uuid() history = mysql_models.RootHistory(uuid, 'root').save() with patch.object(mysql_models.RootHistory, 'load', Mock(return_value=history)): report = mysql_models.RootHistory.create( None, uuid, 'root') self.assertTrue(mysql_models.RootHistory.load.called) self.assertEqual(history.user, report.user) self.assertEqual(history.id, report.id)
def test_root_index(self, root_load): context = Mock() req = Mock() req.environ = Mock() req.environ.__getitem__ = Mock(return_value=context) tenant_id = Mock() uuid = utils.generate_uuid() is_cluster = False self.controller.root_index(req, tenant_id, uuid, is_cluster) root_load.assert_called_with(context, uuid)
def test_instance_root_index_exception(self, mock_cluster_root_load): req = Mock() req.environ = {'trove.context': self.context} tenant_id = Mock() instance_id = utils.generate_uuid() self.assertRaises( exception.UnprocessableEntity, self.controller.instance_root_index, req, tenant_id, instance_id ) mock_cluster_root_load.assert_called_with(self.context, instance_id)
def test_cluster_root_index(self, mock_get_cluster_instance, mock_instance_root_index): req = Mock() tenant_id = Mock() cluster_id = utils.generate_uuid() single_instance_id = Mock() mock_get_cluster_instance.return_value = (single_instance_id, Mock()) self.controller.cluster_root_index(req, tenant_id, cluster_id) mock_get_cluster_instance.assert_called_with(tenant_id, cluster_id) mock_instance_root_index.assert_called_with(req, tenant_id, single_instance_id)
def _create_shard_instances(self, instances, locality, replica_set_name=None, key=None): """Create the instances for a new shard in the cluster.""" shard_id = utils.generate_uuid() if not replica_set_name: replica_set_name = self._gen_replica_set_name() cluster_config = {'shard_id': shard_id, 'instance_type': 'member', 'replica_set_name': replica_set_name} return self._create_instances(instances, cluster_config, replica_set_name, locality, key=key)
def test_root_create_with_cluster(self): req = Mock() tenant_id = Mock() uuid = utils.generate_uuid() is_cluster = True password = Mock() body = {'password': password} self.assertRaises( exception.ClusterOperationNotSupported, self.controller.root_create, req, body, tenant_id, uuid, is_cluster)
def create(self, req, body, tenant_id): """Adds a new datastore version.""" context = req.environ[wsgi.CONTEXT_KEY] datastore_name = body['version']['datastore_name'] version_name = body['version']['name'] manager = body['version']['datastore_manager'] image_id = body['version'].get('image') image_tags = body['version'].get('image_tags') packages = body['version'].get('packages') if type(packages) is list: packages = ','.join(packages) active = body['version']['active'] default = body['version'].get('default', False) # For backward compatibility, use name as default value for version if # not specified version_str = body['version'].get('version', version_name) LOG.info("Tenant: '%(tenant)s' is adding the datastore " "version: '%(version)s' to datastore: '%(datastore)s'", {'tenant': tenant_id, 'version': version_name, 'datastore': datastore_name}) if not image_id and not image_tags: raise exception.BadRequest("Image must be specified.") client = clients.create_glance_client(context) common_glance.get_image_id(client, image_id, image_tags) try: datastore = models.Datastore.load(datastore_name) except exception.DatastoreNotFound: # Create the datastore if datastore_name does not exists. LOG.info("Creating datastore %s", datastore_name) datastore = models.DBDatastore() datastore.id = utils.generate_uuid() datastore.name = datastore_name datastore.save() try: models.DatastoreVersion.load(datastore, version_name, version=version_str) raise exception.DatastoreVersionAlreadyExists( name=version_name, version=version_str) except exception.DatastoreVersionNotFound: models.update_datastore_version(datastore.name, version_name, manager, image_id, image_tags, packages, active, version=version_str) if default: models.update_datastore(datastore.name, version_name) return wsgi.Result(None, 202)
def test_root_delete(self, root_load, root_delete): context = Mock() req = Mock() req.environ = Mock() req.environ.__getitem__ = Mock(return_value=context) tenant_id = Mock() instance_id = utils.generate_uuid() is_cluster = False root_load.return_value = True self.controller.root_delete(req, tenant_id, instance_id, is_cluster) root_load.assert_called_with(context, instance_id) root_delete.assert_called_with(context, instance_id)
def create(cls, **values): init_vals = { 'id': utils.generate_uuid(), 'created': utils.utcnow(), } if hasattr(cls, 'deleted'): init_vals['deleted'] = False init_vals.update(values) instance = cls(**init_vals) if not instance.is_valid(): raise exception.InvalidModelError(errors=instance.errors) return instance.save()