예제 #1
0
def check_resource():

    hosts = res_ops.query_resource(res_ops.HOST, [], None)
    for host in hosts:
        if host.status != "Connected":
            host_ops.reconnect_host(host.uuid)
            return False

    pss = res_ops.query_resource(res_ops.PRIMARY_STORAGE, [], None)
    for ps in pss:
        if ps.status != "Connected":
            ps_ops.reconnect_primary_storage(ps.uuid)
            return False

    bss = res_ops.query_resource(res_ops.BACKUP_STORAGE, [], None)
    for bs in bss:
        if bs.status != "Connected":
            bs_ops.reconnect_backup_storage(bs.uuid)
            return False

    if os.environ.get('WOODPECKER_PARALLEL') != None and os.environ.get(
            'WOODPECKER_PARALLEL') == '0':
        vms = res_ops.query_resource(res_ops.VM_INSTANCE, [], None)
        for vm in vms:
            if vm.type == 'UserVm':
                try:
                    vm_ops.destroy_vm(vm.uuid)
                except:
                    test_util.test_logger('ignore exception try to destroy vm')
                try:
                    vm_ops.expunge_vm(vm.uuid)
                except:
                    test_util.test_logger('ignore exception try to expunge vm')

    vrs = res_ops.query_resource(res_ops.APPLIANCE_VM, [], None)
    for vr in vrs:
        if vr.status != "Connected" or vr.state != "Running":
            if vr.applianceVmType != "vrouter":
                try:
                    vm_ops.stop_vm(vr.uuid, force='cold')
                    vm_ops.start_vm(vr.uuid)
                except:
                    test_util.test_logger('Exception when reboot vr vm')
            else:
                try:
                    vm_ops.stop_vm(vr.uuid, force='cold')
                except:
                    test_util.test_logger('Exception when reboot vr vm')
            return False

    return True
예제 #2
0
def check_resource():

    hosts = res_ops.query_resource(res_ops.HOST, [], None)
    for host in hosts:
        if host.status != "Connected":
            host_ops.reconnect_host(host.uuid) 
            return False

    pss = res_ops.query_resource(res_ops.PRIMARY_STORAGE, [], None)
    for ps in pss:
        if ps.status != "Connected":
            ps_ops.reconnect_primary_storage(ps.uuid)
            return False

    bss = res_ops.query_resource(res_ops.BACKUP_STORAGE, [], None)
    for bs in bss:
        if bs.status != "Connected":
            bs_ops.reconnect_backup_storage(bs.uuid)
            return False

    if os.environ.get('WOODPECKER_PARALLEL') != None and os.environ.get('WOODPECKER_PARALLEL') == '0':
        vms = res_ops.query_resource(res_ops.VM_INSTANCE, [], None)
        for vm in vms:
            if vm.type == 'UserVm':
                try:
                    vm_ops.destroy_vm(vm.uuid)
                except:
                    test_util.test_logger('ignore exception try to destroy vm')
                try:
                    vm_ops.expunge_vm(vm.uuid)
                except:
                    test_util.test_logger('ignore exception try to expunge vm')

    vrs = res_ops.query_resource(res_ops.APPLIANCE_VM, [], None)
    for vr in vrs:
        if vr.status != "Connected" or vr.state != "Running":
            if vr.applianceVmType != "vrouter":
		try:
                    vm_ops.stop_vm(vr.uuid, force='cold')
                    vm_ops.start_vm(vr.uuid)
                except:
                    test_util.test_logger('Exception when reboot vr vm')
            else:
                try:
                    vm_ops.stop_vm(vr.uuid, force='cold')
                except:
                    test_util.test_logger('Exception when reboot vr vm')
            return False

    return True
예제 #3
0
def ensure_pss_connected():
    for i in range(300):
        #time.sleep(1)
        ps_list = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
        for ps in ps_list:
            ps_ops.reconnect_primary_storage(ps.uuid)
            cond = res_ops.gen_query_conditions('uuid', '=', ps.uuid)
            pss = res_ops.query_resource_fields(res_ops.PRIMARY_STORAGE, cond, None)
            if not "connected" in pss[0].status.lower():
                test_util.test_logger("time %s found not connected ps status: %s" %(str(i), pss[0].status))
                break
        else:
            return
    else:
        test_util.test_fail("ps status didn't change to Connected within 300s, therefore, failed")
