mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-25 00:08:11 +00:00
Refactor config management in tests
This commit is contained in:
parent
e54649accd
commit
bcb1ce02ab
@ -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):
|
||||
"""
|
||||
|
@ -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)
|
||||
|
||||
|
@ -15,17 +15,16 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
||||
|
@ -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):
|
||||
|
@ -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)
|
||||
|
@ -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"}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user