Beispiel #1
0
def error_cleanup():
    global project_uuid, project_operator_uuid, vxlan_pool_uuid, vni_range_uuid, l2_vxlan_network_uuid, account1_uuid, account2_uuid

    if project_operator_uuid:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if project_uuid:
        iam2_ops.delete_iam2_project(project_uuid)
        iam2_ops.expunge_iam2_project(project_uuid)

    if vni_range_uuid:
        vxlan_ops.delete_vni_range(vni_range_uuid)
    vpc_ops.remove_all_vpc_vrouter()
    test_lib.lib_error_cleanup(test_obj_dict)

    if vxlan_pool_uuid:
        net_ops.delete_l2(vxlan_pool_uuid)
    if l2_vxlan_network_uuid:
        net_ops.delete_l2(l2_vxlan_network_uuid)

    if account1_uuid:
        acc_ops.delete_account(account1_uuid)
    if account2_uuid:
        acc_ops.delete_account(account2_uuid)
    if platform_admin_uuid:
        iam2_ops.delete_iam2_virtual_id(platform_admin_uuid)
def error_cleanup():
    global project_uuid,project_operator_uuid, vxlan_pool_uuid,vni_range_uuid,l2_vxlan_network_uuid,account1_uuid,account2_uuid

    if project_operator_uuid:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if project_uuid:
        iam2_ops.delete_iam2_project(project_uuid)
        iam2_ops.expunge_iam2_project(project_uuid)
        
                
    if vni_range_uuid:
        vxlan_ops.delete_vni_range(vni_range_uuid)
    vpc_ops.remove_all_vpc_vrouter()
    test_lib.lib_error_cleanup(test_obj_dict)
        
    if vxlan_pool_uuid:
        net_ops.delete_l2(vxlan_pool_uuid)
    if l2_vxlan_network_uuid:
        net_ops.delete_l2(l2_vxlan_network_uuid)
        
    if account1_uuid:
        acc_ops.delete_account(account1_uuid)
    if account2_uuid:
        acc_ops.delete_account(account2_uuid)
    if platform_admin_uuid:
        iam2_ops.delete_iam2_virtual_id(platform_admin_uuid)
def test():
    global curr_deploy_conf
    global l2_name2
    curr_deploy_conf = exp_ops.export_zstack_deployment_config(test_lib.deploy_config)

    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_s')
    image_uuid = test_lib.lib_get_image_by_name(image_name).uuid
    #pick up l3
    l3_1 = res_ops.get_resource(res_ops.L3_NETWORK, name = l3_name1)[0]
    l3_2 = res_ops.get_resource(res_ops.L3_NETWORK, name = l3_name2)[0]
    
    l2_2 = res_ops.get_resource(res_ops.L2_NETWORK, \
            uuid = l3_2.l2NetworkUuid)[0]
    l2_name2 = l2_2.name

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, conditions)[0].uuid
    vm_creation_option.set_image_uuid(image_uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('multizones_basic_vm')
    vm_creation_option.set_l3_uuids([l3_1.uuid, l3_2.uuid])
    cluster1_name = os.environ.get('clusterName2')
    cluster1 = res_ops.get_resource(res_ops.CLUSTER, name = cluster1_name)[0]
    vm_creation_option.set_cluster_uuid(cluster1.uuid)

    vm1 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm1)

    test_util.test_dsc('Delete l2_2')
    net_ops.delete_l2(l2_2.uuid)

    test_obj_dict.mv_vm(vm1, vm_header.RUNNING, vm_header.STOPPED)
    vm1.update()
    vm1.set_state(vm_header.STOPPED)

    vm1.check()

    test_util.test_dsc('start vm again. vm should remove the deleted l2')
    vm1.start()

    net_ops.add_l2_resource(curr_deploy_conf, l2_name = l2_name2)

    #update l3_2, since it is readded.
    l3_2 = res_ops.get_resource(res_ops.L3_NETWORK, name = l3_name2)[0]
    vm_creation_option.set_l3_uuids([l3_1.uuid, l3_2.uuid])

    vm2 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm2)

    #check vm1 vm2 status.
    vm1.check()

    if not len(vm1.get_vm().vmNics) == 1:
        test_util.test_fail('vm1 vmNics still have L3: %s, even if it is deleted' % l3_2.uuid)

    vm2.check()

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Delete L2 Test Success')
def error_cleanup():
    global vm, test_account_uuid, test_account_session, vip, eip, l2_uuid, l2_uuid
    if vm:
        vm.destroy(test_account_session)
    acc_ops.delete_account(test_account_uuid)
    vip.delete()
    net_ops.delete_eip(eip.eip.uuid, session_uuid = test_account_session)
    net_ops.delete_l2(l2_uuid)
    net_ops.delete_l2(l2_uuid2)
def error_cleanup():
    if project_uuid:
        iam2_ops.delete_iam2_project(project_uuid)
        iam2_ops.expunge_iam2_project(project_uuid)
    if project_operator_uuid:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if l2_vxlan_network_uuid:
        net_ops.delete_l2(l2_vxlan_network_uuid)
    if vni_range_uuid:
        vxlan_ops.delete_vni_range(vni_range_uuid)
    if vxlan_pool_uuid:
        net_ops.delete_l2(vxlan_pool_uuid)
    if l3_vpc_network_uuid:
        net_ops.delete_l3(l3_vpc_network_uuid)
def error_cleanup():
    if project_uuid:
        iam2_ops.delete_iam2_project(project_uuid)
        iam2_ops.expunge_iam2_project(project_uuid)
    if project_operator_uuid:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if l2_vxlan_network_uuid:
            net_ops.delete_l2(l2_vxlan_network_uuid)
    if vni_range_uuid:
        vxlan_ops.delete_vni_range(vni_range_uuid)
    if vxlan_pool_uuid:
        net_ops.delete_l2(vxlan_pool_uuid)
    if l3_vr_network_uuid:
        net_ops.delete_l3(l3_vr_network_uuid)
def error_cleanup():
    global project_uuid,project_operator_uuid, test_account_uuid, l2_uuid, l2_uuid1

    if project_operator_uuid:
        iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    if project_uuid:
        iam2_ops.delete_iam2_project(project_uuid)
        iam2_ops.expunge_iam2_project(project_uuid)
    test_lib.lib_error_cleanup(test_obj_dict)
    if l2_uuid:
        net_ops.delete_l2(l2_uuid)
    if l2_uuid1:
        net_ops.delete_l2(l2_uuid1)
    if test_account_uuid:
        acc_ops.delete_account(test_account_uuid)
