Esempio n. 1
0
def test_capture(async_run, project):
    compute1 = MagicMock()

    node_vpcs = Node(project, compute1, "V1", node_type="vpcs")
    node_vpcs._status = "started"
    node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
    node_iou = Node(project, compute1, "I1", node_type="iou")
    node_iou._ports = [EthernetPort("E0", 0, 3, 1)]

    link = UDPLink(project)
    link.create = AsyncioMagicMock()
    async_run(link.add_node(node_vpcs, 0, 4))
    async_run(link.add_node(node_iou, 3, 1))

    capture = async_run(link.start_capture())
    assert link.capturing

    compute1.post.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/start_capture".format(
            project.id, node_vpcs.id),
        data={
            "capture_file_name": link.default_capture_file_name(),
            "data_link_type": "DLT_EN10MB"
        })

    capture = async_run(link.stop_capture())
    assert link.capturing is False

    compute1.post.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/stop_capture".format(
            project.id, node_vpcs.id))
def test_short_name():
    # If no customization of port name format return the default short name
    assert EthernetPort("Ethernet0", 0, 0, 0).short_name == "e0/0"
    assert EthernetPort("Ethernet0", 0, 0, 0,
                        short_name="mgmt").short_name == "mgmt"
    # If port name format has change we use the port name as the short name (1.X behavior)
    assert EthernetPort("eth0", 0, 0, 0).short_name == "eth0"
Esempio n. 3
0
def test_add_node(async_run, project, compute):
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.emit_notification = MagicMock()
    project.dump = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))
    assert link._nodes == [
        {
            "node": node1,
            "port": node1._ports[0],
            "adapter_number": 0,
            "port_number": 4,
            'label': {
                'text': '0/4',
                'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;'
            }
        }
    ]
    assert project.dump.called
    assert not link._project.emit_notification.called

    assert not link.create.called

    # We call link.created only when both side are created
    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]
    async_run(link.add_node(node2, 0, 4))

    assert link.create.called
    link._project.emit_notification.assert_called_with("link.created", link.__json__())
    assert link in node2.links
Esempio n. 4
0
def test_update_link(http_controller, tmpdir, project, compute, async_run):
    response = MagicMock()
    response.json = {"console": 2048}
    compute.post = AsyncioMagicMock(return_value=response)

    node1 = async_run(project.add_node(compute, "node1", None, node_type="qemu"))
    node1._ports = [EthernetPort("E0", 0, 0, 3)]
    node2 = async_run(project.add_node(compute, "node2", None, node_type="qemu"))
    node2._ports = [EthernetPort("E0", 0, 2, 4)]

    filters = {
        "latency": [10],
        "frequency_drop": [50]
    }

    with asyncio_patch("gns3server.controller.udp_link.UDPLink.create"):
        response = http_controller.post("/projects/{}/links".format(project.id), {
            "nodes": [
                {
                    "node_id": node1.id,
                    "adapter_number": 0,
                    "port_number": 3,
                    "label": {
                        "text": "Text",
                        "x": 42,
                        "y": 0
                    }
                },
                {
                    "node_id": node2.id,
                    "adapter_number": 2,
                    "port_number": 4
                }
            ]
        })
    link_id = response.json["link_id"]
    assert response.json["nodes"][0]["label"]["x"] == 42
    response = http_controller.put("/projects/{}/links/{}".format(project.id, link_id), {
        "nodes": [
            {
                "node_id": node1.id,
                "adapter_number": 0,
                "port_number": 3,
                "label": {
                    "text": "Hello",
                    "x": 64,
                    "y": 0
                }
            },
            {
                "node_id": node2.id,
                "adapter_number": 2,
                "port_number": 4
            }
        ],
        "filters": filters
    }, example=True)
    assert response.status == 201
    assert response.json["nodes"][0]["label"]["x"] == 64
    assert list(project.links.values())[0].filters == filters
Esempio n. 5
0
def test_update_filters(async_run, project, compute):
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.emit_notification = MagicMock()
    project.dump = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))

    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]
    async_run(link.add_node(node2, 0, 4))

    link.update = AsyncioMagicMock()
    assert link._created
    async_run(
        link.update_filters({
            "packet_loss": [10],
            "delay": [50, 10],
            "frequency_drop": [0],
            "bpf": [" \n  "]
        }))
    assert link.filters == {"packet_loss": [10], "delay": [50, 10]}
    assert link.update.called
