def test():
    global test_obj_dict
    volumes = []

    ova_image_name = os.environ['vcenterDefaultmplate']
    network_pattern = 'L3-%s'%os.environ['dportgroup']
    if not vct_ops.lib_get_vcenter_l3_by_name(network_pattern):
        network_pattern = 'L3-%s'%os.environ['portgroup0']
    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('largeDiskOfferingName'))
    #create vm with disk
    vm = test_stub.create_vm_in_vcenter(vm_name = 'vm_2', image_name = ova_image_name, l3_name = network_pattern, disk_offering_uuids = [disk_offering.uuid, disk_offering.uuid] )
    test_obj_dict.add_vm(vm)
    vm.check()

    vcenter = os.environ.get('vcenter')
    SI = vct_ops.connect_vcenter(vcenter)
    content = SI.RetrieveContent()
    vm = vct_ops.get_vm(content, name = 'vm_2')[0]
    
    vct_ops.delete_virtual_disk(vm, 2)
    vct_ops.delete_virtual_disk(vm, 2)
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vcenter).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    allvolumes = vct_ops.lib_get_vm_by_name('vm_2').allVolumes
    assert len(allvolumes) == 1
    for volume in allvolumes:
        if volume.type == 'Data':
            volumes.append(volume.installPath)
    assert set(volumes) == set([])

    #cleanup
    test_lib.lib_error_cleanup(test_obj_dict)

    test_util.test_pass("sync after detaching disk from vm in vmware test passed.")
Exemplo n.º 2
0
def test():
    global vc_cluster

    vcenter = os.environ.get('vcenter')
    SI = vct_ops.connect_vcenter(vcenter)
    content = SI.RetrieveContent()
    datacenter_name = os.environ.get('vcdatacenterName')
    datacenter = vct_ops.get_datacenter(content, datacenter_name)[0]
    #create vcenter cluster
    vc_cluster = vct_ops.create_cluster(datacenter)
    #create vcenter host
    host_name = os.environ.get('hostName3')
    managementIp = deploy_operations.get_host_from_scenario_file(
        host_name, test_lib.all_scenario_config, test_lib.scenario_file,
        test_lib.deploy_config)
    vc_host = vct_ops.add_host(vc_cluster, managementIp)

    #sync vcenter
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vcenter).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    assert vc_cluster.name == vct_ops.lib_get_vcenter_cluster_by_name(
        vc_cluster.name).name

    assert managementIp == vct_ops.lib_get_vcenter_host_by_ip(
        managementIp).name
    assert vct_ops.lib_get_vcenter_host_by_ip(
        managementIp).hypervisorType == "ESX"

    vct_ops.remove_cluster(vc_cluster)
    test_util.test_pass(
        'Sync vcenter after creating new vcenter cluster and add host to the cluster successfully'
    )
