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
def test_json(tmpdir): p = Project(project_id=str(uuid4())) assert p.__json__() == { "name": p.name, "project_id": p.id, "variables": None }
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" }]
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)
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))
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)
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 }
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)
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))
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
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))
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))
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)
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)
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
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)
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)
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))
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))
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
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)
def test_affect_uuid(): p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f') assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f'
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
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
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)
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
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
def test_variables(tmpdir): variables = [{"name": "VAR1", "value": "VAL1"}] p = Project(project_id=str(uuid4()), variables=variables) assert p.variables == variables
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
def test_json(tmpdir): p = Project(project_id=str(uuid4())) assert p.__json__() == {"name": p.name, "project_id": p.id}