Esempio n. 6
0
async def test_add_link(project):

    compute = MagicMock()

    response = MagicMock()
    response.json = {"console": 2048}
    compute.post = AsyncioMagicMock(return_value=response)

    vm1 = await project.add_node(compute,
                                 "test1",
                                 None,
                                 node_type="vpcs",
                                 properties={"startup_config": "test.cfg"})
    vm1._ports = [EthernetPort("E0", 0, 3, 1)]
    vm2 = await project.add_node(compute,
                                 "test2",
                                 None,
                                 node_type="vpcs",
                                 properties={"startup_config": "test.cfg"})
    vm2._ports = [EthernetPort("E0", 0, 4, 2)]
    project.emit_notification = MagicMock()
    link = await project.add_link()
    await link.add_node(vm1, 3, 1)
    with asyncio_patch("gns3server.controller.udp_link.UDPLink.create"
                       ) as mock_udp_create:
        await link.add_node(vm2, 4, 2)
    assert mock_udp_create.called
    assert len(link._nodes) == 2
    project.emit_notification.assert_any_call("link.created", link.__json__())
Esempio n. 7
0
def test_list_link(http_controller, tmpdir, project, compute, async_run):
    response = MagicMock()
    response.json = {"console": 2048}
    compute.post = AsyncioMagicMock(return_value=response)

    node1 = async_run(project.add_node(compute, "node1", None, node_type="qemu"))
    node1._ports = [EthernetPort("E0", 0, 0, 3)]
    node2 = async_run(project.add_node(compute, "node2", None, node_type="qemu"))
    node2._ports = [EthernetPort("E0", 0, 2, 4)]

    with asyncio_patch("gns3server.controller.udp_link.UDPLink.create") as mock:
        response = http_controller.post("/projects/{}/links".format(project.id), {
            "nodes": [
                {
                    "node_id": node1.id,
                    "adapter_number": 0,
                    "port_number": 3
                },
                {
                    "node_id": node2.id,
                    "adapter_number": 2,
                    "port_number": 4
                }
            ]
        })
    response = http_controller.get("/projects/{}/links".format(project.id), example=True)
    assert response.status == 200
    assert len(response.json) == 1
Esempio n. 8
0
def test_default_capture_file_name(project, compute, async_run):
    node1 = Node(project, compute, "Hello@", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute, "w0.rld", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 1, 3)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))
    async_run(link.add_node(node2, 1, 3))
    assert link.default_capture_file_name() == "Hello_0-4_to_w0rld_1-3.pcap"
Esempio n. 9
0
def link(async_run, project, compute):
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 1, 3)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))
    async_run(link.add_node(node2, 1, 3))
    return link
Esempio n. 10
0
def test_add_node_cloud(async_run, project, compute):
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute, "node2", node_type="cloud")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.controller.notification.emit = MagicMock()

    async_run(link.add_node(node1, 0, 4))
    async_run(link.add_node(node2, 0, 4))
Esempio n. 11
0
async def test_add_node_cloud(project, compute):

    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute, "node2", node_type="cloud")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.emit_notification = MagicMock()

    await link.add_node(node1, 0, 4)
    await link.add_node(node2, 0, 4)
Esempio n. 12
0
def test_add_node_same_node(async_run, project, compute):
    """
    Connection to the same node is not allowed
    """
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4), EthernetPort("E1", 0, 0, 5)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.controller.notification.emit = MagicMock()

    async_run(link.add_node(node1, 0, 4))
    with pytest.raises(aiohttp.web.HTTPConflict):
        async_run(link.add_node(node1, 0, 5))
Esempio n. 13
0
def test_add_node_cloud_to_cloud(async_run, project, compute):
    """
    Cloud to cloud connection is not allowed
    """
    node1 = Node(project, compute, "node1", node_type="cloud")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute, "node2", node_type="cloud")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.controller.notification.emit = MagicMock()

    async_run(link.add_node(node1, 0, 4))
    with pytest.raises(aiohttp.web.HTTPConflict):
        async_run(link.add_node(node2, 0, 4))
Esempio n. 14
0
def test_available_filters(async_run, project, compute):
    node1 = Node(project, compute, "node1", node_type="ethernet_switch")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    assert link.available_filters() == []

    # Ethernet switch is not supported should return 0 filters
    async_run(link.add_node(node1, 0, 4))
    assert link.available_filters() == []

    node2 = Node(project, compute, "node2", node_type="vpcs")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]
    async_run(link.add_node(node2, 0, 4))
    assert len(link.available_filters()) > 0
