def test_reserve_tcp_port_outside_range():

    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    with patch("gns3server.compute.project.Project.emit") as mock_emit:
        port = pm.reserve_tcp_port(80, project)
        assert port != 80
Пример #2
0
def test_reserve_tcp_port():
    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    pm.reserve_tcp_port(2001, project)
    with patch("gns3server.compute.project.Project.emit") as mock_emit:
        port = pm.reserve_tcp_port(2001, project)
        assert port != 2001
Пример #3
0
def test_reserve_tcp_port():
    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    pm.reserve_tcp_port(2001, project)
    with patch("gns3server.compute.project.Project.emit") as mock_emit:
        port = pm.reserve_tcp_port(2001, project)
        assert port != 2001
Пример #4
0
def test_set_console_host(config):
    """
    If allow remote connection we need to bind console host
    to 0.0.0.0
    """
    p = PortManager()
    config.set_section_config("Server", {"allow_remote_console": False})
    p.console_host = "10.42.1.42"
    assert p.console_host == "10.42.1.42"
    p = PortManager()
    config.set_section_config("Server", {"allow_remote_console": True})
    p.console_host = "10.42.1.42"
    assert p.console_host == "0.0.0.0"
def test_release_udp_port():

    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    pm.reserve_udp_port(20000, project)
    pm.release_udp_port(20000, project)
    pm.reserve_udp_port(20000, project)
Пример #6
0
def port_manager():
    """An instance of port manager"""

    PortManager._instance = None
    p = PortManager.instance()
    p.console_host = "127.0.0.1"
    return p
Пример #7
0
    def allocate_udp_port(request, response):

        pm = ProjectManager.instance()
        project = pm.get_project(request.match_info["project_id"])
        m = PortManager.instance()
        udp_port = m.get_free_udp_port(project)
        response.set_status(201)
        response.json({"udp_port": udp_port})
Пример #8
0
    def allocate_udp_port(request, response):

        pm = ProjectManager.instance()
        project = pm.get_project(request.match_info["project_id"])
        m = PortManager.instance()
        udp_port = m.get_free_udp_port(project)
        response.set_status(201)
        response.json({"udp_port": udp_port})
Пример #9
0
    def _getDebugData():
        try:
            addrs = [
                "* {}: {}".format(key, val)
                for key, val in psutil.net_if_addrs().items()
            ]
        except UnicodeDecodeError:
            addrs = ["INVALID ADDR WITH UNICODE CHARACTERS"]

        data = """Version: {version}
OS: {os}
Python: {python}
CPU: {cpu}
Memory: {memory}

Networks:
{addrs}
""".format(version=__version__,
           os=platform.platform(),
           python=platform.python_version(),
           memory=psutil.virtual_memory(),
           cpu=psutil.cpu_times(),
           addrs="\n".join(addrs))

        try:
            connections = psutil.net_connections()
        # You need to be root for OSX
        except psutil.AccessDenied:
            connections = None

        if connections:
            data += "\n\nConnections:\n"
            for port in PortManager.instance().tcp_ports:
                found = False
                for open_port in connections:
                    if open_port.laddr[1] == port:
                        found = True
                data += "TCP {}: {}\n".format(port, found)
            for port in PortManager.instance().udp_ports:
                found = False
                for open_port in connections:
                    if open_port.laddr[1] == port:
                        found = True
                data += "UDP {}: {}\n".format(port, found)
        return data
