Exemple #1
0
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler',
                                              'simple_stop_vm_scheduler',
                                              vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger(
        'simple_stop_vm_scheduler', start_date + 60, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+60, 120)

    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler',
                                              'simple_start_vm_scheduler',
                                              vm.get_vm().uuid, 'startVm',
                                              None)
    schd_trigger2 = schd_ops.create_scheduler_trigger(
        'simple_start_vm_scheduler', start_date + 120, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+120, 120)

    test_stub.sleep_util(start_date + 58)
    vm.update()
    if not test_lib.lib_is_vm_running(vm.get_vm()):
        test_util.test_fail(
            'VM is expected to run until stop vm scheduler start_date')

    for i in range(0, 10):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 60 + 120 * i + 5)
        test_util.test_logger('check VM status at %s, VM is expected to stop' %
                              (start_date + 60 + 120 * i + 5))
        vm.update()
        if vm.get_vm().state != 'Stopping' and vm.get_vm().state != 'Stopped':
            test_util.test_fail('VM is expected to stop')

        test_stub.sleep_util(start_date + 60 + 120 * i + 65)
        test_util.test_logger(
            'check VM status at %s, VM is expected to start' %
            (start_date + 60 + 120 * i + 65))
        vm.update()
        if vm.get_vm().state != 'Starting' and vm.get_vm().state != 'Running':
            test_util.test_fail('VM is expected to start')

    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)

    vm.destroy()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2
    global new_account

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler', 'simple_stop_vm_scheduler', vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+30, None, 60, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+30, 60)

    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None)
    schd_trigger2 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+60, None, 60, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+60, 60)

    test_stub.sleep_util(start_date+130)

    test_util.test_dsc('check scheduler state after creating scheduler')
    check_scheduler_state(schd_job1, 'Enabled')
    check_scheduler_state(schd_job2, 'Enabled')
    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob', start_date+30):
        test_util.test_fail('StopVmInstanceJob not executed at expected timestamp range')
    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob', start_date+60):
        test_util.test_fail('StartVmInstanceJob not executed at expected timestamp range')
    
#    vm.check();
    new_account = account_operations.create_account('new_account', 'password', 'Normal')

    res_ops.change_recource_owner(new_account.uuid, vm.vm.uuid)

    current_time = int(time.time())
    except_start_time =  start_date + 60 * (((current_time - start_date) % 60) + 1)
    test_stub.sleep_util(except_start_time + 130)
 
    test_util.test_dsc('check scheduler state after changing the owner of vm')
    check_scheduler_state(schd_job1, 'Disabled')
    check_scheduler_state(schd_job2, 'Disabled')
    if check_scheduler_msg('run scheduler for job: StopVmInstanceJob', except_start_time+30):
        test_util.test_fail('StopVmInstanceJob executed at unexpected timestamp range')
    if check_scheduler_msg('run scheduler for job: StartVmInstanceJob', except_start_time+60):
        test_util.test_fail('StartVmInstanceJob executed at unexpected timestamp range')

    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)
    vm.destroy()
    account_operations.delete_account(new_account.uuid)

    test_util.test_pass('Check Scheduler State after Changing VM Owner Success')
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler', 'simple_stop_vm_scheduler', vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+60, 3, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+60, 120, 3)
#    if schd1.stopTime != start_date + 60 + 120 * 3:
#        test_util.test_fail('[scheduler:] %s is expected have stopTime as %s' % (schd1.uuid, start_date + 60 + 120 * 3))
    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None)
    schd_trigger2 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+120, 2, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+120, 120, 2)
#    if schd2.stopTime != start_date + 120 + 120 * 2:
#        test_util.test_fail('[scheduler:] %s is expected have stopTime as %s' % (schd2.uuid, start_date + 120 + 120 * 2))

    test_stub.sleep_util(start_date+58)
    vm.update()
    if not test_lib.lib_is_vm_running(vm.get_vm()):
        test_util.test_fail('VM is expected to run until stop vm scheduler start_date')

    for i in range(0, 5):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 60 + 120*i + 5)
        test_util.test_logger('check VM status at %s, VM is expected to stop' % (start_date + 60 + 120*i + 5))
        vm.update()
        if vm.get_vm().state != 'Stopping' and vm.get_vm().state != 'Stopped':
            test_util.test_fail('VM is expected to stop')

        test_stub.sleep_util(start_date + 60 + 120*i + 65)
	if i >= 2:
            test_util.test_logger('check VM status at %s, VM is expected to stay stopped' % (start_date + 60 + 120*i + 65))
            vm.update()
	    if vm.get_vm().state != 'Stopped':
                test_util.test_fail('VM is expected to stay stop')
	    continue

        test_util.test_logger('check VM status at %s, VM is expected to start' % (start_date + 60 + 120*i + 65))
        vm.update()
        if vm.get_vm().state != 'Starting' and vm.get_vm().state != 'Running':
            test_util.test_fail('VM is expected to start')

    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)

    vm.destroy()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Repeat Count Success')
Exemple #4
0
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler',
                                              'simple_stop_vm_scheduler',
                                              vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger(
        'simple_stop_vm_scheduler', start_date + 60, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+60, 120)

    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler',
                                              'simple_start_vm_scheduler',
                                              vm.get_vm().uuid, 'startVm',
                                              None)
    schd_trigger2 = schd_ops.create_scheduler_trigger(
        'simple_start_vm_scheduler', start_date + 120, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+120, 120)

    test_stub.sleep_util(start_date + 60 + 120 * 2 + 5)
    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)

    time.sleep(60)

    vm.update()
    vm_state = vm.get_vm().state

    for i in range(3, 5):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 60 + 120 * i + 5)
        vm.update()
        test_util.test_logger(
            'check VM status at %s, VM is expected to stay in state %s' %
            (start_date + 60 + 120 * i + 5, vm_state))
        if vm.get_vm().state != vm_state:
            test_util.test_fail('VM is expected to stay in state %s' %
                                (vm_state))

    vm.destroy()
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
def test():
    global vm
    global schd_job
    global schd_trigger
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    vm.check()
    start_date = int(time.time())
    schd_job = schd_ops.create_scheduler_job('simple_reboot_vm_scheduler', 'simple_reboot_vm_scheduler', vm.get_vm().uuid, 'rebootVm', None)
    schd_trigger = schd_ops.create_scheduler_trigger('simple_reboot_vm_scheduler', start_date+60, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)
    #schd = vm_ops.reboot_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_reboot_vm_scheduler', start_date+60, 120)
    test_stub.sleep_util(start_date+58)
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 120):
        test_util.test_fail('VM: %s is not reboot in 120 seconds. Fail to reboot it with scheduler. ' % vm.get_vm().uuid)

    for i in range(0, 58):
        if test_lib.lib_find_in_local_management_server_log(start_date+i, '[msg received]: org.zstack.header.vm.RebootVmInstanceMsg {"org.zstack.header.vm.RebootVmInstanceMsg', vm.get_vm().uuid):
            test_util.test_fail('VM is expected to reboot start from %s' (start_date+60))

    test_stub.sleep_util(start_date+59)
    if not test_lib.lib_find_in_local_management_server_log(start_date+60, '[msg received]: org.zstack.header.vm.RebootVmInstanceMsg {"org.zstack.header.vm.RebootVmInstanceMsg', vm.get_vm().uuid):
        test_util.test_fail('VM is expected to reboot start from %s' % (start_date+60))

    schd_ops.del_scheduler_job(schd_job.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger.uuid)
    vm.destroy()
    test_util.test_pass('Create Simple VM Reboot Scheduler Success')
Exemple #6
0
def test():
    global vm
    global schd_job
    global schd_trigger
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    vm.check()
    start_date = int(time.time())
    schd_job = schd_ops.create_scheduler_job('simple_reboot_vm_scheduler', 'simple_reboot_vm_scheduler', vm.get_vm().uuid, 'rebootVm', None)
    schd_trigger = schd_ops.create_scheduler_trigger('simple_reboot_vm_scheduler', start_date+60, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)
    #schd = vm_ops.reboot_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_reboot_vm_scheduler', start_date+60, 120)
    test_stub.sleep_util(start_date+58)
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 120):
        test_util.test_fail('VM: %s is not reboot in 120 seconds. Fail to reboot it with scheduler. ' % vm.get_vm().uuid)

    for i in range(0, 58):
        if test_lib.lib_find_in_local_management_server_log(start_date+i, '[msg received]: {"org.zstack.header.vm.RebootVmInstanceMsg', vm.get_vm().uuid):
            test_util.test_fail('VM is expected to reboot start from %s' (start_date+60))

    test_stub.sleep_util(start_date+59)
    if not test_lib.lib_find_in_local_management_server_log(start_date+60, '[msg received]: {"org.zstack.header.vm.RebootVmInstanceMsg', vm.get_vm().uuid):
        test_util.test_fail('VM is expected to reboot start from %s' % (start_date+60))

    schd_ops.del_scheduler_job(schd_job.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger.uuid)
    vm.destroy()
    test_util.test_pass('Create Simple VM Reboot Scheduler Success')
