From 3eadbb9adbb565faf9a8bd04b7d5b41a931e54f7 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Fri, 7 Jul 2017 17:50:40 +0200 Subject: [PATCH] Implement an api call for computing the IDLE PC Fix https://github.com/GNS3/gns3-gui/pull/2153 --- gns3server/controller/__init__.py | 21 ++++++++++++++++ gns3server/controller/compute.py | 1 + .../api/controller/compute_handler.py | 15 +++++++++++ tests/controller/test_controller.py | 10 ++++++++ tests/handlers/api/controller/test_compute.py | 25 +++++++++++++++++++ 5 files changed, 72 insertions(+) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 1934b2ff..b818f43a 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -597,3 +597,24 @@ class Controller: if not hasattr(Controller, '_instance') or Controller._instance is None: Controller._instance = Controller() return Controller._instance + + @asyncio.coroutine + def autoidlepc(self, compute_id, platform, image): + """ + Compute and IDLE PC value for an image + + :param compute_id: ID of the compute where the idlepc operation need to run + :param platform: Platform type + :param image: Image to use + """ + compute = self.get_compute(compute_id) + for project in list(self._projects.values()): + if project.name == "AUTOIDLEPC": + yield from project.delete() + self.remove_project(project) + project = yield from self.add_project(name="AUTOIDLEPC") + node = yield from project.add_node(compute, "AUTOIDLEPC", str(uuid.uuid4()), node_type="dynamips", platform=platform, image=image, ram=512) + res = yield from node.dynamips_auto_idlepc() + yield from project.delete() + self.remove_project(project) + return res diff --git a/gns3server/controller/compute.py b/gns3server/controller/compute.py index 02112234..ede441f8 100644 --- a/gns3server/controller/compute.py +++ b/gns3server/controller/compute.py @@ -665,3 +665,4 @@ class Compute: return (this_interface["ip_address"], other_interface["ip_address"]) raise ValueError("No common subnet for compute {} and {}".format(self.name, other_compute.name)) + diff --git a/gns3server/handlers/api/controller/compute_handler.py b/gns3server/handlers/api/controller/compute_handler.py index fa579e96..2091cc63 100644 --- a/gns3server/handlers/api/controller/compute_handler.py +++ b/gns3server/handlers/api/controller/compute_handler.py @@ -154,3 +154,18 @@ class ComputeHandler: controller = Controller.instance() yield from controller.delete_compute(request.match_info["compute_id"]) response.set_status(204) + + @Route.post( + r"/computes/{compute_id}/autoidlepc", + parameters={ + "compute_id": "Compute UUID" + }, + status_codes={ + 200: "Idle PC computed", + }, + description="Compute IDLE PC value") + def autoidlepc(request, response): + controller = Controller.instance() + res = yield from controller.autoidlepc(request.match_info["compute_id"], request.json["platform"], request.json["image"]) + response.set_status(200) + response.json(res) diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index fcae102f..9f342224 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -517,3 +517,13 @@ def test_load_appliances(controller): assert qemu_uuid == appliance.id elif appliance.name == "Cloud": assert cloud_uuid == appliance.id + + +def test_autoidlepc(controller, async_run): + controller._computes["local"] = AsyncioMagicMock() + node_mock = AsyncioMagicMock() + with asyncio_patch("gns3server.controller.Project.add_node", return_value=node_mock): + async_run(controller.autoidlepc("local", "c7200", "test.bin")) + assert node_mock.dynamips_auto_idlepc.called + assert len(controller.projects) == 0 + diff --git a/tests/handlers/api/controller/test_compute.py b/tests/handlers/api/controller/test_compute.py index 8e735687..275d6102 100644 --- a/tests/handlers/api/controller/test_compute.py +++ b/tests/handlers/api/controller/test_compute.py @@ -216,3 +216,28 @@ def test_compute_create_img(http_controller, controller): with asyncio_patch("gns3server.controller.compute.Compute.forward", return_value=[]) as mock: response = http_controller.post("/computes/my_compute/qemu/img", params, example=True) mock.assert_called_with("POST", "qemu", "img", data=unittest.mock.ANY) + + +def test_compute_autoidlepc(http_controller, controller): + + params = { + "compute_id": "my_compute_id", + "protocol": "http", + "host": "localhost", + "port": 84, + "user": "julien", + "password": "secure" + } + response = http_controller.post("/computes", params, example=False) + + params = { + "platform": "c7200", + "image": "test.bin" + } + with asyncio_patch("gns3server.controller.Controller.autoidlepc", return_value={"idlepc": "0x606de20c"}) as mock: + response = http_controller.post("/computes/my_compute_id/autoidlepc", params, example=True) + assert mock.called + assert response.status == 200 + + +