def test():

    # create l3 network
    global l3, l2

    name = 'mini_network_test'
    test_util.test_dsc('create L2_vlan network mini_l2_network_test')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.query_resource(res_ops.CLUSTER)[0].uuid
    l2 = net_ops.create_l2_vlan('l2_vlan', 'zsn0', zone_uuid, '1998')
    l2_uuid = l2.inventory.uuid
    net_ops.attach_l2(l2_uuid, cluster_uuid)

    type='L3BasicNetwork'
    categry='Private'
    l3=net_ops.create_l3(name, l2_uuid, category=categry, Type=type)

    # add dns to l3 network
    l3_dns = '223.5.5.5'
    net_ops.add_dns_to_l3(l3.uuid, l3_dns)
    test_util.test_dsc('add DNS and IP_Range for L3_flat_network')

    # add ip range to l3 network
    ip_range_option = test_util.IpRangeOption()
    ip_range_option.set_l3_uuid(l3.uuid)
    ip_range_option.set_startIp('192.168.40.2')
    ip_range_option.set_endIp('192.168.40.20')
    ip_range_option.set_gateway('192.168.40.1')
    ip_range_option.set_netmask('255.255.255.0')
    ip_range_option.set_name('ip_range_test')

    net_ops.add_ip_range(ip_range_option)

    #attach network service to l3 network
    cond = res_ops.gen_query_conditions('type', '=', 'flat')
    provider1_uuid = res_ops.query_resource(res_ops.NETWORK_SERVICE_PROVIDER, cond)[0].uuid
    cond = res_ops.gen_query_conditions('type', '=', 'SecurityGroup')
    provider2_uuid = res_ops.query_resource(res_ops.NETWORK_SERVICE_PROVIDER, cond)[0].uuid
    providers = {provider1_uuid:['DHCP','Eip'], provider2_uuid:['SecurityGroup']}
    attach_network_service_to_l3network(l3.uuid, providers)
    test_util.test_dsc('a network with dns, ip range and network services has been created successfully')
    net_ops.delete_l3(l3.uuid)
    net_ops.delete_l2(l2_uuid)
    test_util.test_dsc('delete l3 network after test')
def test():
    global project_uuid, project_admin_uuid, virtual_id_uuid, project_operator_uuid, plain_user_uuid

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

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

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

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

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

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

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid

    try:
        l2 = net_ops.create_l2_novlan('l2_for_pm', 'eth0', zone_uuid, session_uuid=project_login_uuid)
        test_util.test_fail("Expect exception: project admin not allowed to create Novlan L2 except vxlan")
    except:
        pass

    try:
        l2 = net_ops.create_l2_vlan('l2_for_pm', 'eth0', zone_uuid, 1234, session_uuid=project_login_uuid)
        test_util.test_fail("Expect exception: project admin not allowed to create vlan L2 except vxlan")
    except:
        pass
    if flavor['target_role'] == 'project_member':
        statements = [{"effect": "Allow", "actions": ["org.zstack.network.l2.**"]}]
        role_uuid = iam2_ops.create_role('test_role', statements).uuid
        iam2_ops.add_roles_to_iam2_virtual_id([role_uuid], plain_user_uuid)

    vxlan_pool = res_ops.get_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    if vxlan_pool == None or len(vxlan_pool) == 0:
        test_util.test_skip('Vxlan is required for testing')
    vxlan_pool_uuid = vxlan_pool[0].uuid
    acc_ops.share_resources([project_linked_account_uuid], [vxlan_pool_uuid])
    vxlan_pool_uuid = res_ops.get_resource(res_ops.L2_VXLAN_NETWORK_POOL, session_uuid=project_login_uuid)[0].uuid
    vxlan_l2_uuid = vxlan_ops.create_l2_vxlan_network('vxlan_for_pm', vxlan_pool_uuid, zone_uuid, session_uuid=project_login_uuid).uuid
    net_ops.delete_l2(vxlan_l2_uuid, session_uuid=project_login_uuid)

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

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

    test_util.test_pass('success test iam2 login in by project admin!')