def create_start_vm_scheduler(vm_uuid, start_date, ops_id):
    global schd_jobs
    global schd_triggers
    schd_job = schd_ops.create_scheduler_job('simple_start_vm_scheduler_%s' % (ops_id), 'simple_stop_vm_scheduler', vm_uuid, 'startVm', None)
    schd_trigger = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+300, None, 1000, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)
    schd_jobs.append(schd_job)
    schd_triggers.append(schd_trigger)
Exemple #8
0
def create_start_vm_scheduler(vm_uuid, start_date, ops_id):
    global schd_jobs
    global schd_triggers
    schd_job = schd_ops.create_scheduler_job('simple_start_vm_scheduler_%s' % (ops_id), 'simple_stop_vm_scheduler', vm_uuid, 'startVm', None)
    schd_trigger = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+100+ops_id, None, 1000, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)
    schd_jobs.append(schd_job)
    schd_triggers.append(schd_trigger)
def test():
    global vm
    global schd_job
    global schd_trigger
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)

    conditions = res_ops.gen_query_conditions('type', '=', 'Ceph')
    pss = res_ops.query_resource(res_ops.PRIMARY_STORAGE, conditions)
    if len(pss) == 0:
        test_util.test_skip('Skip due to no ceph storage available')

    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_name('volume for snapshot scheduler testing')
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)

    volume.attach(vm)
    volume.detach()

    test_util.test_dsc('create snapshot and check')

    start_date = int(time.time())

    parameters = {"snapshotMaxNumber": "3"}
    schd_job = schd_ops.create_scheduler_job('simple_create_snapshot_scheduler', 'simple_create_snapshot_scheduler', volume.get_volume().uuid, 'volumeSnapshot', parameters)
    schd_trigger = schd_ops.create_scheduler_trigger('simple_create_snapshot_scheduler', start_date+60, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)

    snapshot_num = 0
    for i in range(0, 3):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 60 + 120*i - 2)
        test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 60 + 120*i - 2, snapshot_num))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' % (snapshot_num))
        snapshot_num += 1

        test_stub.sleep_util(start_date + 60 + 120*i + 65)
        test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 60 + 120*i + 65, snapshot_num))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' % (snapshot_num))

    test_stub.sleep_util(start_date + 60 + 120*3 +65)
    test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 60 + 120*i + 65, 3))

    new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
    if new_snapshot_num != 3:
        test_util.test_fail('there sholuld be 3 snapshots')

    schd_ops.del_scheduler_job(schd_job.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger.uuid)
    vm.destroy()
    test_util.test_pass('Create limit create volume snapshot num success')
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler', 'simple_stop_vm_scheduler', vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+60, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+60, 120)

    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None)
    schd_trigger2 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+120, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+120, 120)

    test_stub.sleep_util(start_date + 60 + 120*2 + 5)
    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)

    time.sleep(60)

    vm.update()
    vm_state = vm.get_vm().state

    for i in range(3, 5):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 60 + 120*i + 5)
        vm.update()
        test_util.test_logger('check VM status at %s, VM is expected to stay in state %s' % (start_date + 60 + 120*i + 5, vm_state))
        if vm.get_vm().state != vm_state:
            test_util.test_fail('VM is expected to stay in state %s' % (vm_state))

    vm.destroy()
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
Exemple #11
0
def test():
    global vm
    global schd_job
    global schd_trigger
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)

    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_name('volume for snapshot scheduler testing')
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)

    volume.attach(vm)
    volume.detach()

    test_util.test_dsc('create snapshot and check')

    start_date = int(time.time())
    #sp_option = test_util.SnapshotOption()
    #sp_option.set_name('simple_schduler_snapshot')
    #sp_option.set_volume_uuid(volume.get_volume().uuid)

    schd_job = schd_ops.create_scheduler_job('simple_create_snapshot_scheduler', 'simple_create_snapshot_scheduler', volume.get_volume().uuid, 'volumeSnapshot', None)
    schd_trigger = schd_ops.create_scheduler_trigger('simple_create_snapshot_scheduler', start_date+60, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)
    #schd = vol_ops.create_snapshot_scheduler(sp_option, 'simple', 'simple_create_snapshot_scheduler',  start_date+60, 120)

    snapshot_num = 0
    for i in range(0, 3):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 60 + 120*i - 2)
        test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 60 + 120*i - 2, snapshot_num))
	new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
	if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' % (snapshot_num))
	snapshot_num += 1

        test_stub.sleep_util(start_date + 60 + 120*i + 65)
        test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 60 + 120*i + 65, snapshot_num))
	new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
	if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' % (snapshot_num))

    schd_ops.del_scheduler_job(schd_job.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger.uuid)
    vm.destroy()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
def test():
    test_util.test_dsc('Create Scheduler Trigger and Scheduler Job')
    cond = res_ops.gen_query_conditions('type', '=', 'ImageStoreBackupStorage')
    bs = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)
    if not bs :
        test_util.test_skip('Not find image store type backup storage.')
    image_name = os.environ.get('imageName_net')
    l3_name = os.environ.get('l3PublicNetworkName')
    remote_bs_vm = test_stub.create_vm('remote_bs_vm', image_name, l3_name)
    test_obj_dict.add_vm(remote_bs_vm)
    image_name = os.environ.get('imageName_s')
    l3_name = os.environ.get('l3VlanNetworkName1')
    test_vm = test_stub.create_vm('test-vm', image_name, l3_name)
    test_obj_dict.add_vm(test_vm)
    add_local_bs_tag = tag_ops.create_system_tag('ImageStoreBackupStorageVO', bs[0].uuid,'allowbackup')
    #wait for vm start up 
    test_lib.lib_wait_target_up(remote_bs_vm.vm.vmNics[0].ip, '22', 90)
    remote_bs = test_stub.create_image_store_backup_storage('remote_bs', remote_bs_vm.vm.vmNics[0].ip, 'root', 'password', '/zstack_bs', '22')
    add_remote_bs = tag_ops.create_system_tag('ImageStoreBackupStorageVO', remote_bs.uuid,'remotebackup')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(remote_bs.uuid, zone_uuid)    
    parameters= {"retentionType":"Count","retentionValue":"1","backupStorageUuids":bs[0].uuid,"remoteBackupStorageUuid":""}
    schd_job = schd_ops.create_scheduler_job('backup_database_scheduler', 'backup_database_scheduler', '7ae6456c0b01324dae6d4bef358a5772', 'databaseBackup',parameters=parameters)
    schd_trigger = schd_ops.create_scheduler_trigger('backup_database_schedule',type='cron', cron='0 * * ? * *')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)
    #wait for 60s *2
    time.sleep(120)
 
    db_backup1 = schd_ops.query_db_backup()
    if len(db_backup1) != 1:
        test_util.test_fail('there sholuld be 1 db backup,but now there are %s' % len(db_backup1))   
    db_backup2 = schd_ops.get_db_backup_from_imagestore(url = 'ssh://*****:*****@%s:22/zstack_bs' % bs[0].hostname)
    if len(db_backup2.backups) != 1:
        test_util.test_fail('there sholuld be 1 db backup,but now there are %s' % len(db_backup2.backups))

    db_url = schd_ops.export_db_backup_from_bs(bs[0].uuid, db_backup1[0].uuid) 
    test_util.test_dsc('export database backup successfully,url is %s' % db_url.databaseBackupUrl)
    schd_ops.sync_db_from_imagestore_bs(remote_bs.uuid, bs[0].uuid, db_backup1[0].uuid)
    test_vm.destroy()

    test_util.test_dsc('Recover db From BackupStorage')
    backupStorageUrl = 'ssh://*****:*****@%s:22/zstack_bs' % remote_bs_vm.vm.vmNics[0].ip
    recover_db = schd_ops.recover_db_from_backup(backupStorageUrl = backupStorageUrl, backupInstallPath = db_backup2.backups[0].installPath, mysqlRootPassword='******')
    #wait for db recover
    time.sleep(60)
    cond = res_ops.gen_query_conditions('name', '=', 'test-vm')
    vm = res_ops.query_resource(res_ops.VM_INSTANCE, cond)
    if not vm:
        test_util.test_fail('there sholuld be a vm after recovering db from remote backup bs')
    
    schd_ops.change_scheduler_state(schd_job.uuid, 'disable')
    db_backup3 = schd_ops.query_db_backup()
    zone = res_ops.query_resource(res_ops.ZONE)[0]
    zone_ops.delete_zone(zone.uuid)
    recover_db = schd_ops.recover_db_from_backup(uuid=db_backup3[0].uuid, mysqlRootPassword='******')
    #wait for db recover
    time.sleep(60)
    vm = res_ops.query_resource(res_ops.VM_INSTANCE, cond)
    if not vm:
        test_util.test_fail('there sholuld be a vm after recovering db from local backup bs')

    test_util.test_dsc('Clear env')
    schd_ops.del_scheduler_job(schd_job.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger.uuid)
    tag_ops.delete_tag(add_local_bs_tag.uuid)
    bs_ops.delete_backup_storage(remote_bs.uuid)    
    bs_ops.reclaim_space_from_bs(bs[0].uuid)
    remote_bs_vm.destroy()
    test_vm.destroy()