Esempio n. 15
0
async def test_delete(project, compute):

    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.emit_notification = MagicMock()
    project.dump = AsyncioMagicMock()
    await link.add_node(node1, 0, 4)

    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]
    await link.add_node(node2, 0, 4)
    assert link in node2.links
    await link.delete()
    assert link not in node2.links
Esempio n. 16
0
def test_delete(async_run, project):
    compute1 = MagicMock()
    compute2 = MagicMock()

    node1 = Node(project, compute1, "node1", node_type="vpcs")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute2, "node2", node_type="vpcs")
    node2._ports = [EthernetPort("E0", 0, 3, 1)]

    link = UDPLink(project)
    link.create = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))
    async_run(link.add_node(node2, 3, 1))

    async_run(link.delete())

    compute1.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), timeout=120)
    compute2.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), timeout=120)
Esempio n. 17
0
def test_delete(async_run, project, compute):
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.controller.notification.emit = MagicMock()
    project.dump = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))

    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]
    async_run(link.add_node(node2, 0, 4))

    assert link in node2.link

    async_run(link.delete())
    assert link not in node2.link
Esempio n. 18
0
def test_read_pcap_from_source(project, async_run):
    compute1 = MagicMock()

    node_vpcs = Node(project, compute1, "V1", node_type="vpcs")
    node_vpcs._status = "started"
    node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
    node_iou = Node(project, compute1, "I1", node_type="iou")
    node_iou._ports = [EthernetPort("E0", 0, 3, 1)]

    link = UDPLink(project)
    link.create = AsyncioMagicMock()
    async_run(link.add_node(node_vpcs, 0, 4))
    async_run(link.add_node(node_iou, 3, 1))

    capture = async_run(link.start_capture())
    assert link._capture_node is not None

    async_run(link.read_pcap_from_source())
    link._capture_node["node"].compute.stream_file.assert_called_with(project, "tmp/captures/" + link._capture_file_name)
Esempio n. 19
0
def test_add_node_already_connected(async_run, project, compute):
    """
    Raise an error if we try to use an already connected port
    """
    project.dump = AsyncioMagicMock()

    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.controller.notification.emit = MagicMock()
    async_run(link.add_node(node1, 0, 4))
    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]
    async_run(link.add_node(node2, 0, 4))

    assert link.create.called
    link2 = Link(project)
    link2.create = AsyncioMagicMock()
    with pytest.raises(aiohttp.web.HTTPConflict):
        async_run(link2.add_node(node1, 0, 4))
Esempio n. 20
0
def test_create_link_failure(http_controller, tmpdir, project, compute,
                             async_run):
    """
    Make sure the link is deleted if we failed to create the link.

    The failure is trigger by connecting the link to himself
    """
    response = MagicMock()
    response.json = {"console": 2048}
    compute.post = AsyncioMagicMock(return_value=response)

    node1 = async_run(
        project.add_node(compute, "node1", None, node_type="qemu"))
    node1._ports = [EthernetPort("E0", 0, 0, 3), EthernetPort("E0", 0, 0, 4)]

    with asyncio_patch("gns3server.controller.udp_link.UDPLink.create"):
        response = http_controller.post("/projects/{}/links".format(
            project.id), {
                "nodes": [{
                    "node_id": node1.id,
                    "adapter_number": 0,
                    "port_number": 3,
                    "label": {
                        "text": "Text",
                        "x": 42,
                        "y": 0
                    }
                }, {
                    "node_id": node1.id,
                    "adapter_number": 0,
                    "port_number": 4
                }]
            },
                                        example=True)
    #assert mock.called
    assert response.status == 409
    assert len(project.links) == 0
Esempio n. 21
0
def test_create_link(http_controller, tmpdir, project, compute, async_run):
    response = MagicMock()
    response.json = {"console": 2048}
    compute.post = AsyncioMagicMock(return_value=response)

    node1 = async_run(project.add_node(compute, "node1", None, node_type="qemu"))
    node1._ports = [EthernetPort("E0", 0, 0, 3)]
    node2 = async_run(project.add_node(compute, "node2", None, node_type="qemu"))
    node2._ports = [EthernetPort("E0", 0, 2, 4)]

    with asyncio_patch("gns3server.controller.udp_link.UDPLink.create") as mock:
        response = http_controller.post("/projects/{}/links".format(project.id), {
            "nodes": [
                {
                    "node_id": node1.id,
                    "adapter_number": 0,
                    "port_number": 3,
                    "label": {
                        "text": "Text",
                        "x": 42,
                        "y": 0
                    }
                },
                {
                    "node_id": node2.id,
                    "adapter_number": 2,
                    "port_number": 4
                }
            ]
        }, example=True)
    assert mock.called
    assert response.status == 201
    assert response.json["link_id"] is not None
    assert len(response.json["nodes"]) == 2
    assert response.json["nodes"][0]["label"]["x"] == 42
    assert len(project.links) == 1