def test():
    test_stub.reload_default_license()
    test_util.test_logger('Check default community license')
    test_stub.check_license(None, None, 2147483647, False, 'Community')

    test_util.test_logger('Load and Check TrialExt license with 10 day and 1 CPU')
    file_path = test_stub.gen_license('woodpecker', '*****@*****.**', '10', 'Prepaid', '1', '')
    test_stub.load_license(file_path)
    issued_date = test_stub.get_license_info().issuedDate
    expired_date = test_stub.license_date_cal(issued_date, 86400 * 10)
    test_stub.check_license("*****@*****.**", 1, None, False, 'Paid', issued_date=issued_date, expired_date=expired_date)

    # add the vcenter 1.50

    test_stub.create_zone()
    username = os.environ.get("vcenteruser")
    password = os.environ.get("vcenterpwd")
    zone_name = "ZONE1"
    conditions = res_ops.gen_query_conditions('name', '=', zone_name)
    zone_uuid = res_ops.query_resource(res_ops.ZONE, conditions)[0].uuid
    https = "true"
    vcenterdomain = "172.20.0.50"
    try:
        vct_ops.add_vcenter("vcenter_test", vcenterdomain, username, password, https, zone_uuid)
        test_util.test_fail('vCenter can not add success')
    except Exception:
	pass

    test_util.test_logger('load and check addon license vmware with 2 day and 1 CPU ')
    file_path = test_stub.gen_addons_license('woodpecker', '*****@*****.**', '2', 'Addon', '1', '', 'vmware')
    node_uuid = res_ops.query_resource(res_ops.MANAGEMENT_NODE)[0].uuid
    test_stub.update_license(node_uuid, file_path)
    issued_date = test_stub.get_license_addons_info().issuedDate
    expired_date = test_stub.license_date_cal(issued_date, 86400 * 2)
    test_stub.check_license_addons(1, None, False, 'AddOn', issued_date=issued_date, expired_date=expired_date)
    
    vct_ops.add_vcenter("vcenter_test", vcenterdomain, username, password, https, zone_uuid)

    vcenter_uuid = res_ops.get_resource(res_ops.VCENTER)[0].uuid
    # sync vcenter
    time.sleep(10)
    vct_ops.sync_vcenter(vcenter_uuid)

    time.sleep(15)
    vct_ops.delete_vcenter(vcenter_uuid)

    time.sleep(5)
    zone_ops.delete_zone(zone_uuid)

    test_util.test_logger('start to delete the addons vmware license')
    uuid = test_stub.get_license_addons_info().uuid
    lic_ops.delete_license(node_uuid, uuid)
    test_util.test_logger('delete the addons license [uuid:] %s' % uuid)


    test_util.test_pass('Check License and add the vcenter  and check sync Test Success')
def test():
    global portgroup
    global vswitch

    vcenter = os.environ.get('vcenter')
    SI = vct_ops.connect_vcenter(vcenter)
    content = SI.RetrieveContent()
    vc_cluster = vct_ops.get_cluster(content)[0]
    #add host to vcenter cluster
    host_name = os.environ.get('hostName4')
    managementIp = deploy_operations.get_host_from_scenario_file(host_name, test_lib.all_scenario_config, test_lib.scenario_file,test_lib.deploy_config)
    vc_host = vct_ops.add_host(vc_cluster, managementIp)
    #add vswitch and portgroup
    vswitch1 = vct_ops.add_vswitch(vc_host, 'vSwitch1')
    vswitch.append('vSwitch1')
    for port_group in ['port_group0', 'port_group1', 'port_group2', 'port_group3']:
        vct_ops.add_portgroup(vc_host, 'vSwitch0', port_group)
        portgroup.append(port_group)

    #sync vcenter
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vcenter).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    #check
    assert managementIp == vct_ops.lib_get_vcenter_host_by_ip(managementIp).name
    assert vct_ops.lib_get_vcenter_host_by_ip(managementIp).hypervisorType == "ESX"

    assert 'port_group0' == vct_ops.lib_get_vcenter_l2_by_name('port_group0').name
    assert "L3-" + 'port_group0' == vct_ops.lib_get_vcenter_l3_by_name("L3-" +'port_group0' ).name
    vct_ops.remove_portgroup(vc_host, 'port_group0')
    portgroup.remove('port_group0')
    for port_group in portgroup:
        assert vct_ops.lib_get_vcenter_l2_by_name(port_group) == None

    vct_ops.add_portgroup(vc_host, 'vSwitch1', 'port_group0',0)
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    assert vct_ops.lib_get_vcenter_l2_by_name('port_group0') == None

    #cleanup
    vct_ops.remove_vswitch(vc_host,'vSwitch1')
    for name in portgroup:
        vct_ops.remove_portgroup(vc_host, name)
    vct_ops.remove_host(vc_host)

    #recover the test environment
    vc_origin_host = vct_ops.get_host(content)[0]
    #delete the virtualrouter network corresponding portgroups
    vct_ops.remove_portgroup(vc_origin_host, name=os.environ['l2vCenterPublicNetworkName'])
    vct_ops.remove_portgroup(vc_origin_host, name=os.environ['l2vCenterNoVlanNetworkName'])
    vct_ops.sync_vcenter(vcenter_uuid)
    #recover virtualrouter network
    deploy_operations.add_vcenter_l2l3_network(test_lib.all_scenario_config, test_lib.scenario_file,test_lib.deploy_config, acc_ops.login_as_admin())
    deploy_operations.add_vcenter_vrouter(test_lib.all_scenario_config, test_lib.scenario_file,test_lib.deploy_config, acc_ops.login_as_admin())
    vct_ops.sync_vcenter(vcenter_uuid)

    test_util.test_pass('Sync vcenter after adding host to vcenter cluster and add portgroup successfully')