def test():
    global project_uuid, project_admin_uuid, virtual_id_uuid, project_operator_uuid, plain_user_uuid

    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]
    # 1 create project
    project_name = 'test_project'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    project_linked_account_uuid = project.linkedAccountUuid

    if flavor['target_role'] == 'project_admin':
        # 2 create virtual id
        project_admin_name = 'username'
        project_admin_password = '******'
        project_admin_uuid = iam2_ops.create_iam2_virtual_id(project_admin_name, project_admin_password).uuid
        virtual_id_uuid = iam2_ops.create_iam2_virtual_id('usernametwo', 'password').uuid
    
        # 3 create project admin
        iam2_ops.add_iam2_virtual_ids_to_project([project_admin_uuid],project_uuid)
        attributes = [{"name": "__ProjectAdmin__", "value": project_uuid}]
        iam2_ops.add_attributes_to_iam2_virtual_id(project_admin_uuid, attributes)

        # login in project by project admin
        project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(project_admin_name, project_admin_password)
        project_login_uuid = iam2_ops.login_iam2_project(project_name, session_uuid=project_admin_session_uuid).uuid
        # iam2_ops.remove_attributes_from_iam2_virtual_id(virtual_id_uuid, attributes)
    elif flavor['target_role'] == 'project_operator':
        project_operator_name = 'username2'
        project_operator_password = '******'
        attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
        project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid
        virtual_id_uuid = iam2_ops.create_iam2_virtual_id('usernamethree','password').uuid

        # login in project by project operator
        iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
        project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
        project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid
    elif flavor['target_role'] == 'project_member':
	plain_user_name = 'username'
	plain_user_password = '******'
	plain_user_uuid = iam2_ops.create_iam2_virtual_id(plain_user_name, plain_user_password,
	                                                  project_uuid=project_uuid).uuid
	# 3 add virtual id to project
	iam2_ops.add_iam2_virtual_ids_to_project([plain_user_uuid],project_uuid)

	# 4 login in project by plain user
	plain_user_session_uuid = iam2_ops.login_iam2_virtual_id(plain_user_name, plain_user_password)

	# 4 login in project
	#project_inv=iam2_ops.get_iam2_projects_of_virtual_id(plain_user_session_uuid)
	project_login_uuid = iam2_ops.login_iam2_project(project_name, plain_user_session_uuid).uuid


    # Image related ops: Add, Delete, Expunge, sync image size, Update QGA, delete, expunge
    if flavor['target_role'] == 'project_member':
        statements = [{"effect": "Allow", "actions": ["org.zstack.header.vm.**"]}, {"effect": "Allow", "actions": ["org.zstack.scheduler.**"]}]
        role_uuid = iam2_ops.create_role('test_role', statements).uuid
        iam2_ops.add_roles_to_iam2_virtual_id([role_uuid], plain_user_uuid)

    vm_creation_option = test_util.VmOption()
    l3_net_uuid = test_lib.lib_get_l3_by_name(os.environ.get('l3VlanNetwork3')).uuid
    acc_ops.share_resources([project_linked_account_uuid], [l3_net_uuid])
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    image_uuid = test_lib.lib_get_image_by_name("centos").uuid
    vm_creation_option.set_image_uuid(image_uuid)
    acc_ops.share_resources([project_linked_account_uuid], [image_uuid])
    instance_offering_uuid = test_lib.lib_get_instance_offering_by_name(os.environ.get('instanceOfferingName_s')).uuid
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    acc_ops.share_resources([project_linked_account_uuid], [instance_offering_uuid])
    vm_creation_option.set_name('vm_for_project_management')
    vm_creation_option.set_session_uuid(project_login_uuid)
    vm = test_stub.create_vm(image_uuid = image_uuid, session_uuid=project_login_uuid) 

    # scheduler ops:
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None, session_uuid=project_login_uuid)
    schd_trigger1 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+5, None, 15, 'simple', session_uuid=project_login_uuid)
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid, session_uuid=project_login_uuid)
    schd_ops.change_scheduler_state(schd_job1.uuid, 'disable', session_uuid=project_login_uuid)
    schd_ops.change_scheduler_state(schd_job1.uuid, 'enable', session_uuid=project_login_uuid)
    schd_ops.remove_scheduler_job_from_trigger(schd_trigger1.uuid, schd_job1.uuid, session_uuid=project_login_uuid)
    schd_ops.del_scheduler_job(schd_job1.uuid, session_uuid=project_login_uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid, session_uuid=project_login_uuid)
    schd_ops.get_current_time()
    vm_ops.destroy_vm(vm.get_vm().uuid, session_uuid=project_login_uuid)
    vm_ops.expunge_vm(vm.get_vm().uuid, session_uuid=project_login_uuid)

    # 11 delete
    acc_ops.logout(project_login_uuid)
    if virtual_id_uuid != None:
        iam2_ops.delete_iam2_virtual_id(virtual_id_uuid)
    if project_admin_uuid != None:
        iam2_ops.delete_iam2_virtual_id(project_admin_uuid)
    if project_operator_uuid != None:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if plain_user_uuid != None:
        iam2_ops.delete_iam2_virtual_id(plain_user_uuid)

    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)

    test_util.test_pass('success test iam2 login in by project admin!')