예제 #4
0
def ensure_pss_connected():
    for i in range(300):
        #time.sleep(1)
        ps_list = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
        for ps in ps_list:
            ps_ops.reconnect_primary_storage(ps.uuid)
            cond = res_ops.gen_query_conditions('uuid', '=', ps.uuid)
            pss = res_ops.query_resource_fields(res_ops.PRIMARY_STORAGE, cond, None)
            if not "connected" in pss[0].status.lower():
                test_util.test_logger("time %s found not connected ps status: %s" %(str(i), pss[0].status))
                break
        else:
            return
    else:
        test_util.test_fail("ps status didn't change to Connected within 300s, therefore, failed")
def test():
    test_util.test_dsc('primary storage reconnection check test')

    for ps in res_ops.query_resource(res_ops.PRIMARY_STORAGE):
        reconnect_ret =  ps_ops.reconnect_primary_storage(ps.uuid)
        if reconnect_ret.status != "Connected":
            test_util.test_pass("Reconnect primary storage fail: %s" % (reconnect_ret.status))
        
    test_util.test_pass("Reconnect primary storage pass")
def test():
    test_util.test_dsc('primary storage reconnection check test')

    for ps in res_ops.query_resource(res_ops.PRIMARY_STORAGE):
        reconnect_ret = ps_ops.reconnect_primary_storage(ps.uuid)
        if reconnect_ret.status != "Connected":
            test_util.test_pass("Reconnect primary storage fail: %s" %
                                (reconnect_ret.status))

    test_util.test_pass("Reconnect primary storage pass")
def test():
    global ps
    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    if flavor['ceph']:
        cond = res_ops.gen_query_conditions('type', '=', 'Ceph', cond)

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 1)
    if not ps:
        test_util.test_skip('No Enabled/Connected ps was found, skip test.' )

    ps_ops.reconnect_primary_storage(ps[0].uuid)
    saved_ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 1)
    if flavor['ceph']:
        ps[0].managementIp = ps[0].mons[0].hostname
    else:
        ps[0].managementIp = ps[0].hostname
    if flavor['ceph']:
        test_stub.setup_fake_ceph(ps[0], 631242663936, 428968118272)
        fake_total = 631242663936
        fake_available = 428968118272
    else:
        test_stub.setup_fake_fs(ps[0], '2G', saved_ps[0].url)
        fake_total = 2*1024*1024*1024
        fake_available = 2*1024*1024*1024

    ps_ops.reconnect_primary_storage(ps[0].uuid)

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 1)
    if flavor['ceph']:
        ps[0].managementIp = ps[0].mons[0].hostname
    else:
        ps[0].managementIp = ps[0].hostname

    if ps[0].totalCapacity != fake_total:
        test_util.test_fail('totalCapacity %s not updated after reconnect ps' % (ps[0].totalCapacity))
    if ps[0].availableCapacity != fake_available:
        test_util.test_fail('availableCapacity %s not updated after reconnect ps' % (ps[0].availableCapacity))

    if flavor['ceph']:
        test_stub.remove_fake_ceph(ps[0])
    else:
        test_stub.remove_fake_fs(ps[0], saved_ps[0].url)
    ps_ops.reconnect_primary_storage(ps[0].uuid)
    
    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 1)
    if flavor['ceph']:
        ps[0].managementIp = ps[0].mons[0].hostname
    else:
        ps[0].managementIp = ps[0].hostname

    if ps[0].totalCapacity != saved_ps[0].totalCapacity:
        test_util.test_fail('totalCapacity %s not updated after reconnect ps' % (ps[0].totalCapacity))
    if ps[0].availableCapacity == 0:
        test_util.test_fail('availableCapacity %s not updated after reconnect ps' % (ps[0].availableCapacity))

    test_util.test_pass('Test primary storage capacity after reconnect ps pass.')
def test():
    test_util.test_dsc('ceph reconnection check heartbeat test')
    if res_ops.query_resource(res_ops.CEPH_PRIMARY_STORAGE):
        ps = res_ops.query_resource(res_ops.CEPH_PRIMARY_STORAGE)[0]
    else:
        test_util.test_skip("No ceph primarystorage for test. Skip test")
    cond = res_ops.gen_query_conditions('uuid', '=', ps.uuid)
    for key in ps.__dict__:
        if key == "mons":
            mons_ops = ps.__dict__[key][0].__dict__
            for sub_key in mons_ops:
                if sub_key == "monUuid":
                    monUuid = mons_ops[sub_key]
                if sub_key == "hostname":
                    hostname = mons_ops[sub_key]
                if sub_key == "sshPort":
                    sshPort = mons_ops[sub_key]
                if sub_key == "sshUsername":
                    sshUsername = mons_ops[sub_key]
                if sub_key == "sshPassword":
                    sshPassword = mons_ops[sub_key]
    heartbeat_file_name = monUuid + "-this-is-a-test-image-with-long-name"

    ret = os.system(
        "sshpass -p '%s' ssh %s rbd create %s --image-format 2 --size 1" %
        (sshPassword, hostname, heartbeat_file_name))
    return_name = os.system(
        "sshpass -p '%s' ssh %s rbd ls |grep %s |grep %s" %
        (sshPassword, hostname, monUuid, heartbeat_file_name))
    if return_name == heartbeat_file_name:
        print " Create heartbeat file %s success" % heartbeat_file_name

    reconnect_ret = ps_ops.reconnect_primary_storage(ps.uuid)
    test_fail = 1
    for key in reconnect_ret.__dict__:
        if key == "status" and reconnect_ret[key] == "Connected":
            test_fail = 0
            test_util.test_pass("Reconnect status is %s" % reconnect_ret[key])
    if test_fail:
        test_util.test_fail("Reconnect to ceph ps failed")
