def test_vm_renamed(
    minimalistic_topology,
    vnc_test_client,
    vmware_controller,
    vcenter_api_client,
    vmware_vm,
    vmware_dpg,
):
    # User creates a DPG (dpg-1)
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg)
    vmware_controller.handle_update(dpg_created_update)

    # User creates a VM (vm-1)
    vm_created_update = vcenter_api_client.create_vm(vmware_vm)
    vmware_controller.handle_update(vm_created_update)

    # A VPG (esxi-1_dvs-1) and a VMI (esxi-1_dvs-1_dpg-1) should be created
    # in VNC
    vmis = vnc_test_client.read_all_vmis()
    assert len(vmis) == 1
    vpgs = vnc_test_client.read_all_vpgs()
    assert len(vpgs) == 1

    created_vpg = vpgs["esxi-1_dvs-1"]
    created_vmi = vmis["esxi-1_dvs-1_dpg-1"]

    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg,
        vpg_name="esxi-1_dvs-1",
        pi_names=["xe-0/0/0"],
        vmi_names=["esxi-1_dvs-1_dpg-1"],
    )

    utils.verify_vnc_vmi(
        vnc_vmi=created_vmi,
        vmi_name="esxi-1_dvs-1_dpg-1",
        vpg_name="esxi-1_dvs-1",
        vn_name="dvs-1_dpg-1",
        vlan=5,
    )

    # User renames vm-1 to vm-1-renamed
    vm_renamed_update = vcenter_api_client.rename_vm(vmware_vm, "vm-1-renamed")
    vmware_controller.handle_update(vm_renamed_update)

    # User removes vm-1-renamed from vCenter
    vm_removed_update = vcenter_api_client.remove_vm(vmware_vm)
    vmware_controller.handle_update(vm_removed_update)

    # esxi-1_dvs-1 and esxi-1_dvs-1_dpg-1 should be deleted from VNC
    with pytest.raises(vnc_api.NoIdError):
        vnc_test_client.read_vpg(created_vpg.uuid)
    with pytest.raises(vnc_api.NoIdError):
        vnc_test_client.read_vmi(created_vmi.uuid)
def test_detach_last_vmi_from_vpg(vnc_api_client, vnc_lib, vmi_1, vpg_1):
    vnc_lib.virtual_machine_interface_read.return_value = vmi_1
    vpg_1.add_virtual_machine_interface(vmi_1)
    vmi_1.virtual_port_group_back_refs = [{"uuid": vpg_1.uuid}]
    vnc_lib.virtual_port_group_read.return_value = vpg_1

    vnc_api_client.detach_vmi_from_vpg(vmi_1.uuid)

    vnc_lib.virtual_port_group_update.assert_called_once_with(vpg_1)
    vnc_lib.virtual_port_group_delete.assert_called_once_with(id=vpg_1.uuid)
    utils.verify_vnc_vpg(vpg_1, vmi_names=[])
def test_detach_vmi_from_vpg(vnc_api_client, vnc_lib, vmi_1, vmi_2, vpg_1):
    vnc_lib.virtual_machine_interface_read.side_effect = [vmi_1, vmi_2]
    vpg_1.add_virtual_machine_interface(vmi_1)
    vpg_1.add_virtual_machine_interface(vmi_2)
    vmi_1.virtual_port_group_back_refs = [{"uuid": vpg_1.uuid}]
    vmi_2.virtual_port_group_back_refs = [{"uuid": vpg_1.uuid}]
    vnc_lib.virtual_port_group_read.return_value = vpg_1

    vnc_api_client.detach_vmi_from_vpg(vmi_1.uuid)

    vnc_lib.virtual_port_group_update.assert_called_once_with(vpg_1)
    vnc_lib.virtual_port_group_delete.assert_not_called()
    utils.verify_vnc_vpg(vpg_1, vmi_names=["esxi-1_dvs-1_dpg-2"])
