gns3-server/tests/test_config.py

113 lines
3.0 KiB
Python
Raw Normal View History

2015-02-02 16:01:48 +02:00
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 GNS3 Technologies Inc.
2015-02-02 16:01:48 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import configparser
from gns3server.config import Config
def load_config(tmpdir, settings):
"""
Create a configuration file for
the test.
:params tmpdir: Temporary directory
:params settings: Configuration settings
:returns: Configuration instance
"""
path = write_config(tmpdir, settings)
return Config(files=[path])
def write_config(tmpdir, settings):
"""
Write a configuration file for the test.
:params tmpdir: Temporary directory
:params settings: Configuration settings
:returns: File path
"""
path = str(tmpdir / "server.conf")
config = configparser.ConfigParser()
config.read_dict(settings)
with open(path, "w+") as f:
config.write(f)
return path
2024-05-09 13:53:02 +03:00
def test_get_section_config(tmpdir):
2015-02-02 16:01:48 +02:00
config = load_config(tmpdir, {
"Server": {
2016-08-23 23:40:26 +03:00
"host": "127.0.0.1",
2015-02-02 16:01:48 +02:00
}
})
assert dict(config.get_section_config("Server")) == {"host": "127.0.0.1"}
2024-05-09 13:53:02 +03:00
def test_set_section_config(tmpdir):
2015-02-02 16:01:48 +02:00
2015-03-16 16:03:41 +02:00
config = load_config(tmpdir, {
"Server": {
"host": "127.0.0.1",
"local": "false"
}
})
2015-03-16 16:03:41 +02:00
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"}
2024-05-09 13:53:02 +03:00
def test_set(tmpdir):
2015-03-16 16:03:41 +02:00
2015-02-02 16:01:48 +02:00
config = load_config(tmpdir, {
"Server": {
"host": "127.0.0.1"
}
})
2015-02-02 16:01:48 +02:00
assert dict(config.get_section_config("Server")) == {"host": "127.0.0.1"}
2015-03-16 16:03:41 +02:00
config.set("Server", "host", "192.168.1.1")
2015-02-02 16:01:48 +02:00
assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1"}
2024-05-09 13:53:02 +03:00
def test_reload(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"})
assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1"}
write_config(tmpdir, {
"Server": {
"host": "192.168.1.2"
}
})
2015-03-02 21:46:05 +02:00
config.reload()
assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1"}