Refactor template management to use database.

This commit is contained in:
grossmj committed 2021-03-28 11:15:08 +10:30
1 parent b417bc4dec
commit d730c591b3
20 files changed
+1704 -1535

No files matched your search

-27
View File
@@ -445,33 +445,6 @@ def test_appliances(controller, tmpdir):
elif j["name"] == "My Appliance":
assert not j["builtin"]
def test_load_templates(controller):
controller._settings = {}
controller.template_manager.load_templates()
assert "Cloud" in [template.name for template in controller.template_manager.templates.values()]
assert "VPCS" in [template.name for template in controller.template_manager.templates.values()]
for template in controller.template_manager.templates.values():
if template.name == "VPCS":
assert template._settings["properties"] == {"base_script_file": "vpcs_base_config.txt"}
# UUID should not change when you run again the function
for template in controller.template_manager.templates.values():
if template.name == "Test":
qemu_uuid = template.id
elif template.name == "Cloud":
cloud_uuid = template.id
controller.template_manager.load_templates()
for template in controller.template_manager.templates.values():
if template.name == "Test":
assert qemu_uuid == template.id
elif template.name == "Cloud":
assert cloud_uuid == template.id
@pytest.mark.asyncio
async def test_autoidlepc(controller):
+66 -67
View File
@@ -26,7 +26,6 @@ from unittest.mock import patch
from uuid import uuid4
from gns3server.controller.project import Project
from gns3server.controller.template import Template
from gns3server.controller.node import Node
from gns3server.controller.ports.ethernet_port import EthernetPort
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError, ControllerForbiddenError
@@ -343,72 +342,72 @@ async def test_add_node_iou_no_id_available(controller):
await project.add_node(compute, "test1", None, node_type="iou")
@pytest.mark.asyncio
async def test_add_node_from_template(controller):
"""
For a local server we send the project path
"""
compute = MagicMock()
compute.id = "local"
project = Project(controller=controller, name="Test")
project.emit_notification = MagicMock()
template = Template(str(uuid.uuid4()), {
"compute_id": "local",
"name": "Test",
"template_type": "vpcs",
"builtin": False,
})
controller.template_manager.templates[template.id] = template
controller._computes["local"] = compute
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
node = await project.add_node_from_template(template.id, x=23, y=12)
compute.post.assert_any_call('/projects', data={
"name": project._name,
"project_id": project._id,
"path": project._path
})
assert compute in project._project_created_on_compute
project.emit_notification.assert_any_call("node.created", node.__json__())
@pytest.mark.asyncio
async def test_add_builtin_node_from_template(controller):
"""
For a local server we send the project path
"""
compute = MagicMock()
compute.id = "local"
project = Project(controller=controller, name="Test")
project.emit_notification = MagicMock()
template = Template(str(uuid.uuid4()), {
"name": "Builtin-switch",
"template_type": "ethernet_switch",
}, builtin=True)
controller.template_manager.templates[template.id] = template
template.__json__()
controller._computes["local"] = compute
response = MagicMock()
response.json = {"console": 2048}
compute.post = AsyncioMagicMock(return_value=response)
node = await project.add_node_from_template(template.id, x=23, y=12, compute_id="local")
compute.post.assert_any_call('/projects', data={
"name": project._name,
"project_id": project._id,
"path": project._path
})
assert compute in project._project_created_on_compute
project.emit_notification.assert_any_call("node.created", node.__json__())
# @pytest.mark.asyncio
# async def test_add_node_from_template(controller):
# """
# For a local server we send the project path
# """
#
# compute = MagicMock()
# compute.id = "local"
# project = Project(controller=controller, name="Test")
# project.emit_notification = MagicMock()
# template = Template(str(uuid.uuid4()), {
# "compute_id": "local",
# "name": "Test",
# "template_type": "vpcs",
# "builtin": False,
# })
# controller.template_manager.templates[template.id] = template
# controller._computes["local"] = compute
#
# response = MagicMock()
# response.json = {"console": 2048}
# compute.post = AsyncioMagicMock(return_value=response)
#
# node = await project.add_node_from_template(template.id, x=23, y=12)
# compute.post.assert_any_call('/projects', data={
# "name": project._name,
# "project_id": project._id,
# "path": project._path
# })
#
# assert compute in project._project_created_on_compute
# project.emit_notification.assert_any_call("node.created", node.__json__())
#
#
# @pytest.mark.asyncio
# async def test_add_builtin_node_from_template(controller):
# """
# For a local server we send the project path
# """
#
# compute = MagicMock()
# compute.id = "local"
# project = Project(controller=controller, name="Test")
# project.emit_notification = MagicMock()
# template = Template(str(uuid.uuid4()), {
# "name": "Builtin-switch",
# "template_type": "ethernet_switch",
# }, builtin=True)
#
# controller.template_manager.templates[template.id] = template
# template.__json__()
# controller._computes["local"] = compute
#
# response = MagicMock()
# response.json = {"console": 2048}
# compute.post = AsyncioMagicMock(return_value=response)
#
# node = await project.add_node_from_template(template.id, x=23, y=12, compute_id="local")
# compute.post.assert_any_call('/projects', data={
# "name": project._name,
# "project_id": project._id,
# "path": project._path
# })
#
# assert compute in project._project_created_on_compute
# project.emit_notification.assert_any_call("node.created", node.__json__())
@pytest.mark.asyncio
-89
View File
@@ -1,89 +0,0 @@
#!/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/>.
import pytest
import pydantic
from gns3server.controller.template import Template
def test_template_json():
a = Template(None, {
"node_type": "qemu",
"name": "Test",
"default_name_format": "{name}-{0}",
"category": 0,
"symbol": "qemu.svg",
"server": "local",
"platform": "i386"
})
settings = a.__json__()
assert settings["template_id"] == a.id
assert settings["template_type"] == "qemu"
assert settings["builtin"] == False
def test_template_json_with_not_known_category():
with pytest.raises(pydantic.ValidationError):
Template(None, {
"node_type": "qemu",
"name": "Test",
"default_name_format": "{name}-{0}",
"category": 'Not known',
"symbol": "qemu.svg",
"server": "local",
"platform": "i386"
})
def test_template_json_with_platform():
a = Template(None, {
"node_type": "dynamips",
"name": "Test",
"default_name_format": "{name}-{0}",
"category": 0,
"symbol": "dynamips.svg",
"image": "IOS_image.bin",
"server": "local",
"platform": "c3725"
})
settings = a.__json__()
assert settings["template_id"] == a.id
assert settings["template_type"] == "dynamips"
assert settings["builtin"] == False
assert settings["platform"] == "c3725"
def test_template_fix_linked_base():
"""
Version of the gui before 2.1 use linked_base and the server
linked_clone
"""
a = Template(None, {
"node_type": "qemu",
"name": "Test",
"default_name_format": "{name}-{0}",
"category": 0,
"symbol": "qemu.svg",
"server": "local",
"linked_base": True
})
assert a.settings["linked_clone"]
assert "linked_base" not in a.settings