Embed the appliances in the server. (#927)

This add a /appliances call
This commit is contained in:
Julien Duponchelle authored and GitHub committed 2017-04-12 14:35:49 +02:00
1 parent 4f1b738ef5
commit 9dc6f0f486
11 files changed
+432 -27

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
#!/usr/bin/env python
#
# Copyright (C) 2016 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from gns3server.controller.appliance import Appliance
def test_appliance_json():
a = Appliance(None, {
"node_type": "qemu",
"name": "Test",
"default_name_format": "{name}-{0}",
"category": 0,
"symbol": "qemu.svg",
"server": "local"
})
assert a.__json__() == {
"appliance_id": a.id,
"node_type": "qemu",
"builtin": False,
"name": "Test",
"default_name_format": "{name}-{0}",
"category": "router",
"symbol": "qemu.svg",
"compute_id": "local"
}
+21 -1
View File
@@ -53,7 +53,7 @@ def test_load_controller_settings(controller, controller_config_path, async_run)
"compute_id": "test1"
}
]
data["settings"] = {"IOU": True}
data["settings"] = {"IOU": {"test": True}}
data["gns3vm"] = {"vmname": "Test VM"}
with open(controller_config_path, "w+") as f:
json.dump(data, f)
@@ -468,3 +468,23 @@ def test_load_base_files(controller, config, tmpdir):
# Check is the file has not been overwrite
with open(str(tmpdir / 'iou_l2_base_startup-config.txt')) as f:
assert f.read() == 'test'
def test_appliance_templates(controller, async_run):
controller.load_appliances()
assert len(controller.appliance_templates) > 0
def test_load_appliances(controller):
controller._settings = {
"Qemu": {
"vms": [
{
"name": "Test"
}
]
}
}
controller.load_appliances()
assert "Test" in [appliance.name for appliance in controller.appliances.values()]
assert "Cloud" in [appliance.name for appliance in controller.appliances.values()]
+37 -3
View File
@@ -18,17 +18,15 @@
import os
import sys
import uuid
import json
import pytest
import aiohttp
import zipfile
from unittest.mock import MagicMock
from tests.utils import AsyncioMagicMock, asyncio_patch
from unittest.mock import patch
from uuid import uuid4
from gns3server.controller.project import Project
from gns3server.controller.appliance import Appliance
from gns3server.controller.ports.ethernet_port import EthernetPort
from gns3server.config import Config
@@ -182,6 +180,42 @@ def test_add_node_non_local(async_run, controller):
controller.notification.emit.assert_any_call("node.created", node.__json__())
def test_add_node_from_appliance(async_run, controller):
"""
For a local server we send the project path
"""
compute = MagicMock()
compute.id = "local"
project = Project(controller=controller, name="Test")
controller._notification = MagicMock()
controller._appliances["fakeid"] = Appliance("fakeid", {
"server": "local",
"name": "Test",
"default_name_format": "{name}-{0}",
"node_type": "vpcs"
})
controller._computes["local"] = compute
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
node = async_run(project.add_node_from_appliance("fakeid", x=23, y=12))
compute.post.assert_any_call('/projects', data={
"name": project._name,
"project_id": project._id,
"path": project._path
})
compute.post.assert_any_call('/projects/{}/vpcs/nodes'.format(project.id),
data={'node_id': node.id,
'name': 'Test-1'},
timeout=120)
assert compute in project._project_created_on_compute
controller.notification.emit.assert_any_call("node.created", node.__json__())
def test_create_iou_on_multiple_node(async_run, controller):
"""
Due to mac address collision you can't create an IOU node
@@ -16,10 +16,73 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import uuid
import pytest
from unittest.mock import MagicMock
from tests.utils import asyncio_patch
from gns3server.controller import Controller
from gns3server.controller.appliance import Appliance
@pytest.fixture
def compute(http_controller, async_run):
compute = MagicMock()
compute.id = "example.com"
compute.host = "example.org"
Controller.instance()._computes = {"example.com": compute}
return compute
@pytest.fixture
def project(http_controller, async_run):
return async_run(Controller.instance().add_project(name="Test"))
def test_appliance_list(http_controller, controller):
response = http_controller.get("/appliances/templates")
id = str(uuid.uuid4())
controller.load_appliances()
controller._appliances[id] = Appliance(id, {
"node_type": "qemu",
"category": 0,
"name": "test",
"symbol": "guest.svg",
"default_name_format": "{name}-{0}",
"server": "local"
})
response = http_controller.get("/appliances", example=True)
assert response.status == 200
assert response.route == "/appliances/templates"
assert response.route == "/appliances"
assert len(response.json) > 0
def test_appliance_templates_list(http_controller, controller, async_run):
controller.load_appliances()
response = http_controller.get("/appliances/templates", example=True)
assert response.status == 200
assert len(response.json) > 0
def test_create_node_from_appliance(http_controller, controller, project, compute):
id = str(uuid.uuid4())
controller._appliances = {id: Appliance(id, {
"node_type": "qemu",
"category": 0,
"name": "test",
"symbol": "guest.svg",
"default_name_format": "{name}-{0}",
"server": "example.com"
})}
with asyncio_patch("gns3server.controller.project.Project.add_node_from_appliance") as mock:
response = http_controller.post("/projects/{}/appliances/{}".format(project.id, id), {
"x": 42,
"y": 12
})
mock.assert_called_with(id, x=42, y=12, compute_id=None)
print(response.body)
assert response.route == "/projects/{project_id}/appliances/{appliance_id}"
assert response.status == 201