2016-03-02 10:49:52 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2020-06-16 07:29:03 +03:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2016-03-02 10:49:52 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2016-04-19 16:35:50 +03:00
|
|
|
import os
|
2016-03-10 11:32:07 +02:00
|
|
|
import uuid
|
2016-04-19 16:35:50 +03:00
|
|
|
import json
|
|
|
|
import pytest
|
2016-08-25 12:49:06 +03:00
|
|
|
import socket
|
2017-07-19 12:44:05 +03:00
|
|
|
from unittest.mock import MagicMock, patch
|
2016-06-14 17:57:13 +03:00
|
|
|
from tests.utils import AsyncioMagicMock, asyncio_patch
|
2016-03-02 10:49:52 +02:00
|
|
|
|
2016-04-15 18:57:06 +03:00
|
|
|
from gns3server.controller.compute import Compute
|
2020-10-02 09:37:50 +03:00
|
|
|
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError
|
2016-04-19 16:35:50 +03:00
|
|
|
from gns3server.version import __version__
|
|
|
|
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
# def test_save(controller, controller_config_path):
|
|
|
|
#
|
|
|
|
# controller.save()
|
|
|
|
# assert os.path.exists(controller_config_path)
|
|
|
|
# with open(controller_config_path) as f:
|
|
|
|
# data = json.load(f)
|
|
|
|
# assert data["version"] == __version__
|
|
|
|
# assert data["iou_license"] == controller.iou_license
|
2021-04-17 17:04:28 +03:00
|
|
|
# assert data["gns3vm"] == controller.gns3vm.asdict()
|
2021-04-12 10:32:23 +03:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# def test_load_controller_settings(controller, controller_config_path):
|
|
|
|
#
|
|
|
|
# controller.save()
|
|
|
|
# with open(controller_config_path) as f:
|
|
|
|
# data = json.load(f)
|
|
|
|
# data["gns3vm"] = {"vmname": "Test VM"}
|
|
|
|
# with open(controller_config_path, "w+") as f:
|
|
|
|
# json.dump(data, f)
|
|
|
|
# controller._load_controller_settings()
|
|
|
|
# assert controller.gns3vm.settings["vmname"] == "Test VM"
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# def test_load_controller_settings_with_no_computes_section(controller, controller_config_path):
|
|
|
|
#
|
|
|
|
# controller.save()
|
|
|
|
# with open(controller_config_path) as f:
|
|
|
|
# data = json.load(f)
|
|
|
|
# with open(controller_config_path, "w+") as f:
|
|
|
|
# json.dump(data, f)
|
|
|
|
# assert len(controller._load_controller_settings()) == 0
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# def test_import_computes_1_x(controller, controller_config_path):
|
|
|
|
# """
|
|
|
|
# At first start the server should import the
|
|
|
|
# computes from the gns3_gui 1.X
|
|
|
|
# """
|
|
|
|
#
|
|
|
|
# gns3_gui_conf = {
|
|
|
|
# "Servers": {
|
|
|
|
# "remote_servers": [
|
|
|
|
# {
|
|
|
|
# "host": "127.0.0.1",
|
|
|
|
# "password": "",
|
|
|
|
# "port": 3081,
|
|
|
|
# "protocol": "http",
|
|
|
|
# "url": "http://127.0.0.1:3081",
|
|
|
|
# "user": ""
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# config_dir = os.path.dirname(controller_config_path)
|
|
|
|
# os.makedirs(config_dir, exist_ok=True)
|
|
|
|
# with open(os.path.join(config_dir, "gns3_gui.conf"), "w+") as f:
|
|
|
|
# json.dump(gns3_gui_conf, f)
|
|
|
|
#
|
|
|
|
# controller._load_controller_settings()
|
|
|
|
# for compute in controller.computes.values():
|
|
|
|
# if compute.id != "local":
|
|
|
|
# assert len(compute.id) == 36
|
|
|
|
# assert compute.host == "127.0.0.1"
|
|
|
|
# assert compute.port == 3081
|
|
|
|
# assert compute.protocol == "http"
|
|
|
|
# assert compute.name == "http://127.0.0.1:3081"
|
|
|
|
# assert compute.user is None
|
|
|
|
# assert compute.password is None
|
2016-07-14 16:14:56 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_load_projects(controller, projects_dir):
|
2016-06-17 18:50:06 +03:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
controller.save()
|
2016-06-17 18:50:06 +03:00
|
|
|
os.makedirs(os.path.join(projects_dir, "project1"))
|
|
|
|
with open(os.path.join(projects_dir, "project1", "project1.gns3"), "w+") as f:
|
|
|
|
f.write("")
|
|
|
|
with asyncio_patch("gns3server.controller.Controller.load_project") as mock_load_project:
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.load_projects()
|
2016-06-17 18:50:06 +03:00
|
|
|
mock_load_project.assert_called_with(os.path.join(projects_dir, "project1", "project1.gns3"), load=False)
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_add_compute(controller):
|
|
|
|
|
2016-05-23 19:44:20 +03:00
|
|
|
controller._notification = MagicMock()
|
2020-06-16 07:29:03 +03:00
|
|
|
c = await controller.add_compute(compute_id="test1", connect=False)
|
2021-04-17 17:04:28 +03:00
|
|
|
controller._notification.controller_emit.assert_called_with("compute.created", c.asdict())
|
2016-08-24 12:36:32 +03:00
|
|
|
assert len(controller.computes) == 1
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_compute(compute_id="test1", connect=False)
|
2021-04-17 17:04:28 +03:00
|
|
|
controller._notification.controller_emit.assert_called_with("compute.updated", c.asdict())
|
2016-08-24 12:36:32 +03:00
|
|
|
assert len(controller.computes) == 1
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_compute(compute_id="test2", connect=False)
|
2016-08-24 12:36:32 +03:00
|
|
|
assert len(controller.computes) == 2
|
2016-03-10 11:32:07 +02:00
|
|
|
|
2016-04-19 17:53:41 +03:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_addDuplicateCompute(controller):
|
|
|
|
|
2016-06-29 18:39:41 +03:00
|
|
|
controller._notification = MagicMock()
|
2020-06-16 07:29:03 +03:00
|
|
|
c = await controller.add_compute(compute_id="test1", name="Test", connect=False)
|
2016-08-24 12:36:32 +03:00
|
|
|
assert len(controller.computes) == 1
|
2020-10-02 09:37:50 +03:00
|
|
|
with pytest.raises(ControllerError):
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_compute(compute_id="test2", name="Test", connect=False)
|
2016-06-29 18:39:41 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_deleteComputeProjectOpened(controller, controller_config_path):
|
2016-08-26 12:22:09 +03:00
|
|
|
"""
|
|
|
|
When you delete a compute the project using it are close
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
|
|
|
c = await controller.add_compute(compute_id="test1", connect=False)
|
2016-08-26 12:22:09 +03:00
|
|
|
c.post = AsyncioMagicMock()
|
|
|
|
assert len(controller.computes) == 1
|
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
project1 = await controller.add_project(name="Test1")
|
|
|
|
await project1.open()
|
2016-08-26 12:22:09 +03:00
|
|
|
# We simulate that the project use this compute
|
|
|
|
project1._project_created_on_compute.add(c)
|
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
project2 = await controller.add_project(name="Test2")
|
|
|
|
await project2.open()
|
2016-08-26 12:22:09 +03:00
|
|
|
|
|
|
|
controller._notification = MagicMock()
|
|
|
|
c._connected = True
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.delete_compute("test1")
|
2016-08-26 12:22:09 +03:00
|
|
|
assert len(controller.computes) == 0
|
2021-04-17 17:04:28 +03:00
|
|
|
controller._notification.controller_emit.assert_called_with("compute.deleted", c.asdict())
|
2016-08-26 12:22:09 +03:00
|
|
|
assert c.connected is False
|
|
|
|
|
|
|
|
# Project 1 use this compute it should be close before deleting the compute
|
|
|
|
assert project1.status == "closed"
|
|
|
|
assert project2.status == "opened"
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_getCompute(controller):
|
2016-03-10 22:51:29 +02:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
compute = await controller.add_compute(compute_id="test1", connect=False)
|
2016-05-12 00:19:00 +03:00
|
|
|
assert controller.get_compute("test1") == compute
|
2020-10-02 09:37:50 +03:00
|
|
|
with pytest.raises(ControllerNotFoundError):
|
2016-05-12 00:19:00 +03:00
|
|
|
assert controller.get_compute("dsdssd")
|
2016-03-10 22:51:29 +02:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_has_compute(controller):
|
2016-07-25 19:02:22 +03:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_compute(compute_id="test1", connect=False)
|
2016-07-25 19:02:22 +03:00
|
|
|
assert controller.has_compute("test1")
|
|
|
|
assert not controller.has_compute("test2")
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_add_project(controller):
|
|
|
|
|
2016-03-10 11:32:07 +02:00
|
|
|
uuid1 = str(uuid.uuid4())
|
|
|
|
uuid2 = str(uuid.uuid4())
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_project(project_id=uuid1, name="Test")
|
2016-03-10 11:32:07 +02:00
|
|
|
assert len(controller.projects) == 1
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_project(project_id=uuid1, name="Test")
|
2016-03-10 11:32:07 +02:00
|
|
|
assert len(controller.projects) == 1
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_project(project_id=uuid2, name="Test 2")
|
2016-03-10 11:32:07 +02:00
|
|
|
assert len(controller.projects) == 2
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_addDuplicateProject(controller):
|
|
|
|
|
2016-06-29 18:39:41 +03:00
|
|
|
uuid1 = str(uuid.uuid4())
|
|
|
|
uuid2 = str(uuid.uuid4())
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_project(project_id=uuid1, name="Test")
|
2016-06-29 18:39:41 +03:00
|
|
|
assert len(controller.projects) == 1
|
2020-10-02 09:37:50 +03:00
|
|
|
with pytest.raises(ControllerError):
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_project(project_id=uuid2, name="Test")
|
2016-06-29 18:39:41 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_remove_project(controller):
|
2016-03-10 11:32:07 +02:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
uuid1 = str(uuid.uuid4())
|
|
|
|
project1 = await controller.add_project(project_id=uuid1, name="Test")
|
2016-03-10 11:32:07 +02:00
|
|
|
assert len(controller.projects) == 1
|
2016-05-12 00:19:00 +03:00
|
|
|
controller.remove_project(project1)
|
2016-03-10 11:32:07 +02:00
|
|
|
assert len(controller.projects) == 0
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_addProject_with_compute(controller):
|
2016-03-10 11:32:07 +02:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
uuid1 = str(uuid.uuid4())
|
2016-04-15 18:57:06 +03:00
|
|
|
compute = Compute("test1", controller=MagicMock())
|
|
|
|
compute.post = MagicMock()
|
|
|
|
controller._computes = {"test1": compute}
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_project(project_id=uuid1, name="Test")
|
2016-03-10 11:32:07 +02:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_getProject(controller):
|
2016-03-10 11:32:07 +02:00
|
|
|
|
|
|
|
uuid1 = str(uuid.uuid4())
|
2020-06-16 07:29:03 +03:00
|
|
|
project = await controller.add_project(project_id=uuid1, name="Test")
|
2016-05-11 20:35:36 +03:00
|
|
|
assert controller.get_project(uuid1) == project
|
2020-10-02 09:37:50 +03:00
|
|
|
with pytest.raises(ControllerNotFoundError):
|
2016-05-11 20:35:36 +03:00
|
|
|
assert controller.get_project("dsdssd")
|
2016-06-02 14:44:12 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_start(controller):
|
|
|
|
|
2016-09-01 16:36:41 +03:00
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": False,
|
2017-01-27 11:41:39 +02:00
|
|
|
"engine": "vmware",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-09-01 16:36:41 +03:00
|
|
|
}
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2020-10-22 09:17:11 +03:00
|
|
|
#with asyncio_patch("gns3server.controller.compute.Compute.connect") as mock:
|
|
|
|
await controller.start()
|
|
|
|
#assert mock.called
|
2016-08-25 15:26:01 +03:00
|
|
|
assert len(controller.computes) == 1 # Local compute is created
|
2016-08-25 12:49:06 +03:00
|
|
|
assert controller.computes["local"].name == socket.gethostname()
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_start_vm(controller):
|
2016-08-25 12:49:06 +03:00
|
|
|
"""
|
|
|
|
Start the controller with a GNS3 VM
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-08-25 12:49:06 +03:00
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": True,
|
2017-01-27 11:41:39 +02:00
|
|
|
"engine": "vmware",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-08-25 12:49:06 +03:00
|
|
|
}
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-08-25 12:49:06 +03:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.start") as mock:
|
2020-06-16 07:29:03 +03:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.GNS3VM._check_network"):
|
|
|
|
with asyncio_patch("gns3server.controller.compute.Compute.connect"):
|
|
|
|
await controller.start()
|
2017-12-06 00:56:50 +02:00
|
|
|
assert mock.called
|
2016-08-25 12:49:06 +03:00
|
|
|
assert "local" in controller.computes
|
|
|
|
assert "vm" in controller.computes
|
2016-08-26 15:09:18 +03:00
|
|
|
assert len(controller.computes) == 2 # Local compute and vm are created
|
2016-08-25 12:49:06 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_stop(controller):
|
|
|
|
|
|
|
|
c = await controller.add_compute(compute_id="test1", connect=False)
|
2016-06-02 14:44:12 +03:00
|
|
|
c._connected = True
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.stop()
|
2016-06-02 14:44:12 +03:00
|
|
|
assert c.connected is False
|
2016-06-14 17:07:37 +03:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_stop_vm(controller):
|
2016-08-25 15:26:01 +03:00
|
|
|
"""
|
2016-09-08 16:32:35 +03:00
|
|
|
Stop GNS3 VM if configured
|
2016-08-25 15:26:01 +03:00
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-08-25 15:26:01 +03:00
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": True,
|
|
|
|
"engine": "vmware",
|
2017-01-27 11:41:39 +02:00
|
|
|
"when_exit": "stop",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-08-25 15:26:01 +03:00
|
|
|
}
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-10-25 13:13:42 +03:00
|
|
|
controller.gns3vm.current_engine().running = True
|
2016-08-25 15:26:01 +03:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.stop") as mock:
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.stop()
|
2016-08-25 15:26:01 +03:00
|
|
|
assert mock.called
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_suspend_vm(controller):
|
2016-09-08 16:32:35 +03:00
|
|
|
"""
|
|
|
|
Suspend GNS3 VM if configured
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-09-08 16:32:35 +03:00
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": True,
|
|
|
|
"engine": "vmware",
|
2017-01-27 11:41:39 +02:00
|
|
|
"when_exit": "suspend",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-09-08 16:32:35 +03:00
|
|
|
}
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-10-25 13:13:42 +03:00
|
|
|
controller.gns3vm.current_engine().running = True
|
2016-09-08 16:32:35 +03:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.suspend") as mock:
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.stop()
|
2016-09-08 16:32:35 +03:00
|
|
|
assert mock.called
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_keep_vm(controller):
|
2016-09-08 16:32:35 +03:00
|
|
|
"""
|
|
|
|
Keep GNS3 VM if configured
|
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-09-08 16:32:35 +03:00
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": True,
|
|
|
|
"engine": "vmware",
|
2017-01-27 11:41:39 +02:00
|
|
|
"when_exit": "keep",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-09-08 16:32:35 +03:00
|
|
|
}
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2016-10-25 13:13:42 +03:00
|
|
|
controller.gns3vm.current_engine().running = True
|
2016-09-08 16:32:35 +03:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.suspend") as mock:
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.stop()
|
2016-09-08 16:32:35 +03:00
|
|
|
assert not mock.called
|
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_get_free_project_name(controller):
|
2016-07-21 15:48:13 +03:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_project(project_id=str(uuid.uuid4()), name="Test")
|
2016-07-21 15:48:13 +03:00
|
|
|
assert controller.get_free_project_name("Test") == "Test-1"
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.add_project(project_id=str(uuid.uuid4()), name="Test-1")
|
2016-07-21 15:48:13 +03:00
|
|
|
assert controller.get_free_project_name("Test") == "Test-2"
|
|
|
|
assert controller.get_free_project_name("Hello") == "Hello"
|
2017-02-01 12:30:14 +02:00
|
|
|
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_load_base_files(controller, config, tmpdir):
|
2017-02-02 20:13:47 +02:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
config.settings.Server.configs_path = str(tmpdir)
|
2017-02-02 20:13:47 +02:00
|
|
|
with open(str(tmpdir / 'iou_l2_base_startup-config.txt'), 'w+') as f:
|
|
|
|
f.write('test')
|
|
|
|
|
|
|
|
controller.load_base_files()
|
|
|
|
assert os.path.exists(str(tmpdir / 'iou_l3_base_startup-config.txt'))
|
|
|
|
|
|
|
|
# Check is the file has not been overwrite
|
|
|
|
with open(str(tmpdir / 'iou_l2_base_startup-config.txt')) as f:
|
|
|
|
assert f.read() == 'test'
|
2017-04-12 15:35:49 +03:00
|
|
|
|
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
def test_appliances(controller, config, tmpdir):
|
2020-06-16 07:29:03 +03:00
|
|
|
|
2017-07-19 12:44:05 +03:00
|
|
|
my_appliance = {
|
|
|
|
"name": "My Appliance",
|
|
|
|
"status": "stable"
|
|
|
|
}
|
|
|
|
with open(str(tmpdir / "my_appliance.gns3a"), 'w+') as f:
|
|
|
|
json.dump(my_appliance, f)
|
2017-07-28 16:21:35 +03:00
|
|
|
# A broken appliance
|
|
|
|
my_appliance = {
|
|
|
|
"name": "Broken"
|
|
|
|
}
|
|
|
|
with open(str(tmpdir / "my_appliance2.gns3a"), 'w+') as f:
|
|
|
|
json.dump(my_appliance, f)
|
2017-07-19 12:44:05 +03:00
|
|
|
|
2021-04-12 10:32:23 +03:00
|
|
|
config.settings.Server.appliances_path = str(tmpdir)
|
|
|
|
controller.appliance_manager.load_appliances()
|
2019-01-14 11:09:06 +02:00
|
|
|
assert len(controller.appliance_manager.appliances) > 0
|
|
|
|
for appliance in controller.appliance_manager.appliances.values():
|
2021-04-17 17:04:28 +03:00
|
|
|
assert appliance.asdict()["status"] != "broken"
|
|
|
|
assert "Alpine Linux" in [c.asdict()["name"] for c in controller.appliance_manager.appliances.values()]
|
2021-10-18 14:42:10 +03:00
|
|
|
assert "My Appliance" not in [c.asdict()["name"] for c in controller.appliance_manager.appliances.values()]
|
2017-07-19 12:44:05 +03:00
|
|
|
|
2019-01-14 11:09:06 +02:00
|
|
|
for c in controller.appliance_manager.appliances.values():
|
2021-04-17 17:04:28 +03:00
|
|
|
j = c.asdict()
|
2017-07-19 12:44:05 +03:00
|
|
|
if j["name"] == "Alpine Linux":
|
|
|
|
assert j["builtin"]
|
2021-10-18 14:42:10 +03:00
|
|
|
|
2017-04-12 15:35:49 +03:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def test_autoidlepc(controller):
|
|
|
|
|
2017-07-07 18:50:40 +03:00
|
|
|
controller._computes["local"] = AsyncioMagicMock()
|
|
|
|
node_mock = AsyncioMagicMock()
|
|
|
|
with asyncio_patch("gns3server.controller.Project.add_node", return_value=node_mock):
|
2020-06-16 07:29:03 +03:00
|
|
|
await controller.autoidlepc("local", "c7200", "test.bin", 512)
|
2017-07-07 18:50:40 +03:00
|
|
|
assert node_mock.dynamips_auto_idlepc.called
|
|
|
|
assert len(controller.projects) == 0
|