2016-05-20 18:19:31 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
#
|
|
|
|
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
|
|
|
|
#
|
|
|
|
|
# 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 sys
|
|
|
|
|
import pytest
|
2016-08-25 11:15:50 +02:00
|
|
|
import logging
|
2016-05-20 18:19:31 +02:00
|
|
|
import subprocess
|
2016-12-08 17:49:06 +01:00
|
|
|
import unittest
|
2016-05-20 18:19:31 +02:00
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
|
|
from gns3.local_server import LocalServer
|
|
|
|
|
from gns3.local_server_config import LocalServerConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def local_server_path(tmpdir):
|
|
|
|
|
return str(tmpdir / "gns3server")
|
|
|
|
|
|
|
|
|
|
|
2023-08-16 00:30:02 +10:00
|
|
|
@pytest.fixture
|
2016-05-20 18:19:31 +02:00
|
|
|
def local_server(local_server_path, tmpdir):
|
2016-08-25 11:15:50 +02:00
|
|
|
with open(str(tmpdir / "test.cfg"), "w+") as f:
|
|
|
|
|
f.write("""
|
|
|
|
|
[Server]
|
|
|
|
|
path={}""".format(local_server_path))
|
|
|
|
|
|
|
|
|
|
LocalServerConfig.instance().setConfigFile(str(tmpdir / "test.cfg"))
|
|
|
|
|
LocalServer._instance = None
|
2019-01-28 15:24:40 +08:00
|
|
|
with patch("gns3.local_server.LocalServer.localServerAutoStartIfRequired"):
|
2016-09-02 16:05:30 +02:00
|
|
|
local_server = LocalServer.instance()
|
|
|
|
|
local_server._config_directory = str(tmpdir)
|
|
|
|
|
yield local_server
|
2016-05-20 18:19:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_loadSettings_EmptySettings(tmpdir, local_server):
|
|
|
|
|
|
|
|
|
|
with open(str(tmpdir / "test.cfg"), "w+") as f:
|
|
|
|
|
f.write("")
|
|
|
|
|
LocalServerConfig.instance().setConfigFile(str(tmpdir / "test.cfg"))
|
|
|
|
|
|
2017-07-07 10:20:22 +02:00
|
|
|
assert local_server.localServerSettings()["auth"]
|
2016-05-20 18:19:31 +02:00
|
|
|
assert local_server.localServerSettings()["port"] == 3080
|
|
|
|
|
assert len(local_server.localServerSettings()["password"]) == 64
|
|
|
|
|
assert local_server.localServerSettings()["user"] == "admin"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_loadSettings(tmpdir, local_server):
|
|
|
|
|
with open(str(tmpdir / "test.cfg"), "w+") as f:
|
|
|
|
|
f.write("""
|
|
|
|
|
[Server]
|
|
|
|
|
auth=True
|
|
|
|
|
user=world
|
|
|
|
|
password=hello""")
|
|
|
|
|
|
|
|
|
|
LocalServerConfig.instance().setConfigFile(str(tmpdir / "test.cfg"))
|
|
|
|
|
assert local_server.localServerSettings()["password"] == "hello"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.platform.startswith('win') is True, reason='Not for windows')
|
|
|
|
|
def test_startLocalServer(tmpdir, local_server, local_server_path):
|
2016-09-07 10:08:37 +02:00
|
|
|
logging.getLogger().setLevel(logging.DEBUG) # Make sure we are using debug level in order to get the --debug
|
2016-08-25 11:15:50 +02:00
|
|
|
|
2016-05-20 18:19:31 +02:00
|
|
|
process_mock = MagicMock()
|
|
|
|
|
with patch("subprocess.Popen", return_value=process_mock) as mock:
|
|
|
|
|
|
|
|
|
|
# If everything work fine the command is still running and a timeout is raised
|
|
|
|
|
process_mock.communicate.side_effect = subprocess.TimeoutExpired("test", 1)
|
|
|
|
|
|
|
|
|
|
LocalServer.instance().startLocalServer()
|
2016-12-08 17:49:06 +01:00
|
|
|
mock.assert_called_with([unittest.mock.ANY,
|
2016-05-20 18:19:31 +02:00
|
|
|
'--local',
|
|
|
|
|
'--debug',
|
2016-09-07 10:08:37 +02:00
|
|
|
'--log=' + str(tmpdir / "gns3_server.log"),
|
2016-05-20 18:19:31 +02:00
|
|
|
'--pid=' + str(tmpdir / "gns3_server.pid")
|
2023-10-31 15:07:35 +10:00
|
|
|
], stderr=unittest.mock.ANY, env=unittest.mock.ANY)
|
2016-05-20 18:19:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_killAlreadyRunningServer(local_server):
|
2016-08-25 11:15:50 +02:00
|
|
|
with open(local_server._pid_path(), "w+") as f:
|
2016-05-20 18:19:31 +02:00
|
|
|
f.write("42")
|
|
|
|
|
|
|
|
|
|
mock_process = MagicMock()
|
|
|
|
|
with patch("psutil.Process", return_value=mock_process) as mock:
|
|
|
|
|
LocalServer.instance()._killAlreadyRunningServer()
|
|
|
|
|
mock.assert_called_with(pid=42)
|
|
|
|
|
assert mock_process.kill.called
|