From dae48b2de4791b270c4e7a4e2bb127289b1f4887 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Thu, 5 Feb 2015 11:39:32 +0100 Subject: [PATCH] Update temporary status if project change location This avoid race condition during file move. --- gns3server/modules/project.py | 8 ++++++++ tests/modules/test_project.py | 24 ++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index db4f0cfe..ac74fd58 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -114,6 +114,7 @@ class Project: raise aiohttp.web.HTTPForbidden(text="You are not allowed to modifiy the project directory location") self._path = path + self._update_temporary_file() @property def vms(self): @@ -132,6 +133,13 @@ class Project: return self._temporary = temporary + self._update_temporary_file() + + def _update_temporary_file(self): + """ + Update the .gns3_temporary file in order to reflect current + project status. + """ if self._temporary: try: diff --git a/tests/modules/test_project.py b/tests/modules/test_project.py index c7ce93e5..45254a1f 100644 --- a/tests/modules/test_project.py +++ b/tests/modules/test_project.py @@ -53,21 +53,37 @@ def test_path(tmpdir): p = Project(location=str(tmpdir)) assert p.path == os.path.join(str(tmpdir), p.id) assert os.path.exists(os.path.join(str(tmpdir), p.id)) - assert not os.path.exists(os.path.join(p.path, '.gns3_temporary')) + assert not os.path.exists(os.path.join(p.path, ".gns3_temporary")) + + +def test_changing_path_temporary_flag(tmpdir): + + with patch("gns3server.config.Config.get_section_config", return_value={"local": True}): + p = Project(temporary=True) + assert os.path.exists(p.path) + assert os.path.exists(os.path.join(p.path, ".gns3_temporary")) + p.temporary = False + assert not os.path.exists(os.path.join(p.path, ".gns3_temporary")) + + with open(str(tmpdir / ".gns3_temporary"), "w+") as f: + f.write("1") + + p.path = str(tmpdir) + assert not os.path.exists(os.path.join(str(tmpdir), ".gns3_temporary")) def test_temporary_path(): p = Project(temporary=True) assert os.path.exists(p.path) - assert os.path.exists(os.path.join(p.path, '.gns3_temporary')) + assert os.path.exists(os.path.join(p.path, ".gns3_temporary")) def test_remove_temporary_flag(): p = Project(temporary=True) assert os.path.exists(p.path) - assert os.path.exists(os.path.join(p.path, '.gns3_temporary')) + assert os.path.exists(os.path.join(p.path, ".gns3_temporary")) p.temporary = False - assert not os.path.exists(os.path.join(p.path, '.gns3_temporary')) + assert not os.path.exists(os.path.join(p.path, ".gns3_temporary")) def test_changing_location_not_allowed(tmpdir):