Exemplo n.º 5
0
def test():
    global portgroup
    global vswitch

    vcenter = os.environ.get('vcenter')
    SI = vct_ops.connect_vcenter(vcenter)
    content = SI.RetrieveContent()
    vc_cluster = vct_ops.get_cluster(content)[0]
    #add host to vcenter cluster
    host_name = os.environ.get('hostName4')
    managementIp = deploy_operations.get_host_from_scenario_file(host_name, test_lib.all_scenario_config, test_lib.scenario_file,test_lib.deploy_config)
    vc_host = vct_ops.add_host(vc_cluster, managementIp)
    #add vswitch and portgroup
    vswitch1 = vct_ops.add_vswitch(vc_host, 'vSwitch1')
    vswitch.append('vSwitch1')
    for port_group in ['port_group0', 'port_group1', 'port_group2', 'port_group3']:
        vct_ops.add_portgroup(vc_host, 'vSwitch0', port_group)
        portgroup.append(port_group)

    #sync vcenter
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vcenter).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    #check
    assert managementIp == vct_ops.lib_get_vcenter_host_by_ip(managementIp).name
    assert vct_ops.lib_get_vcenter_host_by_ip(managementIp).hypervisorType == "ESX"

    assert 'port_group0' == vct_ops.lib_get_vcenter_l2_by_name('port_group0').name
    assert "L3-" + 'port_group0' == vct_ops.lib_get_vcenter_l3_by_name("L3-" +'port_group0' ).name
    vct_ops.remove_portgroup(vc_host, 'port_group0')
    portgroup.remove('port_group0')
    for port_group in portgroup:
        assert vct_ops.lib_get_vcenter_l2_by_name(port_group) == None

    vct_ops.add_portgroup(vc_host, 'vSwitch1', 'port_group0',0)
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    assert vct_ops.lib_get_vcenter_l2_by_name('port_group0') == None

    #cleanup
    vct_ops.remove_vswitch(vc_host,'vSwitch1')
    for name in portgroup:
        vct_ops.remove_portgroup(vc_host, name)

    test_util.test_pass('Sync vcenter after adding host to vcenter cluster and add portgroup successfully')
def test():
    global vm
    volumes = []

    ova_image_name = os.environ['vcenterDefaultmplate']
    if os.environ['dportgroup']:
        network_pattern = os.environ['dportgroup']
        network_pattern = 'L3-%s' % network_pattern
    else:
        network_pattern = os.environ['portgroup0']
        network_pattern = 'L3-%s' % network_pattern
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('largeDiskOfferingName'))
    #create vm
    vm = test_stub.create_vm_in_vcenter(vm_name='vm_1',
                                        image_name=ova_image_name,
                                        l3_name=network_pattern)
    vm.check()

    vcenter = os.environ.get('vcenter')
    SI = vct_ops.connect_vcenter(vcenter)
    content = SI.RetrieveContent()
    vm = vct_ops.get_vm(content, name='vm_1')[0]
    #add 2 disks to vcenter vm
    disk1 = vct_ops.add_disk(vm, 2)
    disk2 = vct_ops.add_disk(vm, 5)
    disk_file = vct_ops.get_data_volume_attach_to_vm(vm)
    #sync vcenter
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vcenter).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    allvolumes = vct_ops.lib_get_vm_by_name('vm_1').allVolumes
    assert len(allvolumes) == 3
    for volume in allvolumes:
        if volume.type == 'Data':
            volumes.append(volume.installPath)
    assert set(volumes) ^ set(disk_file) == set([])

    #cleanup
    vct_ops.destroy_vm(vm)
    vct_ops.sync_vcenter(vcenter_uuid)

    test_util.test_pass("sync after adding disk to vm in vmware test passed.")