def test_update_pis(
    minimalistic_topology,
    vmware_controller,
    vcenter_api_client,
    vnc_test_client,
    vmware_dpg,
    vmware_vm,
):
    # User creates a DPG (dpg-1) and the event is properly handled
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg)
    vmware_controller.handle_update(dpg_created_update)

    # dpg-1 should be created in VNC
    created_vn = vnc_test_client.read_vn(models.generate_uuid(vmware_dpg.key))
    assert created_vn is not None

    # User creates a VM (vm-1) in dpg-1 and the event properly handled
    vm_created_update = vcenter_api_client.create_vm(vmware_vm)
    vmware_controller.handle_update(vm_created_update)

    # VPG (esxi-1_dvs-1) should be created in VNC and connected to the
    # existing PI
    previous_vpg = vnc_test_client.read_vpg(
        models.generate_uuid("esxi-1_dvs-1"))
    utils.verify_vnc_vpg(previous_vpg, pi_names=["xe-0/0/0"])

    # CVFM shuts down
    # The topology changes - existing PI's port is now connected to dvs-2
    # Another PI and Port is created for esxi-1 - dvs-1 connection
    existing_pi_uuid = vnc_test_client.read_all_physical_interface_uuids()[0]
    existing_pi = vnc_test_client.read_physical_interface(existing_pi_uuid)
    existing_port_uuid = existing_pi.get_port_refs()[0]["uuid"]
    vnc_test_client.update_ports_dvs_name(existing_port_uuid, "dvs-2")
    pr = vnc_test_client.read_physical_router(existing_pi.parent_uuid)
    new_pi = vnc_test_client.create_physical_interface("xe-0/0/1",
                                                       "11:22:33:44:55:04", pr)
    existing_port = vnc_test_client.read_port(existing_port_uuid)
    node = vnc_test_client.read_node(existing_port.parent_uuid)
    new_port = vnc_test_client.create_port("eth1", "11:22:33:44:55:03", node,
                                           "dvs-1")
    vnc_test_client.add_port_to_physical_interface(new_pi, new_port)

    # CVFM starts up - sync
    vmware_controller.sync()

    # VPG should be updated in VNC with the new PI
    current_vpg = vnc_test_client.read_vpg(previous_vpg.uuid)
    utils.verify_vnc_vpg(current_vpg, pi_names=["xe-0/0/1"])
def test_vm_created(
    minimalistic_topology,
    vnc_test_client,
    vmware_controller,
    vcenter_api_client,
    vmware_vm,
    vmware_dpg_1,
):
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg_1)
    vmware_controller.handle_update(dpg_created_update)

    vm_created_update = vcenter_api_client.create_vm(vmware_vm)
    vmware_controller.handle_update(vm_created_update)

    vmis = vnc_test_client.read_all_vmis()
    assert len(vmis) == 1
    vpgs = vnc_test_client.read_all_vpgs()
    assert len(vpgs) == 1

    created_vpg = vpgs["esxi-1_dvs-1"]
    created_vmi = vmis["esxi-1_dvs-1_dpg-1"]

    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg,
        vpg_name="esxi-1_dvs-1",
        pi_names=["xe-0/0/0"],
        vmi_names=["esxi-1_dvs-1_dpg-1"],
    )

    utils.verify_vnc_vmi(
        vnc_vmi=created_vmi,
        vmi_name="esxi-1_dvs-1_dpg-1",
        vpg_name="esxi-1_dvs-1",
        vn_name="dvs-1_dpg-1",
        vlan=5,
    )
    utils.verify_vmi_bindings(created_vmi, created_vpg)