def test():
    imagestore = test_lib.lib_get_image_store_backup_storage()
    if imagestore == None:
        test_util.test_skip('Required imagestore to test')
    image_uuid = test_stub.get_image_by_bs(imagestore.uuid)
    pss = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
    ps_uuid = pss[0].uuid

    vmBackup = 'vmBackup'
    volumeBackup = 'volumeBackup'
    parameters = {'retentionType': 'Count',
                  'retentionValue': '10',
                  'backupStorageUuids': imagestore.uuid,
                  'remoteBackupStorageUuid': '',
                  'networkWriteBandwidth': '',
                  'networkReadBandwidth': '',
                  'volumeReadBandwidth': '',
                  'volumeWriteBandwidth': ''}
    test_util.test_logger(parameters)    

    vm1 = test_stub.create_vm(image_uuid=image_uuid, ps_uuid=ps_uuid)
    vm1_uuid = vm1.get_vm().uuid
    volume1 = test_stub.create_volume()
    vol1_uuid = volume1.get_volume().uuid
    test_lib.lib_attach_volume(vol1_uuid, vm1_uuid)
    job1 = sch_ops.create_scheduler_job(name='vm1', description='vm1 with volume backup', target_uuid=vm1_uuid, type=vmBackup, parameters=parameters)


    vm2 = test_stub.create_vm(image_uuid=image_uuid, ps_uuid=ps_uuid)
    vm2_uuid = vm2.get_vm().uuid
    volume2 = test_stub.create_volume()
    vol2_uuid = volume2.get_volume().uuid
    test_lib.lib_attach_volume(vol2_uuid, vm2_uuid)
    job2 = sch_ops.create_scheduler_job(name='vm2-root', description='vm2 root volume backup', target_uuid=vm2.get_vm().allVolumes[0].uuid, type=volumeBackup, parameters=parameters)

    name1 = 'job_group_1'
    job_group_1 = sch_ops.create_scheduler_job_group(name=name1, description='vmbackup', type=vmBackup, parameters=parameters)
    sch_ops.add_jobs_to_job_group([job1.uuid], job_group_1.uuid)

    name2 = 'job_group_2'
    job_group_2 = sch_ops.create_scheduler_job_group(name=name2, description='volumebackup', type=volumeBackup, parameters=parameters)
    sch_ops.add_jobs_to_job_group([job2.uuid], job_group_2.uuid)

    trigger = sch_ops.create_scheduler_trigger('10sec', start_time = int(time.time()+5), type = 'cron', cron = '0/10 * * * * ?')

    sch_ops.add_scheduler_job_group_to_trigger(trigger.uuid, job_group_1.uuid, triggerNow=True)
    sch_ops.add_scheduler_job_group_to_trigger(trigger.uuid, job_group_2.uuid, triggerNow=False)

    time.sleep(9)
    # check
    # vm1/vol1 backups.lenth == 2
    # vm2 backups.lenth == 1

    cond = res_ops.gen_query_conditions('volumeUuid', '=', vm1.get_vm().allVolumes[0].uuid)
    backups = res_ops.query_resource(res_ops.VOLUME_BACKUP, cond)

    assert len(backups) == 2

    cond = res_ops.gen_query_conditions('volumeUuid', '=', volume1.get_volume().uuid)
    backups = res_ops.query_resource(res_ops.VOLUME_BACKUP, cond)

    assert len(backups) == 2

    cond = res_ops.gen_query_conditions('volumeUuid', '=', vm1.get_vm().allVolumes[0].uuid)
    backups = res_ops.query_resource(res_ops.VOLUME_BACKUP, cond)

    assert len(backups) == 1

    cond = res_ops.gen_query_conditions('volumeUuid', '=',  volume1.get_volume().uuid)
    backups = res_ops.query_resource(res_ops.VOLUME_BACKUP, cond)

    assert len(backups) == 0

    sch_ops.remove_scheduler_job_from_trigger(trigger.uuid, job_group_1.uuid)
    # sch_ops.remove_scheduler_job_from_trigger(trigger.uuid, job_group_2.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', job_group_1.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]

    assert len(job_group_inv.triggersUuid) == 0

    sch_ops.del_scheduler_trigger(trigger.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', trigger.uuid)
    trigger_inv = res_ops.query_resource(res_ops.SCHEDULERTRIGGER, cond)
    cond = res_ops.gen_query_conditions('uuid', '=', job_group_2.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]

    assert len(job_group_inv.triggersUuid) == 0
    assert len(trigger_inv) == 0

    sch_ops.remove_jobs_from_job_group([job1.uuid], job_group_1.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', job_group_1.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]

    assert len(job_group_inv.jobsUuid) == 0

    sch_ops.del_scheduler_job_group(job_group_2.uuid)

    cond = res_ops.gen_query_conditions('uuid', '=', job_group_2.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)

    assert len(job_group_inv) == 0

    cond = res_ops.gen_query_conditions('uuid', '=', job2.uuid)
    job_inv = res_ops.query_resource(res_ops.SCHEDULERJOB, cond)

    assert len(job_inv) == 0
def test():
    global project_uuid, project_admin_uuid, virtual_id_uuid, project_operator_uuid, plain_user_uuid

    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]
    # 1 create project
    if flavor['target_role'] != 'system_admin':
        project_name = 'test_project'
        project = iam2_ops.create_iam2_project(project_name)
        project_uuid = project.uuid
        project_linked_account_uuid = project.linkedAccountUuid

    if flavor['target_role'] == 'project_admin':
        # 2 create virtual id
        project_admin_name = 'username'
        project_admin_password = '******'
        project_admin_uuid = iam2_ops.create_iam2_virtual_id(project_admin_name, project_admin_password).uuid
        virtual_id_uuid = iam2_ops.create_iam2_virtual_id('usernametwo', 'password').uuid
    
        # 3 create project admin
        iam2_ops.add_iam2_virtual_ids_to_project([project_admin_uuid],project_uuid)
        attributes = [{"name": "__ProjectAdmin__", "value": project_uuid}]
        iam2_ops.add_attributes_to_iam2_virtual_id(project_admin_uuid, attributes)

        # login in project by project admin
        project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(project_admin_name, project_admin_password)
        project_login_uuid = iam2_ops.login_iam2_project(project_name, session_uuid=project_admin_session_uuid).uuid
        # iam2_ops.remove_attributes_from_iam2_virtual_id(virtual_id_uuid, attributes)
    elif flavor['target_role'] == 'project_operator':
        project_operator_name = 'username2'
        project_operator_password = '******'
        attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
        project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid
        virtual_id_uuid = iam2_ops.create_iam2_virtual_id('usernamethree','password').uuid

        # login in project by project operator
        iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
        project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
        project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid
    elif flavor['target_role'] == 'project_member':
	plain_user_name = 'username'
	plain_user_password = '******'
	plain_user_uuid = iam2_ops.create_iam2_virtual_id(plain_user_name, plain_user_password,
	                                                  project_uuid=project_uuid).uuid
	# 3 add virtual id to project
	iam2_ops.add_iam2_virtual_ids_to_project([plain_user_uuid],project_uuid)

	# 4 login in project by plain user
	plain_user_session_uuid = iam2_ops.login_iam2_virtual_id(plain_user_name, plain_user_password)

	# 4 login in project
	#project_inv=iam2_ops.get_iam2_projects_of_virtual_id(plain_user_session_uuid)
	project_login_uuid = iam2_ops.login_iam2_project(project_name, plain_user_session_uuid).uuid
    elif flavor['target_role'] == 'system_admin':
        username = "******"
        password = '******'
        vid_tst_obj = test_vid.ZstackTestVid()
        test_stub.create_system_admin(username, password, vid_tst_obj)
        virtual_id_uuid = vid_tst_obj.get_vid().uuid
        project_login_uuid = acc_ops.login_by_account(username, password)


    # Image related ops: Add, Delete, Expunge, sync image size, Update QGA, delete, expunge
    if flavor['target_role'] == 'project_member':
        statements = [{"effect": "Allow", "actions": ["org.zstack.header.vm.**"]}, {"effect": "Allow", "actions": ["org.zstack.scheduler.**"]}]
        role_uuid = iam2_ops.create_role('test_role', statements).uuid
        iam2_ops.add_roles_to_iam2_virtual_id([role_uuid], plain_user_uuid)

    vm_creation_option = test_util.VmOption()
    l3_net_uuid = test_lib.lib_get_l3_by_name(os.environ.get('l3VlanNetwork3')).uuid
    if flavor['target_role'] != 'system_admin':
        acc_ops.share_resources([project_linked_account_uuid], [l3_net_uuid])
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    image_uuid = test_lib.lib_get_image_by_name("centos").uuid
    vm_creation_option.set_image_uuid(image_uuid)
    if flavor['target_role'] != 'system_admin':
        acc_ops.share_resources([project_linked_account_uuid], [image_uuid])
    instance_offering_uuid = test_lib.lib_get_instance_offering_by_name(os.environ.get('instanceOfferingName_s')).uuid
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    if flavor['target_role'] != 'system_admin':
        acc_ops.share_resources([project_linked_account_uuid], [instance_offering_uuid])
    vm_creation_option.set_name('vm_for_project_management')
    vm_creation_option.set_session_uuid(project_login_uuid)
    vm = test_stub.create_vm(image_uuid = image_uuid, session_uuid=project_login_uuid) 

    # scheduler ops:
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None, session_uuid=project_login_uuid)
    schd_trigger1 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+5, None, 15, 'simple', session_uuid=project_login_uuid)
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid, session_uuid=project_login_uuid)
    schd_ops.change_scheduler_state(schd_job1.uuid, 'disable', session_uuid=project_login_uuid)
    schd_ops.change_scheduler_state(schd_job1.uuid, 'enable', session_uuid=project_login_uuid)
    schd_ops.remove_scheduler_job_from_trigger(schd_trigger1.uuid, schd_job1.uuid, session_uuid=project_login_uuid)
    schd_ops.del_scheduler_job(schd_job1.uuid, session_uuid=project_login_uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid, session_uuid=project_login_uuid)
    schd_ops.get_current_time()
    vm_ops.destroy_vm(vm.get_vm().uuid, session_uuid=project_login_uuid)
    vm_ops.expunge_vm(vm.get_vm().uuid, session_uuid=project_login_uuid)

    # 11 delete
    acc_ops.logout(project_login_uuid)
    if virtual_id_uuid != None:
        iam2_ops.delete_iam2_virtual_id(virtual_id_uuid)
    if project_admin_uuid != None:
        iam2_ops.delete_iam2_virtual_id(project_admin_uuid)
    if project_operator_uuid != None:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if plain_user_uuid != None:
        iam2_ops.delete_iam2_virtual_id(plain_user_uuid)

    if flavor['target_role'] != 'system_admin':
        iam2_ops.delete_iam2_project(project_uuid)
        iam2_ops.expunge_iam2_project(project_uuid)

    test_util.test_pass('success test iam2 login in by project admin!')
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler',
                                              'simple_stop_vm_scheduler',
                                              vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger(
        'simple_stop_vm_scheduler', start_date + 10, None, 20, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+10, 20)
    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler',
                                              'simple_start_vm_scheduler',
                                              vm.get_vm().uuid, 'startVm',
                                              None)
    schd_trigger2 = schd_ops.create_scheduler_trigger(
        'simple_start_vm_scheduler', start_date + 20, None, 20, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+20, 20)

    test_stub.sleep_util(start_date + 45)

    test_util.test_dsc('check scheduler state after create scheduler')
    check_scheduler_state(schd_job1, 'Enabled')
    check_scheduler_state(schd_job2, 'Enabled')
    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob',
                               start_date + 10):
        test_util.test_fail(
            'StopVmInstanceJob not executed at expected timestamp range')
    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob',
                               start_date + 20):
        test_util.test_fail(
            'StartVmInstanceJob not executed at expected timestamp range')

    schd_ops.change_scheduler_state(schd_job1.uuid, 'disable')
    schd_ops.change_scheduler_state(schd_job2.uuid, 'disable')

    current_time = int(time.time())
    except_start_time = start_date + 20 * ((
        (current_time - start_date) % 20) + 1)
    test_stub.sleep_util(except_start_time + 45)

    test_util.test_dsc('check scheduler state after pause scheduler')
    check_scheduler_state(schd_job1, 'Disabled')
    check_scheduler_state(schd_job2, 'Disabled')
    if check_scheduler_msg('run scheduler for job: StopVmInstanceJob',
                           except_start_time + 10):
        test_util.test_fail(
            'StopVmInstanceJob executed at unexpected timestamp range')
    if check_scheduler_msg('run scheduler for job: StartVmInstanceJob',
                           except_start_time + 20):
        test_util.test_fail(
            'StartVmInstanceJob executed at unexpected timestamp range')

    schd_ops.change_scheduler_state(schd_job1.uuid, 'enable')
    schd_ops.change_scheduler_state(schd_job2.uuid, 'enable')

    current_time = int(time.time())
    except_start_time = start_date + 20 * ((
        (current_time - start_date) % 20) + 1)
    test_stub.sleep_util(except_start_time + 45)

    test_util.test_dsc('check scheduler state after resume scheduler')
    check_scheduler_state(schd_job1, 'Enabled')
    check_scheduler_state(schd_job2, 'Enabled')
    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob',
                               except_start_time + 10):
        test_util.test_fail(
            'StopVmInstanceJob not executed at expected timestamp range')
    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob',
                               except_start_time + 20):
        test_util.test_fail(
            'StartVmInstanceJob not executed at expected timestamp range')

    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)
    vm.destroy()
    test_util.test_pass(
        'Check Scheduler State after Pause and Resume Scheduler Success')
