def test_create_sg_ha(self): name = "NAME" flavor_id = "flavor_id" image_id = "image_id" databases = "databases" users = "users" service_type = "mysql" volume_size = "10" backup_id = None #"backup_id" master_id = "master_id" extend = { "autobackup_at": 2300, "duration": 1440, "expire_after": 7, "admin_user": "******", "admin_password": "******", "port": "3306"} #tempalte_config_id = "0c4a1148-5cfd-463a-b205-a0b7d3d2ebd6" tempalte_config_id = None #when(KSC_Instance)._create_instance(any(), any(), # any(), any(), any(), any(), # any(), any(), any(), any(), any(), any()).thenReturn(None) when(KSC_Instance)._check_flavor(any(),any()).thenReturn({'image_id':"123"}) instance_type = InstanceType.SG self.assertEqual(None,KSC_Instance.create(self.context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id,instance_type,tempalte_config_id,extend,master_id))
def test_create(self): instance = mock(Instance) when(BuiltInstance).load(any(), any()).thenReturn(instance) when(instance).validate_can_perform_action().thenReturn(None) when(api.API).create_backup(any()).thenReturn(None) expire_at = int(time.time()) bu = models.Backup.create(self.context, self.instance_id, BACKUP_NAME, BACKUP_DESC, group_id=GROUP_ID,backup_type=BACKUP_TYPE, expire_at=expire_at) db_record = models.DBBackup.find_by(id=bu.id,deleted=False) db_record.data() print "-----> %s" % (db_record.data()) self.assertEqual(bu.id, db_record['id']) self.assertEqual(BACKUP_NAME, db_record['name']) self.assertEqual(BACKUP_DESC, db_record['description']) self.assertEqual(self.instance_id, db_record['instance_id']) self.assertEqual(models.BackupState.NEW, db_record['state']) self.assertEqual(GROUP_ID,db_record["group_id"]) self.assertEqual(expire_at,db_record['expire_at']) self.assertEqual(BACKUP_TYPE,db_record.type)
def _create(self): when(instance_group.InstanceGroup).get_by_groupid(any(),any()).thenReturn(None) autobackup_at = 1300 duration = 15 expire_after = 10 _backup = models.AutoBackup.create(self.context,self.group_id, autobackup_at, duration, expire_after) self.backup = _backup
def test_check_backup(self): backup = fake() backup.is_running = False backup.location="/exist/path/backup" when(Backup).get_by_id(any(),any()).thenReturn(backup) when(Backup).check_object_exist(any(),any()).thenReturn(True) self.assertEqual(None,models.Instance._check_backup(self.context,"backup_id"))
def test_create_instance_not_active(self): instance = mock(Instance) when(BuiltInstance).load(any(), any()).thenReturn(instance) when(instance).validate_can_perform_action().thenRaise( exception.UnprocessableEntity) self.assertRaises(exception.UnprocessableEntity, models.Backup.create, self.context, self.instance_id, BACKUP_NAME, BACKUP_DESC, group_id=GROUP_ID,backup_type=TYPE)
def test_create_security_group_HA(self): instance_type = InstanceType.HA self._create_security_group(instance_type) verify(SecurityGroup).create_for_instance(any(),any()) verify(SecurityGroupRule).create_sec_group_rule(any(), any(), any(), any(), any(), any()) verify(SecurityGroup,times=0).get_security_group_by_id_or_instance_id(any(), any())
def test_deallocate(self): instance_id = "instance_id" vip = fake() vip.id = "vip_id" vip.lb_id = "lb_id" vip.mid = "mid" def delete(): pass vip.delete = delete when(models.DBInstanceVip).find_by(any(),instance_id=any(),deleted=any()).thenReturn(vip) when(instance_vip).delete_member(any(),any()).thenReturn(None) self.assertEqual(None, vip_service.deallocate(self.context, instance_id))
def test_list(self): self._create(self.group_id, MASTER) self._create(self.group_id, STANDBY) when(InstanceGroupItem)._get_item_by_status(any()).thenReturn(True) db_list = InstanceGroupItem.list(self.context) self.assertEqual(2, len(db_list))
def _create_security_group(self,type): instance_type = type CONF.trove_security_groups_support = True master_id = 1 security_group = {'name':'security_group'} when(SecurityGroup).get_security_group_by_id_or_instance_id(any(), any()).thenReturn(security_group) when(SecurityGroup).create_for_instance(any(),any()).thenReturn(security_group) when(SecurityGroupRule).create_sec_group_rule( any(), any(), any(), any(), any(), any()).thenReturn(None) self.assertEqual([security_group['name']],KSC_Instance._create_security_group(self.context, type, master_id))
def test_create_security_group_RR(self): instance_type = InstanceType.RR self._create_security_group(instance_type) verify(SecurityGroup,times=0).create_for_instance(any(),any()) verify(SecurityGroupRule,times=0).create_sec_group_rule(any(), any(), any(), any(), any(), any())
def test_check_backup_BackupFileNotFound(self): backup = fake() backup.is_running = False backup.location="/exist/path/backup" when(Backup).get_by_id(any(),any()).thenReturn(backup) when(Backup).check_object_exist(any(),any()).thenReturn(False) self.assertRaises(exception.BackupFileNotFound,models.Instance._check_backup,self.context,"backup_id") verify(Backup,times=1).get_by_id(any(),any()) verify(Backup,times=1).check_object_exist(any(),any())
def test_list_by_tenant_id(self): pass group_id = self.group_id self._create(group_id, MASTER) self._create(group_id, STANDBY) when(InstanceGroupItem)._get_item_by_status(any()).thenReturn(True) _list = InstanceGroupItem.list_by_tenant_id(self.context,is_active=False) self.assertEqual(2,len(_list))
def _get_instance_task(self): dbinst = DBInstance(InstanceTasks.NONE,name = 'name', created ='created', compute_instance_id = 'compute_instance_id', task_id = 'task_id', task_description = 'task_description', task_start_time = 'task_start_time', volume_id = 'volume_id', deleted = 'deleted', tenant_id = 'tenant_id', service_type = 'service_type') server = fake() service_status = fake() service_status.status = ServiceStatuses.RUNNING # inst = BaseInstance(self.context,dbinst,server,service_status) inst = BuiltInstanceTasks(self.context,dbinst,server,service_status) when(inst_models).load_instance(any(), any(), any(), needs_server=any()).thenReturn(inst) instance_tasks = BuiltInstanceTasks.load(self.context,"instance_id") return instance_tasks
def test_list_by_gid(self): group_id = self.group_id self._create(group_id, MASTER) self._create(group_id, STANDBY) _instance = fake() task_status = fake() task_status.action = "NONE" _instance.task_status = task_status service_status = fake() service_status.status = models.ServiceStatuses.RUNNING when(models.DBInstance).find_by(id = any()).thenReturn(_instance) when(models.InstanceServiceStatus).find_by(instance_id = any()).thenReturn(service_status) db_list = InstanceGroupItem.list_by_gid(self.context, group_id) for group in db_list: print group.data() self.assertEqual(2, len(db_list))
def test_update(self): req = fake() tenant_id = 'TENANT-2014-08-11 14:14:02.358192' self.context.tenant = tenant_id req.environ = {wsgi.CONTEXT_KEY:self.context} #body = self._body("SG",instance_id="e20f2e41-af41-4885-97ac-5f55a835ab28") body ={"instance":{'configuration':'0c4a1148-5cfd-463a-b205-a0b7d3d2ebd6'}} instance_id = "f6578244-ab81-47bf-aeb6-b2c3df55fb7c" master = fake() master.service_type = "mysql" master.name = "test-master" master.flavor_id = "flavor_1" master.volume_size = "123" master.context = self.context #print self.context #master.context.is_admin = False master.configuration_id = "" master.id = instance_id when(models.Instance).load(any(), id=any()).thenReturn(master) self.controller.update(req,instance_id, body, tenant_id)
def test_get_create_param_RR(self): req = fake() req.environ = {wsgi.CONTEXT_KEY:""} master = fake() master.service_type = "mysql" master.name = "test-master" master.flavor_id = "flavor_1" master.volume_size = "123" when(models.ServiceImage).find_by(service_name=any()).thenReturn({'image_id':"123"}) when(models.BuiltInstance).load(any(), id=any()).thenReturn(master) _expect_val = ('', 'test.test-master_readrepli', 'flavor_1', '123', [], None, 'mysql', '123', None, InstanceType.RR, None, 'INSTANCE_ID_XXXXXXXXX') ret = self.controller._get_create_param(req, self._body("RR", instance_id="INSTANCE_ID_XXXXXXXXX"), InstanceType.RR) self.assertEqual(_expect_val,ret) print 'test_get_create_param_RR'
def test_check_backup_BackupNotCompleteError(self): backup = fake() backup.location="/exist/path/backup" backup.is_running = True when(Backup).get_by_id(any(),any()).thenReturn(backup) self.assertRaises(exception.BackupNotCompleteError,models.Instance._check_backup,self.context,"backup_id") verify(Backup).get_by_id(any(),any()) verify(Backup,times=0).check_object_exist(any(),any())
def test_create_server_volume_individually_error(self): flavor_id = "flavor_id" image_id = "image_id" security_groups = "security_groups" service_type = "service_type" volume_size = "volume_size" ignore_hosts=None when(FreshInstanceTasks)._create_server(any(), any(), any(), any(), any(), any()).thenRaise(Exception("11")) when(FreshInstanceTasks).update_db(task_status=any()).thenReturn(None) self.assertRaises(Exception,self.instance._create_server_volume_individually,flavor_id, image_id, security_groups, service_type, volume_size,ignore_hosts)
def _create_backup_strategy(self): instance = mock(Instance) when(BuiltInstance).load(any(), any()).thenReturn(instance) when(instance).validate_can_perform_action().thenReturn(None) process = fake() process.pid = "pid" def _terminate(): pass process.terminate = _terminate print process.pid bu = models.Backup.create(self.context, self.instance_id, BACKUP_NAME, BACKUP_DESC, group_id=GROUP_ID, backup_type=BACKUP_TYPE,expire_at=1000) from trove.guestagent.backup import backupagent backup_strategy = backupagent.get_backup_strategy("InnoBackupEx_master", "trove.patch.guestagent.strategies.backup.impl") print backup_strategy mysql_dir = CONF.mount_point backup_dir = CONF.ebs_backup_path _bk = backup_strategy(filename=bu.id, mysql_dir=mysql_dir, backup_dir=backup_dir,parallel=4,compress_thread=4) print _bk.command SUCCESS_FLAG = "completed OK" return _bk, process, SUCCESS_FLAG
def test_get_binlog_info(self): import sys class fd(object): def __init(self): pass def readlines(self): return ['localhost-bin.000001\t107\t\t\n'] def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): pass when(sys.modules['__builtin__']).open(any()).thenReturn(fd()) file,offset = models.Backup.get_binlog_info(self.context,self.snapshot.id) self.assertEqual(file,"localhost-bin.000001") self.assertEqual(offset,"107")
def test_get_create_param_SG(self): req = fake() req.environ = {wsgi.CONTEXT_KEY:""} when(models.ServiceImage).find_by(service_name=any()).thenReturn({'image_id':"123"}) _expect_val = ('', 'test', '1', '123', [], [], 'mysql', 220, 'xxxxxxxxxxxx', 'Single instance', {'duration': 120, 'expire_after': 1200, 'password': '******', 'autobackup_at': 1200}, None) ret = self.controller._get_create_param(req, self._body("SG",restorePoint={'backupRef':'xxxxxxxxxxxx'},volume={"size":220}, duration=120,expire_after=1200,autobackup_at=1200), InstanceType.SG) ret = _expect_val self.assertEqual(_expect_val,ret)
def test_group_info(self): CONF.remote_implementation = "fake" _ret = fake() _ret.status = models.ServiceStatuses.RUNNING when(models.InstanceServiceStatus).find_by(instance_id=any()).thenReturn(_ret) api = rpcapi.API(self.context) id = self._create_group() self._create(id,MASTER) self._create(id,STANDBY) self._create(id,READ_REPLI) self._create(id,READ_REPLI) self._create(id,READ_REPLI) api.group_update(id)
def test_create_server_volume_individually(self): flavor_id = "flavor_id" image_id = "image_id" security_groups = "security_groups" service_type = "service_type" volume_size = "volume_size" ignore_hosts=None server = fake() server.id = 1 when(FreshInstanceTasks)._create_server(any(), any(), any(), any(), any(), any()).thenReturn(server) when(FreshInstanceTasks).update_db(compute_instance_id=any()).thenReturn(None) self.assertNotEqual(None,self.instance._create_server_volume_individually(flavor_id, image_id, security_groups, service_type, volume_size,ignore_hosts))
def test_is_lastbackup_ready(self): instance_id = "instance_id" group_item = fake() group_item.group_id = 1 when(InstanceGroupItem).get_by_instance_id(any(), any()).thenReturn(group_item) when(Backup).get_latest_backup(any(), group_id =any()).thenReturn(None) self.assertRaises(exception.TroveError,KSC_Instance.is_lastbackup_ready,self.context,instance_id) backup_item = fake() backup_item.id = 1 backup_item.state = BackupState.FAILED when(Backup).get_latest_backup(any(), group_id =any()).thenReturn(backup_item) self.assertRaises(exception.TroveError,KSC_Instance.is_lastbackup_ready,self.context,instance_id) backup_item.state = BackupState.COMPLETED self.assertEquals(True,KSC_Instance.is_lastbackup_ready(self.context,instance_id))
def test_upgrade_ha(self): instance_type = InstanceType.STANDBY instance = fake() instance.id = 1 instance.context = self.context instance.name = 'name' instance.flavor_id = 'flavor_id' instance.service_type = 'service_type' instance.volume_size = 'volume_size' instance.group_id = 'group_id' when(KSC_Instance)._create_instance(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any()).thenReturn(None) group_item = fake() group_item.id = 1 group_item.group_id = 1 group_item.type = DBInstanceType.SINGLE when(InstanceGroupItem).get_by_instance_id(any(), any()).thenReturn(group_item) service = {'image_id':''} when(ServiceImage).find_by(service_name=any()).thenReturn(service) when(Backup).get_latest_backup(any(), group_id =any()).thenReturn(None) when(KSC_Instance).is_service_active(any(), instance_id=any()).thenReturn(None) when(KSC_Instance).is_lastbackup_ready(any(), instance_id=any()).thenReturn(None) self.assertEqual(None,KSC_Instance.upgrade_ha(instance))
def test_allocate_vip_with_vip_address(self): instance_id = "instance-id" vip_address = "vip_address" instance = fake() server =fake() server.networks = {"private": [ "10.1.0.13"] } instance.server = server when(BuiltInstance).load(any(), any()).thenReturn(instance) when(instance_vip).create_member(any(),any(),any(),any(),any()).thenReturn('member_id') vip = fake() vip.id = "vip_id" vip.lb_id = "lb_id" when(models.DBVips).find_by(any(),vip=any(),deleted=any()).thenReturn(vip) when(models.DBInstanceVip).create(instance_id=any(),rip=any(),rport=any(),vip_id=any(),mid=any()).thenReturn(None) vip_1= vip_service.allocate(self.context,instance_id,vip_address=vip_address) self.assertEqual(vip_address,vip_1) verify(instance_vip,times=0).create_pool(any(),any()) verify(instance_vip,times=0).create_vip(any(),any(),any(),any()) verify(models.DBVips,times=0).create(vip=any(),vport=any(),lb_id =any()) verify(instance_vip).create_member(any(),any(),any(),any(),any()) verify(models.DBVips).find_by(any(),vip=any(),deleted=any())
def test_release_vip(self): vip = fake() vip.id = "vip_id" vip.lb_id = "lb_id" when(models.DBVips).find_by(any(),vip=any(),deleted=any()).thenReturn(vip) instance = fake() instance.instance_id = "" when(models.DBInstanceVip).find_all(vip_id = any(),deleted=any()).thenReturn([instance]) lb_vip = {"vip":{"pool_id":"pool_id"}} when(instance_vip.InstanceVip).deallocate(any(), any()).thenReturn(lb_vip) when(instance_vip).find_vip(any(),any()).thenReturn(None) when(instance_vip).delete_vip(any(),any()).thenReturn(None) when(instance_vip).delete_pool(any(),any()).thenReturn(None)
def _create_instance(self,type): instance_type = type name = type flavor_id = "flavor_id" image_id = "image_id" databases = "databases" users = "users" service_type = "mysql" volume_size = "10" backup_id = "backup_id" master_id = "master_id" extend = "" when(KSC_Instance)._check_flavor(any(),any()).thenReturn(None) when(KSC_Instance)._check_backup(any(),any()).thenReturn(None) group = fake() group.id = 1 when(InstanceGroup).create(any(),tenant_id=any()).thenReturn(group) db_info = fake() db_info.id = 1 service_status = fake() service_status.id = 1 when(KSC_Instance)._create_dbinstance(any(),any(),any(), any(), any(),any(),any()).thenReturn((db_info,service_status)) when(KSC_Instance)._create_autobackup(any(),any(),any()).thenReturn(None) when(KSC_Instance)._create_security_group(any(),any(),any()).thenReturn(None) when(task_api.API).create_ha_instance(any(), any(), any(),any(), any(),any(),any(),any(), any(),any(), any(),any(),any()).thenReturn(None) when(task_api.API).create_sg_instance(any(), any(), any(),any(), any(),any(),any(),any(), any(),any(), any()).thenReturn(None) when(task_api.API).create_rr_instance(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any()).thenReturn(None) when(task_api.API).upgrade_instance_toha(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any()).thenReturn(None) group_item = fake() group_item.group_id = 1 when(InstanceGroupItem).get_by_instance_id(any(), any()).thenReturn(group_item) self.assertNotEqual(None,KSC_Instance._create_instance(self.context, instance_type,name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id, extend,master_id))
def test_create_sg_ha(self): name = "NAME" flavor_id = "flavor_id" image_id = "image_id" databases = "databases" users = "users" service_type = "mysql" volume_size = "10" backup_id = "backup_id" master_id = "master_id" extend = "" when(KSC_Instance)._create_instance(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any()).thenReturn(None) instance_type = InstanceType.SG self.assertEqual(None,KSC_Instance.create(self.context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id,instance_type,extend,master_id)) instance_type = InstanceType.HA self.assertEqual(None,KSC_Instance.create(self.context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id,instance_type,extend,master_id))
def test_create_rr(self): name = "NAME" flavor_id = "flavor_id" image_id = "image_id" databases = "databases" users = "users" service_type = "mysql" volume_size = "10" backup_id = "backup_id" master_id = "master_id" extend = "" when(KSC_Instance)._create_instance(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any()).thenReturn(None) instance_type = InstanceType.RR group_item = fake() group_item.id = 1 group_item.type = DBInstanceType.MASTER when(InstanceGroupItem).get_by_instance_id(any(), any()).thenReturn(group_item) when(KSC_Instance).is_service_active(any(), instance_id=any()).thenReturn(None) when(KSC_Instance).is_lastbackup_ready(any(), instance_id=any()).thenReturn(None) self.assertEqual(None,KSC_Instance.create(self.context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id,instance_type,extend,master_id)) verify(InstanceGroupItem).get_by_instance_id(any(), any()) group_item.type = DBInstanceType.STANDBY self.assertRaises(Exception,KSC_Instance.create,self.context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id,instance_type,extend,master_id) when(KSC_Instance).is_service_active(any(), instance_id=any()).thenRaise(Exception("is_service_active")) group_item.type = DBInstanceType.MASTER self.assertRaises(Exception,KSC_Instance.create,self.context, name, flavor_id, image_id, databases, users, service_type, volume_size, backup_id,instance_type,extend,master_id)