Exemplo n.º 6
0
def test_vm_created_on_two_nodes(
    topology_with_spine_switch,
    vnc_test_client,
    vmware_controller,
    vcenter_api_client,
    vmware_vm_1,
    vmware_vm_2,
    vmware_vm_3,
    vmware_vm_4,
    vmware_vm_5,
    vmware_dpg_1,
    vmware_dpg_2,
    vmware_dpg_3,
):
    # dpg-1 created on dvs-1
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg_1)
    vmware_controller.handle_update(dpg_created_update)
    # dpg-2 created on dvs-1
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg_2)
    vmware_controller.handle_update(dpg_created_update)
    # dpg-3 created on dvs-2
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg_3)
    vmware_controller.handle_update(dpg_created_update)

    # vm-1 created on host esxi-1 with single interface in (dvs-1, dpg-1)
    vm_created_update_1 = vcenter_api_client.create_vm(vmware_vm_1)
    vmware_controller.handle_update(vm_created_update_1)

    # vm-2 created on host esxi-2 with two interfaces in (dvs-1, dpg-1) and (
    # dvs-2, dpg-3)
    vm_created_update_2 = vcenter_api_client.create_vm(vmware_vm_2)
    vmware_controller.handle_update(vm_created_update_2)

    # vm-3 created on host esxi-1 with two interfaces in (dvs-1, dpg-1) and (
    # dvs-2, dpg-3)
    vm_created_update_3 = vcenter_api_client.create_vm(vmware_vm_3)
    vmware_controller.handle_update(vm_created_update_3)

    # VM4 created on host esxi-1 with one interface in (dvs-1, dpg-2)
    vm_created_update_4 = vcenter_api_client.create_vm(vmware_vm_4)
    vmware_controller.handle_update(vm_created_update_4)

    # VM5 created on host esxi-2 with one interface in (dvs-1, dpg-2)
    vm_created_update_5 = vcenter_api_client.create_vm(vmware_vm_5)
    vmware_controller.handle_update(vm_created_update_5)

    vpgs = vnc_test_client.read_all_vpgs()
    assert len(vpgs) == 4

    created_vpg = vpgs["esxi-1_dvs-1"]
    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg,
        vpg_name="esxi-1_dvs-1",
        pi_names=["xe-0/0/1", "xe-0/0/5"],
        vmi_names=["esxi-1_dvs-1_dpg-1", "esxi-1_dvs-1_dpg-2"],
    )

    created_vpg = vpgs["esxi-1_dvs-2"]
    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg,
        vpg_name="esxi-1_dvs-2",
        pi_names=["xe-0/0/2", "xe-0/0/6"],
        vmi_names=["esxi-1_dvs-2_dpg-3"],
    )

    created_vpg = vpgs["esxi-2_dvs-1"]
    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg,
        vpg_name="esxi-2_dvs-1",
        pi_names=["xe-0/0/3", "xe-0/0/7"],
        vmi_names=["esxi-2_dvs-1_dpg-1", "esxi-2_dvs-1_dpg-2"],
    )

    created_vpg = vpgs["esxi-2_dvs-2"]
    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg,
        vpg_name="esxi-2_dvs-2",
        pi_names=["xe-0/0/4", "xe-0/0/8"],
        vmi_names=["esxi-2_dvs-2_dpg-3"],
    )

    vmis = vnc_test_client.read_all_vmis()
    assert len(vmis) == 6

    for vmi in list(vmis.values()):
        if "dpg-1" in vmi.name:
            expected_vlan = 5
        elif "dpg-2" in vmi.name:
            expected_vlan = 6
        elif "dpg-3" in vmi.name:
            expected_vlan = 7
        utils.verify_vnc_vmi(vnc_vmi=vmi, vlan=expected_vlan)

        vpg_name = "_".join(vmi.name.split("_")[:-1])
        utils.verify_vmi_bindings(vmi, vpgs[vpg_name])
def test_add_new_node(
    topology_update_msg_timeout,
    minimalistic_topology,
    vnc_test_client,
    vmware_controller,
    vcenter_api_client,
    vmware_dpg_1,
    vmware_dpg_2,
    vmware_vm_1,
    vmware_vm_2,
    vnc_monitor,
    lock,
    sync_finished,
):
    topology_update_msg_timeout = 1
    vnc_monitor_greenlet = gevent.spawn(vnc_monitor.start)

    # create a DPG
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg_1)
    vmware_controller.handle_update(dpg_created_update)
    # create a VM in a DPG
    vm_created_update = vcenter_api_client.create_vm(vmware_vm_1)
    vmware_controller.handle_update(vm_created_update)

    with lock:
        # change topology - add a new node and a new dvs supported by this node
        existing_pi_uuid = vnc_test_client.read_all_physical_interface_uuids()[
            0
        ]
        existing_pi = vnc_test_client.read_physical_interface(existing_pi_uuid)
        pr = vnc_test_client.read_physical_router(existing_pi.parent_uuid)
        pi = vnc_test_client.create_physical_interface(
            "xe-0/0/1", "11:22:33:44:55:03", pr
        )
        esxi = vnc_test_client.create_node("esxi-2", "10.10.10.12")
        port = vnc_test_client.create_port(
            "eth1", "11:22:33:44:55:01", esxi, "dvs-2"
        )
        vnc_test_client.add_port_to_physical_interface(pi, port)
        vcenter_api_client.add_host("esxi-2")

    sync_finished.wait()

    # create a new DPG in the new DVS
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg_2)
    vmware_controller.handle_update(dpg_created_update)
    # create a new VM in the new DPG
    vm_created_update = vcenter_api_client.create_vm(vmware_vm_2)
    vmware_controller.handle_update(vm_created_update)

    # verify that VPGs and VMIs exist for all VMs and DPGs
    vmis = vnc_test_client.read_all_vmis()
    assert len(vmis) == 2
    vpgs = vnc_test_client.read_all_vpgs()
    assert len(vpgs) == 2

    created_vpg_1 = vpgs["esxi-1_dvs-1"]
    created_vmi_1 = vmis["esxi-1_dvs-1_dpg-1"]

    created_vpg_2 = vpgs["esxi-2_dvs-2"]
    created_vmi_2 = vmis["esxi-2_dvs-2_dpg-2"]

    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg_1,
        vpg_name="esxi-1_dvs-1",
        pi_names=["xe-0/0/0"],
        vmi_names=["esxi-1_dvs-1_dpg-1"],
    )

    utils.verify_vnc_vmi(
        vnc_vmi=created_vmi_1,
        vmi_name="esxi-1_dvs-1_dpg-1",
        vpg_name="esxi-1_dvs-1",
        vn_name="dvs-1_dpg-1",
        vlan=5,
    )

    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg_2,
        vpg_name="esxi-2_dvs-2",
        pi_names=["xe-0/0/1"],
        vmi_names=["esxi-2_dvs-2_dpg-2"],
    )

    utils.verify_vnc_vmi(
        vnc_vmi=created_vmi_2,
        vmi_name="esxi-2_dvs-2_dpg-2",
        vpg_name="esxi-2_dvs-2",
        vn_name="dvs-2_dpg-2",
        vlan=6,
    )

    utils.verify_vmi_bindings(created_vmi_1, created_vpg_1)
    utils.verify_vmi_bindings(created_vmi_2, created_vpg_2)

    vnc_monitor_greenlet.kill()
