From cd836f146eb8fb81f707faa1945a120c497d8127 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 11 May 2016 10:56:43 +0200 Subject: [PATCH] Create the project on compute only when needed --- gns3server/controller/project.py | 7 ++++++- tests/controller/test_project.py | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index f273b8e1..a421d103 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -60,6 +60,9 @@ class Project: self._links = {} self._listeners = set() + # Create the project on demand on the compute node + self._project_created_on_compute = set() + @property def name(self): return self._name @@ -103,7 +106,6 @@ class Project: @asyncio.coroutine def addCompute(self, compute): self._computes.add(compute) - yield from compute.post("/projects", self) @asyncio.coroutine def addVM(self, compute, vm_id, **kwargs): @@ -114,6 +116,9 @@ class Project: """ if vm_id not in self._vms: vm = VM(self, compute, vm_id=vm_id, **kwargs) + if compute not in self._project_created_on_compute: + yield from compute.post("/projects", self) + self._project_created_on_compute.add(compute) yield from vm.create() self._vms[vm.id] = vm return vm diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index c6aaf64b..cb74d959 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -69,6 +69,13 @@ def test_captures_directory(tmpdir): assert os.path.exists(p.captures_directory) +def test_addCompute(async_run): + compute = MagicMock() + project = Project() + async_run(project.addCompute(compute)) + assert compute in project._computes + + def test_addVM(async_run): compute = MagicMock() project = Project() @@ -79,11 +86,12 @@ def test_addVM(async_run): vm = async_run(project.addVM(compute, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"})) - compute.post.assert_called_with('/projects/{}/vpcs/vms'.format(project.id), - data={'vm_id': vm.id, - 'console_type': 'telnet', - 'startup_config': 'test.cfg', - 'name': 'test'}) + compute.post.assert_any_call('/projects/{}/vpcs/vms'.format(project.id), + data={'vm_id': vm.id, + 'console_type': 'telnet', + 'startup_config': 'test.cfg', + 'name': 'test'}) + assert compute in project._project_created_on_compute def test_getVM(async_run):