def test():
    test_util.test_dsc('ceph reconnection check heartbeat test')
    if res_ops.query_resource(res_ops.CEPH_PRIMARY_STORAGE):
        ps = res_ops.query_resource(res_ops.CEPH_PRIMARY_STORAGE)[0]
    else:
        test_util.test_skip("No ceph primarystorage for test. Skip test")
    cond = res_ops.gen_query_conditions('uuid', '=', ps.uuid)
    for key in ps.__dict__:
        if key == "mons":
            mons_ops = ps.__dict__[key][0].__dict__
            for sub_key in mons_ops:
                if sub_key == "monUuid":
                    monUuid = mons_ops[sub_key]
                if sub_key == "hostname":
                    hostname = mons_ops[sub_key]
                if sub_key == "sshPort":
                    sshPort = mons_ops[sub_key]
                if sub_key == "sshUsername":
                    sshUsername = mons_ops[sub_key]
                if sub_key == "sshPassword":
                    sshPassword = mons_ops[sub_key]
    heartbeat_file_name = monUuid+"-this-is-a-test-image-with-long-name"

    ret = os.system("sshpass -p '%s' ssh %s rbd create %s --image-format 2 --size 1" %(sshPassword, hostname, heartbeat_file_name))
    return_name=os.system("sshpass -p '%s' ssh %s rbd ls |grep %s |grep %s" %(sshPassword, hostname, monUuid, heartbeat_file_name))
    if return_name == heartbeat_file_name: 
        print " Create heartbeat file %s success" %heartbeat_file_name

    reconnect_ret =  ps_ops.reconnect_primary_storage(ps.uuid)
    test_fail = 1
    for key in reconnect_ret.__dict__:
        if key == "status" and reconnect_ret[key] == "Connected":
            test_fail = 0
            test_util.test_pass("Reconnect status is %s"%reconnect_ret[key])
    if test_fail:
        test_util.test_fail("Reconnect to ceph ps failed")
