def setUp(self):
     super(Test_FreshInstanceTask, self).setUp()
     db_info = fake()
     db_info.hostname = "hostname"
     db_info.name = "name"
     db_info.id = "id"
     db_info.task_status = InstanceTasks.NONE
     server = None
     service_status = None
     self.instance = FreshInstanceTasks(self.context, db_info, server, service_status)
class Test_FreshInstanceTask(basetest):
    
    def setUp(self):
        super(Test_FreshInstanceTask, self).setUp()
        db_info = fake()
        db_info.hostname = "hostname"
        db_info.name = "name"
        db_info.id = "id"
        db_info.task_status = InstanceTasks.NONE
        server = None
        service_status = None
        self.instance = FreshInstanceTasks(self.context, db_info, server, service_status)

    def tearDown(self):
        super(Test_FreshInstanceTask, self).tearDown()
        
        
    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_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 ttest_create_server(self):
        CONF.nova_scheduler_ignore = True
        server = fake()
        server.id = 1
         
        when(os.path).isfile(any()).thenReturn(False)
        when(os.path).join(any(),any()).thenReturn(None)
        when(servers.ServerManager).create(any(), any(), any(), files=any(), userdata=any(),
            security_groups=any(), block_device_mapping=any(), 
            availability_zone=any()).thenReturn(server)
         
        flavor_id = "flavor_id"
        image_id = "image_id"
        security_groups = "security_groups"
        service_type = "service_type"
        ignore_hosts=None
        block_device_mapping = None
         
         
         
        self.assertEqual(server,self.instance._create_server(flavor_id, image_id, security_groups,
                                         service_type, block_device_mapping, ignore_hosts))
         
        verify(os.path,times=2).isfile(any())
         
         
    def test_relocate_master(self):
        pass
        master_id = "master_id"
        slave_id = "slave_id"
        instance = fake()
        def get_guest():
            return API(self.context,master_id)
        instance.get_guest = get_guest
        when(FreshInstanceTasks).load(any(),any()).thenReturn(instance)
         
        mStatus = {"file":"test_file","position":"1"}
        when(API).ksc_show_master_status().thenReturn(mStatus)
         
        group_item = fake()
        group_item.group_id = 1
        when(InstanceGroupItem).get_by_instance_id(any(),any()).thenReturn(group_item)
         
        when(FreshInstanceTasks)._get_instance_ip(any()).thenReturn("master_host")
        when(FreshInstanceTasks)._gen_rpl_user_password(any()).thenReturn("repl_password")
         
        when(API).ksc_relocate_master(master_host=any(), repl_user=any(),
                                        repl_password=any(), master_log_file=any(),
                                        master_log_pos=any()).thenReturn(None)
         
         
        self.instance._relocate_master(master_id,slave_id)
         
    def test_create_instance_SG(self):
        self._create_instance(DBInstanceType.SINGLE)
        verify(FreshInstanceTasks)._create_master_user(instance_id=any(), password=any())
        verify(FreshInstanceTasks,times=1)._update_instance_type(any(),any())
         
    def test_create_instance_MASTER(self):
        self._create_instance(DBInstanceType.MASTER)
        verify(FreshInstanceTasks)._create_master_user(instance_id=any(), password=any())
        verify(FreshInstanceTasks,times=1)._update_instance_type(any(),any())
         
    def test_create_instance_STANDBY(self):
        self._create_instance(DBInstanceType.STANDBY)
        verify(FreshInstanceTasks)._guest_prepare(any(), any(), any(),
                                any(), any(), any(),any())
        verify(FreshInstanceTasks,times=2)._update_instance_type(any(),any())
        verify(FreshInstanceTasks,times=1)._relocate_master(master_id=any(), slave_id=any())
         
    def test_create_instance_RR(self):
        self._create_instance(DBInstanceType.READ_REPLI)
        verify(FreshInstanceTasks)._guest_prepare(any(), any(), any(),
                                any(), any(), any(),any())
        verify(FreshInstanceTasks,times=1)._update_instance_type(any(),any())
        verify(FreshInstanceTasks,times=1)._relocate_master(master_id=any(), slave_id=any())
         
         
    def _create_instance(self,type):
        CONF.trove_vip_support = False
        flavor = {'id':"id","ram":"ram"}
        image_id = "image_id"
        databases = "databases"
        users = "users"
        service_type = "service_type"
        volume_size = "volume_size"
        security_groups = "security_groups"
        backup_id = "backup_id"
        instance_type = type
        ignore_hosts=None,
        master_id="master_id"
        extend={}
         
        server = fake()
        volume_info = fake()
        when(FreshInstanceTasks)._create_server_volume_individually(
                any(),any(),any(),
                any(),any(), any()).thenReturn((server,volume_info))
         
        config = fake()
        config.config_contents = ""
        when(FreshInstanceTasks)._render_config(any(),any(),instance_id=any()).thenReturn(config)
         
        when(FreshInstanceTasks)._guest_prepare(any(), any(), any(),
                                any(), any(), any(),any()).thenReturn(None)
                                 
        when(FreshInstanceTasks).update_db(task_status=any()).thenReturn(None)
        when(utils).poll_until(any(),
                             sleep_time=any(),
                             time_out=any()).thenReturn(None)
                              
        group_item = fake()
        group_item.group_id = 1
        when(InstanceGroupItem).get_by_instance_id(any(),any()).thenReturn(group_item)
         
         
        when(FreshInstanceTasks)._create_master_user(instance_id=any(), password=any()).thenReturn(None)
         
        when(FreshInstanceTasks)._update_instance_type(any(),any()).thenReturn(None)
         
        when(FreshInstanceTasks)._relocate_master(master_id=any(), slave_id=any()).thenReturn(None)
         
        when(AutoBackup).get_autobackup_expiretime(any(),any()).thenReturn(None)
         
        when(FreshInstanceTasks)._backup_name(instance_id=any())
         
        when(Backup).create(context=any(), instance=any(),
                              name=any(), description=any(), group_id=any(),
                              backup_type=any(), expire_at=any()).thenReturn(None)
                               
        when(models.Instance).set_blkiotune(context=any(), instance_id=any()).thenReturn(None)
         
        when(group_rpcapi.API).group_update(any()).thenReturn(None)
         
        when(FreshInstanceTasks).send_usage_event(any(), instance_size=any()).thenReturn(None)
         
        self.instance.create_instance(flavor, image_id, databases, users,
                        service_type, volume_size, security_groups,
                        backup_id, instance_type, ignore_hosts=ignore_hosts,
                        master_id=master_id, extend=extend)       
        
        
    def test_create_ha_instance(self):
        backup_item = fake()
        backup_item.id = 1
        when(Backup).get_latest_backup(any(), group_id =any()).thenReturn(backup_item)
        
        when(FreshInstanceTasks).load(any(),any()).thenReturn(self.instance)
        
        when(FreshInstanceTasks).create_instance(any(), any(), any(), any(),
                        any(), any(), any(),
                        any(), any(), ignore_hosts=any(),
                        master_id=any(), extend=any()).thenReturn(None)   
                        
        group_item = fake()
        group_item.group_id = 1
        when(InstanceGroupItem).get_by_instance_id(any(),any()).thenReturn(group_item)
         
        when(models.Instance).get_pyhhostname(any(),any()).thenReturn("master_phyhost")
        
        slave_id = "slave_id"
        slave_name = "slave_name"
        flavor = "flavor"
        image_id = "image_id"
        databases = "databases"
        users = "users"
        service_type = "service_type"
        volume_size = "volume_size"
        security_groups = "security_groups"
        extend = "extend"
        backup_id = "backup_id"
        
        self.instance.create_ha_instance(slave_id, slave_name, flavor, 
                           image_id, databases, users, service_type, 
                           volume_size, security_groups, extend, 
                           backup_id)
        
    def test_upgrade_instance_toha(self):
        flavor = "flavor"
        image_id = "image_id"
        databases = "databases"
        users = "users"
        service_type = "service_type"
        volume_size = "volume_size"
        security_groups = "security_groups"
        backup_id = "backup_id"
        master_id = "master_id"
    
        
        when(models.Instance).get_pyhhostname(any(),any()).thenReturn("master_phyhost")
        when(FreshInstanceTasks).create_instance(any(), any(), any(), any(),
                        any(), any(), any(),
                        any(), any(), ignore_hosts=any(),
                        master_id=any()).thenReturn(None)
        self.instance.upgrade_instance_toha(flavor, image_id, databases, users,
                        service_type, volume_size, security_groups, master_id,
                        backup_id)
        
        verify(models.Instance).get_pyhhostname(any(), any())
        verify(FreshInstanceTasks).create_instance(any(), any(), any(), any(),
                        any(), any(), any(),
                        any(), any(), ignore_hosts=any(),
                        master_id=any())
        
    def test_failover_master(self):
        
        
        standby = fake()
        standby.instance_id = "standby_instance_id"
        standby.id = "standby_id"
        when(InstanceGroupItem).get_by_gid_type(any(), any(),any()).thenReturn([standby])
        
        rr = fake()
        rr.instance_id = "item_instance_id"
        rr.id = "item_id"
        
        item2 = fake()
        item2.instance_id = "item_instance_id2"
        item2.id = "item_id2"
        when(InstanceGroupItem).list_by_gid(any(), any(), is_active=any()).thenReturn([rr,standby])
        
        
        inst = fake()
        inst.guest = KSC_API(self.context,"id")
        when(models.Instance).load(any(),any()).thenReturn(inst)
        
        statusDict = {'master_server_id':'1','relay_master_log_file':'relay_master_log_file','exec_master_log_pos':1}
        when(KSC_API).ksc_show_slave_status().thenReturn(statusDict)
        
        when(KSC_API).ksc_gen_sqlfile_by_relaylog(slavestatus=any(),group_id=any(), 
                                                                         instance_id=any()).thenReturn(None)
                                                                         
        when(KSC_API).ksc_apply_sql(any(),any()).thenReturn(None)
        
        when(InstanceGroupItem).update_type(context=any(), 
                  item_id=any(), 
                  type=any()).thenReturn(None)
                  
        when(FreshInstanceTasks)._update_instance_type(any(), instance_type=any()).thenReturn(None)
        
        when(FreshInstanceTasks)._relocate_master(master_id=any(), slave_id=any()).thenReturn(None)
        
        master_db_info = fake()
        master_db_info.name = "name"
        def save():
            pass
        master_db_info.save = save
        master_db_info.compute_instance_id = "compute_instance_id"
        when(models).get_db_info(any(), any()).thenReturn(master_db_info)
        
        new_master_server = fake()
        new_master_server.update = lambda x:x
        when(models).load_server(any(), any(), any()).thenReturn(new_master_server)
        
        CONF.trove_vip_support = True
        
        when(InstanceVip).get_by_instance_id(any(),any()).thenReturn("localhost")
        when(InstanceVip).allocate(any(),instance_id=any(),vip=any()).thenReturn("localhost")
        
        
        
        old_group_item = fake()
        old_group_item.instance_id = "old_instance_id"
        old_group_item.group_id = "group_id"
        old_group_item.id = "old_group_item_id"
        self.instance._failover_master(old_group_item)
        
        verify(KSC_API,times=2).ksc_show_slave_status()
        
        verify(KSC_API).ksc_gen_sqlfile_by_relaylog(slavestatus=any(),group_id=any(), 
                                                                         instance_id=any())
        
        verify(KSC_API).ksc_apply_sql(any(),any())
        
        verify(FreshInstanceTasks,times=2)._update_instance_type(any(), instance_type=any())
        verify(FreshInstanceTasks,times=1)._relocate_master(master_id=any(), slave_id=any())
        
        verify(InstanceVip).get_by_instance_id(any(),any())
        verify(InstanceVip).allocate(any(),instance_id=any(),vip=any())
        
    def test_failover_standby(self):
        
        master = fake()
        master.instance_id = "master_instance_id"
        master.id = "master_id"
        when(InstanceGroupItem).get_by_gid_type(any(), any(),any()).thenReturn([master])
        
        when(InstanceGroupItem).update_type(context=any(), 
                  item_id=any(), 
                  type=any()).thenReturn(None)
        group_item = fake()
        group_item.group_id = 1
        group_item.id = 1
        when(InstanceGroupItem).get_by_instance_id(any(),any()).thenReturn(group_item)
        
        when(FreshInstanceTasks)._relocate_master(master_id=any(), slave_id=any()).thenReturn(None)
        pass
        old_group_item = fake()
        old_group_item.instance_id = "old_instance_id"
        old_group_item.group_id = "old_group_id"
        old_group_item.id = "old_group_item_id"
        self.instance._failover_standby(old_group_item)
        
        verify(InstanceGroupItem,times=2).update_type(context=any(), 
                  item_id=any(), 
                  type=any())
        verify(FreshInstanceTasks,times=1)._relocate_master(master_id=any(), slave_id=any())
        
    def test_failover_rr(self):
        
        master = fake()
        master.instance_id = "master_instance_id"
        master.id = "master_id"
        when(InstanceGroupItem).get_by_gid_type(any(), any(),any()).thenReturn([master])
        
        when(InstanceGroupItem).update_type(context=any(), 
                  item_id=any(), 
                  type=any()).thenReturn(None)
        group_item = fake()
        group_item.group_id = 1
        group_item.id = 1
        when(InstanceGroupItem).get_by_instance_id(any(),any()).thenReturn(group_item)
        
        when(FreshInstanceTasks)._relocate_master(master_id=any(), slave_id=any()).thenReturn(None)
        
        CONF.trove_vip_support = True
        when(InstanceVip).get_by_instance_id(any(),any()).thenReturn("localhost")
        when(InstanceVip).allocate(any(),instance_id=any(),vip=any()).thenReturn("localhost")
        
        
        pass
        old_group_item = fake()
        old_group_item.instance_id = "old_instance_id"
        old_group_item.group_id = "old_group_id"
        old_group_item.id = "old_group_item_id"
        self.instance._failover_rr(old_group_item)
        
        verify(InstanceGroupItem,times=2).update_type(context=any(), 
                  item_id=any(), 
                  type=any())
        verify(FreshInstanceTasks,times=1)._relocate_master(master_id=any(), slave_id=any())
        verify(InstanceVip).get_by_instance_id(any(),any())
        verify(InstanceVip).allocate(any(),instance_id=any(),vip=any())
        
        
    def test_failover_create_instance(self):
        when(FreshInstanceTasks)._waite_relay_finished(any()).thenReturn(None)
        
        group_item = fake()
        group_item.group_id = 1
        group_item.id = 1
        group_item.type = DBInstanceType.MASTER
        when(InstanceGroupItem).get_by_instance_id(any(),any()).thenReturn(group_item)
        
        when(FreshInstanceTasks)._failover_master(any()).thenReturn(None)
        when(FreshInstanceTasks)._get_ignore_hosts(any()).thenReturn(None)
        
        when(FreshInstanceTasks)._create_server_volume_individually(any(),any(),any(),any(),any(),any()).thenReturn((None,None))
        
        config = fake()
        config.config_contents = ""
        when(FreshInstanceTasks)._render_config(any(), any(), instance_id=any()).thenReturn(config)
        
        when(FreshInstanceTasks)._guest_prepare(any(), any(), any(),
                                any(), any(), any(),
                                any())
        when(FreshInstanceTasks).update_db(task_status=any()).thenReturn(None)
        
        when(utils).poll_until(any(),
                         sleep_time=any(),
                         time_out=any()).thenRaise(Exception("mm"))
                         
        when(FreshInstanceTasks)._failover_master(any()).thenReturn(None)
        when(FreshInstanceTasks).send_usage_event(any(), instance_size=any()).thenReturn(None)
        when(FreshInstanceTasks).failover_release_resource(any()).thenReturn(None)
        
        item = fake()
        item.id = "123"
        when(InstanceGroupItem).get_by_gid_type(any(), any(),any()).thenReturn(item)
        
        when(InstanceGroupItem).update_type(context=any(), item_id=any(),type=any()).thenReturn(None)
        
        
        
        
        flavor = {"ram":"1000","id":"1"}
        image_id ="image_id"
        service_type = "service_type"
        volume_size = "volume_size"
        security_groups = "security_groups"
        backup_id = "backup_id"
        old_instance_id = "old_instance_id"
        
        self.assertRaises(Exception,self.instance.failover_create_instance,flavor, image_id, service_type, volume_size, 
                                 security_groups, backup_id, old_instance_id)
        
        
        when(utils).poll_until(any(),
                         sleep_time=any(),
                         time_out=any()).thenReturn(None)
        self.assertEqual(None,self.instance.failover_create_instance(flavor, image_id, service_type, volume_size, 
                                 security_groups, backup_id, old_instance_id))

        
        pass