def test():
    global vm
    global schd_job
    global schd_trigger
    global new_account

    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)

    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_name('volume for snapshot scheduler testing')
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)

    volume.attach(vm)
    volume.detach()

    test_util.test_dsc('create snapshot scheduler')
    start_date = int(time.time())
    #sp_option = test_util.SnapshotOption()
    #sp_option.set_name('simple_schduler_snapshot')
    #sp_option.set_volume_uuid(volume.get_volume().uuid)
    schd_job = schd_ops.create_scheduler_job('simple_create_snapshot_scheduler', 'simple_create_snapshot_scheduler', volume.get_volume().uuid, 'volumeSnapshot', None)
    schd_trigger = schd_ops.create_scheduler_trigger('simple_create_snapshot_scheduler', start_date+60, None, 120, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)

    #schd = vol_ops.create_snapshot_scheduler(sp_option, 'simple', 'simple_create_snapshot_scheduler',  start_date+60, 120)

    check_scheduler_state(schd_job, 'Enabled')

    snapshot_num = 0
    for i in range(0, 3):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 60 + 120*i - 2)
        test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 60 + 120*i - 2, snapshot_num))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' % (snapshot_num))
        snapshot_num += 1

        test_stub.sleep_util(start_date + 60 + 120*i + 65)
        test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 60 + 120*i + 65, snapshot_num+1))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' % (snapshot_num))

    new_account = account_operations.create_account('new_account', 'password', 'Normal')

    res_ops.change_recource_owner(new_account.uuid, volume.get_volume().uuid)

    test_util.test_dsc('check scheduler state after changing the owner of volume')
    check_scheduler_state(schd_job, 'Enabled')

    current_time = int(time.time())
    except_start_time =  start_date + 120 * (((current_time - start_date) % 120) + 1)

    snapshot_num = (except_start_time - start_date) // 120
    for i in range(0, 3):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(except_start_time + 60 + 120*i - 2)
        test_util.test_logger('check volume snapshot number at %s, there should be %s' % (except_start_time + 60 + 120*i - 2, snapshot_num))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' % (snapshot_num))
        snapshot_num += 1

        test_stub.sleep_util(except_start_time + 60 + 120*i + 60)
        test_util.test_logger('check volume snapshot number at %s, there should be %s' % (except_start_time + 60 + 120*i + 65, snapshot_num+1))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' % (snapshot_num))

    schd_ops.del_scheduler_job(schd_job.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger.uuid)
    vm.destroy()
    account_operations.delete_account(new_account.uuid)

    test_util.test_pass('Check Scheduler State after Changing Volume Owner Success')
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2

    delete_policy = test_lib.lib_get_delete_policy('vm')
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    vm.set_delete_policy('Delay')

    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler', 'simple_stop_vm_scheduler', vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+10, None, 20, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+10, 20)

    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None)
    schd_trigger2 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+20, None, 20, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+20, 20)

    test_stub.sleep_util(start_date+45)

    test_util.test_dsc('check scheduler state after create scheduler')
    check_scheduler_state(schd_job1, 'Enabled')
    check_scheduler_state(schd_job2, 'Enabled')
    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob', start_date+10):
        test_util.test_fail('StopVmInstanceJob not executed at expected timestamp range')
    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob', start_date+20):
        test_util.test_fail('StartVmInstanceJob not executed at expected timestamp range')

    vm.destroy()

    current_time = int(time.time())
    except_start_time =  start_date + 20 * (((current_time - start_date) % 20) + 1)
    test_stub.sleep_util(except_start_time + 45)

    test_util.test_dsc('check scheduler state after destroy vm')
    check_scheduler_state(schd_job1, 'Disabled')
    check_scheduler_state(schd_job2, 'Disabled')
    if check_scheduler_msg('run scheduler for job: StopVmInstanceJob', except_start_time+10):
        test_util.test_fail('StopVmInstanceJob executed at unexpected timestamp range')
    if check_scheduler_msg('run scheduler for job: StartVmInstanceJob', except_start_time+20):
        test_util.test_fail('StartVmInstanceJob executed at unexpected timestamp range')

    vm.recover()

    current_time = int(time.time())
    except_start_time =  start_date + 20 * (((current_time - start_date) % 20) + 1)
    test_stub.sleep_util(except_start_time + 45)

    test_util.test_dsc('check scheduler state after recover vm')
    check_scheduler_state(schd_job1, 'Disabled')
    check_scheduler_state(schd_job2, 'Disabled')
#    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob', except_start_time+10):
#        test_util.test_fail('StopVmInstanceJob not executed at expected timestamp range')
#    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob', except_start_time+20):
#        test_util.test_fail('StartVmInstanceJob not executed at expected timestamp range' )

    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)

    vm.set_delete_policy(delete_policy)
    vm.destroy()

    conditions = res_ops.gen_query_conditions('uuid', '=', schd_job1.uuid)
    if len(res_ops.query_resource(res_ops.SCHEDULERJOB, conditions)) > 0:
        test_util.test_fail('check stop vm scheduler, it is expected to be destroied, but it still exists')
   
    conditions = res_ops.gen_query_conditions('uuid', '=', schd_job2.uuid)
    if len(res_ops.query_resource(res_ops.SCHEDULERJOB, conditions)) > 0:
        test_util.test_fail('check start vm scheduler, it is expected to be destroied, but it still exists')

    test_util.test_pass('Check Scheduler State after Destroy and Recover VM Success')
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2

    delete_policy = test_lib.lib_get_delete_policy('vm')
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    vm.set_delete_policy('Delay')

    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler',
                                              'simple_stop_vm_scheduler',
                                              vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger(
        'simple_stop_vm_scheduler', start_date + 10, None, 20, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+10, 20)

    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler',
                                              'simple_start_vm_scheduler',
                                              vm.get_vm().uuid, 'startVm',
                                              None)
    schd_trigger2 = schd_ops.create_scheduler_trigger(
        'simple_start_vm_scheduler', start_date + 20, None, 20, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+20, 20)

    test_stub.sleep_util(start_date + 45)

    test_util.test_dsc('check scheduler state after create scheduler')
    check_scheduler_state(schd_job1, 'Enabled')
    check_scheduler_state(schd_job2, 'Enabled')
    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob',
                               start_date + 10):
        test_util.test_fail(
            'StopVmInstanceJob not executed at expected timestamp range')
    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob',
                               start_date + 20):
        test_util.test_fail(
            'StartVmInstanceJob not executed at expected timestamp range')

    vm.destroy()

    current_time = int(time.time())
    except_start_time = start_date + 20 * ((
        (current_time - start_date) % 20) + 1)
    test_stub.sleep_util(except_start_time + 45)

    test_util.test_dsc('check scheduler state after destroy vm')
    check_scheduler_state(schd_job1, 'Disabled')
    check_scheduler_state(schd_job2, 'Disabled')
    if check_scheduler_msg('run scheduler for job: StopVmInstanceJob',
                           except_start_time + 10):
        test_util.test_fail(
            'StopVmInstanceJob executed at unexpected timestamp range')
    if check_scheduler_msg('run scheduler for job: StartVmInstanceJob',
                           except_start_time + 20):
        test_util.test_fail(
            'StartVmInstanceJob executed at unexpected timestamp range')

    vm.recover()

    current_time = int(time.time())
    except_start_time = start_date + 20 * ((
        (current_time - start_date) % 20) + 1)
    test_stub.sleep_util(except_start_time + 45)

    test_util.test_dsc('check scheduler state after recover vm')
    check_scheduler_state(schd_job1, 'Disabled')
    check_scheduler_state(schd_job2, 'Disabled')
    #    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob', except_start_time+10):
    #        test_util.test_fail('StopVmInstanceJob not executed at expected timestamp range')
    #    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob', except_start_time+20):
    #        test_util.test_fail('StartVmInstanceJob not executed at expected timestamp range' )

    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)

    vm.set_delete_policy(delete_policy)
    vm.destroy()

    conditions = res_ops.gen_query_conditions('uuid', '=', schd_job1.uuid)
    if len(res_ops.query_resource(res_ops.SCHEDULERJOB, conditions)) > 0:
        test_util.test_fail(
            'check stop vm scheduler, it is expected to be destroied, but it still exists'
        )

    conditions = res_ops.gen_query_conditions('uuid', '=', schd_job2.uuid)
    if len(res_ops.query_resource(res_ops.SCHEDULERJOB, conditions)) > 0:
        test_util.test_fail(
            'check start vm scheduler, it is expected to be destroied, but it still exists'
        )

    test_util.test_pass(
        'Check Scheduler State after Destroy and Recover VM Success')
