def compute(controller): compute = Compute("my_compute_id", protocol="https", host="example.com", port=84, controller=controller) compute._connected = True return compute
def test_addProject_with_compute(controller, async_run): uuid1 = str(uuid.uuid4()) compute = Compute("test1", controller=MagicMock()) compute.post = MagicMock() controller._computes = {"test1": compute} project1 = async_run(controller.add_project(project_id=uuid1, name="Test"))
def test_get_url(controller): compute = Compute("my_compute_id", protocol="https", host="localhost", port=84, controller=controller) with patch('gns3server.controller.compute.Compute._getUrl', return_value="returned") as getURL: assert compute.get_url("/test") == 'returned' getURL.assert_called_once_with('/test')
def test_project_to_topology(tmpdir, controller): variables = [{"name": "TEST1"}, {"name": "TEST2", "value": "value1"}] supplier = {'logo': 'logo.png', 'url': 'http://example.com'} project = Project(name="Test", controller=controller) compute = Compute("my_compute", controller) compute.http_query = MagicMock() project.variables = variables project.supplier = supplier topo = project_to_topology(project) assert topo["variables"] == variables assert topo["supplier"] == supplier
def test_load_project(controller, tmpdir, demo_topology, async_run, http_server): with open(str(tmpdir / "demo.gns3"), "w+") as f: json.dump(demo_topology, f) controller._computes["local"] = Compute("local", controller=controller, host=http_server[0], port=http_server[1]) controller._computes["vm"] = controller._computes["local"] with asyncio_patch( "gns3server.compute.vpcs.vpcs_vm.VPCSVM.add_ubridge_udp_connection" ): project = async_run(controller.load_project(str(tmpdir / "demo.gns3"))) assert project.status == "opened" assert len(project.computes) == 1 assert len(project.nodes) == 2 assert project.nodes["64ba8408-afbf-4b66-9cdd-1fd854427478"].name == "PC1" assert len(project.links) == 1 assert project.links["5a3e3a64-e853-4055-9503-4a14e01290f1"].created assert len(project.drawings) == 1 assert project.name == "demo" assert project.scene_height == 500 assert project.scene_width == 700
def test_host_ip(controller): compute = Compute("my_compute_id", protocol="https", host="localhost", port=84, controller=controller) assert compute.host_ip == "127.0.0.1"
def test_project_to_topology(tmpdir, controller): variables = [ {"name": "TEST1"}, {"name": "TEST2", "value": "value1"} ] supplier = { 'logo': 'logo.png', 'url': 'http://example.com' } project = Project(name="Test", controller=controller) compute = Compute("my_compute", controller) compute.http_query = MagicMock() project.variables = variables project.supplier = supplier topo = project_to_topology(project) assert topo["variables"] == variables assert topo["supplier"] == supplier
def test_name(): c = Compute("my_compute_id", protocol="https", host="example.com", port=84, controller=MagicMock(), name=None) assert c.name == "https://example.com:84" c = Compute("world", protocol="https", host="example.com", port=84, controller=MagicMock(), name="hello") assert c.name == "hello" c = Compute("world", protocol="https", host="example.com", port=84, controller=MagicMock(), user="******") assert c.name == "https://[email protected]:84"
def test_basic_topology(tmpdir, async_run, controller): project = Project(name="Test", controller=controller) compute = Compute("my_compute", controller) compute.http_query = MagicMock() with asyncio_patch("gns3server.controller.node.Node.create"): node1 = async_run(project.add_node(compute, "Node 1", str(uuid.uuid4()), node_type="qemu")) node2 = async_run(project.add_node(compute, "Node 2", str(uuid.uuid4()), node_type="qemu")) link = async_run(project.add_link()) async_run(link.add_node(node1, 0, 0)) with asyncio_patch("gns3server.controller.udp_link.UDPLink.create") as mock_udp_create: async_run(link.add_node(node2, 0, 0)) drawing = async_run(project.add_drawing(svg="<svg></svg>")) topo = project_to_topology(project) assert len(topo["topology"]["nodes"]) == 2 assert node1.__json__(topology_dump=True) in topo["topology"]["nodes"] assert topo["topology"]["links"][0] == link.__json__(topology_dump=True) assert topo["topology"]["computes"][0] == compute.__json__(topology_dump=True) assert topo["topology"]["drawings"][0] == drawing.__json__(topology_dump=True)
def test_open(controller, tmpdir, demo_topology, async_run, http_server): with open(str(tmpdir / "demo.gns3"), "w+") as f: json.dump(demo_topology, f) controller._computes["local"] = Compute("local", controller=controller, host=http_server[0], port=http_server[1]) controller._computes["vm"] = controller._computes["local"] project = async_run(controller.load_project(str(tmpdir / "demo.gns3"))) assert project.status == "opened" assert len(project.computes) == 1 assert len(project.nodes) == 2 assert project.nodes["64ba8408-afbf-4b66-9cdd-1fd854427478"].name == "PC1" assert len(project.links) == 1 assert project.links["5a3e3a64-e853-4055-9503-4a14e01290f1"].created assert len(project.drawings) == 1 assert project.name == "demo"
def test_open_missing_compute(controller, tmpdir, demo_topology, async_run, http_server): """ If a compute is missing the project should not be open and the .gns3 should be the one before opening the project """ with open(str(tmpdir / "demo.gns3"), "w+") as f: json.dump(demo_topology, f) controller._computes["local"] = Compute("local", controller=controller, host=http_server[0], port=http_server[1]) with pytest.raises(aiohttp.web_exceptions.HTTPNotFound): project = async_run(controller.load_project(str(tmpdir / "demo.gns3"))) assert controller.get_project( "3c1be6f9-b4ba-4737-b209-63c47c23359f").status == "closed" with open(str(tmpdir / "demo.gns3"), "r") as f: topo = json.load(f) assert len(topo["topology"]["nodes"]) == 2
def compute(): return Compute("example.com", controller=MagicMock())
def test_getUrl(controller): compute = Compute("my_compute_id", protocol="https", host="localhost", port=84, controller=controller) assert compute._getUrl("/test") == "https://localhost:84/v2/compute/test" # IPV6 localhost compute = Compute("my_compute_id", protocol="https", host="::1", port=84, controller=controller) assert compute._getUrl("/test") == "https://[::1]:84/v2/compute/test" # Listen on all interfaces aka 0.0.0.0 require us to connect via 127.0.0.1 compute = Compute("my_compute_id", protocol="https", host="0.0.0.0", port=84, controller=controller) assert compute._getUrl("/test") == "https://127.0.0.1:84/v2/compute/test" # IPV6 compute = Compute("my_compute_id", protocol="https", host="::", port=84, controller=controller) assert compute._getUrl("/test") == "https://[::1]:84/v2/compute/test"
def test_get_ip_on_same_subnet(controller, async_run): compute1 = Compute("compute1", host="192.168.1.1", controller=controller) compute1._interfaces_cache = [ { "ip_address": "127.0.0.1", "netmask": "255.255.255.255" }, { "ip_address": "192.168.2.1", "netmask": "255.255.255.0" }, { "ip_address": "192.168.1.1", "netmask": "255.255.255.0" }, ] # Case 1 both host are on the same network compute2 = Compute("compute2", host="192.168.1.2", controller=controller) compute2._interfaces_cache = [ { "ip_address": "127.0.0.1", "netmask": "255.255.255.255" }, { "ip_address": "192.168.2.2", "netmask": "255.255.255.0" }, { "ip_address": "192.168.1.2", "netmask": "255.255.255.0" } ] assert async_run(compute1.get_ip_on_same_subnet(compute2)) == ("192.168.1.1", "192.168.1.2") # Case 2 compute2 host is on a different network but a common interface is available compute2 = Compute("compute2", host="127.0.0.1", controller=controller) compute2._interfaces_cache = [ { "ip_address": "127.0.0.1", "netmask": "255.255.255.255" }, { "ip_address": "192.168.4.2", "netmask": "255.255.255.0" }, { "ip_address": "192.168.1.2", "netmask": "255.255.255.0" } ] assert async_run(compute1.get_ip_on_same_subnet(compute2)) == ("192.168.1.1", "192.168.1.2") #No common interface compute2 = Compute("compute2", host="127.0.0.1", controller=controller) compute2._interfaces_cache = [ { "ip_address": "127.0.0.1", "netmask": "255.255.255.255" } ] with pytest.raises(ValueError): async_run(compute1.get_ip_on_same_subnet(compute2)) # Ignore 169.254 network because it's for Windows special purpose compute2 = Compute("compute2", host="192.168.1.2", controller=controller) compute1 = Compute("compute1", host="192.168.2.1", controller=controller) compute1._interfaces_cache = [ { "ip_address": "127.0.0.1", "netmask": "255.255.255.255" }, { "ip_address": "169.254.1.1", "netmask": "255.255.0.0" }, ] compute2._interfaces_cache = [ { "ip_address": "127.0.0.1", "netmask": "255.255.255.255" }, { "ip_address": "169.254.2.1", "netmask": "255.255.0.0" }, ] assert async_run(compute1.get_ip_on_same_subnet(compute2)) == ('192.168.2.1', '192.168.1.2')