def test_remove_node(
    topology_update_msg_timeout,
    topology_with_two_nodes,
    vnc_test_client,
    vmware_controller,
    vcenter_api_client,
    vmware_dpg_1,
    vmware_dpg_2,
    vmware_vm_1,
    vmware_vm_2,
    vnc_monitor,
    lock,
    sync_finished,
):
    topology_update_msg_timeout = 1
    vnc_monitor_greenlet = gevent.spawn(vnc_monitor.start)

    # create a DPG
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg_1)
    vmware_controller.handle_update(dpg_created_update)
    # create a VM in a DPG
    vm_created_update = vcenter_api_client.create_vm(vmware_vm_1)
    vmware_controller.handle_update(vm_created_update)

    # create a new DPG in the new DVS
    dpg_created_update = vcenter_api_client.create_dpg(vmware_dpg_2)
    vmware_controller.handle_update(dpg_created_update)
    # create a new VM in the new DPG
    vm_created_update = vcenter_api_client.create_vm(vmware_vm_2)
    vmware_controller.handle_update(vm_created_update)

    with lock:
        # change topology - remove a node (esxi-2) and a dvs (dvs-2) supported
        # by this node
        vnc_test_client.vnc_lib.physical_interface_delete(
            ["default-global-system-config", "qfx-1", "xe-0/0/2"]
        )

        vnc_test_client.vnc_lib.port_delete(
            ["default-global-system-config", "esxi-2", "eth2"]
        )

        vnc_test_client.vnc_lib.node_delete(
            ["default-global-system-config", "esxi-2"]
        )

    sync_finished.wait()

    # verify that VPGs and VMIs exist for all VMs and DPGs
    vmis = vnc_test_client.read_all_vmis()
    assert len(vmis) == 1
    vpgs = vnc_test_client.read_all_vpgs()
    assert len(vpgs) == 1

    created_vpg_1 = vpgs["esxi-1_dvs-1"]
    created_vmi_1 = vmis["esxi-1_dvs-1_dpg-1"]

    utils.verify_vnc_vpg(
        vnc_vpg=created_vpg_1,
        vpg_name="esxi-1_dvs-1",
        pi_names=["xe-0/0/0"],
        vmi_names=["esxi-1_dvs-1_dpg-1"],
    )

    utils.verify_vnc_vmi(
        vnc_vmi=created_vmi_1,
        vmi_name="esxi-1_dvs-1_dpg-1",
        vpg_name="esxi-1_dvs-1",
        vn_name="dvs-1_dpg-1",
        vlan=5,
    )
    utils.verify_vmi_bindings(created_vmi_1, created_vpg_1)

    vnc_monitor_greenlet.kill()