Example #1
0
async def test_project_add_node(manager):

    project = Project(project_id=str(uuid4()))
    node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project,
                  manager)
    project.add_node(node)
    assert len(project.nodes) == 1
Example #2
0
def test_json(tmpdir):
    p = Project(project_id=str(uuid4()))
    assert p.__json__() == {
        "name": p.name,
        "project_id": p.id,
        "variables": None
    }
Example #3
0
def test_list_files(tmpdir, loop):

    with patch("gns3server.config.Config.get_section_config",
               return_value={"projects_path": str(tmpdir)}):
        project = Project(project_id=str(uuid4()))
        path = project.path
        os.makedirs(os.path.join(path, "vm-1", "dynamips"))
        with open(os.path.join(path, "vm-1", "dynamips", "test.bin"),
                  "w+") as f:
            f.write("test")
        open(os.path.join(path, "vm-1", "dynamips", "test.ghost"),
             "w+").close()
        with open(os.path.join(path, "test.txt"), "w+") as f:
            f.write("test2")

        files = loop.run_until_complete(
            asyncio.ensure_future(project.list_files()))

        assert files == [{
            "path": "test.txt",
            "md5sum": "ad0234829205b9033196ba818f7a872b"
        }, {
            "path": os.path.join("vm-1", "dynamips", "test.bin"),
            "md5sum": "098f6bcd4621d373cade4e832627b4f6"
        }]
Example #4
0
async def test_changing_path_not_allowed(tmpdir):

    with patch("gns3server.compute.project.Project.is_local",
               return_value=False):
        with pytest.raises(aiohttp.web.HTTPForbidden):
            p = Project(project_id=str(uuid4()))
            p.path = str(tmpdir)
Example #5
0
def test_node_working_directory(tmpdir, node):
    directory = Config.instance().get_section_config("Server").get("projects_path")

    with patch("gns3server.compute.project.Project.is_local", return_value=True):
        p = Project(project_id=str(uuid4()))
        assert p.node_working_directory(node) == os.path.join(directory, p.id, 'project-files', node.module_name, node.id)
        assert os.path.exists(p.node_working_directory(node))
Example #6
0
def test_project_delete_permission_issue(loop):
    project = Project(project_id=str(uuid4()))
    directory = project.path
    assert os.path.exists(directory)
    os.chmod(directory, 0)
    with pytest.raises(aiohttp.web.HTTPInternalServerError):
        loop.run_until_complete(asyncio.async(project.delete()))
    os.chmod(directory, 700)
Example #7
0
def test_json_with_variables(tmpdir):
    variables = [{"name": "VAR1", "value": "VAL1"}]
    p = Project(project_id=str(uuid4()), variables=variables)
    assert p.__json__() == {
        "name": p.name,
        "project_id": p.id,
        "variables": variables
    }
Example #8
0
def test_project_delete_permission_issue(loop):
    project = Project(project_id=str(uuid4()))
    directory = project.path
    assert os.path.exists(directory)
    os.chmod(directory, 0)
    with pytest.raises(aiohttp.web.HTTPInternalServerError):
        loop.run_until_complete(asyncio.ensure_future(project.delete()))
    os.chmod(directory, 700)
Example #9
0
async def test_node_working_directory(node, projects_dir):

    directory = projects_dir
    with patch("gns3server.compute.project.Project.is_local",
               return_value=True):
        p = Project(project_id=str(uuid4()))
        assert p.node_working_directory(node) == os.path.join(
            directory, p.id, 'project-files', node.module_name, node.id)
        assert os.path.exists(p.node_working_directory(node))
Example #10
0
def test_emit(async_run):

    with NotificationManager.instance().queue() as queue:
        (action, event, context) = async_run(queue.get(0.5))  #  Ping

        project = Project(project_id=str(uuid4()))
        project.emit("test", {})
        (action, event, context) = async_run(queue.get(0.5))
        assert action == "test"
        assert context["project_id"] == project.id
Example #11
0
def test_emit(async_run):

    with NotificationManager.instance().queue() as queue:
        (action, event, context) = async_run(queue.get(0.5))  #  Ping

        project = Project(project_id=str(uuid4()))
        project.emit("test", {})
        (action, event, context) = async_run(queue.get(0.5))
        assert action == "test"
        assert context["project_id"] == project.id
Example #12
0
async def test_node_working_path(node, projects_dir):

    directory = projects_dir
    with patch("gns3server.compute.project.Project.is_local",
               return_value=True):
        p = Project(project_id=str(uuid4()))
        assert p.node_working_path(node) == os.path.join(
            directory, p.id, 'project-files', node.module_name, node.id)
        # after this execution directory structure should not be created
        assert not os.path.exists(p.node_working_path(node))
Example #13
0
def test_node_working_directory(tmpdir, node):
    directory = Config.instance().get_section_config("Server").get(
        "projects_path")

    with patch("gns3server.compute.project.Project.is_local",
               return_value=True):
        p = Project(project_id=str(uuid4()))
        assert p.node_working_directory(node) == os.path.join(
            directory, p.id, 'project-files', node.module_name, node.id)
        assert os.path.exists(p.node_working_directory(node))
