From bcb1ce02abb64c8f7f2bf368a76ec6d553e644b1 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 16 Mar 2015 15:03:41 +0100 Subject: [PATCH] Refactor config management in tests --- gns3server/config.py | 12 ++++++++++++ tests/conftest.py | 13 +++---------- tests/handlers/api/test_config.py | 17 ++++++----------- tests/handlers/api/test_version.py | 15 +++++---------- tests/modules/dynamips/test_dynamips_router.py | 6 ++---- tests/test_config.py | 15 ++++++++++++++- 6 files changed, 42 insertions(+), 36 deletions(-) diff --git a/gns3server/config.py b/gns3server/config.py index 7a91d530..8f3c86a1 100644 --- a/gns3server/config.py +++ b/gns3server/config.py @@ -151,9 +151,21 @@ class Config(object): if not self._config.has_section(section): self._config.add_section(section) for key in content: + if isinstance(content[key], bool): + content[key] = str(content[key]).lower() self._config.set(section, key, content[key]) self._override_config[section] = content + def set(self, section, key, value): + """ + Set a config value. + It's not dumped on the disk. + + If the section doesn't exists the section is created + """ + + self.set_section_config(section, {key: value}) + @staticmethod def instance(files=None): """ diff --git a/tests/conftest.py b/tests/conftest.py index 977fd13a..bb856412 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -135,18 +135,11 @@ def run_around_tests(monkeypatch): Config.reset() config = Config.instance() - server_section = config.get_section_config("Server") - server_section["project_directory"] = tmppath - config.set_section_config("Server", server_section) + config.set("Server", "project_directory", tmppath) # Prevent exectuions of the VM if we forgot to mock something - vbox_section = config.get_section_config("VirtualBox") - vbox_section["vboxmanage_path"] = tmppath - config.set_section_config("VirtualBox", vbox_section) - - vbox_section = config.get_section_config("VPCS") - vbox_section["vpcs_path"] = tmppath - config.set_section_config("VPCS", vbox_section) + config.set("VirtualBox", "vboxmanage_path", tmppath) + config.set("VPCS", "vpcs_path", tmppath) monkeypatch.setattr("gns3server.modules.project.Project._get_default_project_directory", lambda *args: tmppath) diff --git a/tests/handlers/api/test_config.py b/tests/handlers/api/test_config.py index b081ca13..542a1b72 100644 --- a/tests/handlers/api/test_config.py +++ b/tests/handlers/api/test_config.py @@ -15,17 +15,16 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from configparser import ConfigParser -from unittest.mock import patch, MagicMock +from unittest.mock import MagicMock, patch +from gns3server.config import Config def test_reload_accepted(server): gns_config = MagicMock() - config = ConfigParser() - config.add_section("Server") + config = Config.instance() config.set("Server", "local", "true") - gns_config.get_section_config.return_value = config["Server"] + gns_config.get_section_config.return_value = config.get_section_config("Server") with patch("gns3server.config.Config.instance", return_value=gns_config): response = server.post('/config/reload', example=True) @@ -36,13 +35,9 @@ def test_reload_accepted(server): def test_reload_forbidden(server): - gns_config = MagicMock() - config = ConfigParser() - config.add_section("Server") + config = Config.instance() config.set("Server", "local", "false") - gns_config.get_section_config.return_value = config["Server"] - with patch("gns3server.config.Config.instance", return_value=gns_config): - response = server.post('/config/reload') + response = server.post('/config/reload') assert response.status == 403 diff --git a/tests/handlers/api/test_version.py b/tests/handlers/api/test_version.py index 1af2ac89..29cdebae 100644 --- a/tests/handlers/api/test_version.py +++ b/tests/handlers/api/test_version.py @@ -20,23 +20,18 @@ This test suite check /version endpoint It's also used for unittest the HTTP implementation. """ -from unittest.mock import patch, MagicMock -from configparser import ConfigParser +from gns3server.config import Config from gns3server.version import __version__ def test_version_output(server): - gns_config = MagicMock() - config = ConfigParser() - config.add_section("Server") + config = Config.instance() config.set("Server", "local", "true") - gns_config.get_section_config.return_value = config["Server"] - with patch("gns3server.config.Config.instance", return_value=gns_config): - response = server.get('/version', example=True) - assert response.status == 200 - assert response.json == {'local': True, 'version': __version__} + response = server.get('/version', example=True) + assert response.status == 200 + assert response.json == {'local': True, 'version': __version__} def test_version_input(server): diff --git a/tests/modules/dynamips/test_dynamips_router.py b/tests/modules/dynamips/test_dynamips_router.py index add442c0..f32b84ba 100644 --- a/tests/modules/dynamips/test_dynamips_router.py +++ b/tests/modules/dynamips/test_dynamips_router.py @@ -47,10 +47,8 @@ def test_router(project, manager): def test_router_invalid_dynamips_path(project, manager, loop): config = Config.instance() - dynamips_section = config.get_section_config("Dynamips") - dynamips_section["dynamips_path"] = "/bin/test_fake" - dynamips_section["allocate_aux_console_ports"] = "false" - config.set_section_config("Dynamips", dynamips_section) + config.set("Dynamips", "dynamips_path", "/bin/test_fake") + config.set("Dynamips", "allocate_aux_console_ports", False) with pytest.raises(DynamipsError): router = Router("test", "00010203-0405-0607-0809-0a0b0c0d0e0e", project, manager) diff --git a/tests/test_config.py b/tests/test_config.py index 3f119619..a1a0c33e 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -67,13 +67,26 @@ def test_get_section_config(tmpdir): def test_set_section_config(tmpdir): + config = load_config(tmpdir, { + "Server": { + "host": "127.0.0.1", + "local": "false" + } + }) + assert dict(config.get_section_config("Server")) == {"host": "127.0.0.1", "local": "false"} + config.set_section_config("Server", {"host": "192.168.1.1", "local": True}) + assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1", "local": "true"} + + +def test_set(tmpdir): + config = load_config(tmpdir, { "Server": { "host": "127.0.0.1" } }) assert dict(config.get_section_config("Server")) == {"host": "127.0.0.1"} - config.set_section_config("Server", {"host": "192.168.1.1"}) + config.set("Server", "host", "192.168.1.1") assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1"}