From 499a8f10ae08cac5dc19cbad2408f0ab07077722 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Fri, 23 Jan 2015 16:38:59 -0700 Subject: [PATCH] Update tests. --- tests/api/test_virtualbox.py | 41 ++++++++++++++++++- tests/api/test_vpcs.py | 14 +++---- tests/conftest.py | 2 +- tests/modules/test_project.py | 26 ++++++------ .../modules/virtualbox/test_virtualbox_vm.py | 4 -- 5 files changed, 60 insertions(+), 27 deletions(-) diff --git a/tests/api/test_virtualbox.py b/tests/api/test_virtualbox.py index 5b148b3d..d056bec9 100644 --- a/tests/api/test_virtualbox.py +++ b/tests/api/test_virtualbox.py @@ -22,8 +22,8 @@ from tests.utils import asyncio_patch @pytest.fixture(scope="module") def vm(server, project): with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock: - response = server.post("/virtualbox", {"name": "VM1", - "vmname": "VM1", + response = server.post("/virtualbox", {"name": "VMTEST", + "vmname": "VMTEST", "linked_clone": False, "project_uuid": project.uuid}) assert mock.called @@ -44,6 +44,14 @@ def test_vbox_create(server, project): assert response.json["project_uuid"] == project.uuid +def test_vbox_get(server, project, vm): + response = server.get("/virtualbox/{}".format(vm["uuid"]), example=True) + assert response.status == 200 + assert response.route == "/virtualbox/{uuid}" + assert response.json["name"] == "VMTEST" + assert response.json["project_uuid"] == project.uuid + + def test_vbox_start(server, vm): with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.start", return_value=True) as mock: response = server.post("/virtualbox/{}/start".format(vm["uuid"])) @@ -77,3 +85,32 @@ def test_vbox_reload(server, vm): response = server.post("/virtualbox/{}/reload".format(vm["uuid"])) assert mock.called assert response.status == 204 + + +def test_vbox_nio_create_udp(server, vm): + response = server.post("/virtualbox/{}/ports/0/nio".format(vm["uuid"]), {"type": "nio_udp", + "lport": 4242, + "rport": 4343, + "rhost": "127.0.0.1"}, + example=True) + assert response.status == 201 + assert response.route == "/virtualbox/{uuid}/ports/{port_id:\d+}/nio" + assert response.json["type"] == "nio_udp" + + +def test_vbox_delete_nio(server, vm): + server.post("/virtualbox/{}/ports/0/nio".format(vm["uuid"]), {"type": "nio_udp", + "lport": 4242, + "rport": 4343, + "rhost": "127.0.0.1"}) + response = server.delete("/virtualbox/{}/ports/0/nio".format(vm["uuid"]), example=True) + assert response.status == 204 + assert response.route == "/virtualbox/{uuid}/ports/{port_id:\d+}/nio" + + +def test_vpcs_update(server, vm, free_console_port): + response = server.put("/virtualbox/{}".format(vm["uuid"]), {"name": "test", + "console": free_console_port}) + assert response.status == 200 + assert response.json["name"] == "test" + assert response.json["console"] == free_console_port diff --git a/tests/api/test_vpcs.py b/tests/api/test_vpcs.py index d8a0af1f..d07dcc50 100644 --- a/tests/api/test_vpcs.py +++ b/tests/api/test_vpcs.py @@ -82,7 +82,7 @@ def test_vpcs_nio_create_udp(server, vm): "rhost": "127.0.0.1"}, example=True) assert response.status == 201 - assert response.route == "/vpcs/{uuid}/ports/{port_id}/nio" + assert response.route == "/vpcs/{uuid}/ports/{port_id:\d+}/nio" assert response.json["type"] == "nio_udp" @@ -91,18 +91,18 @@ def test_vpcs_nio_create_tap(server, vm): response = server.post("/vpcs/{}/ports/0/nio".format(vm["uuid"]), {"type": "nio_tap", "tap_device": "test"}) assert response.status == 201 - assert response.route == "/vpcs/{uuid}/ports/{port_id}/nio" + assert response.route == "/vpcs/{uuid}/ports/{port_id:\d+}/nio" assert response.json["type"] == "nio_tap" def test_vpcs_delete_nio(server, vm): - response = server.post("/vpcs/{}/ports/0/nio".format(vm["uuid"]), {"type": "nio_udp", - "lport": 4242, - "rport": 4343, - "rhost": "127.0.0.1"}) + server.post("/vpcs/{}/ports/0/nio".format(vm["uuid"]), {"type": "nio_udp", + "lport": 4242, + "rport": 4343, + "rhost": "127.0.0.1"}) response = server.delete("/vpcs/{}/ports/0/nio".format(vm["uuid"]), example=True) assert response.status == 204 - assert response.route == "/vpcs/{uuid}/ports/{port_id}/nio" + assert response.route == "/vpcs/{uuid}/ports/{port_id:\d+}/nio" def test_vpcs_start(server, vm): diff --git a/tests/conftest.py b/tests/conftest.py index 630322ca..5f2de4c4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -91,7 +91,7 @@ def project(): def port_manager(): """An instance of port manager""" - return PortManager("127.0.0.1", False) + return PortManager("127.0.0.1") @pytest.fixture(scope="function") diff --git a/tests/modules/test_project.py b/tests/modules/test_project.py index ee20d306..909a09b6 100644 --- a/tests/modules/test_project.py +++ b/tests/modules/test_project.py @@ -45,12 +45,12 @@ def test_affect_uuid(): assert p.uuid == '00010203-0405-0607-0809-0a0b0c0d0e0f' -@patch("gns3server.config.Config.get_section_config", return_value={"local": True}) def test_path(tmpdir): - p = Project(location=str(tmpdir)) - assert p.path == os.path.join(str(tmpdir), p.uuid) - assert os.path.exists(os.path.join(str(tmpdir), p.uuid)) - assert os.path.exists(os.path.join(str(tmpdir), p.uuid, 'vms')) + with patch("gns3server.config.Config.get_section_config", return_value={"local": True}): + p = Project(location=str(tmpdir)) + assert p.path == os.path.join(str(tmpdir), p.uuid) + assert os.path.exists(os.path.join(str(tmpdir), p.uuid)) + assert os.path.exists(os.path.join(str(tmpdir), p.uuid, 'vms')) def test_temporary_path(): @@ -58,10 +58,10 @@ def test_temporary_path(): assert os.path.exists(p.path) -@patch("gns3server.config.Config.get_section_config", return_value={"local": False}) -def test_changing_location_not_allowed(mock, tmpdir): - with pytest.raises(aiohttp.web.HTTPForbidden): - p = Project(location=str(tmpdir)) +def test_changing_location_not_allowed(tmpdir): + with patch("gns3server.config.Config.get_section_config", return_value={"local": False}): + with pytest.raises(aiohttp.web.HTTPForbidden): + p = Project(location=str(tmpdir)) def test_json(tmpdir): @@ -69,11 +69,11 @@ def test_json(tmpdir): assert p.__json__() == {"location": p.location, "uuid": p.uuid, "temporary": False} -@patch("gns3server.config.Config.get_section_config", return_value={"local": True}) def test_vm_working_directory(tmpdir, vm): - p = Project(location=str(tmpdir)) - assert os.path.exists(p.vm_working_directory(vm)) - assert os.path.exists(os.path.join(str(tmpdir), p.uuid, vm.module_name, vm.uuid)) + with patch("gns3server.config.Config.get_section_config", return_value={"local": True}): + p = Project(location=str(tmpdir)) + assert os.path.exists(p.vm_working_directory(vm)) + assert os.path.exists(os.path.join(str(tmpdir), p.uuid, vm.module_name, vm.uuid)) def test_mark_vm_for_destruction(vm): diff --git a/tests/modules/virtualbox/test_virtualbox_vm.py b/tests/modules/virtualbox/test_virtualbox_vm.py index 6a80d77e..56a31b5b 100644 --- a/tests/modules/virtualbox/test_virtualbox_vm.py +++ b/tests/modules/virtualbox/test_virtualbox_vm.py @@ -60,7 +60,6 @@ def test_vm_non_executable_vboxmanage_path(project, manager, loop): vm = VirtualBoxVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0e", project, manager, "test", False) vm._find_vboxmanage() - def test_vm_valid_virtualbox_api_version(loop, project, manager): with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM._execute", return_value=["API version: 4_3"]): vm = VirtualBoxVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, "test", False) @@ -72,6 +71,3 @@ def test_vm_invalid_virtualbox_api_version(loop, project, manager): with pytest.raises(VirtualBoxError): vm = VirtualBoxVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager, "test", False) loop.run_until_complete(asyncio.async(vm.create())) - - -# TODO: find a way to test start, stop, suspend, resume and reload