def test():
    global test_obj_dict
    volumes = []

    ova_image_name = os.environ['vcenterDefaultmplate']
    network_pattern = 'L3-%s' % os.environ['dportgroup']
    if not vct_ops.lib_get_vcenter_l3_by_name(network_pattern):
        network_pattern = 'L3-%s' % os.environ['portgroup0']
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('largeDiskOfferingName'))
    #create vm with disk
    vm = test_stub.create_vm_in_vcenter(
        vm_name='vm_2',
        image_name=ova_image_name,
        l3_name=network_pattern,
        disk_offering_uuids=[disk_offering.uuid, disk_offering.uuid])
    test_obj_dict.add_vm(vm)
    vm.check()

    vcenter = os.environ.get('vcenter')
    SI = vct_ops.connect_vcenter(vcenter)
    content = SI.RetrieveContent()
    vm = vct_ops.get_vm(content, name='vm_2')[0]

    vct_ops.delete_virtual_disk(vm, 2)
    vct_ops.delete_virtual_disk(vm, 2)
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vcenter).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    allvolumes = vct_ops.lib_get_vm_by_name('vm_2').allVolumes
    assert len(allvolumes) == 1
    for volume in allvolumes:
        if volume.type == 'Data':
            volumes.append(volume.installPath)
    assert set(volumes) == set([])

    #cleanup
    test_lib.lib_error_cleanup(test_obj_dict)

    test_util.test_pass(
        "sync after detaching disk from vm in vmware test passed.")
def test():
    global vm
    volumes = []

    ova_image_name = os.environ['vcenterDefaultmplate']
    network_pattern = 'L3-%s'%os.environ['dportgroup']
    if not vct_ops.lib_get_vcenter_l3_by_name(network_pattern):
        network_pattern = 'L3-%s'%os.environ['portgroup0']
    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('largeDiskOfferingName'))
    #create vm
    vm = test_stub.create_vm_in_vcenter(vm_name = 'vm_1', image_name = ova_image_name, l3_name = network_pattern)
    vm.check()

    vcenter = os.environ.get('vcenter')
    SI = vct_ops.connect_vcenter(vcenter)
    content = SI.RetrieveContent()
    vm = vct_ops.get_vm(content, name = 'vm_1')[0]
    #add 2 disks to vcenter vm
    disk1 = vct_ops.add_disk(vm, 2)
    disk2 = vct_ops.add_disk(vm, 5)
    disk_file = vct_ops.get_data_volume_attach_to_vm(vm)
    #sync vcenter
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vcenter).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    allvolumes = vct_ops.lib_get_vm_by_name('vm_1').allVolumes
    assert len(allvolumes) == 3
    for volume in allvolumes:
        if volume.type == 'Data':
            volumes.append(volume.installPath)
    assert set(volumes) ^ set(disk_file) == set([])

    #cleanup
    vct_ops.destroy_vm(vm)
    vct_ops.sync_vcenter(vcenter_uuid)

    test_util.test_pass("sync after adding disk to vm in vmware test passed.")