Esempio n. 22
0
def test_add_node(async_run, project, compute):
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.controller.notification.emit = MagicMock()
    project.dump = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))
    assert link._nodes == [{
        "node": node1,
        "port": node1._ports[0],
        "adapter_number": 0,
        "port_number": 4,
        'label': {
            'y': -10,
            'text': '0/4',
            'x': -10,
            'rotation': 0,
            'style': 'font-size: 10; font-style: Verdana'
        }
    }]
    assert project.dump.called
    assert not link._project.controller.notification.emit.called

    assert not link.create.called

    # We call link.created only when both side are created
    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 0, 4)]
    async_run(link.add_node(node2, 0, 4))

    assert link.create.called
    link._project.controller.notification.emit.assert_called_with(
        "link.created", link.__json__())
    assert link in node2.link
Esempio n. 23
0
async def test_choose_capture_side(project):
    """
    The link capture should run on the optimal node
    """

    compute1 = MagicMock()
    compute2 = MagicMock()
    compute2.id = "local"

    # Capture should run on the local node
    node_vpcs = Node(project, compute1, "node1", node_type="vpcs")
    node_vpcs._status = "started"
    node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
    node_iou = Node(project, compute2, "node2", node_type="iou")
    node_iou._status = "started"
    node_iou._ports = [EthernetPort("E0", 0, 3, 1)]

    link = UDPLink(project)
    link.create = AsyncioMagicMock()
    await link.add_node(node_vpcs, 0, 4)
    await link.add_node(node_iou, 3, 1)

    assert link._choose_capture_side()["node"] == node_iou

    # Capture should choose always running node
    node_iou = Node(project, compute1, "node5", node_type="iou")
    node_iou._status = "started"
    node_iou._ports = [EthernetPort("E0", 0, 0, 4)]
    node_switch = Node(project, compute1, "node6", node_type="ethernet_switch")
    node_switch._status = "started"
    node_switch._ports = [EthernetPort("E0", 0, 3, 1)]

    link = UDPLink(project)
    link.create = AsyncioMagicMock()
    await link.add_node(node_iou, 0, 4)
    await link.add_node(node_switch, 3, 1)

    assert link._choose_capture_side()["node"] == node_switch

    # Capture should raise error if node are not started
    node_vpcs = Node(project, compute1, "node1", node_type="vpcs")
    node_vpcs._ports = [EthernetPort("E0", 0, 0, 4)]
    node_iou = Node(project, compute2, "node2", node_type="iou")
    node_iou._ports = [EthernetPort("E0", 0, 3, 1)]

    link = UDPLink(project)
    link.create = AsyncioMagicMock()
    await link.add_node(node_vpcs, 0, 4)
    await link.add_node(node_iou, 3, 1)

    with pytest.raises(aiohttp.web.HTTPConflict):
        link._choose_capture_side()
    # If you start a node you can capture on it
    node_vpcs._status = "started"
    assert link._choose_capture_side()["node"] == node_vpcs
Esempio n. 24
0
def test_add_node_serial_to_ethernet(async_run, project, compute):
    """
    Serial to ethernet connection is not allowed
    """
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [SerialPort("E0", 0, 0, 4)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    link._project.emit_notification = MagicMock()

    async_run(link.add_node(node1, 0, 4))
    with pytest.raises(aiohttp.web.HTTPConflict):
        async_run(link.add_node(node2, 0, 4))
Esempio n. 25
0
def test_json(async_run, project, compute, link):
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 1, 3)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))
    async_run(link.add_node(node2, 1, 3))
    assert link.__json__() == {
        "link_id": link.id,
        "project_id": project.id,
        "nodes": [
            {
                "node_id": node1.id,
                "adapter_number": 0,
                "port_number": 4,
                'label': {
                    'text': '0/4',
                    'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;'
                }
            },
            {
                "node_id": node2.id,
                "adapter_number": 1,
                "port_number": 3,
                'label': {
                    'text': '1/3',
                    'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;'
                }
            }
        ],
        "filters": {},
        "suspend": False,
        "link_type": "ethernet",
        "capturing": False,
        "capture_file_name": None,
        "capture_file_path": None,
        "capture_compute_id": None
    }
    assert link.__json__(topology_dump=True) == {
        "link_id": link.id,
        "nodes": [
            {
                "node_id": node1.id,
                "adapter_number": 0,
                "port_number": 4,
                'label': {
                    'text': '0/4',
                    'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;'
                }
            },
            {
                "node_id": node2.id,
                "adapter_number": 1,
                "port_number": 3,
                'label': {
                    'text': '1/3',
                    'style': 'font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;'
                }
            }
        ],
        "filters": {},
        "suspend": False
    }
