diff --git a/gns3server/controller/controller_error.py b/gns3server/controller/controller_error.py new file mode 100644 index 00000000..8f93dd1b --- /dev/null +++ b/gns3server/controller/controller_error.py @@ -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 . + + +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 + + diff --git a/gns3server/controller/server.py b/gns3server/controller/server.py index fef33c3d..047cf236 100644 --- a/gns3server/controller/server.py +++ b/gns3server/controller/server.py @@ -16,6 +16,14 @@ # along with this program. If not, see . +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 + diff --git a/gns3server/modules/vm_error.py b/gns3server/modules/vm_error.py index 55cfc4cf..98826542 100644 --- a/gns3server/modules/vm_error.py +++ b/gns3server/modules/vm_error.py @@ -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 diff --git a/gns3server/web/route.py b/gns3server/web/route.py index 4511cbc2..97b9d0f9 100644 --- a/gns3server/web/route.py +++ b/gns3server/web/route.py @@ -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) diff --git a/tests/controller/test_server.py b/tests/controller/test_server.py index ad2093f8..dd619644 100644 --- a/tests/controller/test_server.py +++ b/tests/controller/test_server.py @@ -15,9 +15,11 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -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")