def test():
    global l2_vxlan_network_uuid,project_uuid,project_operator_uuid,vni_range_uuid,vxlan_pool_uuid,l3_vr_network_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(vxlan_pool_name,zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range',20,40,vxlan_pool_uuid)

    systemTags = ["l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}"%(vxlan_pool_uuid,cluster_uuid)]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid,cluster_uuid,systemTags)

    # 1 create project
    project_name = 'test_project7'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    #cond = res_ops.gen_query_conditions("name",'=',"test_project7")
    #linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT,cond)[0].uuid
    linked_account_uuid = project.linkedAccountUuid

    # 2 create project operator
    project_operator_name = 'username7'
    project_operator_password = '******'
    attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
    project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid
    
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    # 3 login in project by project operator
    iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
    project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
    project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid

    # 4 share vxlan pool to project
    l2vxlan_pools = res_ops.get_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    for l2vxlan_pool in l2vxlan_pools:
        acc_ops.share_resources([linked_account_uuid],[l2vxlan_pool.uuid])
    # 5 create l2 vxlan 
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network('l2_vxlan',vxlan_pool_uuid,zone_uuid,session_uuid=project_login_uuid).uuid
    
    
    # 6 use the l2 vxlan to create l3
    # 6.1 create l3_vr_network
    l3_vr_network_uuid = net_ops.create_l3('l3_vr_network',l2_vxlan_network_uuid,session_uuid=project_login_uuid).uuid
    
    cond = res_ops.gen_query_conditions('name','=','virtual-router-vm')
    vr_instance_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,cond)[0].uuid
    tag = "guestL3Network::" + l3_vr_network_uuid
    tag_ops.create_system_tag("InstanceOfferingVO",vr_instance_uuid,tag)
    # add ip range
    ir_option = test_util.IpRangeOption()
    ir_option.set_name('iprange1')
    ir_option.set_description('iprange for vrouter network')
    ir_option.set_netmask('255.255.255.0')
    ir_option.set_gateway('192.168.22.1')
    ir_option.set_l3_uuid(l3_vr_network_uuid)
    ir_option.set_startIp('192.168.22.2')
    ir_option.set_endIp('192.168.22.254')
    
    net_ops.add_ip_range(ir_option)
    # add dns
    AddDnsToL3Network(l3_vr_network_uuid,dns_text,session_uuid = project_login_uuid)
    # add network service
    AttachNetworkServiceToL3Network(l3_vr_network_uuid,allservices,session_uuid = project_login_uuid)
    
    # create vrouter vm 
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])
    vr_vm = test_stub.create_vr_vm(test_obj_dict, l3_vr_network_uuid,session_uuid=project_login_uuid)
    test_stub.check_resource_not_exist(vr_vm.uuid,res_ops.VM_INSTANCE)
    
    #6 expunge the project and check the l2 vxlan
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    
    try:
        l2_vxlan_network_test_uuid = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK)[0].uuid
    except: 
        
        test_util.test_pass(
            "l2 vxlan  is delete after delete the project " )
    test_util.test_dsc('test l2 l2 cascade delete')

    # 7 check the l3 vrouter network
    try:
        cond = res_ops.gen_query_conditions("name",'=',"l3_vr_network")
        l3_vrouter_network_uuid = res_ops.query_resource(res_ops.L3_NETWORK,cond)[0].uuid
    except:
        
        test_util.test_pass(
            "l3_vrouter network  is deleted after delete the project")
    
    # todo:check the virtual router vm 
    


    # 8 delete 
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    net_ops.delete_l2(vxlan_pool_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
def test():
    global vm, session_uuid
    global test_account_uuid, test_account_session
    
    test_util.test_dsc('Test normal account change the qos network and volume ')

    #create normal account
    test_util.test_dsc('create normal account')
    account_name = 'test_a'
    account_pass = hashlib.sha512(account_name).hexdigest()
    test_account = acc_ops.create_normal_account(account_name, account_pass)
    test_account_uuid = test_account.uuid
    test_account_session = acc_ops.login_by_account(account_name, account_pass)

    #create L3 flat network
    test_util.test_dsc('create L2_vlan network  names is L2_vlan')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.query_resource(res_ops.CLUSTER)[0].uuid
    
    l2_inv = sce_ops.create_l2_vlan(zstack_management_ip, 'L2_vlan_2215', 'eth0', '2215', zone_uuid)
    l2_uuid = l2_inv.inventory.uuid

    test_util.test_dsc('attach L2 netowrk to cluster')
    sce_ops.attach_l2(zstack_management_ip, l2_uuid, cluster_uuid)

    #share admin resoure to normal account
    test_util.test_dsc('share admin resoure to normal account')
    cond = res_ops.gen_query_conditions('name', '=', 'L2_vlan_2215')
    flat_l2_uuid = res_ops.query_resource(res_ops.L2_NETWORK, cond)[0].uuid
    acc_ops.share_resources([test_account_uuid], [flat_l2_uuid])

    test_util.test_dsc('create L3_flat_network names is L3_flat_network by normal account')
    l3_inv = sce_ops.create_l3(zstack_management_ip, 'l3_flat_network', 'L3BasicNetwork', l2_uuid, 'local.com', session_uuid = test_account_session)
    l3_uuid = l3_inv.inventory.uuid

    l3_dns = '223.5.5.5'
    start_ip = '192.168.129.2'
    end_ip = '192.168.129.10'
    gateway = '192.168.129.1'
    netmask = '255.255.255.0'

    test_util.test_dsc('add DNS and IP_Range for L3_flat_network')
    sce_ops.add_dns_to_l3(zstack_management_ip, l3_uuid, l3_dns, session_uuid = test_account_session)
    sce_ops.add_ip_range(zstack_management_ip,'IP_range', l3_uuid, start_ip, end_ip, gateway, netmask, session_uuid = test_account_session)

    test_util.test_dsc('query flat provider and attach network service to  L3_flat_network')
    provider_name = 'Flat Network Service Provider'
    conditions = res_ops.gen_query_conditions('name', '=', provider_name)
    net_provider_list = sce_ops.query_resource(zstack_management_ip, res_ops.NETWORK_SERVICE_PROVIDER, conditions, session_uuid = test_account_session).inventories[0]
    pro_uuid = net_provider_list.uuid
    sce_ops.attach_flat_network_service_to_l3network(zstack_management_ip, l3_uuid, pro_uuid, session_uuid = test_account_session)

    instance_offerings = res_ops.get_resource(res_ops.INSTANCE_OFFERING)
    for instance_offering in instance_offerings:
        acc_ops.share_resources([test_account_uuid], [instance_offering.uuid])

    #acc_ops.share_resources([test_account_uuid], [instance_offering_uuid])
    cond = res_ops.gen_query_conditions('mediaType', '!=', 'ISO')
    images = res_ops.query_resource(res_ops.IMAGE, cond)
    for image in images:
        acc_ops.share_resources([test_account_uuid], [image.uuid])

    #create vm
    test_util.test_dsc('create vm by normal account test_a')
    vm = test_stub.create_vm(session_uuid = test_account_session)
    
    vm.check()
    vm.destroy(test_account_session)
    net_ops.delete_l2(l2_uuid)
    vm.check()
    acc_ops.delete_account(test_account_uuid)    
    test_util.test_pass('normal account create eip by admin shared vip Test Success')
def error_cleanup():
    global vm, test_account_uuid, test_account_session, l2_uuid 
    if vm:
        vm.destroy(test_account_session)
    acc_ops.delete_account(test_account_uuid)
    net_ops.delete_l2(l2_uuid)
Beispiel #13
0
def test():
    global linked_account_uuid, project_uuid, project_operator_uuid, account_lists, vni_range_uuid, vxlan_pool_uuid, l2_vxlan_network_uuid, account1_uuid, account2_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(
        vxlan_pool_name, zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range', 20, 40, vxlan_pool_uuid)

    systemTags = [
        "l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}" %
        (vxlan_pool_uuid, cluster_uuid)
    ]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid, cluster_uuid, systemTags)

    # 1 create project
    project_name = 'test_share_project1'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    cond = res_ops.gen_query_conditions("name", '=', "test_share_project1")
    linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT, cond)[0].uuid

    # 2 create project operator
    project_operator_name = 'share_username1'
    project_operator_password = '******'
    attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
    project_operator_uuid = iam2_ops.create_iam2_virtual_id(
        project_operator_name,
        project_operator_password,
        attributes=attributes).uuid
    # 3 login in project by project operator
    iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],
                                             project_uuid)
    project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(
        project_operator_name, project_operator_password)
    project_login_uuid = iam2_ops.login_iam2_project(
        project_name, session_uuid=project_operator_session_uuid).uuid
    # todo:use the shared resources

    # 4 share admin resources to project
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])
    # use the shared resources to create vm
    vm = test_stub.create_vm(session_uuid=project_login_uuid)
    volume = test_stub.create_volume(session_uuid=project_login_uuid)
    test_obj_dict.add_volume(volume)
    test_obj_dict.add_vm(vm)
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network(
        'l2_vxlan',
        vxlan_pool_uuid,
        zone_uuid,
        session_uuid=project_login_uuid).uuid
    virtual_router_offering_uuid = res_ops.get_resource(
        res_ops.VR_OFFERING)[0].uuid
    vpc_ops.create_vpc_vrouter('vpc_router',
                               virtual_router_offering_uuid,
                               session_uuid=project_login_uuid)

    # 5 revoke admin resources from project
    test_stub.revoke_admin_resource([linked_account_uuid])

    # 6 share to all
    #create_account
    account1_uuid = acc_ops.create_account('user1', 'password', 'Normal').uuid
    account2_uuid = acc_ops.create_account('user2', 'password', 'Normal').uuid

    account_lists = res_ops.query_resource(res_ops.ACCOUNT)
    for account in account_lists:
        test_stub.share_admin_resource_include_vxlan_pool([account.uuid])

    # 7 revoke resources from all
    for account in account_lists:
        test_stub.revoke_admin_resource([account.uuid])

    # 8 Negative test
    test_util.test_dsc(
        'Doing negative test.Try to use the resources not shared to create vm')
    try:
        test_stub.create_vm(session_uuid=project_login_uuid)
    except:
        test_util.test_logger(
            'Catch excepted excepttion.can not use the resources not shared to create vm'
        )
    else:
        test_util.test_fail(
            'Catch wrong logic:create vm success with the resources not shared '
        )

    test_util.test_dsc(
        'Doing negative test.Try to use the resources not shared to create volume'
    )
    try:
        test_stub.create_volume(session_uuid=project_login_uuid)
    except:
        test_util.test_logger(
            'Catch excepted excepttion.can not use the resources not shared to create volume'
        )
    else:
        test_util.test_fail(
            'Catch wrong logic:create volume success with the resources not shared '
        )

    test_util.test_dsc(
        'Doing negative test.Try to use the resources not shared to create vxlan network'
    )
    try:
        vxlan_ops.create_l2_vxlan_network('l2_vxlan',
                                          vxlan_pool_uuid,
                                          zone_uuid,
                                          session_uuid=project_login_uuid)
    except:
        test_util.test_logger(
            'Catch excepted excepttion.can not use the resources not shared to create l2 vxlan'
        )
    else:
        test_util.test_fail(
            'Catch wrong logic:create l2 vxlan success with the resources not shared '
        )

    test_util.test_dsc(
        'Doing negative test.Try to use the resources not shared to create vpc_vrouter '
    )
    try:
        vpc_ops.create_vpc_vrouter('vpc_router',
                                   virtual_router_offerings,
                                   session_uuid=project_login_uuid)
    except:
        test_util.test_logger(
            'Catch excepted excepttion.can not use the resources not shared to create vpc_router'
        )
    else:
        test_util.test_fail(
            'Catch wrong logic:create vpc_router success with the resources not shared '
        )

    # 9 delete
    acc_ops.logout(project_login_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    vpc_ops.remove_all_vpc_vrouter()
    test_lib.lib_error_cleanup(test_obj_dict)

    net_ops.delete_l2(vxlan_pool_uuid)
    net_ops.delete_l2(l2_vxlan_network_uuid)

    acc_ops.delete_account(account1_uuid)
    acc_ops.delete_account(account2_uuid)
def test():
    global linked_account_uuid,project_uuid,project_operator_uuid,account_lists,vni_range_uuid,vxlan_pool_uuid,l2_vxlan_network_uuid,account1_uuid,account2_uuid,platform_admin_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(vxlan_pool_name,zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range',20,40,vxlan_pool_uuid)
        
    systemTags = ["l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}"%(vxlan_pool_uuid,cluster_uuid)]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid,cluster_uuid,systemTags)
        
    # 1 create project
    project_name = 'test_share_project1'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    #cond = res_ops.gen_query_conditions("name",'=',"test_share_project1")
    #linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT,cond)[0].uuid
    linked_account_uuid = project.linkedAccountUuid

    # 2 create project operator
    project_operator_name = 'share_username1'
    project_operator_password = '******'
    attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
    project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid

    # 3 login in project by project operator
    iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
    project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
    project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid
    # todo:use the shared resources
        
    # 4 create platformAdmin and login
    username = '******'
    password = '******'
    platform_admin_uuid = iam2_ops.create_iam2_virtual_id(username, password).uuid
    attributes = [{"name":"__PlatformAdmin__"}]
    iam2_ops.add_attributes_to_iam2_virtual_id(platform_admin_uuid, attributes)
    platform_admin_session_uuid = iam2_ops.login_iam2_virtual_id(username, password)

        
    # 5 share platform admin resources to project 
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid],platform_admin_session_uuid)
    # use the shared resources to create vm
    vm = test_stub.create_vm(session_uuid=project_login_uuid)
    volume = test_stub.create_volume(session_uuid=project_login_uuid)
    test_obj_dict.add_volume(volume)
    test_obj_dict.add_vm(vm)
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network('l2_vxlan',vxlan_pool_uuid,zone_uuid,session_uuid=project_login_uuid).uuid
    virtual_router_offering_uuid = res_ops.get_resource(res_ops.VR_OFFERING)[0].uuid
    vpc_ops.create_vpc_vrouter('vpc_router',virtual_router_offering_uuid,session_uuid=project_login_uuid)
        
        
    # 6 revoke platform admin resources from project
    test_stub.revoke_admin_resource([linked_account_uuid],platform_admin_session_uuid)
        
       
        
    # 7 share to all
    #create_account
    account1_uuid = acc_ops.create_account('user1','password','Normal').uuid
    account2_uuid = acc_ops.create_account('user2','password','Normal').uuid
        
    account_lists = res_ops.query_resource(res_ops.ACCOUNT)
    for account in account_lists:
        test_stub.share_admin_resource_include_vxlan_pool([account.uuid],platform_admin_session_uuid)
        
            
    # 8 revoke resources from all
    for account in account_lists:
        test_stub.revoke_admin_resource([account.uuid],platform_admin_session_uuid)
        
        
            
    # 9 Negative test
    test_util.test_dsc('Doing negative test.Try to use the resources not shared to create vm')
    try:
        test_stub.create_vm(session_uuid=project_login_uuid)
    except:
        test_util.test_logger('Catch excepted excepttion.can not use the resources not shared to create vm')
    else:
        test_util.test_fail('Catch wrong logic:create vm success with the resources not shared ')
        
    test_util.test_dsc('Doing negative test.Try to use the resources not shared to create volume')
    try:
        test_stub.create_volume(session_uuid=project_login_uuid)
    except:
        test_util.test_logger('Catch excepted excepttion.can not use the resources not shared to create volume')
    else:
        test_util.test_fail('Catch wrong logic:create volume success with the resources not shared ')
    
    test_util.test_dsc('Doing negative test.Try to use the resources not shared to create vxlan network')
    try:
        vxlan_ops.create_l2_vxlan_network('l2_vxlan',vxlan_pool_uuid,zone_uuid,session_uuid=project_login_uuid)
    except:
        test_util.test_logger('Catch excepted excepttion.can not use the resources not shared to create l2 vxlan')
    else:
        test_util.test_fail('Catch wrong logic:create l2 vxlan success with the resources not shared ')
    
    test_util.test_dsc('Doing negative test.Try to use the resources not shared to create vpc_vrouter ')    
    try:
        vpc_ops.create_vpc_vrouter('vpc_router',virtual_router_offerings,session_uuid=project_login_uuid)
    except: 
        test_util.test_logger('Catch excepted excepttion.can not use the resources not shared to create vpc_router')
    else:
        test_util.test_fail('Catch wrong logic:create vpc_router success with the resources not shared ')
            
            
    # 10 delete
    acc_ops.logout(project_login_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    vpc_ops.remove_all_vpc_vrouter()
    test_lib.lib_error_cleanup(test_obj_dict)
        
        
    net_ops.delete_l2(vxlan_pool_uuid)
    net_ops.delete_l2(l2_vxlan_network_uuid)
        
        
    acc_ops.delete_account(account1_uuid)
    acc_ops.delete_account(account2_uuid)
    iam2_ops.delete_iam2_virtual_id(platform_admin_uuid)
Beispiel #15
0
def test():
    global curr_deploy_conf
    global l2_name2
    curr_deploy_conf = exp_ops.export_zstack_deployment_config(
        test_lib.deploy_config)

    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_s')
    image_uuid = test_lib.lib_get_image_by_name(image_name).uuid
    #pick up l3
    l3_1 = res_ops.get_resource(res_ops.L3_NETWORK, name=l3_name1)[0]
    l3_2 = res_ops.get_resource(res_ops.L3_NETWORK, name=l3_name2)[0]

    l2_2 = res_ops.get_resource(res_ops.L2_NETWORK, \
            uuid = l3_2.l2NetworkUuid)[0]
    l2_name2 = l2_2.name

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                                    conditions)[0].uuid
    vm_creation_option.set_image_uuid(image_uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('multizones_basic_vm')
    vm_creation_option.set_l3_uuids([l3_1.uuid, l3_2.uuid])
    cluster1_name = os.environ.get('clusterName2')
    cluster1 = res_ops.get_resource(res_ops.CLUSTER, name=cluster1_name)[0]
    vm_creation_option.set_cluster_uuid(cluster1.uuid)

    vm1 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm1)

    test_util.test_dsc('Delete l2_2')
    net_ops.delete_l2(l2_2.uuid)

    test_obj_dict.mv_vm(vm1, vm_header.RUNNING, vm_header.STOPPED)
    vm1.update()
    vm1.set_state(vm_header.STOPPED)

    vm1.check()

    test_util.test_dsc('start vm again. vm should remove the deleted l2')
    vm1.start()

    net_ops.add_l2_resource(curr_deploy_conf, l2_name=l2_name2)

    #update l3_2, since it is readded.
    l3_2 = res_ops.get_resource(res_ops.L3_NETWORK, name=l3_name2)[0]
    vm_creation_option.set_l3_uuids([l3_1.uuid, l3_2.uuid])

    vm2 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm2)

    #check vm1 vm2 status.
    vm1.check()

    if not len(vm1.get_vm().vmNics) == 1:
        test_util.test_fail(
            'vm1 vmNics still have L3: %s, even if it is deleted' % l3_2.uuid)

    vm2.check()

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Delete L2 Test Success')
def test():
    global project_uuid, project_admin_uuid, virtual_id_uuid, project_operator_uuid, plain_user_uuid

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

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

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

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

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

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

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid

    try:
        l2 = net_ops.create_l2_novlan('l2_for_pm', 'eth0', zone_uuid, session_uuid=project_login_uuid)
        test_util.test_fail("Expect exception: project admin not allowed to create Novlan L2 except vxlan")
    except:
        pass

    try:
        l2 = net_ops.create_l2_vlan('l2_for_pm', 'eth0', zone_uuid, 1234, session_uuid=project_login_uuid)
        test_util.test_fail("Expect exception: project admin not allowed to create vlan L2 except vxlan")
    except:
        pass
    if flavor['target_role'] == 'project_member':
        statements = [{"effect": "Allow", "actions": ["org.zstack.network.l2.**"]}]
        role_uuid = iam2_ops.create_role('test_role', statements).uuid
        iam2_ops.add_roles_to_iam2_virtual_id([role_uuid], plain_user_uuid)

    vxlan_pool = res_ops.get_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    if vxlan_pool == None or len(vxlan_pool) == 0:
        test_util.test_skip('Vxlan is required for testing')
    vxlan_pool_uuid = vxlan_pool[0].uuid
    if flavor['target_role'] != 'system_admin':
        acc_ops.share_resources([project_linked_account_uuid], [vxlan_pool_uuid])
    vxlan_pool_uuid = res_ops.get_resource(res_ops.L2_VXLAN_NETWORK_POOL, session_uuid=project_login_uuid)[0].uuid
    vxlan_l2_uuid = vxlan_ops.create_l2_vxlan_network('vxlan_for_pm', vxlan_pool_uuid, zone_uuid, session_uuid=project_login_uuid).uuid
    net_ops.delete_l2(vxlan_l2_uuid, session_uuid=project_login_uuid)

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

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

    test_util.test_pass('success test iam2 login in by project admin!')