Esempio n. 26
0
def test_update(async_run, project):
    compute1 = MagicMock()
    compute2 = MagicMock()

    node1 = Node(project, compute1, "node1", node_type="vpcs")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute2, "node2", node_type="vpcs")
    node2._ports = [EthernetPort("E0", 0, 3, 1)]

    async def subnet_callback(compute2):
        """
        Fake subnet callback
        """
        return ("192.168.1.1", "192.168.1.2")

    compute1.get_ip_on_same_subnet.side_effect = subnet_callback

    link = UDPLink(project)
    async_run(link.add_node(node1, 0, 4))
    async_run(link.update_filters({"latency": [10]}))

    async def compute1_callback(path, data={}, **kwargs):
        """
        Fake server
        """
        if "/ports/udp" in path:
            response = MagicMock()
            response.json = {"udp_port": 1024}
            return response

    async def compute2_callback(path, data={}, **kwargs):
        """
        Fake server
        """
        if "/ports/udp" in path:
            response = MagicMock()
            response.json = {"udp_port": 2048}
            return response

    compute1.post.side_effect = compute1_callback
    compute1.host = "example.com"
    compute2.post.side_effect = compute2_callback
    compute2.host = "example.org"
    async_run(link.add_node(node2, 3, 1))

    compute1.post.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(
            project.id, node1.id),
        data={
            "lport": 1024,
            "rhost": "192.168.1.2",
            "rport": 2048,
            "type": "nio_udp",
            "suspend": False,
            "filters": {
                "latency": [10]
            }
        },
        timeout=120)
    compute2.post.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(
            project.id, node2.id),
        data={
            "lport": 2048,
            "rhost": "192.168.1.1",
            "rport": 1024,
            "type": "nio_udp",
            "suspend": False,
            "filters": {}
        },
        timeout=120)

    assert link.created
    async_run(
        link.update_filters({
            "drop": [5],
            "bpf": ["icmp[icmptype] == 8"]
        }))
    compute1.put.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(
            project.id, node1.id),
        data={
            "lport": 1024,
            "rhost": "192.168.1.2",
            "rport": 2048,
            "type": "nio_udp",
            "suspend": False,
            "filters": {
                "drop": [5],
                "bpf": ["icmp[icmptype] == 8"]
            }
        },
        timeout=120)
Esempio n. 27
0
def test_create_one_side_failure(async_run, project):
    compute1 = MagicMock()
    compute2 = MagicMock()

    node1 = Node(project, compute1, "node1", node_type="vpcs")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute2, "node2", node_type="vpcs")
    node2._ports = [EthernetPort("E0", 0, 3, 1)]

    @asyncio.coroutine
    def subnet_callback(compute2):
        """
        Fake subnet callback
        """
        return ("192.168.1.1", "192.168.1.2")

    compute1.get_ip_on_same_subnet.side_effect = subnet_callback

    link = UDPLink(project)
    async_run(link.add_node(node1, 0, 4))

    @asyncio.coroutine
    def compute1_callback(path, data={}, **kwargs):
        """
        Fake server
        """
        if "/ports/udp" in path:
            response = MagicMock()
            response.json = {"udp_port": 1024}
            return response

    @asyncio.coroutine
    def compute2_callback(path, data={}, **kwargs):
        """
        Fake server
        """
        if "/ports/udp" in path:
            response = MagicMock()
            response.json = {"udp_port": 2048}
            return response
        elif "/adapters" in path:
            raise aiohttp.web.HTTPConflict(text="Error when creating the NIO")

    compute1.post.side_effect = compute1_callback
    compute1.host = "example.com"
    compute2.post.side_effect = compute2_callback
    compute2.host = "example.org"
    with pytest.raises(aiohttp.web.HTTPConflict):
        async_run(link.add_node(node2, 3, 1))

    compute1.post.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(
            project.id, node1.id),
        data={
            "lport": 1024,
            "rhost": "192.168.1.2",
            "rport": 2048,
            "type": "nio_udp"
        },
        timeout=120)
    compute2.post.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(
            project.id, node2.id),
        data={
            "lport": 2048,
            "rhost": "192.168.1.1",
            "rport": 1024,
            "type": "nio_udp"
        },
        timeout=120)
    # The link creation has failed we rollback the nio
    compute1.delete.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(
            project.id, node1.id),
        timeout=120)