Пример #10
0
    def _getDebugData():
        try:
            addrs = ["* {}: {}".format(key, val) for key, val in psutil.net_if_addrs().items()]
        except UnicodeDecodeError:
            addrs = ["INVALID ADDR WITH UNICODE CHARACTERS"]

        data = """Version: {version}
OS: {os}
Python: {python}
CPU: {cpu}
Memory: {memory}

Networks:
{addrs}
""".format(
            version=__version__,
            os=platform.platform(),
            python=platform.python_version(),
            memory=psutil.virtual_memory(),
            cpu=psutil.cpu_times(),
            addrs="\n".join(addrs)
        )

        try:
            connections = psutil.net_connections()
        # You need to be root for OSX
        except psutil.AccessDenied:
            connections = None

        if connections:
            data += "\n\nConnections:\n"
            for port in PortManager.instance().tcp_ports:
                found = False
                for open_port in connections:
                    if open_port.laddr[1] == port:
                        found = True
                data += "TCP {}: {}\n".format(port, found)
            for port in PortManager.instance().udp_ports:
                found = False
                for open_port in connections:
                    if open_port.laddr[1] == port:
                        found = True
                data += "UDP {}: {}\n".format(port, found)
        return data
Пример #11
0
def test_reserve_tcp_port_already_used():
    """
    This test simulate a scenario where the port is already taken
    by another programm on the server
    """

    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    with patch("gns3server.compute.port_manager.PortManager._check_port") as mock_check:

        def execute_mock(host, port, *args):
            if port == 2001:
                raise OSError("Port is already used")
            else:
                return True

        mock_check.side_effect = execute_mock

        with patch("gns3server.compute.project.Project.emit") as mock_emit:
            port = pm.reserve_tcp_port(2001, project)
            assert port != 2001
Пример #12
0
def test_reserve_udp_port():

    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    pm.reserve_udp_port(20000, project)
    with pytest.raises(aiohttp.web.HTTPConflict):
        pm.reserve_udp_port(20000, project)
Пример #13
0
def test_reserve_tcp_port_already_used():
    """
    This test simulate a scenario where the port is already taken
    by another program on the server
    """

    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    with patch("gns3server.compute.port_manager.PortManager._check_port"
               ) as mock_check:

        def execute_mock(host, port, *args):
            if port == 2001:
                raise OSError("Port is already used")
            else:
                return True

        mock_check.side_effect = execute_mock

        with patch("gns3server.compute.project.Project.emit"):
            port = pm.reserve_tcp_port(2001, project)
            assert port != 2001
Пример #14
0
def test_set_console_host(config):
    """
    If allow remote connection we need to bind console host
    to 0.0.0.0
    """
    p = PortManager()
    config.set_section_config("Server", {"allow_remote_console": False})
    p.console_host = "10.42.1.42"
    assert p.console_host == "10.42.1.42"
    p = PortManager()
    config.set_section_config("Server", {"allow_remote_console": True})
    p.console_host = "10.42.1.42"
    assert p.console_host == "0.0.0.0"
Пример #15
0
    def network_ports(request, response):

        m = PortManager.instance()
        response.json(m)
Пример #16
0
def port_manager():
    """An instance of port manager"""
    PortManager._instance = None
    p = PortManager.instance()
    p.console_host = "127.0.0.1"
    return p
Пример #17
0
def test_find_unused_port_invalid_range():

    with pytest.raises(aiohttp.web.HTTPConflict):
        p = PortManager().find_unused_port(10000, 1000)
Пример #18
0
def test_release_udp_port():
    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    pm.reserve_udp_port(10000, project)
    pm.release_udp_port(10000, project)
    pm.reserve_udp_port(10000, project)
Пример #19
0
def test_find_unused_port():

    p = PortManager().find_unused_port(1000, 10000)
    assert p is not None
Пример #20
0
def test_reserve_tcp_port_outside_range():
    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    with patch("gns3server.compute.project.Project.emit") as mock_emit:
        port = pm.reserve_tcp_port(80, project)
        assert port != 80
Пример #21
0
 def compute(request, response):
     response.template("compute.html",
                       port_manager=PortManager.instance(),
                       project_manager=ProjectManager.instance())
Пример #22
0
 def compute(request, response):
     response.template("compute.html",
                       port_manager=PortManager.instance(),
                       project_manager=ProjectManager.instance())
Пример #23
0
def test_reserve_udp_port_outside_range():
    pm = PortManager()
    project = Project(project_id=str(uuid.uuid4()))
    with pytest.raises(aiohttp.web.HTTPConflict):
        pm.reserve_udp_port(80, project)