From 7ce1cf3f8492d568c3af62f6351eb2bb661b1782 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 21 Jan 2015 14:01:15 -0700 Subject: [PATCH] Return correct status codes and fix tests. --- gns3server/handlers/virtualbox_handler.py | 8 +++++--- gns3server/handlers/vpcs_handler.py | 13 ++++++++----- tests/api/base.py | 5 +++-- tests/api/test_virtualbox.py | 14 +++++++------- tests/api/test_vpcs.py | 20 ++++++++++---------- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/gns3server/handlers/virtualbox_handler.py b/gns3server/handlers/virtualbox_handler.py index 55d9a53b..84a340ed 100644 --- a/gns3server/handlers/virtualbox_handler.py +++ b/gns3server/handlers/virtualbox_handler.py @@ -44,7 +44,9 @@ class VirtualBoxHandler: vm = yield from vbox_manager.create_vm(request.json["name"], request.json["project_uuid"], request.json.get("uuid"), - vmname=request.json["vmname"]) + request.json["vmname"], + request.json.get("linked_clone", False)) + response.set_status(201) response.json({"name": vm.name, "uuid": vm.uuid, "project_uuid": vm.project.uuid}) @@ -65,7 +67,7 @@ class VirtualBoxHandler: vbox_manager = VirtualBox.instance() yield from vbox_manager.start_vm(request.match_info["uuid"]) - response.json({}) + response.set_status(204) @classmethod @Route.post( @@ -83,4 +85,4 @@ class VirtualBoxHandler: vbox_manager = VirtualBox.instance() yield from vbox_manager.stop_vm(request.match_info["uuid"]) - response.json({}) + response.set_status(204) diff --git a/gns3server/handlers/vpcs_handler.py b/gns3server/handlers/vpcs_handler.py index 633a668e..0eb354b5 100644 --- a/gns3server/handlers/vpcs_handler.py +++ b/gns3server/handlers/vpcs_handler.py @@ -49,6 +49,7 @@ class VPCSHandler: console=request.json.get("console"), script_file=request.json.get("script_file"), startup_script=request.json.get("startup_script")) + response.set_status(201) response.json(vm) @classmethod @@ -58,7 +59,7 @@ class VPCSHandler: "uuid": "VPCS instance UUID" }, status_codes={ - 200: "VPCS instance started", + 200: "Success", 404: "VPCS instance doesn't exist" }, description="Get a VPCS instance") @@ -105,7 +106,7 @@ class VPCSHandler: vpcs_manager = VPCS.instance() yield from vpcs_manager.start_vm(request.match_info["uuid"]) - response.json({}) + response.set_status(204) @classmethod @Route.post( @@ -123,7 +124,7 @@ class VPCSHandler: vpcs_manager = VPCS.instance() yield from vpcs_manager.stop_vm(request.match_info["uuid"]) - response.json({}) + response.set_status(204) @Route.post( r"/vpcs/{uuid}/ports/{port_id}/nio", @@ -144,6 +145,7 @@ class VPCSHandler: vpcs_manager = VPCS.instance() vm = vpcs_manager.get_vm(request.match_info["uuid"]) nio = vm.port_add_nio_binding(int(request.match_info["port_id"]), request.json) + response.set_status(201) response.json(nio) @classmethod @@ -154,7 +156,7 @@ class VPCSHandler: "port_id": "ID of the port where the nio should be removed" }, status_codes={ - 200: "NIO deleted", + 204: "NIO deleted", 400: "Invalid VPCS instance UUID", 404: "VPCS instance doesn't exist" }, @@ -164,4 +166,5 @@ class VPCSHandler: vpcs_manager = VPCS.instance() vm = vpcs_manager.get_vm(request.match_info["uuid"]) nio = vm.port_remove_nio_binding(int(request.match_info["port_id"])) - response.json({}) + response.set_status(204) + diff --git a/tests/api/base.py b/tests/api/base.py index 4664f41a..c2b6d299 100644 --- a/tests/api/base.py +++ b/tests/api/base.py @@ -118,8 +118,9 @@ class Query: value = "Thu, 08 Jan 2015 16:09:15 GMT" f.write("{}: {}\n".format(header, value)) f.write("\n") - f.write(json.dumps(json.loads(response.body.decode('utf-8')), sort_keys=True, indent=4)) - f.write("\n") + if response.body: + f.write(json.dumps(json.loads(response.body.decode('utf-8')), sort_keys=True, indent=4)) + f.write("\n") def _example_file_path(self, method, path): path = re.sub('[^a-z0-9]', '', path) diff --git a/tests/api/test_virtualbox.py b/tests/api/test_virtualbox.py index 3d9e56c3..6ce767aa 100644 --- a/tests/api/test_virtualbox.py +++ b/tests/api/test_virtualbox.py @@ -22,20 +22,20 @@ from gns3server.modules.virtualbox.virtualbox_vm import VirtualBoxVM def test_vbox_create(server, project): response = server.post("/virtualbox", {"name": "VM1", "vmname": "VM1", "project_uuid": project.uuid}, example=True) - assert response.status == 200 + assert response.status == 201 assert response.route == "/virtualbox" assert response.json["name"] == "VM1" def test_vbox_start(server): - with asyncio_patch("gns3server.modules.VirtualBox.start_vm", return_value=True): + with asyncio_patch("gns3server.modules.VirtualBox.start_vm", return_value=True) as mock: response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/start", {}, example=True) - assert response.status == 200 - assert response.route == "/virtualbox/{uuid}/start" + assert mock.called + assert response.status == 204 def test_vbox_stop(server): - with asyncio_patch("gns3server.modules.VirtualBox.stop_vm", return_value=True): + with asyncio_patch("gns3server.modules.VirtualBox.stop_vm", return_value=True) as mock: response = server.post("/virtualbox/61d61bdd-aa7d-4912-817f-65a9eb54d3ab/stop", {}, example=True) - assert response.status == 200 - assert response.route == "/virtualbox/{uuid}/stop" + assert mock.called + assert response.status == 204 diff --git a/tests/api/test_vpcs.py b/tests/api/test_vpcs.py index 56b5bc56..029e4db1 100644 --- a/tests/api/test_vpcs.py +++ b/tests/api/test_vpcs.py @@ -26,13 +26,13 @@ from gns3server.modules.vpcs.vpcs_vm import VPCSVM @pytest.fixture(scope="module") def vm(server, project): response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid}) - assert response.status == 200 + assert response.status == 201 return response.json def test_vpcs_create(server, project): response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid}, example=True) - assert response.status == 200 + assert response.status == 201 assert response.route == "/vpcs" assert response.json["name"] == "PC TEST 1" assert response.json["project_uuid"] == project.uuid @@ -52,7 +52,7 @@ def test_vpcs_create_script_file(server, project, tmpdir): with open(path, 'w+') as f: f.write("ip 192.168.1.2") response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid, "script_file": path}) - assert response.status == 200 + assert response.status == 201 assert response.route == "/vpcs" assert response.json["name"] == "PC TEST 1" assert response.json["project_uuid"] == project.uuid @@ -61,7 +61,7 @@ def test_vpcs_create_script_file(server, project, tmpdir): def test_vpcs_create_startup_script(server, project): response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid, "startup_script": "ip 192.168.1.2\necho TEST"}) - assert response.status == 200 + assert response.status == 201 assert response.route == "/vpcs" assert response.json["name"] == "PC TEST 1" assert response.json["project_uuid"] == project.uuid @@ -70,7 +70,7 @@ def test_vpcs_create_startup_script(server, project): def test_vpcs_create_port(server, project, free_console_port): response = server.post("/vpcs", {"name": "PC TEST 1", "project_uuid": project.uuid, "console": free_console_port}) - assert response.status == 200 + assert response.status == 201 assert response.route == "/vpcs" assert response.json["name"] == "PC TEST 1" assert response.json["project_uuid"] == project.uuid @@ -83,7 +83,7 @@ def test_vpcs_nio_create_udp(server, vm): "rport": 4343, "rhost": "127.0.0.1"}, example=True) - assert response.status == 200 + assert response.status == 201 assert response.route == "/vpcs/{uuid}/ports/{port_id}/nio" assert response.json["type"] == "nio_udp" @@ -92,7 +92,7 @@ def test_vpcs_nio_create_udp(server, vm): def test_vpcs_nio_create_tap(mock, server, vm): response = server.post("/vpcs/{}/ports/0/nio".format(vm["uuid"]), {"type": "nio_tap", "tap_device": "test"}) - assert response.status == 200 + assert response.status == 201 assert response.route == "/vpcs/{uuid}/ports/{port_id}/nio" assert response.json["type"] == "nio_tap" @@ -103,7 +103,7 @@ def test_vpcs_delete_nio(server, vm): "rport": 4343, "rhost": "127.0.0.1"}) response = server.delete("/vpcs/{}/ports/0/nio".format(vm["uuid"]), example=True) - assert response.status == 200 + assert response.status == 204 assert response.route == "/vpcs/{uuid}/ports/{port_id}/nio" @@ -111,14 +111,14 @@ def test_vpcs_start(server, vm): with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock: response = server.post("/vpcs/{}/start".format(vm["uuid"])) assert mock.called - assert response.status == 200 + assert response.status == 204 def test_vpcs_stop(server, vm): with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.stop", return_value=True) as mock: response = server.post("/vpcs/{}/stop".format(vm["uuid"])) assert mock.called - assert response.status == 200 + assert response.status == 204 def test_vpcs_update(server, vm, tmpdir, free_console_port):