def test():
    global l2_vxlan_network_uuid, project_uuid, project_operator_uuid, vni_range_uuid, vxlan_pool_uuid, l3_vr_network_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(
        vxlan_pool_name, zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range', 20, 40, vxlan_pool_uuid)

    systemTags = [
        "l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}" %
        (vxlan_pool_uuid, cluster_uuid)
    ]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid, cluster_uuid, systemTags)

    # 1 create project
    project_name = 'test_project7'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    cond = res_ops.gen_query_conditions("name", '=', "test_project7")
    linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT, cond)[0].uuid

    # 2 create project operator
    project_operator_name = 'username7'
    project_operator_password = '******'
    attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
    project_operator_uuid = iam2_ops.create_iam2_virtual_id(
        project_operator_name,
        project_operator_password,
        attributes=attributes).uuid

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    # 3 login in project by project operator
    iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],
                                             project_uuid)
    project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(
        project_operator_name, project_operator_password)
    project_login_uuid = iam2_ops.login_iam2_project(
        project_name, session_uuid=project_operator_session_uuid).uuid

    # 4 share vxlan pool to project
    l2vxlan_pools = res_ops.get_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    for l2vxlan_pool in l2vxlan_pools:
        acc_ops.share_resources([linked_account_uuid], [l2vxlan_pool.uuid])
    # 5 create l2 vxlan
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network(
        'l2_vxlan',
        vxlan_pool_uuid,
        zone_uuid,
        session_uuid=project_login_uuid).uuid

    # 6 use the l2 vxlan to create l3
    # 6.1 create l3_vr_network
    l3_vr_network_uuid = net_ops.create_l3(
        'l3_vr_network',
        l2_vxlan_network_uuid,
        session_uuid=project_login_uuid).uuid

    cond = res_ops.gen_query_conditions('name', '=', 'virtual-router-vm')
    vr_instance_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                              cond)[0].uuid
    tag = "guestL3Network::" + l3_vr_network_uuid
    tag_ops.create_system_tag("InstanceOfferingVO", vr_instance_uuid, tag)
    # add ip range
    ir_option = test_util.IpRangeOption()
    ir_option.set_name('iprange1')
    ir_option.set_description('iprange for vrouter network')
    ir_option.set_netmask('255.255.255.0')
    ir_option.set_gateway('192.168.22.1')
    ir_option.set_l3_uuid(l3_vr_network_uuid)
    ir_option.set_startIp('192.168.22.2')
    ir_option.set_endIp('192.168.22.254')

    net_ops.add_ip_range(ir_option)
    # add dns
    AddDnsToL3Network(l3_vr_network_uuid,
                      dns_text,
                      session_uuid=project_login_uuid)
    # add network service
    AttachNetworkServiceToL3Network(l3_vr_network_uuid,
                                    allservices,
                                    session_uuid=project_login_uuid)

    # create vrouter vm
    test_stub.share_admin_resource_include_vxlan_pool([linked_account_uuid])
    vr_vm = test_stub.create_vr_vm(test_obj_dict,
                                   l3_vr_network_uuid,
                                   session_uuid=project_login_uuid)
    test_stub.check_resource_not_exist(vr_vm.uuid, res_ops.VM_INSTANCE)

    #6 expunge the project and check the l2 vxlan
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)

    try:
        l2_vxlan_network_test_uuid = res_ops.query_resource(
            res_ops.L2_VXLAN_NETWORK)[0].uuid
    except:

        test_util.test_pass("l2 vxlan  is delete after delete the project ")
    test_util.test_dsc('test l2 l2 cascade delete')

    # 7 check the l3 vrouter network
    try:
        cond = res_ops.gen_query_conditions("name", '=', "l3_vr_network")
        l3_vrouter_network_uuid = res_ops.query_resource(
            res_ops.L3_NETWORK, cond)[0].uuid
    except:

        test_util.test_pass(
            "l3_vrouter network  is deleted after delete the project")

    # todo:check the virtual router vm

    # 8 delete
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    net_ops.delete_l2(vxlan_pool_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
def test():
    global l2_vxlan_network_uuid,project_uuid,project_operator_uuid,vni_range_uuid,vxlan_pool_uuid,l3_vpc_network_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(vxlan_pool_name,zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range',20,40,vxlan_pool_uuid)

    systemTags = ["l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}"%(vxlan_pool_uuid,cluster_uuid)]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid,cluster_uuid,systemTags)

    # 1 create project
    project_name = 'test_project7'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    #cond = res_ops.gen_query_conditions("name",'=',"test_project7")
    #linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT,cond)[0].uuid
    linked_account_uuid = project.linkedAccountUuid

    # 2 create project operator
    project_operator_name = 'username7'
    project_operator_password = '******'
    attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
    project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid
    
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    # 3 login in project by project operator
    iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
    project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
    project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid

    # 4 share vxlan pool to project
    l2vxlan_pools = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    for l2vxlan_pool in l2vxlan_pools:
        acc_ops.share_resources([linked_account_uuid],[l2vxlan_pool.uuid])
    # 5 create l2 vxlan 
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network('l2_vxlan',vxlan_pool_uuid,zone_uuid,session_uuid=project_login_uuid).uuid
    
    # 6 use l2 vxlan to create l3 vpc 
    l3_vpc_network = create_l3_vpc('test_vpc',l2_vxlan_network_uuid,project_login_uuid)
    
    l3_vpc_network_uuid = l3_vpc_network.uuid
    # add ip range
    ir_option = test_util.IpRangeOption()
    ir_option.set_name('iprange2')
    ir_option.set_description('iprange for vpc')
    ir_option.set_netmask('255.255.255.0')
    ir_option.set_gateway('192.168.23.1')
    ir_option.set_l3_uuid(l3_vpc_network_uuid)
    ir_option.set_startIp('192.168.23.2')
    ir_option.set_endIp('192.168.23.254')
    
    net_ops.add_ip_range(ir_option)
     
    # add network service
    AttachNetworkServiceToL3Network(l3_vpc_network_uuid,allservices,session_uuid = project_login_uuid)
    
    # share the vr_offering to project and do create vpc router and vpc network
    cond = res_ops.gen_query_conditions("name",'=',"virtual-router-vm")
    vr_offering_uuid = res_ops.query_resource(res_ops.VR_OFFERING,cond)[0].uuid
    acc_ops.share_resources([linked_account_uuid],[vr_offering_uuid])
    vpc_ops.create_vpc_vrouter(name = 'test_vpc_vr', virtualrouter_offering_uuid = vr_offering_uuid,session_uuid = project_login_uuid)
    vpc_vr = test_stub.query_vpc_vrouter('test_vpc_vr')
    vpc_vr.add_nic(l3_vpc_network_uuid)
    
    # 7 expunge the project and check the l2 vxlan
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    try:
        l2_vxlan_network_test_uuid = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK)[0].uuid
    except: 
        
        test_util.test_pass(
            "l2 vxlan  is delete after deleted the project " )
    test_util.test_dsc('test l2 l2 cascade delete')
    
    # 8 check the vpc network and vpc_vr
    try:
        cond = res_ops.gen_query_conditions("name",'=',"test_vpc")
        l3_vpc_network_uuid = res_ops.query_resource(res_ops.L3_NETWORK,cond)[0].uuid
    except:
        
        test_util.test_pass(
            "l3_vpc  is delete after deleted the project")
    
   
    cond = res_ops.gen_query_conditions("name",'=',"test_vpc_vr")
    vpc_vr = res_ops.query_resource(res_ops.VIRTUALROUTER_VM,cond)
    
    if not vpc_vr.inv.state is 'Paused':
        test_util.test_fail(
            "vpc vr [%s] is still exist after delete and expunge the project [%s]" % (vpc_vr.uuid,project_uuid))
   
    # 9 delete 
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    net_ops.delete_l2(vxlan_pool_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
def error_cleanup():
    global l3, l2
    if l2:
        net_ops.delete_l2(l2.inventory.uuid)
    if l3:
        net_ops.delete_l3(l3.uuid)
def test():
    global linked_account_uuid,project_uuid,project_operator_uuid,account_lists,l2_uuid,account1_uuid,account2_uuid

    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    # 1 create project
    project_name = 'test_shared_project1'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    linked_account_uuid = project.linkedAccountUuid

    # 2 create project operator
    project_operator_name = 'username_share1'
    project_operator_password = '******'
    attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
    project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid
    # 3 login in project by project operator
    iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
    project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
    project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid

    l2_inv = net_ops.create_l2_vlan('L2_vlan_2221', 'eth0', zone_uuid, '2221')   
    l2_uuid = l2_inv.inventory.uuid

    test_util.test_dsc('share admin resoure to normal account')
    cond = res_ops.gen_query_conditions('name', '=', 'L2_vlan_2221')
    flat_l2_uuid = res_ops.query_resource(res_ops.L2_NETWORK, cond)[0].uuid
    acc_ops.share_resources([linked_account_uuid], [flat_l2_uuid])

    # ut_util.test_dsc('create L3_flat_network names is L3_flat_network by normal account')
    l3_inv = sce_ops.create_l3(zstack_management_ip, 'l3_flat_network', 'L3BasicNetwork', flat_l2_uuid, 'local.com', session_uuid = project_login_uuid)
    l3_uuid = l3_inv.inventory.uuid

    l3_dns = '223.5.5.5'
    start_ip = '192.168.123.2'
    end_ip = '192.168.123.10'
    gateway = '192.168.123.1'
    netmask = '255.255.255.0'

    test_util.test_dsc('add DNS and IP_Range for L3_flat_network')
    sce_ops.add_dns_to_l3(zstack_management_ip, l3_uuid, l3_dns, session_uuid = project_login_uuid)
    sce_ops.add_ip_range(zstack_management_ip,'IP_range', l3_uuid, start_ip, end_ip, gateway, netmask, session_uuid = project_login_uuid)

    test_util.test_dsc('query flat provider and attach network service to  L3_flat_network')
    provider_name = 'Flat Network Service Provider'
    conditions = res_ops.gen_query_conditions('name', '=', provider_name)
    net_provider_list = sce_ops.query_resource(zstack_management_ip, res_ops.NETWORK_SERVICE_PROVIDER, conditions, session_uuid = project_login_uuid).inventories[0]
    pro_uuid = net_provider_list.uuid
    sce_ops.attach_flat_network_service_to_l3network(zstack_management_ip, l3_uuid,pro_uuid, session_uuid = project_login_uuid)

    test_stub.share_admin_resource_1([linked_account_uuid])

    vm = test_stub.create_vm(session_uuid=project_login_uuid)
    test_obj_dict.add_vm(vm)

    #create normal account
    test_util.test_dsc('create normal account')
    account_name = 'test_abc'
    #account_pass = hashlib.sha512(account_name).hexdigest()
    account_pass = '******'
    test_account = acc_ops.create_normal_account(account_name, account_pass)
    test_account_uuid = test_account.uuid
    test_account_session = acc_ops.login_by_account(account_name, account_pass)

    test_util.test_dsc('share admin resoure to normal account test_abc')
    test_stub.share_admin_resource_1([test_account_uuid])

    l2_inv1 = sce_ops.create_l2_vlan(zstack_management_ip, 'L2_vlan_2215', 'eth0', '2215', zone_uuid)
    l2_uuid1 = l2_inv1.inventory.uuid

    test_util.test_dsc('attach L2 netowrk to cluster')
    sce_ops.attach_l2(zstack_management_ip, l2_uuid1, cluster_uuid)

    #share admin resoure to normal account
    test_util.test_dsc('share L2 L2_vlan_2215 to normal account test_a')
    cond = res_ops.gen_query_conditions('name', '=', 'L2_vlan_2215')
    flat_l2_uuid1 = res_ops.query_resource(res_ops.L2_NETWORK, cond)[0].uuid
    acc_ops.share_resources([test_account_uuid], [flat_l2_uuid1])

    test_account_session = acc_ops.login_by_account(account_name, account_pass)
    l3_inv1 = sce_ops.create_l3(zstack_management_ip, 'l3_flat_network1', 'L3BasicNetwork', flat_l2_uuid1, 'local.com', session_uuid = test_account_session)
    l3_uuid1 = l3_inv1.inventory.uuid

    l3_dns = '223.5.5.5'
    start_ip = '192.168.126.2'
    end_ip = '192.168.126.10'
    gateway = '192.168.126.1'
    netmask = '255.255.255.0'

    test_util.test_dsc('add DNS and IP_Range for L3_flat_network1')
    sce_ops.add_dns_to_l3(zstack_management_ip, l3_uuid1, l3_dns, session_uuid = test_account_session)
    sce_ops.add_ip_range(zstack_management_ip,'IP_range', l3_uuid1, start_ip, end_ip, gateway, netmask, session_uuid = test_account_session)

    test_util.test_dsc('query flat provider and attach network service to  L3_flat_network')
    provider_name = 'Flat Network Service Provider'
    conditions = res_ops.gen_query_conditions('name', '=', provider_name)
    net_provider_list = sce_ops.query_resource(zstack_management_ip, res_ops.NETWORK_SERVICE_PROVIDER, conditions, session_uuid = test_account_session).inventories[0]
    pro_uuid = net_provider_list.uuid
    sce_ops.attach_flat_network_service_to_l3network(zstack_management_ip, l3_uuid1,pro_uuid, session_uuid = test_account_session)

    vm2 = test_stub.create_vm(session_uuid = test_account_session)

    test_util.test_dsc('test success normal acount create L3 by admin share L2 ')
    test_obj_dict.add_vm(vm2)

    # 9 delete
    acc_ops.logout(project_login_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    test_lib.lib_error_cleanup(test_obj_dict)

    net_ops.delete_l2(l2_uuid)
    net_ops.delete_l2(l2_uuid1)

    acc_ops.delete_account(test_account_uuid)
def test():
    global l2_vxlan_network_uuid,project_uuid,project_operator_uuid,vni_range_uuid,vxlan_pool_uuid,l3_vpc_network_uuid

    # create vxlan pool and vni range
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    cluster_uuid = res_ops.get_resource(res_ops.CLUSTER)[0].uuid
    vxlan_pool_name = 'vxlan_pool_name'

    vxlan_pool_uuid = vxlan_ops.create_l2_vxlan_network_pool(vxlan_pool_name,zone_uuid).uuid
    vxlan_ops.create_vni_range('vni_range',20,40,vxlan_pool_uuid)

    systemTags = ["l2NetworkUuid::%s::clusterUuid::%s::cidr::{172.20.0.1/16}"%(vxlan_pool_uuid,cluster_uuid)]
    net_ops.attach_l2_vxlan_pool(vxlan_pool_uuid,cluster_uuid,systemTags)

    # 1 create project
    project_name = 'test_project7'
    project = iam2_ops.create_iam2_project(project_name)
    project_uuid = project.uuid
    cond = res_ops.gen_query_conditions("name",'=',"test_project7")
    linked_account_uuid = res_ops.query_resource(res_ops.ACCOUNT,cond)[0].uuid

    # 2 create project operator
    project_operator_name = 'username7'
    project_operator_password = '******'
    attributes = [{"name": "__ProjectOperator__", "value": project_uuid}]
    project_operator_uuid = iam2_ops.create_iam2_virtual_id(project_operator_name,project_operator_password,attributes=attributes).uuid
    
    zone_uuid = res_ops.get_resource(res_ops.ZONE)[0].uuid
    attributes = [{"name": "__ProjectRelatedZone__", "value": zone_uuid}]
    iam2_ops.add_attributes_to_iam2_project(project_uuid, attributes)
    # 3 login in project by project operator
    iam2_ops.add_iam2_virtual_ids_to_project([project_operator_uuid],project_uuid)
    project_operator_session_uuid = iam2_ops.login_iam2_virtual_id(project_operator_name,project_operator_password)
    project_login_uuid = iam2_ops.login_iam2_project(project_name,session_uuid=project_operator_session_uuid).uuid

    # 4 share vxlan pool to project
    l2vxlan_pools = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK_POOL)
    for l2vxlan_pool in l2vxlan_pools:
        acc_ops.share_resources([linked_account_uuid],[l2vxlan_pool.uuid])
    # 5 create l2 vxlan 
    l2_vxlan_network_uuid = vxlan_ops.create_l2_vxlan_network('l2_vxlan',vxlan_pool_uuid,zone_uuid,session_uuid=project_login_uuid).uuid
    
    # 6 use l2 vxlan to create l3 vpc 
    l3_vpc_network = create_l3_vpc('test_vpc',l2_vxlan_network_uuid,project_login_uuid)
    
    l3_vpc_network_uuid = l3_vpc_network.uuid
    # add ip range
    ir_option = test_util.IpRangeOption()
    ir_option.set_name('iprange2')
    ir_option.set_description('iprange for vpc')
    ir_option.set_netmask('255.255.255.0')
    ir_option.set_gateway('192.168.23.1')
    ir_option.set_l3_uuid(l3_vpc_network_uuid)
    ir_option.set_startIp('192.168.23.2')
    ir_option.set_endIp('192.168.23.254')
    
    net_ops.add_ip_range(ir_option)
     
    # add network service
    AttachNetworkServiceToL3Network(l3_vpc_network_uuid,allservices,session_uuid = project_login_uuid)
    
    # share the vr_offering to project and do create vpc router and vpc network
    cond = res_ops.gen_query_conditions("name",'=',"virtual-router-vm")
    vr_offering_uuid = res_ops.query_resource(res_ops.VR_OFFERING,cond)[0].uuid
    acc_ops.share_resources([linked_account_uuid],[vr_offering_uuid])
    vpc_ops.create_vpc_vrouter(name = 'test_vpc_vr', virtualrouter_offering_uuid = vr_offering_uuid,session_uuid = project_login_uuid)
    vpc_vr = test_stub.query_vpc_vrouter('test_vpc_vr')
    vpc_vr.add_nic(l3_vpc_network_uuid)
    
    # 7 expunge the project and check the l2 vxlan
    iam2_ops.delete_iam2_project(project_uuid)
    iam2_ops.expunge_iam2_project(project_uuid)
    try:
        l2_vxlan_network_test_uuid = res_ops.query_resource(res_ops.L2_VXLAN_NETWORK)[0].uuid
    except: 
        
        test_util.test_pass(
            "l2 vxlan  is delete after deleted the project " )
    test_util.test_dsc('test l2 l2 cascade delete')
    
    # 8 check the vpc network and vpc_vr
    try:
        cond = res_ops.gen_query_conditions("name",'=',"test_vpc")
        l3_vpc_network_uuid = res_ops.query_resource(res_ops.L3_NETWORK,cond)[0].uuid
    except:
        
        test_util.test_pass(
            "l3_vpc  is delete after deleted the project")
    
   
    cond = res_ops.gen_query_conditions("name",'=',"test_vpc_vr")
    vpc_vr = res_ops.query_resource(res_ops.VIRTUALROUTER_VM,cond)
    
    if not vpc_vr.inv.state is 'Paused':
        test_util.test_fail(
            "vpc vr [%s] is still exist after delete and expunge the project [%s]" % (vpc_vr.uuid,project_uuid))
   
    # 9 delete 
    vni_range_uuid = res_ops.get_resource(res_ops.VNI_RANGE)[0].uuid
    vxlan_ops.delete_vni_range(vni_range_uuid)
    net_ops.delete_l2(vxlan_pool_uuid)
    iam2_ops.delete_iam2_virtual_id(project_operator_uuid)