Exemplo n.º 9
0
def test():
    global test_obj_dict

    ova_image_name = os.environ['vcenterDefaultmplate']
    network_pattern1 = os.environ['l3vCenterNoVlanNetworkName']
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('largeDiskOfferingName'))
    test_util.test_dsc('Create vm and check')
    vm = test_stub.create_vm_in_vcenter(vm_name='test_volume_after_sync_vm',
                                        image_name=ova_image_name,
                                        l3_name=network_pattern1)
    test_obj_dict.add_vm(vm)
    vm.check()
    ps_uuid = vm.vm.allVolumes[0].primaryStorageUuid
    vc_ps = test_lib.lib_get_primary_storage_by_uuid(ps_uuid)
    vc_host = test_lib.lib_find_host_by_vm(vm.vm).managementIp

    test_util.test_dsc('Create volumes and check')
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume_creation_option.set_name('vcenter_volume')
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.attach(vm)
    volume.check()

    volume_creation_option.set_name('vcenter_volume1')
    volume_creation_option.set_primary_storage_uuid(ps_uuid)
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    volume1.check()

    volume.detach()
    volume.check()
    volume1.attach(vm)
    volume1.check()
    volume1.delete()
    volume1.check()

    test_util.test_dsc('Sync vcenter')
    vc_name = os.environ['vcenter']
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vc_name).uuid
    test_util.test_logger(vcenter_uuid)
    vct_ops.sync_vcenter(vcenter_uuid)

    time.sleep(5)

    test_util.test_dsc('check volumes after synchronizing vcenter')
    db_volume = test_lib.lib_get_volume_by_uuid(volume.get_volume().uuid)
    db_volume1 = test_lib.lib_get_volume_by_uuid(volume1.get_volume().uuid)
    if db_volume.status != 'Ready' or db_volume1.status != 'Deleted':
        test_util.test_fail(
            "check data volumes fail after synchronizing vcenter")

    #connect vcenter
    import ssl
    from pyVmomi import vim
    import atexit
    from pyVim import connect
    import zstackwoodpecker.zstack_test.vcenter_checker.zstack_vcenter_vm_checker as vm_checker
    vcenter_password = os.environ['vcenterpwd']
    vcenter_server = os.environ['vcenter']
    vcenter_username = os.environ['vcenteruser']
    sslContext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    sslContext.verify_mode = ssl.CERT_NONE
    SI = connect.SmartConnect(host=vcenter_server,
                              user=vcenter_username,
                              pwd=vcenter_password,
                              port=443,
                              sslContext=sslContext)
    if not SI:
        test_util.test_fail("Unable to connect to the vCenter")
    content = SI.RetrieveContent()
    volume_installPath = vc_ps.url.split(
        '//')[1] + db_volume.installPath.split('[' + vc_ps.name +
                                               ']')[1].lstrip()
    test_util.test_logger(volume_installPath)
    cmd = 'rm -f %s' % volume_installPath
    vchost_user = os.environ['vchostUser']
    vchost_password = os.environ['vchostpwd']
    result = test_lib.lib_execute_ssh_cmd(vc_host, vchost_user,
                                          vchost_password, cmd, 180)
    atexit.register(connect.Disconnect, SI)

    test_util.test_dsc('Sync vcenter')
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    db_volume = test_lib.lib_get_volume_by_uuid(volume.get_volume().uuid)
    if db_volume:
        test_util.test_fail(
            "check data volumes fail after synchronizing vcenter")

    #cleanup
    vm.destroy()
    vm.expunge()
    volume1.expunge()

    test_util.test_pass("Test sync volume in vcenter passed.")
