Raise an error if server flagged as local but should not

If the controller is not started with --local but the server ID
is local it's raise an error.
This commit is contained in:
Julien Duponchelle 2016-03-04 16:11:31 +01:00
parent aad69e9650
commit 4d77b2918e
No known key found for this signature in database
GPG Key ID: F1E2485547D4595D
5 changed files with 68 additions and 13 deletions

View File

@ -0,0 +1,30 @@
#!/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/>.
class ControllerError(Exception):
def __init__(self, message):
super().__init__(message)
self._message = message
def __repr__(self):
return self._message
def __str__(self):
return self._message

View File

@ -16,6 +16,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ..controller.controller_error import ControllerError
from ..config import Config
class ServerError(ControllerError):
pass
class Server:
"""
A GNS3 server.
@ -32,6 +40,11 @@ class Server:
# The remote server version
self._version = None
# If the server is local but the server id is local
# it's a configuration issue
if server_id == "local" and Config.instance().get_section_config("Server")["local"] is False:
raise ServerError("The local server is started without --local")
@property
def id(self):
"""
@ -57,7 +70,4 @@ class Server:
"version": self._version
}
def __eq__(self, other):
if not isinstance(other, Server):
return False
return other._id == self._id

View File

@ -20,7 +20,7 @@ class VMError(Exception):
def __init__(self, message, original_exception=None):
Exception.__init__(self, message)
super().__init__(message)
if isinstance(message, Exception):
message = str(message)
self._message = message

View File

@ -26,6 +26,7 @@ import traceback
log = logging.getLogger(__name__)
from ..modules.vm_error import VMError
from ..controller.controller_error import ControllerError
from ..ubridge.ubridge_error import UbridgeError
from .response import Response
from ..crash_report import CrashReport
@ -185,6 +186,11 @@ class Route(object):
response = Response(request=request, route=route)
response.set_status(e.status)
response.json({"message": e.text, "status": e.status})
except (ControllerError) as e:
log.error("Controller error detected: {type}".format(type=type(e)), exc_info=1)
response = Response(request=request, route=route)
response.set_status(409)
response.json({"message": str(e), "status": 409})
except (VMError, UbridgeError) as e:
log.error("VM error detected: {type}".format(type=type(e)), exc_info=1)
response = Response(request=request, route=route)

View File

@ -15,9 +15,11 @@
# 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 pytest
from gns3server.controller.server import Server
import pytest
from unittest.mock import patch
from gns3server.controller.server import Server, ServerError
@pytest.fixture
@ -29,6 +31,19 @@ def test_init(server):
assert server.id == "my_server_id"
def test_server_local(server):
"""
If the server is local but the server id is local
it's a configuration issue
"""
with patch("gns3server.config.Config.get_section_config", return_value={"local": False}):
with pytest.raises(ServerError):
s = Server("local")
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
s = Server("test")
def test_json(server):
assert server.__json__() == {
"server_id": "my_server_id",
@ -40,9 +55,3 @@ def test_json(server):
"version": None
}
def test__eq__(server):
assert server != 1
assert server == server
assert server == Server("my_server_id")
assert server != Server("test")