2016-03-02 08:49:52 +00:00
|
|
|
#!/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/>.
|
|
|
|
|
2016-04-19 13:35:50 +00:00
|
|
|
import os
|
2016-03-10 09:32:07 +00:00
|
|
|
import uuid
|
2016-04-19 13:35:50 +00:00
|
|
|
import json
|
|
|
|
import pytest
|
2016-08-25 09:49:06 +00:00
|
|
|
import socket
|
2016-03-10 09:32:07 +00:00
|
|
|
import aiohttp
|
2017-07-19 09:44:05 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2016-06-14 14:57:13 +00:00
|
|
|
from tests.utils import AsyncioMagicMock, asyncio_patch
|
2016-03-02 08:49:52 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
from gns3server.controller.compute import Compute
|
2016-04-19 13:35:50 +00:00
|
|
|
from gns3server.version import __version__
|
|
|
|
|
|
|
|
|
|
|
|
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["computes"] == []
|
|
|
|
assert data["version"] == __version__
|
2016-06-29 12:16:29 +00:00
|
|
|
assert data["settings"] == {}
|
2016-08-24 09:36:32 +00:00
|
|
|
assert data["gns3vm"] == controller.gns3vm.__json__()
|
2016-04-19 13:35:50 +00:00
|
|
|
|
|
|
|
|
2017-02-28 11:08:47 +00:00
|
|
|
def test_load_controller_settings(controller, controller_config_path, async_run):
|
2016-04-19 13:35:50 +00:00
|
|
|
controller.save()
|
|
|
|
with open(controller_config_path) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
data["computes"] = [
|
|
|
|
{
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 8000,
|
|
|
|
"protocol": "http",
|
|
|
|
"user": "admin",
|
|
|
|
"password": "root",
|
|
|
|
"compute_id": "test1"
|
|
|
|
}
|
|
|
|
]
|
2017-04-12 12:35:49 +00:00
|
|
|
data["settings"] = {"IOU": {"test": True}}
|
2016-08-24 09:36:32 +00:00
|
|
|
data["gns3vm"] = {"vmname": "Test VM"}
|
2016-04-19 13:35:50 +00:00
|
|
|
with open(controller_config_path, "w+") as f:
|
|
|
|
json.dump(data, f)
|
2017-03-07 14:36:35 +00:00
|
|
|
assert len(async_run(controller._load_controller_settings())) == 1
|
2016-06-29 12:16:29 +00:00
|
|
|
assert controller.settings["IOU"]
|
2016-08-24 09:36:32 +00:00
|
|
|
assert controller.gns3vm.settings["vmname"] == "Test VM"
|
2016-03-02 08:49:52 +00:00
|
|
|
|
|
|
|
|
2017-06-07 08:50:40 +00:00
|
|
|
def test_load_controller_settings_with_no_computes_section(controller, controller_config_path, async_run):
|
|
|
|
controller.save()
|
|
|
|
with open(controller_config_path) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
del data['computes']
|
|
|
|
with open(controller_config_path, "w+") as f:
|
|
|
|
json.dump(data, f)
|
|
|
|
assert len(async_run(controller._load_controller_settings())) == 0
|
|
|
|
|
|
|
|
|
2016-09-06 11:06:20 +00:00
|
|
|
def test_import_computes_1_x(controller, controller_config_path, async_run):
|
2016-07-14 13:14:56 +00:00
|
|
|
"""
|
|
|
|
At first start the server should import the
|
2016-09-06 11:06:20 +00:00
|
|
|
computes from the gns3_gui 1.X
|
2016-07-14 13:14:56 +00:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
2017-02-28 11:08:47 +00:00
|
|
|
async_run(controller._load_controller_settings())
|
2016-08-23 20:40:26 +00:00
|
|
|
for compute in controller.computes.values():
|
|
|
|
if compute.id != "local":
|
2016-09-30 09:20:25 +00:00
|
|
|
assert len(compute.id) == 36
|
2016-08-23 20:40:26 +00:00
|
|
|
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 13:14:56 +00:00
|
|
|
|
|
|
|
|
2016-09-06 11:06:20 +00:00
|
|
|
def test_import_gns3vm_1_x(controller, controller_config_path, async_run):
|
|
|
|
"""
|
|
|
|
At first start the server should import the
|
|
|
|
gns3vm settings from the gns3_gui 1.X
|
|
|
|
"""
|
|
|
|
gns3_gui_conf = {
|
|
|
|
"Servers": {
|
|
|
|
"vm": {
|
|
|
|
"adjust_local_server_ip": True,
|
|
|
|
"auto_start": True,
|
|
|
|
"auto_stop": False,
|
|
|
|
"headless": True,
|
|
|
|
"remote_vm_host": "",
|
|
|
|
"remote_vm_password": "",
|
|
|
|
"remote_vm_port": 3080,
|
|
|
|
"remote_vm_protocol": "http",
|
|
|
|
"remote_vm_url": "",
|
|
|
|
"remote_vm_user": "",
|
|
|
|
"virtualization": "VMware",
|
|
|
|
"vmname": "GNS3 VM",
|
|
|
|
"vmx_path": "/Users/joe/Documents/Virtual Machines.localized/GNS3 VM.vmwarevm/GNS3 VM.vmx"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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.gns3vm.settings["engine"] = None
|
2017-02-28 11:08:47 +00:00
|
|
|
async_run(controller._load_controller_settings())
|
2016-09-06 11:06:20 +00:00
|
|
|
assert controller.gns3vm.settings["engine"] == "vmware"
|
|
|
|
assert controller.gns3vm.settings["enable"]
|
|
|
|
assert controller.gns3vm.settings["headless"]
|
2016-09-08 13:32:35 +00:00
|
|
|
assert controller.gns3vm.settings["when_exit"] == "keep"
|
2016-09-06 11:06:20 +00:00
|
|
|
assert controller.gns3vm.settings["vmname"] == "GNS3 VM"
|
|
|
|
|
|
|
|
|
|
|
|
def test_import_remote_gns3vm_1_x(controller, controller_config_path, async_run):
|
|
|
|
"""
|
|
|
|
At first start the server should import the
|
|
|
|
computes and remote GNS3 VM from the gns3_gui 1.X
|
|
|
|
"""
|
|
|
|
gns3_gui_conf = {
|
|
|
|
"Servers": {
|
|
|
|
"remote_servers": [
|
|
|
|
{
|
|
|
|
"host": "127.0.0.1",
|
|
|
|
"password": "",
|
|
|
|
"port": 3080,
|
|
|
|
"protocol": "http",
|
|
|
|
"url": "http://127.0.0.1:3080",
|
|
|
|
"user": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"host": "127.0.0.1",
|
|
|
|
"password": "",
|
|
|
|
"port": 3081,
|
|
|
|
"protocol": "http",
|
|
|
|
"url": "http://127.0.0.1:3081",
|
|
|
|
"user": ""
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"vm": {
|
|
|
|
"adjust_local_server_ip": True,
|
|
|
|
"auto_start": True,
|
|
|
|
"auto_stop": False,
|
|
|
|
"headless": True,
|
|
|
|
"remote_vm_host": "127.0.0.1",
|
|
|
|
"remote_vm_password": "",
|
|
|
|
"remote_vm_port": 3081,
|
|
|
|
"remote_vm_protocol": "http",
|
|
|
|
"remote_vm_url": "http://127.0.0.1:3081",
|
|
|
|
"remote_vm_user": "",
|
|
|
|
"virtualization": "remote",
|
|
|
|
"vmname": "GNS3 VM",
|
|
|
|
"vmx_path": "/Users/joe/Documents/Virtual Machines.localized/GNS3 VM.vmwarevm/GNS3 VM.vmx"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2016-11-03 13:27:03 +00:00
|
|
|
with asyncio_patch("gns3server.controller.compute.Compute.connect"):
|
2017-02-28 11:08:47 +00:00
|
|
|
async_run(controller._load_controller_settings())
|
2016-09-06 11:06:20 +00:00
|
|
|
assert controller.gns3vm.settings["engine"] == "remote"
|
|
|
|
assert controller.gns3vm.settings["vmname"] == "http://127.0.0.1:3081"
|
|
|
|
|
|
|
|
|
2016-06-29 12:16:29 +00:00
|
|
|
def test_settings(controller):
|
|
|
|
controller._notification = MagicMock()
|
|
|
|
controller.settings = {"a": 1}
|
2017-03-21 12:35:02 +00:00
|
|
|
controller._notification.emit.assert_called_with("settings.updated", controller.settings)
|
|
|
|
assert controller.settings["modification_uuid"] is not None
|
2016-06-29 12:16:29 +00:00
|
|
|
|
|
|
|
|
2016-06-17 15:50:06 +00:00
|
|
|
def test_load_projects(controller, projects_dir, async_run):
|
|
|
|
controller.save()
|
|
|
|
|
|
|
|
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:
|
2016-08-16 14:04:20 +00:00
|
|
|
async_run(controller.load_projects())
|
2016-06-17 15:50:06 +00:00
|
|
|
mock_load_project.assert_called_with(os.path.join(projects_dir, "project1", "project1.gns3"), load=False)
|
|
|
|
|
|
|
|
|
2016-08-29 13:53:10 +00:00
|
|
|
def test_add_compute(controller, controller_config_path, async_run):
|
2016-05-23 16:44:20 +00:00
|
|
|
controller._notification = MagicMock()
|
2016-08-30 07:58:37 +00:00
|
|
|
c = async_run(controller.add_compute(compute_id="test1", connect=False))
|
2016-05-23 16:44:20 +00:00
|
|
|
controller._notification.emit.assert_called_with("compute.created", c.__json__())
|
2016-08-24 09:36:32 +00:00
|
|
|
assert len(controller.computes) == 1
|
2016-08-30 07:58:37 +00:00
|
|
|
async_run(controller.add_compute(compute_id="test1", connect=False))
|
2016-05-23 16:44:20 +00:00
|
|
|
controller._notification.emit.assert_called_with("compute.updated", c.__json__())
|
2016-08-24 09:36:32 +00:00
|
|
|
assert len(controller.computes) == 1
|
2016-08-30 07:58:37 +00:00
|
|
|
async_run(controller.add_compute(compute_id="test2", connect=False))
|
2016-08-24 09:36:32 +00:00
|
|
|
assert len(controller.computes) == 2
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-04-19 14:53:41 +00:00
|
|
|
|
2016-06-29 15:39:41 +00:00
|
|
|
def test_addDuplicateCompute(controller, controller_config_path, async_run):
|
|
|
|
controller._notification = MagicMock()
|
2016-08-30 07:58:37 +00:00
|
|
|
c = async_run(controller.add_compute(compute_id="test1", name="Test", connect=False))
|
2016-08-24 09:36:32 +00:00
|
|
|
assert len(controller.computes) == 1
|
2016-06-29 15:39:41 +00:00
|
|
|
with pytest.raises(aiohttp.web.HTTPConflict):
|
2016-08-30 07:58:37 +00:00
|
|
|
async_run(controller.add_compute(compute_id="test2", name="Test", connect=False))
|
2016-06-29 15:39:41 +00:00
|
|
|
|
|
|
|
|
2016-05-25 09:27:41 +00:00
|
|
|
def test_deleteCompute(controller, controller_config_path, async_run):
|
2016-08-30 07:58:37 +00:00
|
|
|
c = async_run(controller.add_compute(compute_id="test1", connect=False))
|
2016-08-24 09:36:32 +00:00
|
|
|
assert len(controller.computes) == 1
|
2016-05-25 09:27:41 +00:00
|
|
|
controller._notification = MagicMock()
|
2016-06-02 11:44:12 +00:00
|
|
|
c._connected = True
|
2016-05-25 09:27:41 +00:00
|
|
|
async_run(controller.delete_compute("test1"))
|
2016-08-24 09:36:32 +00:00
|
|
|
assert len(controller.computes) == 0
|
2016-05-25 09:27:41 +00:00
|
|
|
controller._notification.emit.assert_called_with("compute.deleted", c.__json__())
|
2016-05-25 12:10:03 +00:00
|
|
|
with open(controller_config_path) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
assert len(data["computes"]) == 0
|
2016-06-02 11:44:12 +00:00
|
|
|
assert c.connected is False
|
2016-05-25 09:27:41 +00:00
|
|
|
|
|
|
|
|
2016-08-26 09:22:09 +00:00
|
|
|
def test_deleteComputeProjectOpened(controller, controller_config_path, async_run):
|
|
|
|
"""
|
|
|
|
When you delete a compute the project using it are close
|
|
|
|
"""
|
2016-08-30 07:58:37 +00:00
|
|
|
c = async_run(controller.add_compute(compute_id="test1", connect=False))
|
2016-08-26 09:22:09 +00:00
|
|
|
c.post = AsyncioMagicMock()
|
|
|
|
assert len(controller.computes) == 1
|
|
|
|
|
|
|
|
project1 = async_run(controller.add_project(name="Test1"))
|
|
|
|
async_run(project1.open())
|
|
|
|
# We simulate that the project use this compute
|
|
|
|
project1._project_created_on_compute.add(c)
|
|
|
|
|
|
|
|
project2 = async_run(controller.add_project(name="Test2"))
|
|
|
|
async_run(project2.open())
|
|
|
|
|
|
|
|
controller._notification = MagicMock()
|
|
|
|
c._connected = True
|
|
|
|
async_run(controller.delete_compute("test1"))
|
|
|
|
assert len(controller.computes) == 0
|
|
|
|
controller._notification.emit.assert_called_with("compute.deleted", c.__json__())
|
|
|
|
with open(controller_config_path) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
assert len(data["computes"]) == 0
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
2016-04-19 13:35:50 +00:00
|
|
|
def test_addComputeConfigFile(controller, controller_config_path, async_run):
|
2016-08-30 07:58:37 +00:00
|
|
|
async_run(controller.add_compute(compute_id="test1", name="Test", connect=False))
|
2016-08-24 09:36:32 +00:00
|
|
|
assert len(controller.computes) == 1
|
2016-04-19 13:35:50 +00:00
|
|
|
with open(controller_config_path) as f:
|
|
|
|
data = json.load(f)
|
|
|
|
assert data["computes"] == [
|
|
|
|
{
|
|
|
|
'compute_id': 'test1',
|
2016-06-08 12:25:11 +00:00
|
|
|
'name': 'Test',
|
2016-04-19 13:35:50 +00:00
|
|
|
'host': 'localhost',
|
2016-05-25 12:10:03 +00:00
|
|
|
'port': 3080,
|
2016-04-19 13:35:50 +00:00
|
|
|
'protocol': 'http',
|
|
|
|
'user': None,
|
|
|
|
'password': None
|
|
|
|
}
|
|
|
|
]
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2016-04-19 13:35:50 +00:00
|
|
|
def test_getCompute(controller, async_run):
|
2016-08-30 07:58:37 +00:00
|
|
|
compute = async_run(controller.add_compute(compute_id="test1", connect=False))
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2016-05-11 21:19:00 +00:00
|
|
|
assert controller.get_compute("test1") == compute
|
2016-03-10 20:51:29 +00:00
|
|
|
with pytest.raises(aiohttp.web.HTTPNotFound):
|
2016-05-11 21:19:00 +00:00
|
|
|
assert controller.get_compute("dsdssd")
|
2016-03-10 20:51:29 +00:00
|
|
|
|
|
|
|
|
2016-07-25 16:02:22 +00:00
|
|
|
def test_has_compute(controller, async_run):
|
2016-08-30 07:58:37 +00:00
|
|
|
compute = async_run(controller.add_compute(compute_id="test1", connect=False))
|
2016-07-25 16:02:22 +00:00
|
|
|
|
|
|
|
assert controller.has_compute("test1")
|
|
|
|
assert not controller.has_compute("test2")
|
|
|
|
|
|
|
|
|
2016-07-11 13:36:52 +00:00
|
|
|
def test_add_project(controller, async_run):
|
2016-03-10 09:32:07 +00:00
|
|
|
uuid1 = str(uuid.uuid4())
|
|
|
|
uuid2 = str(uuid.uuid4())
|
|
|
|
|
2016-07-11 13:36:52 +00:00
|
|
|
async_run(controller.add_project(project_id=uuid1, name="Test"))
|
2016-03-10 09:32:07 +00:00
|
|
|
assert len(controller.projects) == 1
|
2016-07-11 13:36:52 +00:00
|
|
|
async_run(controller.add_project(project_id=uuid1, name="Test"))
|
2016-03-10 09:32:07 +00:00
|
|
|
assert len(controller.projects) == 1
|
2016-07-11 13:36:52 +00:00
|
|
|
async_run(controller.add_project(project_id=uuid2, name="Test 2"))
|
2016-03-10 09:32:07 +00:00
|
|
|
assert len(controller.projects) == 2
|
|
|
|
|
|
|
|
|
2016-06-29 15:39:41 +00:00
|
|
|
def test_addDuplicateProject(controller, async_run):
|
|
|
|
uuid1 = str(uuid.uuid4())
|
|
|
|
uuid2 = str(uuid.uuid4())
|
|
|
|
|
|
|
|
async_run(controller.add_project(project_id=uuid1, name="Test"))
|
|
|
|
assert len(controller.projects) == 1
|
|
|
|
with pytest.raises(aiohttp.web.HTTPConflict):
|
|
|
|
async_run(controller.add_project(project_id=uuid2, name="Test"))
|
|
|
|
|
|
|
|
|
2016-05-11 21:19:00 +00:00
|
|
|
def test_remove_project(controller, async_run):
|
2016-03-10 09:32:07 +00:00
|
|
|
uuid1 = str(uuid.uuid4())
|
|
|
|
|
2016-07-11 13:36:52 +00:00
|
|
|
project1 = async_run(controller.add_project(project_id=uuid1, name="Test"))
|
2016-03-10 09:32:07 +00:00
|
|
|
assert len(controller.projects) == 1
|
|
|
|
|
2016-05-11 21:19:00 +00:00
|
|
|
controller.remove_project(project1)
|
2016-03-10 09:32:07 +00:00
|
|
|
assert len(controller.projects) == 0
|
|
|
|
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
def test_addProject_with_compute(controller, async_run):
|
2016-03-10 09:32:07 +00:00
|
|
|
uuid1 = str(uuid.uuid4())
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
compute = Compute("test1", controller=MagicMock())
|
|
|
|
compute.post = MagicMock()
|
|
|
|
controller._computes = {"test1": compute}
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-07-11 13:36:52 +00:00
|
|
|
project1 = async_run(controller.add_project(project_id=uuid1, name="Test"))
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_getProject(controller, async_run):
|
|
|
|
uuid1 = str(uuid.uuid4())
|
|
|
|
|
2016-07-11 13:36:52 +00:00
|
|
|
project = async_run(controller.add_project(project_id=uuid1, name="Test"))
|
2016-05-11 17:35:36 +00:00
|
|
|
assert controller.get_project(uuid1) == project
|
2016-03-10 09:32:07 +00:00
|
|
|
with pytest.raises(aiohttp.web.HTTPNotFound):
|
2016-05-11 17:35:36 +00:00
|
|
|
assert controller.get_project("dsdssd")
|
2016-06-02 11:44:12 +00:00
|
|
|
|
|
|
|
|
2016-08-25 09:49:06 +00:00
|
|
|
def test_start(controller, async_run):
|
2016-09-01 13:36:41 +00:00
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": False,
|
2017-01-27 09:41:39 +00:00
|
|
|
"engine": "vmware",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-09-01 13:36:41 +00:00
|
|
|
}
|
|
|
|
with asyncio_patch("gns3server.controller.compute.Compute.connect") as mock:
|
|
|
|
async_run(controller.start())
|
2016-08-25 12:26:01 +00:00
|
|
|
assert len(controller.computes) == 1 # Local compute is created
|
2016-08-25 09:49:06 +00:00
|
|
|
assert controller.computes["local"].name == socket.gethostname()
|
|
|
|
|
|
|
|
|
|
|
|
def test_start_vm(controller, async_run):
|
|
|
|
"""
|
|
|
|
Start the controller with a GNS3 VM
|
|
|
|
"""
|
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": True,
|
2017-01-27 09:41:39 +00:00
|
|
|
"engine": "vmware",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-08-25 09:49:06 +00:00
|
|
|
}
|
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.start") as mock:
|
2017-12-06 02:30:28 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.GNS3VM._check_network") as mock_check_network:
|
2017-12-05 22:56:50 +00:00
|
|
|
with asyncio_patch("gns3server.controller.compute.Compute.connect") as mock_connect:
|
|
|
|
async_run(controller.start())
|
|
|
|
assert mock.called
|
2016-08-25 09:49:06 +00:00
|
|
|
assert "local" in controller.computes
|
|
|
|
assert "vm" in controller.computes
|
2016-08-26 12:09:18 +00:00
|
|
|
assert len(controller.computes) == 2 # Local compute and vm are created
|
2016-08-25 09:49:06 +00:00
|
|
|
|
|
|
|
|
2016-08-24 09:36:32 +00:00
|
|
|
def test_stop(controller, async_run):
|
2016-08-30 07:58:37 +00:00
|
|
|
c = async_run(controller.add_compute(compute_id="test1", connect=False))
|
2016-06-02 11:44:12 +00:00
|
|
|
c._connected = True
|
2016-08-24 09:36:32 +00:00
|
|
|
async_run(controller.stop())
|
2016-06-02 11:44:12 +00:00
|
|
|
assert c.connected is False
|
2016-06-14 14:07:37 +00:00
|
|
|
|
|
|
|
|
2016-08-25 12:26:01 +00:00
|
|
|
def test_stop_vm(controller, async_run):
|
|
|
|
"""
|
2016-09-08 13:32:35 +00:00
|
|
|
Stop GNS3 VM if configured
|
2016-08-25 12:26:01 +00:00
|
|
|
"""
|
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": True,
|
|
|
|
"engine": "vmware",
|
2017-01-27 09:41:39 +00:00
|
|
|
"when_exit": "stop",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-08-25 12:26:01 +00:00
|
|
|
}
|
2016-10-25 10:13:42 +00:00
|
|
|
controller.gns3vm.current_engine().running = True
|
2016-08-25 12:26:01 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.stop") as mock:
|
|
|
|
async_run(controller.stop())
|
|
|
|
assert mock.called
|
|
|
|
|
|
|
|
|
2016-09-08 13:32:35 +00:00
|
|
|
def test_suspend_vm(controller, async_run):
|
|
|
|
"""
|
|
|
|
Suspend GNS3 VM if configured
|
|
|
|
"""
|
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": True,
|
|
|
|
"engine": "vmware",
|
2017-01-27 09:41:39 +00:00
|
|
|
"when_exit": "suspend",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-09-08 13:32:35 +00:00
|
|
|
}
|
2016-10-25 10:13:42 +00:00
|
|
|
controller.gns3vm.current_engine().running = True
|
2016-09-08 13:32:35 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.suspend") as mock:
|
|
|
|
async_run(controller.stop())
|
|
|
|
assert mock.called
|
|
|
|
|
|
|
|
|
|
|
|
def test_keep_vm(controller, async_run):
|
|
|
|
"""
|
|
|
|
Keep GNS3 VM if configured
|
|
|
|
"""
|
|
|
|
controller.gns3vm.settings = {
|
|
|
|
"enable": True,
|
|
|
|
"engine": "vmware",
|
2017-01-27 09:41:39 +00:00
|
|
|
"when_exit": "keep",
|
|
|
|
"vmname": "GNS3 VM"
|
2016-09-08 13:32:35 +00:00
|
|
|
}
|
2016-10-25 10:13:42 +00:00
|
|
|
controller.gns3vm.current_engine().running = True
|
2016-09-08 13:32:35 +00:00
|
|
|
with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.suspend") as mock:
|
|
|
|
async_run(controller.stop())
|
|
|
|
assert not mock.called
|
|
|
|
|
|
|
|
|
2016-07-21 12:48:13 +00:00
|
|
|
def test_get_free_project_name(controller, async_run):
|
|
|
|
|
|
|
|
async_run(controller.add_project(project_id=str(uuid.uuid4()), name="Test"))
|
|
|
|
assert controller.get_free_project_name("Test") == "Test-1"
|
|
|
|
async_run(controller.add_project(project_id=str(uuid.uuid4()), name="Test-1"))
|
|
|
|
assert controller.get_free_project_name("Test") == "Test-2"
|
|
|
|
assert controller.get_free_project_name("Hello") == "Hello"
|
2017-02-01 10:30:14 +00:00
|
|
|
|
|
|
|
|
2017-02-02 18:13:47 +00:00
|
|
|
def test_load_base_files(controller, config, tmpdir):
|
|
|
|
config.set_section_config("Server", {"configs_path": str(tmpdir)})
|
|
|
|
|
|
|
|
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 12:35:49 +00:00
|
|
|
|
|
|
|
|
2017-07-19 09:44:05 +00:00
|
|
|
def test_appliance_templates(controller, async_run, tmpdir):
|
|
|
|
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 13:21:35 +00: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 09:44:05 +00:00
|
|
|
|
|
|
|
with patch("gns3server.config.Config.get_section_config", return_value={"appliances_path": str(tmpdir)}):
|
|
|
|
controller.load_appliances()
|
2017-04-12 12:35:49 +00:00
|
|
|
assert len(controller.appliance_templates) > 0
|
2017-07-19 09:44:05 +00:00
|
|
|
for appliance in controller.appliance_templates.values():
|
|
|
|
assert appliance.__json__()["status"] != "broken"
|
2017-06-27 16:44:31 +00:00
|
|
|
assert "Alpine Linux" in [c.__json__()["name"] for c in controller.appliance_templates.values()]
|
2017-07-19 09:44:05 +00:00
|
|
|
assert "My Appliance" in [c.__json__()["name"] for c in controller.appliance_templates.values()]
|
|
|
|
|
|
|
|
for c in controller.appliance_templates.values():
|
|
|
|
j = c.__json__()
|
|
|
|
if j["name"] == "Alpine Linux":
|
|
|
|
assert j["builtin"]
|
|
|
|
elif j["name"] == "My Appliance":
|
|
|
|
assert not j["builtin"]
|
2017-04-12 12:35:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_load_appliances(controller):
|
|
|
|
controller._settings = {
|
|
|
|
"Qemu": {
|
|
|
|
"vms": [
|
|
|
|
{
|
2018-02-06 08:15:34 +00:00
|
|
|
"name": "Test",
|
|
|
|
"node_type": "qemu",
|
2018-02-06 08:55:40 +00:00
|
|
|
"category": "router"
|
2017-04-12 12:35:49 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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()]
|
2017-06-27 16:44:31 +00:00
|
|
|
assert "VPCS" in [appliance.name for appliance in controller.appliances.values()]
|
|
|
|
|
|
|
|
for appliance in controller.appliances.values():
|
|
|
|
if appliance.name == "VPCS":
|
|
|
|
assert appliance._data["properties"] == {"base_script_file": "vpcs_base_config.txt"}
|
2017-06-19 06:47:50 +00:00
|
|
|
|
|
|
|
# UUID should not change when you run again the function
|
|
|
|
for appliance in controller.appliances.values():
|
|
|
|
if appliance.name == "Test":
|
|
|
|
qemu_uuid = appliance.id
|
|
|
|
elif appliance.name == "Cloud":
|
|
|
|
cloud_uuid = appliance.id
|
|
|
|
controller.load_appliances()
|
|
|
|
for appliance in controller.appliances.values():
|
|
|
|
if appliance.name == "Test":
|
|
|
|
assert qemu_uuid == appliance.id
|
|
|
|
elif appliance.name == "Cloud":
|
|
|
|
assert cloud_uuid == appliance.id
|
2017-07-07 15:50:40 +00:00
|
|
|
|
|
|
|
|
2018-03-02 12:48:27 +00:00
|
|
|
def test_load_appliances_deprecated_features_default_symbol(controller):
|
|
|
|
controller._settings = {
|
|
|
|
"Qemu": {
|
|
|
|
"vms": [
|
|
|
|
{
|
|
|
|
"name": "Test",
|
|
|
|
"node_type": "qemu",
|
|
|
|
"category": "router",
|
|
|
|
"default_symbol": ":/symbols/iosv_virl.normal.svg",
|
|
|
|
"hover_symbol": ":/symbols/iosv_virl.selected.svg",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
controller.load_appliances()
|
|
|
|
appliances = dict([(a.name, a) for a in controller.appliances.values()])
|
|
|
|
|
|
|
|
assert appliances["Test"].__json__()["symbol"] == ":/symbols/computer.svg"
|
|
|
|
assert "default_symbol" not in appliances["Test"].data.keys()
|
|
|
|
assert "hover_symbol" not in appliances["Test"].data.keys()
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_appliances_deprecated_features_default_symbol_with_symbol(controller):
|
|
|
|
controller._settings = {
|
|
|
|
"Qemu": {
|
|
|
|
"vms": [
|
|
|
|
{
|
|
|
|
"name": "Test",
|
|
|
|
"node_type": "qemu",
|
|
|
|
"category": "router",
|
|
|
|
"default_symbol": ":/symbols/iosv_virl.normal.svg",
|
|
|
|
"hover_symbol": ":/symbols/iosv_virl.selected.svg",
|
|
|
|
"symbol": ":/symbols/my-symbol.svg"
|
|
|
|
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
controller.load_appliances()
|
|
|
|
appliances = dict([(a.name, a) for a in controller.appliances.values()])
|
|
|
|
|
|
|
|
assert appliances["Test"].__json__()["symbol"] == ":/symbols/my-symbol.svg"
|
|
|
|
assert "default_symbol" not in appliances["Test"].data.keys()
|
|
|
|
assert "hover_symbol" not in appliances["Test"].data.keys()
|
|
|
|
|
|
|
|
|
2017-07-07 15:50:40 +00:00
|
|
|
def test_autoidlepc(controller, async_run):
|
|
|
|
controller._computes["local"] = AsyncioMagicMock()
|
|
|
|
node_mock = AsyncioMagicMock()
|
|
|
|
with asyncio_patch("gns3server.controller.Project.add_node", return_value=node_mock):
|
2017-11-23 16:46:22 +00:00
|
|
|
async_run(controller.autoidlepc("local", "c7200", "test.bin", 512))
|
2017-07-07 15:50:40 +00:00
|
|
|
assert node_mock.dynamips_auto_idlepc.called
|
|
|
|
assert len(controller.projects) == 0
|