def test():
    global test_obj_dict

    ova_image_name = os.environ['vcenterDefaultmplate']
    network_pattern1 = os.environ['l3vCenterNoVlanNetworkName']
    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('largeDiskOfferingName'))
    test_util.test_dsc('Create vm and check')
    vm = test_stub.create_vm_in_vcenter(vm_name = 'test_volume_after_sync_vm', image_name = ova_image_name, l3_name = network_pattern1)
    test_obj_dict.add_vm(vm)
    vm.check()
    ps_uuid = vm.vm.allVolumes[0].primaryStorageUuid
    vc_ps = test_lib.lib_get_primary_storage_by_uuid(ps_uuid)
    vc_host = test_lib.lib_find_host_by_vm(vm.vm).managementIp

    test_util.test_dsc('Create volumes and check')
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume_creation_option.set_name('vcenter_volume')
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.attach(vm)
    volume.check()
    volume.detach()
    volume.check()

    volume_creation_option.set_name('vcenter_volume1')
    volume_creation_option.set_primary_storage_uuid(ps_uuid)
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    volume1.check()
    volume1.attach(vm) 
    volume1.check()
    volume1.delete()
    volume1.check()

    test_util.test_dsc('Sync vcenter')
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(os.environ['vcenter']).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    test_util.test_dsc('check volumes after synchronizing vcenter')
    db_volume = test_lib.lib_get_volume_by_uuid(volume.get_volume().uuid)
    db_volume1 = test_lib.lib_get_volume_by_uuid(volume1.get_volume().uuid)
    if db_volume.status != 'Ready' or db_volume1.status != 'Deleted':
        test_util.test_fail("check data volumes fail after synchronizing vcenter")

    #delete volume file
    volume_installPath = vc_ps.url.split('//')[1] + db_volume.installPath.split('[' + vc_ps.name + ']')[1].lstrip()
    test_util.test_logger(volume_installPath)
    cmd = 'rm -f %s' %volume_installPath
    vchost_user = os.environ['vchostUser']
    vchost_password = os.environ['vchostpwd'] 
    result = test_lib.lib_execute_ssh_cmd(vc_host, vchost_user, vchost_password, cmd, 180)
    

    test_util.test_dsc('Sync vcenter')
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    db_volume = test_lib.lib_get_volume_by_uuid(volume.get_volume().uuid)
    if db_volume:
        test_util.test_fail("check data volumes fail after synchronizing vcenter")

    #cleanup
    vm.destroy()
    vm.expunge()
    volume1.expunge()

    test_util.test_pass("Test sync volume in vcenter passed.")