def test():
    global email_endpoint_uuid, email_platform_uuid, my_sns_topic_uuid, host_sns_topic_uuid, ps_uuid, hostname, host_management_ip, host_uuid, host_status, bs_uuid, bs_type, bs_status
    bs_cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    bs_list = res_ops.query_resource(res_ops.BACKUP_STORAGE, bs_cond)
    for bss in bs_list:
        if bss.type == res_ops.SFTP_BACKUP_STORAGE:
            bs_uuid = bss.uuid
            bs_type = bss.type
            hostname = bss.hostname
            break
        elif bss.type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
            bs_uuid = bss.uuid
            bs_type = bss.type
            hostname = bss.hostname
            break
        else:
            test_util.test_skip('No match backupStorage,test skip')

    smtp_server = os.environ.get('smtpServer')
    pop_server = os.environ.get('popServer')
    smtp_port = os.environ.get('smtpPort')
    username = os.environ.get('mailUsername')
    password = os.environ.get('mailPassword')
    email_platform_name = 'Alarm_email'
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port,
                                                       email_platform_name,
                                                       username, password)
    email_platform_uuid = email_platform.uuid
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail(
            'Validate SNS Email Platform Failed, Email Plarform: %s' %
            email_platform_uuid)
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(
        username, 'test_qa', email_platform_uuid).uuid

    my_sns_topic = zwt_ops.create_sns_topic('my_sns_topic')
    my_sns_topic_uuid = my_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(my_sns_topic_uuid, email_endpoint_uuid)

    host_sns_topic = zwt_ops.create_sns_topic('host_topic')
    host_sns_topic_uuid = host_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(host_sns_topic_uuid, email_endpoint_uuid)

    ps_actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    ps_namespace = 'ZStack/PrimaryStorage'
    ps_disconnected = 'PrimaryStorageDisconnected'
    ps_event_sub_uuid = zwt_ops.subscribe_event(ps_namespace, ps_disconnected,
                                                ps_actions).uuid
    event_list.append(ps_event_sub_uuid)

    bs_actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    bs_namespace = 'ZStack/BackupStorage'
    bs_disconnected = 'BackupStorageDisconnected'
    bs_event_sub_uuid = zwt_ops.subscribe_event(bs_namespace, bs_disconnected,
                                                bs_actions).uuid
    event_list.append(bs_event_sub_uuid)

    host_actions = [{"actionUuid": host_sns_topic_uuid, "actionType": "sns"}]
    host_namespace = 'ZStack/Host'
    host_status_changed = 'HostStatusChanged'
    host_status_labels = [{
        "key": "NewStatus",
        "op": "Equal",
        "value": "Disconnected"
    }]
    host_status_event_sub_uuid = zwt_ops.subscribe_event(
        host_namespace, host_status_changed, host_actions,
        host_status_labels).uuid
    event_list.append(host_status_event_sub_uuid)

    host_disconnected = 'HostDisconnected'
    host_disconnected_event_sub_uuid = zwt_ops.subscribe_event(
        host_namespace, host_disconnected, host_actions).uuid
    event_list.append(host_disconnected_event_sub_uuid)

    if zwt_ops.check_sns_email(pop_server, username, password, ps_disconnected,
                               ps_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password, bs_disconnected,
                               bs_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password,
                               host_status_changed,
                               host_status_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password,
                               host_disconnected,
                               host_disconnected_event_sub_uuid):
        test_util.test_fail('email already exsist before test')

    # Disconnected ps ,bs and host
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    primary_storage_option = test_util.PrimaryStorageOption()
    primary_storage_option.set_type('nfs')
    primary_storage_option.set_zone_uuid(zone_uuid)
    primary_storage_option.set_name('test_nfs_ps')
    primary_storage_option.set_url('222.222.222.222/nfs/')
    try:
        ps_uuid = ps_ops.create_nfs_primary_storage(
            primary_storage_option).uuid
        ps_ops.reconnect_primary_storage(ps_uuid)
    except:
        pass

    if bs_type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
        bs_ops.update_image_store_backup_storage_info(bs_uuid, 'hostname',
                                                      '222.222.222.222')
        try:
            bs_ops.reconnect_backup_storage(bs_uuid)
        except:
            cond = res_ops.gen_query_conditions('uuid', '=', bs_uuid)
            bs_status = res_ops.query_resource(
                res_ops.IMAGE_STORE_BACKUP_STORAGE, cond)[0].status
    elif bs_type == res_ops.SFTP_BACKUP_STORAGE:
        bs_ops.update_sftp_backup_storage_info(bs_uuid, 'hostname',
                                               '222.222.222.222')
        try:
            bs_ops.reconnect_backup_storage(bs_uuid)
        except:
            cond = res_ops.gen_query_conditions('uuid', '=', bs_uuid)
            bs_status = res_ops.query_resource(res_ops.SFTP_BACKUP_STORAGE,
                                               cond)[0].status

    host_cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    host = res_ops.query_resource_with_num(res_ops.HOST,
                                           host_cond,
                                           start=0,
                                           limit=1)[0]
    host_uuid = host.uuid
    host_management_ip = host.managementIp
    host_ops.update_host(host_uuid, 'managementIp', '222.222.222.222')
    try:
        host_ops.reconnect_host(host_uuid)
    except:
        cond = res_ops.gen_query_conditions('uuid', '=', host_uuid)
        bs_status = res_ops.query_resource(res_ops.HOST, cond)[0].status

    # wait for send email
    time.sleep(60)

    ps_ops.delete_primary_storage(ps_uuid)
    if hostname:
        if bs_type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
            bs_ops.update_image_store_backup_storage_info(
                bs_uuid, 'hostname', hostname)
            bs_ops.reconnect_backup_storage(bs_uuid)
        elif bs_type == res_ops.SFTP_BACKUP_STORAGE:
            bs_ops.update_sftp_backup_storage_info(bs_uuid, 'hostname',
                                                   hostname)
            bs_ops.reconnect_backup_storage(bs_uuid)
    host_ops.update_host(host_uuid, 'managementIp', host_management_ip)
    host_ops.reconnect_host(host_uuid)
    zwt_ops.delete_sns_topic(host_sns_topic_uuid)
    zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    for event_uuid in event_list:
        zwt_ops.unsubscribe_event(event_uuid)
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    check_1 = zwt_ops.check_sns_email(pop_server, username, password,
                                      ps_disconnected, ps_event_sub_uuid)
    check_2 = zwt_ops.check_sns_email(pop_server, username, password,
                                      bs_disconnected, bs_event_sub_uuid)
    check_3 = zwt_ops.check_sns_email(pop_server, username, password,
                                      host_status_changed,
                                      host_status_event_sub_uuid)
    check_4 = zwt_ops.check_sns_email(pop_server, username, password,
                                      host_disconnected,
                                      host_disconnected_event_sub_uuid)

    if check_1 and check_2 and check_3 and check_4:
        test_util.test_pass(
            'test host ,bs, ps disconnected event with email success!')
    else:
        test_util.test_fail('cannt receive all event mail')