Esempio n. 28
0
def test_json(async_run, project, compute, link):
    node1 = Node(project, compute, "node1", node_type="qemu")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute, "node2", node_type="qemu")
    node2._ports = [EthernetPort("E0", 0, 1, 3)]

    link = Link(project)
    link.create = AsyncioMagicMock()
    async_run(link.add_node(node1, 0, 4))
    async_run(link.add_node(node2, 1, 3))
    assert link.__json__() == {
        "link_id":
        link.id,
        "project_id":
        project.id,
        "nodes": [{
            "node_id": node1.id,
            "adapter_number": 0,
            "port_number": 4,
            'label': {
                'y': -10,
                'text': '0/4',
                'x': -10,
                'rotation': 0,
                'style': 'font-size: 10; font-style: Verdana'
            }
        }, {
            "node_id": node2.id,
            "adapter_number": 1,
            "port_number": 3,
            'label': {
                'y': -10,
                'text': '1/3',
                'x': -10,
                'rotation': 0,
                'style': 'font-size: 10; font-style: Verdana'
            }
        }],
        "link_type":
        "ethernet",
        "capturing":
        False,
        "capture_file_name":
        None,
        "capture_file_path":
        None
    }
    assert link.__json__(topology_dump=True) == {
        "link_id":
        link.id,
        "nodes": [{
            "node_id": node1.id,
            "adapter_number": 0,
            "port_number": 4,
            'label': {
                'y': -10,
                'text': '0/4',
                'x': -10,
                'rotation': 0,
                'style': 'font-size: 10; font-style: Verdana'
            }
        }, {
            "node_id": node2.id,
            "adapter_number": 1,
            "port_number": 3,
            'label': {
                'y': -10,
                'text': '1/3',
                'x': -10,
                'rotation': 0,
                'style': 'font-size: 10; font-style: Verdana'
            }
        }]
    }
Esempio n. 29
0
def test_create(async_run, project):
    compute1 = MagicMock()
    compute2 = MagicMock()

    node1 = Node(project, compute1, "node1", node_type="vpcs")
    node1._ports = [EthernetPort("E0", 0, 0, 4)]
    node2 = Node(project, compute2, "node2", node_type="vpcs")
    node2._ports = [EthernetPort("E0", 0, 3, 1)]

    @asyncio.coroutine
    def subnet_callback(compute2):
        """
        Fake subnet callback
        """
        return ("192.168.1.1", "192.168.1.2")

    compute1.get_ip_on_same_subnet.side_effect = subnet_callback

    link = UDPLink(project)
    async_run(link.add_node(node1, 0, 4))

    @asyncio.coroutine
    def compute1_callback(path, data={}, **kwargs):
        """
        Fake server
        """
        if "/ports/udp" in path:
            response = MagicMock()
            response.json = {"udp_port": 1024}
            return response

    @asyncio.coroutine
    def compute2_callback(path, data={}, **kwargs):
        """
        Fake server
        """
        if "/ports/udp" in path:
            response = MagicMock()
            response.json = {"udp_port": 2048}
            return response

    compute1.post.side_effect = compute1_callback
    compute1.host = "example.com"
    compute2.post.side_effect = compute2_callback
    compute2.host = "example.org"
    async_run(link.add_node(node2, 3, 1))

    compute1.post.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(
            project.id, node1.id),
        data={
            "lport": 1024,
            "rhost": "192.168.1.2",
            "rport": 2048,
            "type": "nio_udp"
        },
        timeout=120)
    compute2.post.assert_any_call(
        "/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(
            project.id, node2.id),
        data={
            "lport": 2048,
            "rhost": "192.168.1.1",
            "rport": 1024,
            "type": "nio_udp"
        },
        timeout=120)