Exemplo n.º 11
0
def test():
    global test_obj_dict

    ova_image_name = os.environ['vcenterDefaultmplate']
    network_pattern1 = os.environ['l3vCenterNoVlanNetworkName']
    disk_offering1 = test_lib.lib_get_disk_offering_by_name(os.environ.get('largeDiskOfferingName'))

    test_util.test_dsc('Create vm and attach volume')
    vm = test_stub.create_vm_in_vcenter(vm_name = 'test_for_sync_vcenter_vm', image_name = ova_image_name, l3_name = network_pattern1)
    test_obj_dict.add_vm(vm)
    vm.check()
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering1.uuid)
    volume_creation_option.set_name('vcenter3_volume')
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.attach(vm)
    test_util.test_logger(volume.target_vm.state)
    volume.check()

    test_util.test_dsc('Destroy vm and its attached data volume both')
    vm.destroy()
    volume.delete()
    vm.check()
    volume.check()

    test_util.test_dsc('Check in vcenter after destroying vm and its attached data volume both in zstack')
    #connect vcenter
    import ssl
    from pyVmomi import vim
    import atexit
    from pyVim import connect
    import zstackwoodpecker.zstack_test.vcenter_checker.zstack_vcenter_vm_checker as vm_checker
    vcenter_password = os.environ['vcenterpwd']
    vcenter_server = os.environ['vcenter']
    vcenter_username = os.environ['vcenteruser']
    sslContext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    sslContext.verify_mode = ssl.CERT_NONE
    SI = connect.SmartConnect(host=vcenter_server, user=vcenter_username, pwd=vcenter_password, port=443, sslContext=sslContext)
    if not SI:
        test_util.test_fail("Unable to connect to the vCenter")
    content = SI.RetrieveContent()
    vc_vm = vm_checker.get_obj(content, [vim.VirtualMachine], name='test_for_sync_vcenter_vm')
    test_util.test_logger(vc_vm.summary.runtime.powerState)
    if not (vc_vm.summary.runtime.powerState == 'poweredOff'):
        test_util.test_fail("Vm should stop in vcenter")
    atexit.register(connect.Disconnect, SI)

    test_util.test_dsc('Sync vcenter')
    vc_name = os.environ['vcenter']
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vc_name).uuid
    test_util.test_logger(vcenter_uuid)
    vct_ops.sync_vcenter(vcenter_uuid)

    #After synchronization, wait for the database update
    time.sleep(5)

    test_util.test_dsc('check vm and volumes after synchronizing vcenter')
    db_volume = test_lib.lib_get_volume_by_uuid(volume.get_volume().uuid)
    test_util.test_logger(db_volume.status)
    if db_volume.status != 'Deleted':
        test_util.test_fail("check data volume fail")
    db_vm = test_lib.lib_get_vm_by_uuid(vm.vm.uuid)
    if db_vm.state == 'Destroyed':
        test_util.test_logger('check vm success')
    else:
        test_util.test_fail("check vm fail")
   
    vm.recover()
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    db_vm = test_lib.lib_get_vm_by_uuid(vm.vm.uuid)
    if db_vm.state == 'Stopped' and len(db_vm.allVolumes) == 1 and db_vm.allVolumes[0].type == 'Root' and db_vm.allVolumes[0].status == 'Ready':
        test_util.test_logger('check vm success')
    else:
        test_util.test_fail("check vm fail")

    vm.destroy()
    vm.expunge()
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    if test_lib.lib_get_vm_by_uuid(vm.vm.uuid):
        test_util.test_fail("check vm fail: vm has been expunged")
    #connect vcenter
    import ssl
    from pyVmomi import vim
    import atexit
    from pyVim import connect
    import zstackwoodpecker.zstack_test.vcenter_checker.zstack_vcenter_vm_checker as vm_checker
    vcenter_password = os.environ['vcenterpwd']
    vcenter_server = os.environ['vcenter']
    vcenter_username = os.environ['vcenteruser']
    sslContext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    sslContext.verify_mode = ssl.CERT_NONE
    SI = connect.SmartConnect(host=vcenter_server, user=vcenter_username, pwd=vcenter_password, port=443, sslContext=sslContext)
    if not SI:
        test_util.test_fail("Unable to connect to the vCenter")
    content = SI.RetrieveContent()
    vc_vm = vm_checker.get_obj(content, [vim.VirtualMachine], name='test_for_sync_vcenter_vm')
    if not isinstance(vc_vm, list):
        test_util.test_fail("check vm fail: vm has been expunged.")

    volume.expunge()

    test_util.test_pass("Sync vcenter test passed after destroying vm and its attached data volume both.")
def test():
    global test_obj_dict

    ova_image_name = os.environ['vcenterDefaultmplate']
    network_pattern1 = os.environ['l3vCenterNoVlanNetworkName']
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('largeDiskOfferingName'))
    test_util.test_dsc('Create vm and check')
    vm = test_stub.create_vm_in_vcenter(vm_name='test_volume_after_sync_vm',
                                        image_name=ova_image_name,
                                        l3_name=network_pattern1)
    test_obj_dict.add_vm(vm)
    vm.check()
    ps_uuid = vm.vm.allVolumes[0].primaryStorageUuid
    vc_ps = test_lib.lib_get_primary_storage_by_uuid(ps_uuid)
    vc_host = test_lib.lib_find_host_by_vm(vm.vm).managementIp

    test_util.test_dsc('Create volumes and check')
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume_creation_option.set_name('vcenter_volume')
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.attach(vm)
    volume.check()
    volume.detach()
    volume.check()

    volume_creation_option.set_name('vcenter_volume1')
    volume_creation_option.set_primary_storage_uuid(ps_uuid)
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    volume1.check()
    volume1.attach(vm)
    volume1.check()
    volume1.delete()
    volume1.check()

    test_util.test_dsc('Sync vcenter')
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(os.environ['vcenter']).uuid
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)

    test_util.test_dsc('check volumes after synchronizing vcenter')
    db_volume = test_lib.lib_get_volume_by_uuid(volume.get_volume().uuid)
    db_volume1 = test_lib.lib_get_volume_by_uuid(volume1.get_volume().uuid)
    if db_volume.status != 'Ready' or db_volume1.status != 'Deleted':
        test_util.test_fail(
            "check data volumes fail after synchronizing vcenter")

    #delete volume file
    volume_installPath = vc_ps.url.split(
        '//')[1] + db_volume.installPath.split('[' + vc_ps.name +
                                               ']')[1].lstrip()
    test_util.test_logger(volume_installPath)
    cmd = 'rm -f %s' % volume_installPath
    vchost_user = os.environ['vchostUser']
    vchost_password = os.environ['vchostpwd']
    result = test_lib.lib_execute_ssh_cmd(vc_host, vchost_user,
                                          vchost_password, cmd, 180)

    test_util.test_dsc('Sync vcenter')
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    db_volume = test_lib.lib_get_volume_by_uuid(volume.get_volume().uuid)
    if db_volume:
        test_util.test_fail(
            "check data volumes fail after synchronizing vcenter")

    #cleanup
    vm.destroy()
    vm.expunge()
    volume1.expunge()

    test_util.test_pass("Test sync volume in vcenter passed.")