Exemple #20
0
def test():
    global vm
    global schd_job
    global schd_trigger
    global new_account

    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)

    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_name('volume for snapshot scheduler testing')
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)

    volume.attach(vm)
    volume.detach()

    test_util.test_dsc('create snapshot scheduler')
    start_date = int(time.time())
    #sp_option = test_util.SnapshotOption()
    #sp_option.set_name('simple_schduler_snapshot')
    #sp_option.set_volume_uuid(volume.get_volume().uuid)
    schd_job = schd_ops.create_scheduler_job(
        'simple_create_snapshot_scheduler', 'simple_create_snapshot_scheduler',
        volume.get_volume().uuid, 'volumeSnapshot', None)
    schd_trigger = schd_ops.create_scheduler_trigger(
        'simple_create_snapshot_scheduler', start_date + 60, None, 120,
        'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)

    #schd = vol_ops.create_snapshot_scheduler(sp_option, 'simple', 'simple_create_snapshot_scheduler',  start_date+60, 120)

    check_scheduler_state(schd_job, 'Enabled')

    snapshot_num = 0
    for i in range(0, 3):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 60 + 120 * i - 2)
        test_util.test_logger(
            'check volume snapshot number at %s, there should be %s' %
            (start_date + 60 + 120 * i - 2, snapshot_num))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' %
                                (snapshot_num))
        snapshot_num += 1

        test_stub.sleep_util(start_date + 60 + 120 * i + 65)
        test_util.test_logger(
            'check volume snapshot number at %s, there should be %s' %
            (start_date + 60 + 120 * i + 65, snapshot_num + 1))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' %
                                (snapshot_num))

    new_account = account_operations.create_account('new_account', 'password',
                                                    'Normal')

    res_ops.change_recource_owner(new_account.uuid, volume.get_volume().uuid)

    test_util.test_dsc(
        'check scheduler state after changing the owner of volume')
    check_scheduler_state(schd_job, 'Disabled')

    current_time = int(time.time())
    except_start_time = start_date + 120 * ((
        (current_time - start_date) % 120) + 1)

    for i in range(0, 3):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(except_start_time + 60 + 120 * i - 2)
        test_util.test_logger(
            'check volume snapshot number at %s, there should be %s' %
            (except_start_time + 60 + 120 * i - 2, snapshot_num))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' %
                                (snapshot_num))

        test_stub.sleep_util(except_start_time + 60 + 120 * i + 60)
        test_util.test_logger(
            'check volume snapshot number at %s, there should be %s' %
            (except_start_time + 60 + 120 * i + 65, snapshot_num + 1))
        new_snapshot_num = query_snapshot_number(volume.get_volume().uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there sholuld be %s snapshots' %
                                (snapshot_num))

    schd_ops.del_scheduler_job(schd_job.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger.uuid)
    vm.destroy()
    account_operations.delete_account(new_account.uuid)

    test_util.test_pass(
        'Check Scheduler State after Changing Volume Owner Success')
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2
    iam2_ops.clean_iam2_enviroment()

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    # 1 create project
    project_name = 'test_project'
    password = \
        'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    linked_account_uuid = project.linkedAccountUuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])

    # 2 create projectAdmin  into project
    project_admin_name = 'projectadmin'
    project_admin_uuid = iam2_ops.create_iam2_virtual_id(project_admin_name, password).uuid
    iam2_ops.add_iam2_virtual_ids_to_project([project_admin_uuid], project_uuid)
    attributes = [{"name": "__ProjectAdmin__", "value": project_uuid}]
    iam2_ops.add_attributes_to_iam2_virtual_id(project_admin_uuid, attributes)
    project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(project_admin_name,password)
    project_admin_session_uuid = iam2_ops.login_iam2_project(project_name,project_admin_session_uuid).uuid

    # 3 create scheduler job and trigger
    vm = test_stub.create_vm(session_uuid=project_admin_session_uuid)
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler', 'simple_stop_vm_scheduler', vm.get_vm().uuid, 'stopVm', None,session_uuid=project_admin_session_uuid)
    schd_trigger1 = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+60, None, 120, 'simple',session_uuid=project_admin_session_uuid)
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid,session_uuid=project_admin_session_uuid)

    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None,session_uuid=project_admin_session_uuid)
    schd_trigger2 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+120, None, 120, 'simple',session_uuid=project_admin_session_uuid)
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid,session_uuid=project_admin_session_uuid)

    acc_ops.logout(project_admin_session_uuid)

    # 4 delete project
    iam2_ops.delete_iam2_project(project_uuid)
    try:
        schd_job1_inv = res_ops.get_resource(res_ops.SCHEDULERJOB,uuid=schd_job1.uuid)[0]
    except:
        test_util.test_fail("delete project but not expunge,the schedulerjob [%s] is lost"%schd_job1.uuid)
    try:
        res_ops.get_resource(res_ops.SCHEDULERJOB,uuid=schd_job2.uuid)[0]
    except:
        test_util.test_fail("delete project but not expunge,the schedulerjob [%s] is lost"%schd_job2.uuid)
    try:
        schd_trigger1_inv = res_ops.get_resource(res_ops.SCHEDULERTRIGGER,uuid=schd_trigger1.uuid)[0]
    except:
        test_util.test_fail("delete project but not expunge,the scheduler trigger [%s] is lost"%schd_job1.uuid)
    try:
        res_ops.get_resource(res_ops.SCHEDULERTRIGGER,uuid=schd_trigger2.uuid)[0]
    except:
        test_util.test_fail("delete project but not expunge,the scheduler trigger [%s] is lost"%schd_job1.uuid)

    iam2_ops.recover_iam2_project(project_uuid)

    try:
        project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(project_admin_name,password)
        project_admin_session_uuid = iam2_ops.login_iam2_project(project_name,project_admin_session_uuid).uuid
    except:
        test_util.test_fail("recover the deleted project , but can't login in")


    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)

    # 5 check for cascade delete
    test_stub.check_resource_not_exist(schd_job1.uuid,res_ops.SCHEDULERJOB)
    test_stub.check_resource_not_exist(schd_job2.uuid,res_ops.SCHEDULERJOB)
    test_stub.check_resource_not_exist(schd_trigger1.uuid,res_ops.SCHEDULERTRIGGER)
    test_stub.check_resource_not_exist(schd_trigger2.uuid,res_ops.SCHEDULERTRIGGER)

    vm.clean()
    iam2_ops.clean_iam2_enviroment()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2
    iam2_ops.clean_iam2_enviroment()

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    # 1 create project
    project_name = 'test_project'
    password = \
        'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    linked_account_uuid = project.linkedAccountUuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])

    # 2 create projectAdmin  into project
    project_admin_name = 'projectadmin'
    project_admin_uuid = iam2_ops.create_iam2_virtual_id(
        project_admin_name, password).uuid
    iam2_ops.add_iam2_virtual_ids_to_project([project_admin_uuid],
                                             project_uuid)
    attributes = [{"name": "__ProjectAdmin__", "value": project_uuid}]
    iam2_ops.add_attributes_to_iam2_virtual_id(project_admin_uuid, attributes)
    project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(
        project_admin_name, password)
    project_admin_session_uuid = iam2_ops.login_iam2_project(
        project_name, project_admin_session_uuid).uuid

    # 3 create scheduler job and trigger
    vm = test_stub.create_vm(session_uuid=project_admin_session_uuid)
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job(
        'simple_stop_vm_scheduler',
        'simple_stop_vm_scheduler',
        vm.get_vm().uuid,
        'stopVm',
        None,
        session_uuid=project_admin_session_uuid)
    schd_trigger1 = schd_ops.create_scheduler_trigger(
        'simple_stop_vm_scheduler',
        start_date + 60,
        None,
        120,
        'simple',
        session_uuid=project_admin_session_uuid)
    schd_ops.add_scheduler_job_to_trigger(
        schd_trigger1.uuid,
        schd_job1.uuid,
        session_uuid=project_admin_session_uuid)

    schd_job2 = schd_ops.create_scheduler_job(
        'simple_start_vm_scheduler',
        'simple_start_vm_scheduler',
        vm.get_vm().uuid,
        'startVm',
        None,
        session_uuid=project_admin_session_uuid)
    schd_trigger2 = schd_ops.create_scheduler_trigger(
        'simple_start_vm_scheduler',
        start_date + 120,
        None,
        120,
        'simple',
        session_uuid=project_admin_session_uuid)
    schd_ops.add_scheduler_job_to_trigger(
        schd_trigger2.uuid,
        schd_job2.uuid,
        session_uuid=project_admin_session_uuid)

    acc_ops.logout(project_admin_session_uuid)

    # 4 delete project
    iam2_ops.delete_iam2_project(project_uuid)
    try:
        schd_job1_inv = res_ops.get_resource(res_ops.SCHEDULERJOB,
                                             uuid=schd_job1.uuid)[0]
    except:
        test_util.test_fail(
            "delete project but not expunge,the schedulerjob [%s] is lost" %
            schd_job1.uuid)
    try:
        res_ops.get_resource(res_ops.SCHEDULERJOB, uuid=schd_job2.uuid)[0]
    except:
        test_util.test_fail(
            "delete project but not expunge,the schedulerjob [%s] is lost" %
            schd_job2.uuid)
    try:
        schd_trigger1_inv = res_ops.get_resource(res_ops.SCHEDULERTRIGGER,
                                                 uuid=schd_trigger1.uuid)[0]
    except:
        test_util.test_fail(
            "delete project but not expunge,the scheduler trigger [%s] is lost"
            % schd_job1.uuid)
    try:
        res_ops.get_resource(res_ops.SCHEDULERTRIGGER,
                             uuid=schd_trigger2.uuid)[0]
    except:
        test_util.test_fail(
            "delete project but not expunge,the scheduler trigger [%s] is lost"
            % schd_job1.uuid)

    iam2_ops.recover_iam2_project(project_uuid)

    try:
        project_admin_session_uuid = iam2_ops.login_iam2_virtual_id(
            project_admin_name, password)
        project_admin_session_uuid = iam2_ops.login_iam2_project(
            project_name, project_admin_session_uuid).uuid
    except:
        test_util.test_fail("recover the deleted project , but can't login in")

    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)

    # 5 check for cascade delete
    test_stub.check_resource_not_exist(schd_job1.uuid, res_ops.SCHEDULERJOB)
    test_stub.check_resource_not_exist(schd_job2.uuid, res_ops.SCHEDULERJOB)
    test_stub.check_resource_not_exist(schd_trigger1.uuid,
                                       res_ops.SCHEDULERTRIGGER)
    test_stub.check_resource_not_exist(schd_trigger2.uuid,
                                       res_ops.SCHEDULERTRIGGER)

    vm.clean()
    iam2_ops.clean_iam2_enviroment()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