def test():
    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]
    ps_env = test_stub.PSEnvChecker()
    local_ps, shared_ps = ps_env.get_two_ps()

    vm_list=list(test_stub.generate_local_shared_test_vms(test_obj_dict, vm_ha=flavor['vm_ha']))
    (vm_root_local, vm_root_local_data_local,
     vm_root_local_data_shared, vm_root_local_data_mixed,
     vm_root_shared, vm_root_shared_data_local,
     vm_root_shared_data_shared, vm_root_shared_data_mixed) = vm_list

    if flavor['local_state'] is DISABLED:
        ps_ops.change_primary_storage_state(local_ps.uuid, state='disable')
        time.sleep(10)
        for vm in vm_list:
            vm.update()
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['shared_state'] is DISABLED:
        ps_ops.change_primary_storage_state(shared_ps.uuid, state='disable')
        time.sleep(10)
        for vm in vm_list:
            vm.update()
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['reconnect']:
        for ps in (local_ps, shared_ps):
            ps_ops.reconnect_primary_storage(ps.uuid)
        for vm in vm_list:
            vm.update()
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['local_state'] is MAINTENANCE:
        ps_ops.change_primary_storage_state(local_ps.uuid, state='maintain')
        maintain_ps = local_ps
    if flavor['shared_state'] is MAINTENANCE:
        ps_ops.change_primary_storage_state(shared_ps.uuid, state='maintain')
        maintain_ps = shared_ps
    time.sleep(60)

    if MAINTENANCE in (flavor['local_state'], flavor['shared_state']):
        vr_vm_list = test_lib.lib_find_vr_by_vm(vm_list[0].get_vm())
        vr_vm = None
        if vr_vm_list:
            vr_vm = vr_vm_list[0]
            if vr_vm.allVolumes[0].primaryStorageUuid == maintain_ps.uuid:
                assert vr_vm.state == inventory.STOPPED
            else:
                assert vr_vm.state == inventory.RUNNING

        for vm in vm_list:
            vm.update()

    if flavor['local_state'] is MAINTENANCE:
        for vm in (vm_root_local, vm_root_local_data_local,vm_root_local_data_shared, vm_root_local_data_mixed,
                   vm_root_shared_data_mixed,vm_root_shared_data_local):
            assert vm.get_vm().state == inventory.STOPPED
            with test_stub.expected_failure("start vm in maintenance ps", Exception):
                vm.start()

        for vm in (vm_root_shared, vm_root_shared_data_shared):
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['shared_state'] is MAINTENANCE:
        for vm in (vm_root_shared, vm_root_shared_data_shared,vm_root_shared_data_local, vm_root_shared_data_mixed,
                   vm_root_local_data_mixed,vm_root_local_data_shared):
            assert vm.get_vm().state == inventory.STOPPED
            with test_stub.expected_failure("start vm in maintenance ps", Exception):
                vm.start()
        for vm in (vm_root_local, vm_root_local_data_local):
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['local_state'] is MAINTENANCE:
        ps_ops.change_primary_storage_state(local_ps.uuid, state='enable')
    if flavor['shared_state'] is MAINTENANCE:
        ps_ops.change_primary_storage_state(shared_ps.uuid, state='enable')

#    if MAINTENANCE in (flavor['local_state'], flavor['shared_state']):
#        if vr_vm and vr_vm.state == inventory.STOPPED:
#            vm_ops.start_vm(vr_vm.uuid)

    for vm in vm_list:
        vm.update()
        if vm.get_vm().state == inventory.STOPPED and vm.get_vm().type != 'ApplianceVm':
            vm.start()
        vm.check()