Example #14
0
def test_clean_tmp_directory(async_run):
    """
    The tmp directory should be clean at project open and close
    """

    p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f')
    path = p.tmp_working_directory()
    os.makedirs(path)
    async_run(p.close())
    assert not os.path.exists(path)

    os.makedirs(path)
    p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f')
    assert not os.path.exists(path)
Example #15
0
async def test_clean_tmp_directory():
    """
    The tmp directory should be clean at project open and close
    """

    p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f')
    path = p.tmp_working_directory()
    os.makedirs(path)
    await p.close()
    assert not os.path.exists(path)

    os.makedirs(path)
    p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f')
    assert not os.path.exists(path)
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)
Example #17
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
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
Example #19
0
async def test_project_delete():

    project = Project(project_id=str(uuid4()))
    directory = project.path
    assert os.path.exists(directory)
    await project.delete()
    assert os.path.exists(directory) is False
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)
Example #21
0
async def test_project_delete_permission_issue():

    project = Project(project_id=str(uuid4()))
    directory = project.path
    assert os.path.exists(directory)
    os.chmod(directory, 0)
    with pytest.raises(aiohttp.web.HTTPInternalServerError):
        await project.delete()
    os.chmod(directory, 700)
Example #22
0
async def test_path(projects_dir):

    directory = projects_dir
    with patch("gns3server.compute.project.Project.is_local",
               return_value=True):
        with patch("gns3server.utils.path.get_default_project_directory",
                   return_value=directory):
            p = Project(project_id=str(uuid4()))
            assert p.path == os.path.join(directory, p.id)
            assert os.path.exists(os.path.join(directory, p.id))
Example #23
0
def test_path(tmpdir):

    directory = Config.instance().get_section_config("Server").get(
        "projects_path")

    with patch("gns3server.compute.project.Project.is_local",
               return_value=True):
        with patch("gns3server.utils.path.get_default_project_directory",
                   return_value=directory):
            p = Project(project_id=str(uuid4()))
            assert p.path == os.path.join(directory, p.id)
            assert os.path.exists(os.path.join(directory, p.id))
Example #24
0
def test_list_files(tmpdir, loop):

    with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
        project = Project(project_id=str(uuid4()))
        path = project.path
        os.makedirs(os.path.join(path, "vm-1", "dynamips"))
        with open(os.path.join(path, "vm-1", "dynamips", "test.bin"), "w+") as f:
            f.write("test")
        open(os.path.join(path, "vm-1", "dynamips", "test.ghost"), "w+").close()
        with open(os.path.join(path, "test.txt"), "w+") as f:
            f.write("test2")

        files = loop.run_until_complete(asyncio.async(project.list_files()))

        assert files == [
            {
                "path": "test.txt",
                "md5sum": "ad0234829205b9033196ba818f7a872b"
            },
            {
                "path": os.path.join("vm-1", "dynamips", "test.bin"),
                "md5sum": "098f6bcd4621d373cade4e832627b4f6"
            }
        ]
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
Example #26
0
def test_init_path(tmpdir):

    with patch("gns3server.compute.project.Project.is_local",
               return_value=True):
        p = Project(path=str(tmpdir), project_id=str(uuid4()))
        assert p.path == str(tmpdir)
Example #27
0
def test_affect_uuid():
    p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f')
    assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f'
Example #28
0
def test_update_project(loop):
    variables = [{"name": "TEST", "value": "VAL"}]
    project = Project(project_id=str(uuid.uuid4()))
    loop.run_until_complete(
        asyncio.ensure_future(project.update(variables=variables)))
    assert project.variables == variables
Example #29
0
def test_project_add_node(manager):
    project = Project(project_id=str(uuid4()))
    node = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
    project.add_node(node)
    assert len(project.nodes) == 1
Example #30
0
def test_changing_path_not_allowed(tmpdir):
    with patch("gns3server.compute.project.Project.is_local", return_value=False):
        with pytest.raises(aiohttp.web.HTTPForbidden):
            p = Project(project_id=str(uuid4()))
            p.path = str(tmpdir)
Example #31
0
def test_project_delete(loop):
    project = Project(project_id=str(uuid4()))
    directory = project.path
    assert os.path.exists(directory)
    loop.run_until_complete(asyncio.ensure_future(project.delete()))
    assert os.path.exists(directory) is False
Example #32
0
async def test_update_project():

    variables = [{"name": "TEST", "value": "VAL"}]
    project = Project(project_id=str(uuid.uuid4()))
    await project.update(variables=variables)
    assert project.variables == variables
Example #33
0
def test_variables(tmpdir):
    variables = [{"name": "VAR1", "value": "VAL1"}]
    p = Project(project_id=str(uuid4()), variables=variables)
    assert p.variables == variables
Example #34
0
def test_project_delete(loop):
    project = Project(project_id=str(uuid4()))
    directory = project.path
    assert os.path.exists(directory)
    loop.run_until_complete(asyncio.async(project.delete()))
    assert os.path.exists(directory) is False
Example #35
0
def test_json(tmpdir):
    p = Project(project_id=str(uuid4()))
    assert p.__json__() == {"name": p.name, "project_id": p.id}