def scheduler_vm_operations(vm, bss):

    vm_ops.stop_vm(vm.get_vm().uuid)
    volume = vm.get_vm().allVolumes[0]

    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_start_vm_scheduler',
                                              'simple_start_vm_scheduler',
                                              vm.get_vm().uuid, 'startVm',
                                              None)
    schd_trigger1 = schd_ops.create_scheduler_trigger(
        'simple_start_vm_scheduler', start_date + 5, None, 15, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)

    schd_job2 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler',
                                              'simple_stop_vm_scheduler',
                                              vm.get_vm().uuid, 'stopVm', None)
    schd_trigger2 = schd_ops.create_scheduler_trigger(
        'simple_stop_vm_scheduler', start_date + 15, None, 15, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)

    schd_job3 = schd_ops.create_scheduler_job('simple_reboot_vm_scheduler',
                                              'simple_reboot_vm_scheduler',
                                              vm.get_vm().uuid, 'rebootVm',
                                              None)
    schd_trigger3 = schd_ops.create_scheduler_trigger(
        'simple_reboot_vm_scheduler', start_date + 10, None, 15, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger3.uuid, schd_job3.uuid)

    schd_job4 = schd_ops.create_scheduler_job(
        'simple_create_snapshot_scheduler', 'simple_create_snapshot_scheduler',
        volume.uuid, 'volumeSnapshot', None)
    schd_trigger4 = schd_ops.create_scheduler_trigger(
        'simple_create_snapshot_scheduler', start_date + 12, None, 15,
        'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger4.uuid, schd_job4.uuid)

    snapshot_num = 0
    for i in range(0, 3):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 5 + 15 * i + 1)
        #test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 5 +15*i, snapshot_num))
        test_util.test_logger(
            'check VM status at %s, VM is expected to start' %
            (start_date + 15 + 15 * i))
        vm.update()
        if vm.get_vm().state != 'Starting' and vm.get_vm().state != 'Running':
            test_util.test_fail('VM is expected to start')

        test_stub.sleep_util(start_date + 10 + 15 * i + 1)
        test_util.test_logger(
            'check VM status at %s, VM is expected to reboot' %
            (start_date + 10 + 15 * i))
        vm.update()
        if not test_lib.lib_find_in_local_management_server_log(
                start_date + 10,
                '[msg received]: {"org.zstack.header.vm.RebootVmInstanceMsg',
                vm.get_vm().uuid):
            test_util.test_fail('VM is expected to reboot start from %s' %
                                (start_date + 10 + 15 * i))

        test_stub.sleep_util(start_date + 12 + 15 * i + 1)
        snapshot_num += 1
        new_snapshot_num = query_snapshot_number(volume.uuid)
        if snapshot_num != new_snapshot_num:
            test_util.test_fail('there should be %s snapshots' %
                                (snapshot_num))
        #snapshot_num +=1
        test_util.test_logger(
            'check volume snapshot number at %s, there should be %s' %
            (start_date + 5 + 15 * i, snapshot_num))
        #test_util.test_logger('check VM status at %s, VM is expected to stop' % (start_date + 6 + 15*i +1))

        test_stub.sleep_util(start_date + 15 + 15 * i + 1)
        test_util.test_logger('check VM status at %s, VM is expected to stop' %
                              (start_date + 15 + 15 * i))
        vm.update()
        if vm.get_vm().state != 'Stopping' and vm.get_vm().state != 'Stopped':
            test_util.test_fail('VM is expected to stop')

    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)
    schd_ops.del_scheduler_job(schd_job3.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger3.uuid)
    schd_ops.del_scheduler_job(schd_job4.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger4.uuid)

    vm.update()
    vm_state = vm.get_vm().state
    snapshot_num_after = query_snapshot_number(volume.uuid)
    test_util.test_logger('snapshotnumber:%s' % snapshot_num_after)
    for i in range(3, 5):
        test_util.test_logger('round %s' % (i))
        test_stub.sleep_util(start_date + 1 + 15 * i)
        vm.update()
        test_util.test_logger(
            'check vm status at %s, vm is expected to stay in state %s' %
            (start_date + 5 + 15 * i, vm_state))
        if vm.get_vm().state != vm_state:
            test_util.test_fail('vm is expected to stay in state %s' %
                                (vm_state))
        if snapshot_num_after != snapshot_num:
            test_util.test_fail(
                'the number of snapshots is expected to stay in %s' %
                (snapshot_num))

    vm.destroy()
    vm.expunge()