예제 #12
0
def test():
    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]
    ps_env = test_stub.PSEnvChecker()
    local_ps, shared_ps = ps_env.get_two_ps()

    vm_list = list(
        test_stub.generate_local_shared_test_vms(test_obj_dict,
                                                 vm_ha=flavor['vm_ha']))
    (vm_root_local, vm_root_local_data_local, vm_root_local_data_shared,
     vm_root_local_data_mixed, vm_root_shared, vm_root_shared_data_local,
     vm_root_shared_data_shared, vm_root_shared_data_mixed) = vm_list

    if flavor['local_state'] is DISABLED:
        ps_ops.change_primary_storage_state(local_ps.uuid, state='disable')
        time.sleep(10)
        for vm in vm_list:
            vm.update()
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['shared_state'] is DISABLED:
        ps_ops.change_primary_storage_state(shared_ps.uuid, state='disable')
        time.sleep(10)
        for vm in vm_list:
            vm.update()
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['reconnect']:
        for ps in (local_ps, shared_ps):
            ps_ops.reconnect_primary_storage(ps.uuid)
        for vm in vm_list:
            vm.update()
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['local_state'] is MAINTENANCE:
        ps_ops.change_primary_storage_state(local_ps.uuid, state='maintain')
        maintain_ps = local_ps
    if flavor['shared_state'] is MAINTENANCE:
        ps_ops.change_primary_storage_state(shared_ps.uuid, state='maintain')
        maintain_ps = shared_ps
    time.sleep(30)

    if MAINTENANCE in (flavor['local_state'], flavor['shared_state']):
        vr_vm_list = test_lib.lib_find_vr_by_vm(vm_list[0].get_vm())
        vr_vm = None
        if vr_vm_list:
            vr_vm = vr_vm_list[0]
            if vr_vm.allVolumes[0].primaryStorageUuid == maintain_ps.uuid:
                assert vr_vm.state == inventory.STOPPED
            else:
                assert vr_vm.state == inventory.RUNNING

        for vm in vm_list:
            vm.update()

    if flavor['local_state'] is MAINTENANCE:
        for vm in (vm_root_local, vm_root_local_data_local,
                   vm_root_local_data_shared, vm_root_local_data_mixed,
                   vm_root_shared_data_mixed, vm_root_shared_data_local):
            assert vm.get_vm().state == inventory.STOPPED
            with test_stub.expected_failure("start vm in maintenance ps",
                                            Exception):
                vm.start()

        for vm in (vm_root_shared, vm_root_shared_data_shared):
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['shared_state'] is MAINTENANCE:
        for vm in (vm_root_shared, vm_root_shared_data_shared,
                   vm_root_shared_data_local, vm_root_shared_data_mixed,
                   vm_root_local_data_mixed, vm_root_local_data_shared):
            assert vm.get_vm().state == inventory.STOPPED
            with test_stub.expected_failure("start vm in maintenance ps",
                                            Exception):
                vm.start()
        for vm in (vm_root_local, vm_root_local_data_local):
            assert vm.get_vm().state == inventory.RUNNING

    if flavor['local_state'] is MAINTENANCE:
        ps_ops.change_primary_storage_state(local_ps.uuid, state='enable')
    if flavor['shared_state'] is MAINTENANCE:
        ps_ops.change_primary_storage_state(shared_ps.uuid, state='enable')

    if MAINTENANCE in (flavor['local_state'], flavor['shared_state']):
        if vr_vm and vr_vm.state == inventory.STOPPED:
            vm_ops.start_vm(vr_vm.uuid)

    for vm in vm_list:
        if vm.get_vm().state == inventory.STOPPED:
            vm.start()
        vm.check()