def test():
    global test_obj_dict

    ova_image_name = os.environ['vcenterDefaultmplate']
    network_pattern1 = os.environ['l3vCenterNoVlanNetworkName']
    disk_offering1 = test_lib.lib_get_disk_offering_by_name(os.environ.get('largeDiskOfferingName'))

    test_util.test_dsc('Create vm and attach volume')
    vm = test_stub.create_vm_in_vcenter(vm_name = 'test_for_sync_vcenter_vm', image_name = ova_image_name, l3_name = network_pattern1)
    test_obj_dict.add_vm(vm)
    vm.check()
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering1.uuid)
    volume_creation_option.set_name('vcenter3_volume')
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.attach(vm)
    test_util.test_logger(volume.target_vm.state)
    volume.check()

    test_util.test_dsc('Destroy vm and its attached data volume both')
    vm.destroy()
    volume.delete()
    vm.check()
    volume.check()

    test_util.test_dsc('Sync vcenter')
    vc = os.environ.get('vcenter')
    vcenter_uuid = vct_ops.lib_get_vcenter_by_name(vc).uuid
    vct_ops.sync_vcenter(vcenter_uuid)

    #After synchronization, wait for the database update
    time.sleep(5)

    test_util.test_dsc('check vm and volumes after synchronizing vcenter')
    db_volume = test_lib.lib_get_volume_by_uuid(volume.get_volume().uuid)
    if db_volume.status != 'Deleted':
        test_util.test_fail("check data volume fail")
    db_vm = test_lib.lib_get_vm_by_uuid(vm.vm.uuid)
    if db_vm.state == 'Destroyed':
        test_util.test_logger('check vm success')
    else:
        test_util.test_fail("check vm fail")
   
    vm.recover()
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    db_vm = test_lib.lib_get_vm_by_uuid(vm.vm.uuid)
    if db_vm.state == 'Stopped' and len(db_vm.allVolumes) == 1 and db_vm.allVolumes[0].type == 'Root' and db_vm.allVolumes[0].status == 'Ready':
        test_util.test_logger('check vm success')
    else:
        test_util.test_fail("check vm fail")

    vm.destroy()
    vm.expunge()
    #check in vcenter
    SI = vct_ops.connect_vcenter(vc)
    content = SI.RetrieveContent()
    vc_vm = vct_ops.get_vm(content, name='test_for_sync_vcenter_vm')[0]
    if vc_vm.name == 'test_for_sync_vcenter_vm' :
        test_util.test_fail("check vm fail: vm has been expunged.")
    vct_ops.sync_vcenter(vcenter_uuid)
    time.sleep(5)
    if test_lib.lib_get_vm_by_uuid(vm.vm.uuid):
        test_util.test_fail("check vm fail: vm has been expunged")

    volume.expunge()

    test_util.test_pass("Sync vcenter test passed after destroying vm and its attached data volume both.")