def test():
    global vm
    global schd_job1
    global schd_job2
    global schd_trigger1
    global schd_trigger2

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd_job1 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler', 'simple_stop_vm_scheduler', vm.get_vm().uuid, 'stopVm', None)
    schd_trigger1 = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+10, None, 20, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)
    #schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+10, 20)
    schd_job2 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None)
    schd_trigger2 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+20, None, 20, 'simple')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)
    #schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_start_vm_scheduler', start_date+20, 20)

    test_stub.sleep_util(start_date+45)

    test_util.test_dsc('check scheduler state after create scheduler')
    check_scheduler_state(schd_job1, 'Enabled')
    check_scheduler_state(schd_job2, 'Enabled')
    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob', start_date+10):
        test_util.test_fail('StopVmInstanceJob not executed at expected timestamp range')
    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob', start_date+20):
        test_util.test_fail('StartVmInstanceJob not executed at expected timestamp range')

    schd_ops.change_scheduler_state(schd_job1.uuid, 'disable')
    schd_ops.change_scheduler_state(schd_job2.uuid, 'disable')
   
    current_time = int(time.time())
    except_start_time =  start_date + 20 * (((current_time - start_date) // 20) + 1) 
    test_stub.sleep_util(except_start_time+45)
 
    test_util.test_dsc('check scheduler state after pause scheduler')
    check_scheduler_state(schd_job1, 'Disabled')
    check_scheduler_state(schd_job2, 'Disabled')
    if check_scheduler_msg('run scheduler for job: StopVmInstanceJob', except_start_time+10):
        test_util.test_fail('StopVmInstanceJob executed at unexpected timestamp range')
    if check_scheduler_msg('run scheduler for job: StartVmInstanceJob', except_start_time+20):
        test_util.test_fail('StartVmInstanceJob executed at unexpected timestamp range')

    schd_ops.change_scheduler_state(schd_job1.uuid, 'enable')
    schd_ops.change_scheduler_state(schd_job2.uuid, 'enable')

    current_time = int(time.time())
    except_start_time =  start_date + 20 * (((current_time - start_date) // 20) + 1)
    test_stub.sleep_util(except_start_time+45)

    test_util.test_dsc('check scheduler state after resume scheduler')
    check_scheduler_state(schd_job1, 'Enabled')
    check_scheduler_state(schd_job2, 'Enabled')
    if not check_scheduler_msg('run scheduler for job: StopVmInstanceJob', except_start_time+10):
        test_util.test_fail('StopVmInstanceJob not executed at expected timestamp range')
    if not check_scheduler_msg('run scheduler for job: StartVmInstanceJob', except_start_time+20):
        test_util.test_fail('StartVmInstanceJob not executed at expected timestamp range')

    schd_ops.del_scheduler_job(schd_job1.uuid)
    schd_ops.del_scheduler_job(schd_job2.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger2.uuid)
    vm.destroy()
    test_util.test_pass('Check Scheduler State after Pause and Resume Scheduler Success')
def scheduler_vm_operations(vm,bss):

   vm_ops.stop_vm(vm.get_vm().uuid)
   volume = vm.get_vm().allVolumes[0]

   start_date = int(time.time())
   schd_job1 = schd_ops.create_scheduler_job('simple_start_vm_scheduler', 'simple_start_vm_scheduler', vm.get_vm().uuid, 'startVm', None)
   schd_trigger1 = schd_ops.create_scheduler_trigger('simple_start_vm_scheduler', start_date+5, None, 15, 'simple')
   schd_ops.add_scheduler_job_to_trigger(schd_trigger1.uuid, schd_job1.uuid)

   schd_job2 = schd_ops.create_scheduler_job('simple_stop_vm_scheduler', 'simple_stop_vm_scheduler', vm.get_vm().uuid, 'stopVm', None)
   schd_trigger2 = schd_ops.create_scheduler_trigger('simple_stop_vm_scheduler', start_date+15, None, 15, 'simple')
   schd_ops.add_scheduler_job_to_trigger(schd_trigger2.uuid, schd_job2.uuid)

   schd_job3 = schd_ops.create_scheduler_job('simple_reboot_vm_scheduler', 'simple_reboot_vm_scheduler', vm.get_vm().uuid, 'rebootVm', None)
   schd_trigger3 = schd_ops.create_scheduler_trigger('simple_reboot_vm_scheduler', start_date+10, None, 15, 'simple')
   schd_ops.add_scheduler_job_to_trigger(schd_trigger3.uuid, schd_job3.uuid)

   schd_job4 = schd_ops.create_scheduler_job('simple_create_snapshot_scheduler', 'simple_create_snapshot_scheduler', volume.uuid, 'volumeSnapshot', None)
   schd_trigger4 = schd_ops.create_scheduler_trigger('simple_create_snapshot_scheduler', start_date+12, None, 15, 'simple')
   schd_ops.add_scheduler_job_to_trigger(schd_trigger4.uuid, schd_job4.uuid)

   snapshot_num = 0
   for i in range(0,3):
      test_util.test_logger('round %s' % (i))
      test_stub.sleep_util(start_date + 5 + 15*i + 1)  
      #test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 5 +15*i, snapshot_num))
      test_util.test_logger('check VM status at %s, VM is expected to start' % (start_date + 15 + 15*i))
      vm.update()
      if vm.get_vm().state != 'Starting' and vm.get_vm().state != 'Running':
         test_util.test_fail('VM is expected to start')
      
      test_stub.sleep_util(start_date +10 +15*i + 1)
      test_util.test_logger('check VM status at %s, VM is expected to reboot' % (start_date + 10 + 15*i))
      vm.update()
      if not test_lib.lib_find_in_local_management_server_log(start_date+10, '[msg received]: {"org.zstack.header.vm.RebootVmInstanceMsg', vm.get_vm().uuid):
         test_util.test_fail('VM is expected to reboot start from %s' % (start_date+ 10 + 15*i))
     
      test_stub.sleep_util(start_date + 12 + 15*i + 1)
      snapshot_num +=1
      new_snapshot_num = query_snapshot_number(volume.uuid)
      if snapshot_num != new_snapshot_num:
         test_util.test_fail('there should be %s snapshots' % (snapshot_num))
      #snapshot_num +=1
      test_util.test_logger('check volume snapshot number at %s, there should be %s' % (start_date + 5 +15*i, snapshot_num))
      #test_util.test_logger('check VM status at %s, VM is expected to stop' % (start_date + 6 + 15*i +1))
 
     
      test_stub.sleep_util(start_date +15 +15*i + 1)
      test_util.test_logger('check VM status at %s, VM is expected to stop' % (start_date + 15 + 15*i))
      vm.update()
      if vm.get_vm().state != 'Stopping' and vm.get_vm().state != 'Stopped':
         test_util.test_fail('VM is expected to stop')
   
   schd_ops.del_scheduler_job(schd_job1.uuid)
   schd_ops.del_scheduler_trigger(schd_trigger1.uuid)
   schd_ops.del_scheduler_job(schd_job2.uuid)
   schd_ops.del_scheduler_trigger(schd_trigger2.uuid)
   schd_ops.del_scheduler_job(schd_job3.uuid)
   schd_ops.del_scheduler_trigger(schd_trigger3.uuid)
   schd_ops.del_scheduler_job(schd_job4.uuid)
   schd_ops.del_scheduler_trigger(schd_trigger4.uuid)
   
   vm.update()
   vm_state = vm.get_vm().state
   snapshot_num_after = query_snapshot_number(volume.uuid) 
   test_util.test_logger('snapshotnumber:%s' % snapshot_num_after)
   for i in range(3,5):
      test_util.test_logger('round %s' % (i))
      test_stub.sleep_util(start_date + 1 +15*i)
      vm.update()
      test_util.test_logger('check vm status at %s, vm is expected to stay in state %s' % (start_date + 5 + 15*i, vm_state))
      if vm.get_vm().state != vm_state:
         test_util.test_fail('vm is expected to stay in state %s' % (vm_state))
      if snapshot_num_after != snapshot_num:
         test_util.test_fail('the number of snapshots is expected to stay in %s' % (snapshot_num))

   vm.destroy()
   vm.expunge()
def test():
    global job1
    global job2
    global job_group
    global trigger1
    global trigger2

    imagestore = test_lib.lib_get_image_store_backup_storage()
    if imagestore == None:
        test_util.test_skip('Required imagestore to test')

    vm1 = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    vm2 = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))

    volume = test_stub.create_volume()
    volume.attach(vm2)

    test_obj_dict.add_vm(vm1)
    test_obj_dict.add_vm(vm2)
    test_obj_dict.add_volume(volume)

    parameters = {'retentionType': 'Count',
                  'retentionValue': '10',
                  'backupStorageUuids': imagestore.uuid,
                  'remoteBackupStorageUuid': '',
                  'networkWriteBandwidth': '',
                  'networkReadBandwidth': '',
                  'volumeReadBandwidth': '',
                  'volumeWriteBandwidth': ''}
    test_util.test_logger(parameters)

    job1 = sch_ops.create_scheduler_job(name='root_volume', description='vm1 root volume backup', target_uuid=vm1.get_vm().allVolumes[0].uuid,
                                        type=volumeBackup, parameters=parameters)
    job2 = sch_ops.create_scheduler_job(name='data_volume', description='data volume backup',
                                        target_uuid=volume.get_volume().uuid, type=volumeBackup,
                                        parameters=parameters)

    name1 = 'job_group'
    job_group = sch_ops.create_scheduler_job_group(name=name1, description='vmbackup', type=volumeBackup,
                                                   parameters=parameters)

    cond = res_ops.gen_query_conditions('uuid', '=', job_group.uuid)

    sch_ops.add_jobs_to_job_group([job1.uuid], job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.jobsUuid) == 1

    sch_ops.add_jobs_to_job_group([job2.uuid], job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.jobsUuid) == 2

    sch_ops.remove_jobs_from_job_group([job2.uuid], job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.jobsUuid) == 1

    sch_ops.add_jobs_to_job_group([job2.uuid], job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.jobsUuid) == 2

    sch_ops.remove_jobs_from_job_group([job1.uuid, job2.uuid], job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.jobsUuid) == 0

    sch_ops.add_jobs_to_job_group([job1.uuid, job2.uuid], job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.jobsUuid) == 2

    sch_ops.del_scheduler_job(job2.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.jobsUuid) == 1
    job2 = None

    trigger1 = sch_ops.create_scheduler_trigger('10sec', start_time=int(time.time() + 5), type='cron',
                                                cron='0/10 * * * * ?')
    trigger2 = sch_ops.create_scheduler_trigger('30sec', start_time=int(time.time() + 5), type='cron',
                                                cron='0/30 * * * * ?')

    sch_ops.add_scheduler_job_group_to_trigger(trigger1.uuid, job_group.uuid, triggerNow=True)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.triggersUuid) == 1

    sch_ops.add_scheduler_job_group_to_trigger(trigger2.uuid, job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.triggersUuid) == 2

    sch_ops.remove_scheduler_job_group_from_trigger(trigger2.uuid, job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.triggersUuid) == 1

    sch_ops.add_scheduler_job_group_to_trigger(trigger2.uuid, job_group.uuid, triggerNow=True)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.triggersUuid) == 2

    sch_ops.del_scheduler_trigger(trigger2.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)[0]
    assert len(job_group_inv.triggersUuid) == 1
    trigger2 = None

    sch_ops.del_scheduler_job_group(job_group.uuid)
    job_group_inv = res_ops.query_resource(res_ops.SCHEDULERJOBGROUP, cond)
    assert len(job_group_inv) == 0
    job_group = None

    cond1 = res_ops.gen_query_conditions('uuid', '=', job1.uuid)
    cond2 = res_ops.gen_query_conditions('uuid', '=', trigger1.uuid)

    job_inv = res_ops.query_resource(res_ops.SCHEDULERJOB, cond1)
    assert len(job_inv) == 0

    trigger_inv = res_ops.query_resource(res_ops.SCHEDULERTRIGGER, cond2)
    assert len(trigger_inv) == 1

    trigger1 = None
    job1 = None

    test_lib.lib_robot_cleanup(test_obj_dict)