mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Merge pull request #1332 from GNS3/global-vars
Docker `ExtraHosts`, global variables for project and supplier support, Fixes: #2482
This commit is contained in:
20 files changed
+507
-32
No files matched your search
@@ -61,6 +61,7 @@ def test_json(vm, project):
|
||||
'console_resolution': '1024x768',
|
||||
'console_http_port': 80,
|
||||
'console_http_path': '/',
|
||||
'extra_hosts': None,
|
||||
'aux': vm.aux,
|
||||
'start_command': vm.start_command,
|
||||
'environment': vm.environment,
|
||||
@@ -202,6 +203,77 @@ def test_create_vnc(loop, project, manager):
|
||||
assert vm._console_type == "vnc"
|
||||
|
||||
|
||||
def test_create_with_extra_hosts(loop, project, manager):
|
||||
extra_hosts = "test:199.199.199.1\ntest2:199.199.199.1"
|
||||
|
||||
response = {
|
||||
"Id": "e90e34656806",
|
||||
"Warnings": []
|
||||
}
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
||||
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", extra_hosts=extra_hosts)
|
||||
loop.run_until_complete(asyncio.async(vm.create()))
|
||||
called_kwargs = mock.call_args[1]
|
||||
assert "GNS3_EXTRA_HOSTS=199.199.199.1\ttest\n199.199.199.1\ttest2" in called_kwargs["data"]["Env"]
|
||||
assert vm._extra_hosts == extra_hosts
|
||||
|
||||
|
||||
def test_create_with_extra_hosts_wrong_format(loop, project, manager):
|
||||
extra_hosts = "test"
|
||||
|
||||
response = {
|
||||
"Id": "e90e34656806",
|
||||
"Warnings": []
|
||||
}
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
||||
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", extra_hosts=extra_hosts)
|
||||
with pytest.raises(DockerError):
|
||||
loop.run_until_complete(asyncio.async(vm.create()))
|
||||
|
||||
|
||||
def test_create_with_empty_extra_hosts(loop, project, manager):
|
||||
extra_hosts = "test:\n"
|
||||
|
||||
response = {
|
||||
"Id": "e90e34656806",
|
||||
"Warnings": []
|
||||
}
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
||||
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", extra_hosts=extra_hosts)
|
||||
loop.run_until_complete(asyncio.async(vm.create()))
|
||||
called_kwargs = mock.call_args[1]
|
||||
assert len([ e for e in called_kwargs["data"]["Env"] if "GNS3_EXTRA_HOSTS" in e]) == 0
|
||||
|
||||
|
||||
def test_create_with_project_variables(loop, project, manager):
|
||||
response = {
|
||||
"Id": "e90e34656806",
|
||||
"Warnings": []
|
||||
}
|
||||
|
||||
project.variables = [
|
||||
{"name": "VAR1"},
|
||||
{"name": "VAR2", "value": "VAL1"},
|
||||
{"name": "VAR3", "value": "2x${VAR2}"}
|
||||
]
|
||||
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
||||
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
|
||||
loop.run_until_complete(asyncio.async(vm.create()))
|
||||
called_kwargs = mock.call_args[1]
|
||||
assert "VAR1=" in called_kwargs["data"]["Env"]
|
||||
assert "VAR2=VAL1" in called_kwargs["data"]["Env"]
|
||||
assert "VAR3=2xVAL1" in called_kwargs["data"]["Env"]
|
||||
project.variables = None
|
||||
|
||||
|
||||
def test_create_start_cmd(loop, project, manager):
|
||||
|
||||
response = {
|
||||
|
||||
@@ -92,9 +92,29 @@ def test_changing_path_not_allowed(tmpdir):
|
||||
p.path = str(tmpdir)
|
||||
|
||||
|
||||
def test_variables(tmpdir):
|
||||
variables = [{"name": "VAR1", "value": "VAL1"}]
|
||||
p = Project(project_id=str(uuid4()), variables=variables)
|
||||
assert p.variables == variables
|
||||
|
||||
|
||||
def test_json(tmpdir):
|
||||
p = Project(project_id=str(uuid4()))
|
||||
assert p.__json__() == {"name": p.name, "project_id": p.id}
|
||||
assert p.__json__() == {
|
||||
"name": p.name,
|
||||
"project_id": p.id,
|
||||
"variables": None
|
||||
}
|
||||
|
||||
|
||||
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_node_working_directory(tmpdir, node):
|
||||
@@ -185,3 +205,10 @@ def test_emit(async_run):
|
||||
(action, event, context) = async_run(queue.get(0.5))
|
||||
assert action == "test"
|
||||
assert context["project_id"] == project.id
|
||||
|
||||
|
||||
def test_update_project(loop):
|
||||
variables = [{"name": "TEST", "value": "VAL"}]
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
loop.run_until_complete(asyncio.async(project.update(variables=variables)))
|
||||
assert project.variables == variables
|
||||
@@ -77,19 +77,35 @@ def test_json(tmpdir):
|
||||
"show_layers": False,
|
||||
"snap_to_grid": False,
|
||||
"grid_size": 0,
|
||||
"supplier": None,
|
||||
"variables": None
|
||||
}
|
||||
|
||||
|
||||
def test_update(controller, async_run):
|
||||
project = Project(controller=controller, name="Hello")
|
||||
controller._notification = MagicMock()
|
||||
|
||||
assert project.name == "Hello"
|
||||
async_run(project.update(name="World"))
|
||||
assert project.name == "World"
|
||||
controller.notification.emit.assert_any_call("project.updated", project.__json__())
|
||||
|
||||
|
||||
def test_update_on_compute(controller, async_run):
|
||||
variables = [{"name": "TEST", "value": "VAL1"}]
|
||||
compute = MagicMock()
|
||||
compute.id = "local"
|
||||
project = Project(controller=controller, name="Test")
|
||||
project._project_created_on_compute = [compute]
|
||||
controller._notification = MagicMock()
|
||||
|
||||
async_run(project.update(variables=variables))
|
||||
|
||||
compute.put.assert_any_call('/projects/{}'.format(project.id), {
|
||||
"variables": variables
|
||||
})
|
||||
|
||||
|
||||
def test_path(tmpdir):
|
||||
|
||||
directory = Config.instance().get_section_config("Server").get("projects_path")
|
||||
@@ -148,7 +164,8 @@ def test_add_node_local(async_run, controller):
|
||||
compute.post.assert_any_call('/projects', data={
|
||||
"name": project._name,
|
||||
"project_id": project._id,
|
||||
"path": project._path
|
||||
"path": project._path,
|
||||
"variables": None
|
||||
})
|
||||
compute.post.assert_any_call('/projects/{}/vpcs/nodes'.format(project.id),
|
||||
data={'node_id': node.id,
|
||||
@@ -176,7 +193,8 @@ def test_add_node_non_local(async_run, controller):
|
||||
|
||||
compute.post.assert_any_call('/projects', data={
|
||||
"name": project._name,
|
||||
"project_id": project._id
|
||||
"project_id": project._id,
|
||||
"variables": None
|
||||
})
|
||||
compute.post.assert_any_call('/projects/{}/vpcs/nodes'.format(project.id),
|
||||
data={'node_id': node.id,
|
||||
@@ -216,7 +234,8 @@ def test_add_node_from_appliance(async_run, controller):
|
||||
compute.post.assert_any_call('/projects', data={
|
||||
"name": project._name,
|
||||
"project_id": project._id,
|
||||
"path": project._path
|
||||
"path": project._path,
|
||||
"variables": None
|
||||
})
|
||||
compute.post.assert_any_call('/projects/{}/vpcs/nodes'.format(project.id),
|
||||
data={'node_id': node.id,
|
||||
@@ -395,6 +414,26 @@ def test_clean_pictures(async_run, project, controller):
|
||||
assert not os.path.exists(os.path.join(project.pictures_directory, "test2.png"))
|
||||
|
||||
|
||||
def test_clean_pictures_and_keep_supplier_logo(async_run, project, controller):
|
||||
"""
|
||||
When a project is close old pictures should be removed
|
||||
"""
|
||||
project.supplier = {
|
||||
'logo': 'logo.png'
|
||||
}
|
||||
|
||||
drawing = async_run(project.add_drawing())
|
||||
drawing._svg = "test.png"
|
||||
open(os.path.join(project.pictures_directory, "test.png"), "w+").close()
|
||||
open(os.path.join(project.pictures_directory, "test2.png"), "w+").close()
|
||||
open(os.path.join(project.pictures_directory, "logo.png"), "w+").close()
|
||||
|
||||
async_run(project.close())
|
||||
assert os.path.exists(os.path.join(project.pictures_directory, "test.png"))
|
||||
assert not os.path.exists(os.path.join(project.pictures_directory, "test2.png"))
|
||||
assert os.path.exists(os.path.join(project.pictures_directory, "logo.png"))
|
||||
|
||||
|
||||
def test_delete(async_run, project, controller):
|
||||
assert os.path.exists(project.path)
|
||||
async_run(project.delete())
|
||||
|
||||
@@ -53,6 +53,8 @@ def test_project_to_topology_empty(tmpdir):
|
||||
"drawings": []
|
||||
},
|
||||
"type": "topology",
|
||||
"supplier": None,
|
||||
"variables": None,
|
||||
"version": __version__
|
||||
}
|
||||
|
||||
@@ -81,6 +83,26 @@ def test_basic_topology(tmpdir, async_run, controller):
|
||||
assert topo["topology"]["drawings"][0] == drawing.__json__(topology_dump=True)
|
||||
|
||||
|
||||
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_topology(tmpdir):
|
||||
data = {
|
||||
"project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
|
||||
@@ -137,3 +159,55 @@ def test_load_newer_topology(tmpdir):
|
||||
json.dump(data, f)
|
||||
with pytest.raises(aiohttp.web.HTTPConflict):
|
||||
topo = load_topology(path)
|
||||
|
||||
|
||||
def test_load_topology_with_variables(tmpdir):
|
||||
variables = [
|
||||
{"name": "TEST1"},
|
||||
{"name": "TEST2", "value": "value1"}
|
||||
]
|
||||
data = {
|
||||
"project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
|
||||
"name": "Test",
|
||||
"revision": GNS3_FILE_FORMAT_REVISION,
|
||||
"topology": {
|
||||
"nodes": [],
|
||||
"links": [],
|
||||
"computes": [],
|
||||
"drawings": []
|
||||
},
|
||||
"variables": variables,
|
||||
"type": "topology",
|
||||
"version": __version__}
|
||||
|
||||
path = str(tmpdir / "test.gns3")
|
||||
with open(path, "w+") as f:
|
||||
json.dump(data, f)
|
||||
topo = load_topology(path)
|
||||
assert topo == data
|
||||
|
||||
|
||||
def test_load_topology_with_supplier(tmpdir):
|
||||
supplier = {
|
||||
'logo': 'logo.png',
|
||||
'url': 'http://example.com'
|
||||
}
|
||||
data = {
|
||||
"project_id": "69f26504-7aa3-48aa-9f29-798d44841211",
|
||||
"name": "Test",
|
||||
"revision": GNS3_FILE_FORMAT_REVISION,
|
||||
"topology": {
|
||||
"nodes": [],
|
||||
"links": [],
|
||||
"computes": [],
|
||||
"drawings": []
|
||||
},
|
||||
"supplier": supplier,
|
||||
"type": "topology",
|
||||
"version": __version__}
|
||||
|
||||
path = str(tmpdir / "test.gns3")
|
||||
with open(path, "w+") as f:
|
||||
json.dump(data, f)
|
||||
topo = load_topology(path)
|
||||
assert topo == data
|
||||
@@ -32,7 +32,7 @@ pytestmark = pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supp
|
||||
@pytest.fixture
|
||||
def base_params():
|
||||
"""Return standard parameters"""
|
||||
return {"name": "PC TEST 1", "image": "nginx", "start_command": "nginx-daemon", "adapters": 2, "environment": "YES=1\nNO=0", "console_type": "telnet", "console_resolution": "1280x1024"}
|
||||
return {"name": "PC TEST 1", "image": "nginx", "start_command": "nginx-daemon", "adapters": 2, "environment": "YES=1\nNO=0", "console_type": "telnet", "console_resolution": "1280x1024", "extra_hosts": "test:127.0.0.1"}
|
||||
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
@@ -69,7 +69,7 @@ def test_docker_create(http_compute, project, base_params):
|
||||
assert response.json["adapters"] == 2
|
||||
assert response.json["environment"] == "YES=1\nNO=0"
|
||||
assert response.json["console_resolution"] == "1280x1024"
|
||||
|
||||
assert response.json["extra_hosts"] == "test:127.0.0.1"
|
||||
|
||||
def test_docker_start(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start", return_value=True) as mock:
|
||||
@@ -150,7 +150,8 @@ def test_docker_update(http_compute, vm, tmpdir, free_console_port):
|
||||
response = http_compute.put("/projects/{project_id}/docker/nodes/{node_id}".format(project_id=vm["project_id"], node_id=vm["node_id"]), {"name": "test",
|
||||
"console": free_console_port,
|
||||
"start_command": "yes",
|
||||
"environment": "GNS3=1\nGNS4=0"},
|
||||
"environment": "GNS3=1\nGNS4=0",
|
||||
"extra_hosts": "test:127.0.0.1"},
|
||||
example=True)
|
||||
assert mock.called
|
||||
assert response.status == 200
|
||||
@@ -158,6 +159,7 @@ def test_docker_update(http_compute, vm, tmpdir, free_console_port):
|
||||
assert response.json["console"] == free_console_port
|
||||
assert response.json["start_command"] == "yes"
|
||||
assert response.json["environment"] == "GNS3=1\nGNS4=0"
|
||||
assert response.json["extra_hosts"] == "test:127.0.0.1"
|
||||
|
||||
|
||||
def test_docker_start_capture(http_compute, vm, tmpdir, project):
|
||||
|
||||
@@ -21,9 +21,6 @@ This test suite check /project endpoint
|
||||
|
||||
import uuid
|
||||
import os
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import zipfile
|
||||
|
||||
from unittest.mock import patch
|
||||
from tests.utils import asyncio_patch
|
||||
@@ -60,9 +57,10 @@ def test_show_project(http_compute):
|
||||
response = http_compute.post("/projects", query)
|
||||
assert response.status == 201
|
||||
response = http_compute.get("/projects/40010203-0405-0607-0809-0a0b0c0d0e02", example=True)
|
||||
assert len(response.json.keys()) == 2
|
||||
assert len(response.json.keys()) == 3
|
||||
assert response.json["project_id"] == "40010203-0405-0607-0809-0a0b0c0d0e02"
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["variables"] is None
|
||||
|
||||
|
||||
def test_show_project_invalid_uuid(http_compute):
|
||||
@@ -93,6 +91,23 @@ def test_delete_project(http_compute, project):
|
||||
assert mock.called
|
||||
|
||||
|
||||
def test_update_project(http_compute):
|
||||
query = {"name": "test", "project_id": "51010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_compute.post("/projects", query)
|
||||
assert response.status == 201
|
||||
|
||||
query = {
|
||||
"variables": [{"name": "TEST1", "value": "VAL1"}]
|
||||
}
|
||||
response = http_compute.put(
|
||||
"/projects/{project_id}".format(project_id="51010203-0405-0607-0809-0a0b0c0d0e0f"),
|
||||
query,
|
||||
example=True
|
||||
)
|
||||
assert response.status == 200
|
||||
assert response.json["variables"] == [{"name": "TEST1", "value": "VAL1"}]
|
||||
|
||||
|
||||
def test_delete_project_invalid_uuid(http_compute):
|
||||
response = http_compute.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
|
||||
assert response.status == 404
|
||||
|
||||
@@ -67,6 +67,31 @@ def test_create_project_with_uuid(http_controller):
|
||||
assert response.json["name"] == "test"
|
||||
|
||||
|
||||
def test_create_project_with_variables(http_controller):
|
||||
variables = [
|
||||
{"name": "TEST1"},
|
||||
{"name": "TEST2", "value": "value1"}
|
||||
]
|
||||
query = {"name": "test", "project_id": "30010203-0405-0607-0809-0a0b0c0d0e0f", "variables": variables}
|
||||
response = http_controller.post("/projects", query)
|
||||
assert response.status == 201
|
||||
assert response.json["variables"] == [
|
||||
{"name": "TEST1"},
|
||||
{"name": "TEST2", "value": "value1"}
|
||||
]
|
||||
|
||||
|
||||
def test_create_project_with_supplier(http_controller):
|
||||
supplier = {
|
||||
'logo': 'logo.png',
|
||||
'url': 'http://example.com'
|
||||
}
|
||||
query = {"name": "test", "project_id": "30010203-0405-0607-0809-0a0b0c0d0e0f", "supplier": supplier}
|
||||
response = http_controller.post("/projects", query)
|
||||
assert response.status == 201
|
||||
assert response.json["supplier"] == supplier
|
||||
|
||||
|
||||
def test_update_project(http_controller):
|
||||
query = {"name": "test", "project_id": "10010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_controller.post("/projects", query)
|
||||
@@ -79,6 +104,20 @@ def test_update_project(http_controller):
|
||||
assert response.json["name"] == "test2"
|
||||
|
||||
|
||||
def test_update_project_with_variables(http_controller):
|
||||
variables = [
|
||||
{"name": "TEST1"},
|
||||
{"name": "TEST2", "value": "value1"}
|
||||
]
|
||||
query = {"name": "test", "project_id": "10010203-0405-0607-0809-0a0b0c0d0e0f", "variables": variables}
|
||||
response = http_controller.post("/projects", query)
|
||||
assert response.status == 201
|
||||
query = {"name": "test2"}
|
||||
response = http_controller.put("/projects/10010203-0405-0607-0809-0a0b0c0d0e0f", query, example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["variables"] == variables
|
||||
|
||||
|
||||
def test_list_projects(http_controller, tmpdir):
|
||||
http_controller.post("/projects", {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"})
|
||||
response = http_controller.get("/projects", example=True)
|
||||
|
||||
Reference in new issue
Block a user