def test():
    global vm
    global schd1
    global schd2
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date-60, 120)
    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, 4):
        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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.uuid)
    vm.destroy()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
示例#2
0
def test():
    global vm
    global schd1
    global schd2
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple',
                                     'simple_stop_vm_scheduler',
                                     start_date + 60, 120)
    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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.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()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
def test():
    global vm
    global schd1
    global schd2
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+60, 120)
    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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.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()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
示例#4
0
def test():
    global vm
    global schd1
    global schd2

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+10, 20)
    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(schd1, 'Enabled')
    check_scheduler_state(schd2, '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(schd1.uuid, 'disable')
    schd_ops.change_scheduler_state(schd2.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(schd1, 'Disabled')
    check_scheduler_state(schd2, '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(schd1.uuid, 'enable')
    schd_ops.change_scheduler_state(schd2.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(schd1, 'Enabled')
    check_scheduler_state(schd2, '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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.uuid)
    vm.destroy()
    test_util.test_pass('Check Scheduler State after Pause and Resume Scheduler Success')
def test():
    global vm
    global schd1
    global schd2
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple',
                                     'simple_stop_vm_scheduler',
                                     start_date + 60, 120, 3)
    schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple',
                                      'simple_start_vm_scheduler',
                                      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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.uuid)
    vm.destroy()
    test_util.test_pass(
        'Create Simple VM Stop Start Scheduler Repeat Count Success')
def test():
    global vm
    global schd1
    global schd2
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    minute = start_date / 60 % 60 % 2
    if minute == 0:
        test_stub.sleep_util((start_date + 59) / 60 * 60)
    else:
        test_stub.sleep_util((start_date + 59) / 60 * 60 + 60)

    start_date = int(time.time())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'cron',
                                     'cron_stop_vm_scheduler', None, None,
                                     None, '0 0/2 * * * ?')
    schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'cron',
                                      'cron_start_vm_scheduler', None, None,
                                      None, '59 0/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, 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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.uuid)
    vm.destroy()
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
示例#7
0
def test():
    global vm
    global schd1
    global schd2
    global new_account

    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+30, 60)
    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(schd1, 'Enabled')
    check_scheduler_state(schd2, '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(schd1, 'Disabled')
    check_scheduler_state(schd2, '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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.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 schd1
    global schd2
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))
    start_date = int(time.time())
    test_util.test_logger('Setup stop and start VM scheduler')
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple',
                                     'simple_stop_vm_scheduler', start_date, 1)
    schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'simple',
                                      'simple_start_vm_scheduler', start_date,
                                      2)
    test_stub.sleep_util(start_date + 58)

    stop_msg_mismatch = 0
    start_msg_mismatch = 0
    for i in range(0, 58):
        if not test_lib.lib_find_in_local_management_server_log(
                start_date + i,
                '[msg received]: {"org.zstack.header.vm.StopVmInstanceMsg',
                vm.get_vm().uuid):
            stop_msg_mismatch += 1
            test_util.test_warn(
                'StopVmInstanceMsg is expected to execute at %s' %
                (start_date + i))
        if i % 2 == 0:
            if not test_lib.lib_find_in_local_management_server_log(
                    start_date + i,
                    '[msg received]: {"org.zstack.header.vm.StartVmInstanceMsg',
                    vm.get_vm().uuid):
                start_msg_mismatch += 1
                test_util.test_warn(
                    'StartVmInstanceMsg is expected to execute at %s' %
                    (start_date + i))
        else:
            if test_lib.lib_find_in_local_management_server_log(
                    start_date + i,
                    '[msg received]: {"org.zstack.header.vm.StartVmInstanceMsg',
                    vm.get_vm().uuid):
                start_msg_mismatch += 1
                test_util.test_warn(
                    'StartVmInstanceMsg is not expected to execute at %s' %
                    (start_date + i))

    if stop_msg_mismatch > 10:
        test_util.test_fail(
            '%s of 58 StopVmInstanceMsg not executed at expected timestamp' %
            (stop_msg_mismatch))

    if start_msg_mismatch > 5:
        test_util.test_fail(
            '%s of 58 StartVmInstanceMsg not executed at expected timestamp' %
            (start_msg_mismatch))

    test_util.test_logger('Update stop and start VM scheduler to cron type')
    start_date = int(time.time())
    test_stub.sleep_util((start_date + 59) / 60 * 60)
    schd_ops.update_scheduler(schd1.uuid, 'cron', 'cron_stop_vm_scheduler2',
                              None, None, None, '0 * * * * ?')
    schd_ops.update_scheduler(schd2.uuid, 'cron', 'cron_start_vm_scheduler2',
                              None, None, None, '0 * * * * ?')
    change_date = int(time.time())
    test_stub.sleep_util(start_date + 59)

    for i in range(2, 58):
        if test_lib.lib_find_in_local_management_server_log(
                change_date + i,
                '[msg received]: {"org.zstack.header.vm.StopVmInstanceMsg',
                vm.get_vm().uuid):
            test_util.test_fail(
                'StopVmInstanceMsg is not expected to execute at %s' %
                (change_date + i))
        if test_lib.lib_find_in_local_management_server_log(
                change_date + i,
                '[msg received]: {"org.zstack.header.vm.StartVmInstanceMsg',
                vm.get_vm().uuid):
            test_util.test_fail(
                'StartVmInstanceMsg is not expected to execute at %s' %
                (change_date + i))

    schd_ops.update_scheduler(schd1.uuid, 'cron', 'cron_stop_vm_scheduler3',
                              None, None, None, '0/2 * * * * ?')
    schd_ops.update_scheduler(schd2.uuid, 'cron', 'cron_start_vm_scheduler3',
                              None, None, None, '* * * * * ?')
    change_date = int(time.time())

    test_stub.sleep_util(change_date + 60)
    stop_msg_mismatch = 0
    start_msg_mismatch = 0
    for i in range(1, 58):
        if (change_date + i) % 2 == 0:
            if not test_lib.lib_find_in_local_management_server_log(
                    change_date + i,
                    '[msg received]: {"org.zstack.header.vm.StopVmInstanceMsg',
                    vm.get_vm().uuid):
                stop_msg_mismatch += 1
                test_util.test_warn(
                    'StopVmInstanceMsg is expected to execute at %s' %
                    (change_date + i))
        else:
            if test_lib.lib_find_in_local_management_server_log(
                    change_date + i,
                    '[msg received]: {"org.zstack.header.vm.StopVmInstanceMsg',
                    vm.get_vm().uuid):
                stop_msg_mismatch += 1
                test_util.test_warn(
                    'StopVmInstanceMsg is not expected to execute at %s' %
                    (change_date + i))

        if not test_lib.lib_find_in_local_management_server_log(
                change_date + i,
                '[msg received]: {"org.zstack.header.vm.StartVmInstanceMsg',
                vm.get_vm().uuid):
            start_msg_mismatch += 1
            test_util.test_warn(
                'StartVmInstanceMsg is expected to execute at %s' %
                (change_date + i))

    if stop_msg_mismatch > 5:
        test_util.test_fail(
            '%s of 58 StopVmInstanceMsg not executed at expected timestamp' %
            (stop_msg_mismatch))

    if start_msg_mismatch > 10:
        test_util.test_fail(
            '%s of 58 StartVmInstanceMsg not executed at expected timestamp' %
            (start_msg_mismatch))

    schd_ops.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.uuid)
    try:
        vm.destroy()
    except:
        test_util.test_logger(
            'expected exception when destroy VM since too many queued task')
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
def test():
    global vm
    global schd1
    global schd2

    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())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple',
                                     'simple_stop_vm_scheduler',
                                     start_date + 10, 20)
    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(schd1, 'Enabled')
    check_scheduler_state(schd2, '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(schd1, 'Disabled')
    check_scheduler_state(schd2, '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(schd1, 'Enabled')
    check_scheduler_state(schd2, '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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.uuid)
    vm.set_delete_policy(delete_policy)
    vm.destroy()

    conditions = res_ops.gen_query_conditions('uuid', '=', schd1.uuid)
    if len(res_ops.query_resource(res_ops.SCHEDULER, 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', '=', schd2.uuid)
    if len(res_ops.query_resource(res_ops.SCHEDULER, 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 schd1
    global schd2
    vm = test_stub.create_vlan_vm(os.environ.get('l3VlanNetworkName1'))

    test_util.test_logger('Update stop and start VM scheduler to cron type')
    start_date = int(time.time())
    test_stub.sleep_util((start_date+59)/60*60)
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'cron', 'cron_stop_vm_scheduler', None, None, None, '0 * * * * ?')
    schd2 = vm_ops.start_vm_scheduler(vm.get_vm().uuid, 'cron', 'cron_start_vm_scheduler', None, None, None, '0 * * * * ?')

    change_date = int(time.time())
    test_stub.sleep_util(start_date+30)

    for i in range(2, 30):
        if test_lib.lib_find_in_local_management_server_log(change_date+i, '[msg received]: {"org.zstack.header.vm.StopVmInstanceMsg', vm.get_vm().uuid):
            test_util.test_fail('StopVmInstanceMsg is not expected to execute at %s' % (change_date+i))
        if test_lib.lib_find_in_local_management_server_log(change_date+i, '[msg received]: {"org.zstack.header.vm.StartVmInstanceMsg', vm.get_vm().uuid):
            test_util.test_fail('StartVmInstanceMsg is not expected to execute at %s' % (change_date+i))
   
    schd_ops.update_scheduler(schd1.uuid, 'simple', 'simple_stop_vm_scheduler2', start_date, 2, 10)
    schd_ops.update_scheduler(schd2.uuid, 'simple', 'simple_start_vm_scheduler2', start_date, 1, 20)
    change_date = int(time.time())

    test_stub.sleep_util(change_date+60)
    stop_msg_mismatch = 0
    start_msg_mismatch = 0
    for i in range(1, 15):
        if (change_date+i-start_date) % 2 == 0:
            if not test_lib.lib_find_in_local_management_server_log(change_date+i, '[msg received]: {"org.zstack.header.vm.StopVmInstanceMsg', vm.get_vm().uuid):
                stop_msg_mismatch += 1
                test_util.test_warn('StopVmInstanceMsg is expected to execute at %s' % (change_date+i))
        else:
            if test_lib.lib_find_in_local_management_server_log(change_date+i, '[msg received]: {"org.zstack.header.vm.StopVmInstanceMsg', vm.get_vm().uuid):
                stop_msg_mismatch += 1
                test_util.test_warn('StopVmInstanceMsg is not expected to execute at %s' % (change_date+i))

        if not test_lib.lib_find_in_local_management_server_log(change_date+i, '[msg received]: {"org.zstack.header.vm.StartVmInstanceMsg', vm.get_vm().uuid):
                start_msg_mismatch += 1
                test_util.test_warn('StartVmInstanceMsg is expected to execute at %s' % (change_date+i))

    for i in range(21, 60):
        if test_lib.lib_find_in_local_management_server_log(change_date+i, '[msg received]: {"org.zstack.header.vm.StopVmInstanceMsg', vm.get_vm().uuid):
            stop_msg_mismatch += 1
            test_util.test_warn('StopVmInstanceMsg is not expected to execute at %s' % (change_date+i))

        if test_lib.lib_find_in_local_management_server_log(change_date+i, '[msg received]: {"org.zstack.header.vm.StartVmInstanceMsg', vm.get_vm().uuid):
            start_msg_mismatch += 1
            test_util.test_warn('StartVmInstanceMsg is not expected to execute at %s' % (change_date+i))
    if stop_msg_mismatch > 5:
        test_util.test_fail('%s of 58 StopVmInstanceMsg not executed at expected timestamp' % (stop_msg_mismatch))

    if start_msg_mismatch > 10:
        test_util.test_fail('%s of 58 StartVmInstanceMsg not executed at expected timestamp' % (start_msg_mismatch))

    schd_ops.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.uuid)
    try:
        vm.destroy()
    except:
	test_util.test_logger('expected exception when destroy VM since too many queued task')
    test_util.test_pass('Create Simple VM Stop Start Scheduler Success')
def test():
    global vm
    global schd1
    global schd2

    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())
    schd1 = vm_ops.stop_vm_scheduler(vm.get_vm().uuid, 'simple', 'simple_stop_vm_scheduler', start_date+10, 20)
    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(schd1, 'Enabled')
    check_scheduler_state(schd2, '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(schd1, 'Disabled')
    check_scheduler_state(schd2, '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(schd1, 'Disabled')
    check_scheduler_state(schd2, '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.delete_scheduler(schd1.uuid)
    schd_ops.delete_scheduler(schd2.uuid)
    vm.set_delete_policy(delete_policy)
    vm.destroy()

    conditions = res_ops.gen_query_conditions('uuid', '=', schd1.uuid)
    if len(res_ops.query_resource(res_ops.SCHEDULER, 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', '=', schd2.uuid)
    if len(res_ops.query_resource(res_ops.SCHEDULER, 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')