def test():
    global ps
    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    if flavor['ceph']:
        cond = res_ops.gen_query_conditions('type', '=', 'Ceph', cond)

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE,
                                         cond,
                                         limit=1)
    if not ps:
        test_util.test_skip('No Enabled/Connected ps was found, skip test.')

    ps_ops.reconnect_primary_storage(ps[0].uuid)
    saved_ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE,
                                               cond,
                                               limit=1)
    if flavor['ceph']:
        ps[0].managementIp = ps[0].mons[0].hostname
    else:
        ps[0].managementIp = ps[0].hostname
    if flavor['ceph']:
        test_stub.setup_fake_ceph(ps[0], 631242663936, 428968118272)
        fake_total = 631242663936
        fake_available = 428968118272
    else:
        test_stub.setup_fake_fs(ps[0], '2G', saved_ps[0].url)
        fake_total = 2 * 1024 * 1024 * 1024
        fake_available = 2 * 1024 * 1024 * 1024

    ps_ops.reconnect_primary_storage(ps[0].uuid)

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE,
                                         cond,
                                         limit=1)
    if flavor['ceph']:
        ps[0].managementIp = ps[0].mons[0].hostname
    else:
        ps[0].managementIp = ps[0].hostname

    if ps[0].totalCapacity != fake_total:
        test_util.test_fail('totalCapacity %s not updated after reconnect ps' %
                            (ps[0].totalCapacity))
    if ps[0].availableCapacity != fake_available:
        test_util.test_fail(
            'availableCapacity %s not updated after reconnect ps' %
            (ps[0].availableCapacity))

    if flavor['ceph']:
        test_stub.remove_fake_ceph(ps[0])
    else:
        test_stub.remove_fake_fs(ps[0], saved_ps[0].url)
    ps_ops.reconnect_primary_storage(ps[0].uuid)

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE,
                                         cond,
                                         limit=1)
    if flavor['ceph']:
        ps[0].managementIp = ps[0].mons[0].hostname
    else:
        ps[0].managementIp = ps[0].hostname

    if ps[0].totalCapacity != saved_ps[0].totalCapacity:
        test_util.test_fail('totalCapacity %s not updated after reconnect ps' %
                            (ps[0].totalCapacity))
    if ps[0].availableCapacity == 0:
        test_util.test_fail(
            'availableCapacity %s not updated after reconnect ps' %
            (ps[0].availableCapacity))

    test_util.test_pass(
        'Test primary storage capacity after reconnect ps pass.')
def test():
    global email_endpoint_uuid,email_platform_uuid,my_sns_topic_uuid,host_sns_topic_uuid,ps_uuid,hostname,host_management_ip,host_uuid,host_status,bs_uuid,bs_type,bs_status
    bs_cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    bs_list = res_ops.query_resource(res_ops.BACKUP_STORAGE, bs_cond)
    for bss in bs_list:
        if bss.type ==res_ops.SFTP_BACKUP_STORAGE:
            bs_uuid =bss.uuid
            bs_type =bss.type
            hostname = bss.hostname
            break
        elif bss.type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
            bs_uuid=bss.uuid
            bs_type=bss.type
            hostname = bss.hostname
            break
        else:
            test_util.test_skip('No match backupStorage,test skip')

    smtp_server = os.environ.get('smtpServer')
    pop_server = os.environ.get('popServer')
    smtp_port = os.environ.get('smtpPort')
    username = os.environ.get('mailUsername')
    password = os.environ.get('mailPassword')
    email_platform_name='Alarm_email'
    email_platform = zwt_ops.create_sns_email_platform(smtp_server, smtp_port,email_platform_name , username, password)
    email_platform_uuid = email_platform.uuid
    try:
        zwt_ops.validate_sns_email_platform(email_platform_uuid)
    except:
        test_util.test_fail('Validate SNS Email Platform Failed, Email Plarform: %s' % email_platform_uuid)
    email_endpoint_uuid = zwt_ops.create_sns_email_endpoint(username, 'test_qa', email_platform_uuid).uuid

    my_sns_topic = zwt_ops.create_sns_topic('my_sns_topic')
    my_sns_topic_uuid = my_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(my_sns_topic_uuid, email_endpoint_uuid)

    host_sns_topic = zwt_ops.create_sns_topic('host_topic')
    host_sns_topic_uuid = host_sns_topic.uuid
    zwt_ops.subscribe_sns_topic(host_sns_topic_uuid, email_endpoint_uuid)

    ps_actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    ps_namespace = 'ZStack/PrimaryStorage'
    ps_disconnected = 'PrimaryStorageDisconnected'
    ps_event_sub_uuid = zwt_ops.subscribe_event(ps_namespace, ps_disconnected, ps_actions).uuid
    event_list.append(ps_event_sub_uuid)

    bs_actions = [{"actionUuid": my_sns_topic_uuid, "actionType": "sns"}]
    bs_namespace = 'ZStack/BackupStorage'
    bs_disconnected = 'BackupStorageDisconnected'
    bs_event_sub_uuid = zwt_ops.subscribe_event(bs_namespace, bs_disconnected, bs_actions).uuid
    event_list.append(bs_event_sub_uuid)

    host_actions = [{"actionUuid": host_sns_topic_uuid, "actionType": "sns"}]
    host_namespace = 'ZStack/Host'
    host_status_changed = 'HostStatusChanged'
    host_status_labels = [{"key": "NewStatus", "op": "Equal", "value": "Disconnected"}]
    host_status_event_sub_uuid = zwt_ops.subscribe_event(host_namespace, host_status_changed, host_actions, host_status_labels).uuid
    event_list.append(host_status_event_sub_uuid)

    host_disconnected = 'HostDisconnected'
    host_disconnected_event_sub_uuid = zwt_ops.subscribe_event(host_namespace, host_disconnected, host_actions).uuid
    event_list.append(host_disconnected_event_sub_uuid)

    if zwt_ops.check_sns_email(pop_server, username, password, ps_disconnected, ps_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password, bs_disconnected, bs_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password, host_status_changed, host_status_event_sub_uuid):
        test_util.test_fail('email already exsist before test')
    if zwt_ops.check_sns_email(pop_server, username, password, host_disconnected, host_disconnected_event_sub_uuid):
        test_util.test_fail('email already exsist before test')

    # Disconnected ps ,bs and host
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    primary_storage_option = test_util.PrimaryStorageOption()
    primary_storage_option.set_type('nfs')
    primary_storage_option.set_zone_uuid(zone_uuid)
    primary_storage_option.set_name('test_nfs_ps')
    primary_storage_option.set_url('222.222.222.222/nfs/')
    try:
        ps_uuid = ps_ops.create_nfs_primary_storage(primary_storage_option).uuid
        ps_ops.reconnect_primary_storage(ps_uuid)
    except:
        pass

    if bs_type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
        bs_ops.update_image_store_backup_storage_info(bs_uuid, 'hostname', '222.222.222.222')
        try:
            bs_ops.reconnect_backup_storage(bs_uuid)
        except:
            cond = res_ops.gen_query_conditions('uuid', '=', bs_uuid)
            bs_status = res_ops.query_resource(res_ops.IMAGE_STORE_BACKUP_STORAGE, cond)[0].status
    elif bs_type == res_ops.SFTP_BACKUP_STORAGE:
        bs_ops.update_sftp_backup_storage_info(bs_uuid,'hostname','222.222.222.222')
        try:
            bs_ops.reconnect_backup_storage(bs_uuid)
        except:
            cond=res_ops.gen_query_conditions('uuid','=',bs_uuid)
            bs_status=res_ops.query_resource(res_ops.SFTP_BACKUP_STORAGE,cond)[0].status

    host_cond = res_ops.gen_query_conditions('status', '=', 'Connected')
    host = res_ops.query_resource_with_num(res_ops.HOST, host_cond, start=0, limit=1)[0]
    host_uuid = host.uuid
    host_management_ip = host.managementIp
    host_ops.update_host(host_uuid, 'managementIp', '222.222.222.222')
    try:
        host_ops.reconnect_host(host_uuid)
    except:
        cond = res_ops.gen_query_conditions('uuid', '=', host_uuid)
        bs_status = res_ops.query_resource(res_ops.HOST, cond)[0].status

    # wait for send email
    time.sleep(120)

    ps_ops.delete_primary_storage(ps_uuid)
    if hostname:
        if bs_type == res_ops.IMAGE_STORE_BACKUP_STORAGE:
            bs_ops.update_image_store_backup_storage_info(bs_uuid, 'hostname', hostname)
            bs_ops.reconnect_backup_storage(bs_uuid)
        elif bs_type == res_ops.SFTP_BACKUP_STORAGE:
            bs_ops.update_sftp_backup_storage_info(bs_uuid, 'hostname', hostname)
            bs_ops.reconnect_backup_storage(bs_uuid)
    host_ops.update_host(host_uuid, 'managementIp', host_management_ip)
    host_ops.reconnect_host(host_uuid)
    zwt_ops.delete_sns_topic(host_sns_topic_uuid)
    zwt_ops.delete_sns_topic(my_sns_topic_uuid)
    for event_uuid in event_list:
        zwt_ops.unsubscribe_event(event_uuid)
    zwt_ops.delete_sns_application_endpoint(email_endpoint_uuid)
    zwt_ops.delete_sns_application_platform(email_platform_uuid)

    check_1 = zwt_ops.check_sns_email(pop_server, username, password, ps_disconnected, ps_event_sub_uuid)
    check_2 = zwt_ops.check_sns_email(pop_server, username, password, bs_disconnected, bs_event_sub_uuid)
    check_3 = zwt_ops.check_sns_email(pop_server, username, password, host_status_changed, host_status_event_sub_uuid)
    check_4 = zwt_ops.check_sns_email(pop_server, username, password, host_disconnected, host_disconnected_event_sub_uuid)

    if check_1 and check_2 and check_3 and check_4:
        test_util.test_pass('test host ,bs, ps disconnected event with email success!')
    else:
        test_util.test_fail('cannt receive all event mail')