2014-05-10 16:41:04 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-06-18 23:18:23 +02:00
|
|
|
import pytest
|
|
|
|
|
import os
|
2015-01-27 19:14:30 +01:00
|
|
|
import uuid
|
2015-09-09 11:36:06 +02:00
|
|
|
from unittest.mock import MagicMock
|
2015-08-27 15:40:31 +02:00
|
|
|
import tempfile
|
2015-09-09 11:36:06 +02:00
|
|
|
import urllib.request
|
2015-01-29 19:26:50 +01:00
|
|
|
import sys
|
2015-04-30 17:29:36 +02:00
|
|
|
sys._called_from_test = True
|
2014-06-18 23:18:23 +02:00
|
|
|
|
2016-09-27 14:21:59 +02:00
|
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
|
|
|
|
|
2015-02-03 11:37:35 +01:00
|
|
|
# If the QT application is not initialized we can got segfault
|
2015-04-17 12:14:21 +02:00
|
|
|
from gns3.qt.QtWidgets import QApplication
|
2015-02-03 11:37:35 +01:00
|
|
|
app = QApplication([])
|
|
|
|
|
|
2015-11-09 12:39:14 +01:00
|
|
|
|
2015-01-30 16:31:00 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
|
def reset_qt_signal():
|
|
|
|
|
"""
|
|
|
|
|
Reset the QT signals between tests. Otherwise
|
|
|
|
|
you can get callback on code from previous test.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from gns3.qt import FakeQtSignal
|
|
|
|
|
FakeQtSignal.reset()
|
|
|
|
|
|
|
|
|
|
|
2015-02-02 20:10:03 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
|
def reset_modules():
|
|
|
|
|
"""
|
|
|
|
|
Reset modules (VPCS, VirtualBox...) internal variables.
|
|
|
|
|
"""
|
|
|
|
|
|
2015-02-16 11:44:46 +01:00
|
|
|
from gns3.ports.port import Port
|
2016-07-10 20:33:19 -06:00
|
|
|
from gns3.modules.vpcs.vpcs_node import VPCSNode
|
2015-02-02 20:10:03 +01:00
|
|
|
from gns3.modules.virtualbox.virtualbox_vm import VirtualBoxVM
|
2015-02-16 11:44:46 +01:00
|
|
|
from gns3.modules.iou.iou_device import IOUDevice
|
2016-05-20 18:19:31 +02:00
|
|
|
from gns3.compute_manager import ComputeManager
|
2015-02-02 20:10:03 +01:00
|
|
|
|
2016-05-20 18:19:31 +02:00
|
|
|
ComputeManager.reset()
|
2016-07-10 20:33:19 -06:00
|
|
|
VPCSNode.reset()
|
2015-02-02 20:10:03 +01:00
|
|
|
VirtualBoxVM.reset()
|
2015-02-16 11:44:46 +01:00
|
|
|
IOUDevice.reset()
|
2015-02-02 20:10:03 +01:00
|
|
|
|
|
|
|
|
|
2015-09-16 11:54:50 +02:00
|
|
|
@pytest.fixture
|
2016-05-20 18:19:31 +02:00
|
|
|
def project():
|
2015-01-29 19:26:50 +01:00
|
|
|
|
|
|
|
|
from gns3.project import Project
|
|
|
|
|
|
2015-02-03 20:12:28 +01:00
|
|
|
project = Project()
|
2015-02-04 13:48:29 -07:00
|
|
|
project.setId(str(uuid.uuid4()))
|
2016-03-10 09:07:30 +01:00
|
|
|
project._created = True
|
2015-01-29 15:01:50 +01:00
|
|
|
project.setName("unsaved")
|
2015-01-27 19:14:30 +01:00
|
|
|
return project
|
|
|
|
|
|
|
|
|
|
|
2015-09-16 11:54:50 +02:00
|
|
|
@pytest.fixture
|
2015-01-27 19:14:30 +01:00
|
|
|
def local_server():
|
2016-05-20 18:19:31 +02:00
|
|
|
from gns3.compute_manager import ComputeManager
|
|
|
|
|
return ComputeManager.instance().getCompute("local")
|
2015-01-27 19:14:30 +01:00
|
|
|
|
|
|
|
|
|
2015-09-16 11:54:50 +02:00
|
|
|
@pytest.fixture
|
2015-02-06 22:50:12 +01:00
|
|
|
def remote_server():
|
2016-05-20 18:19:31 +02:00
|
|
|
from gns3.compute_manager import ComputeManager
|
2016-06-08 10:13:12 +02:00
|
|
|
return ComputeManager.instance().getCompute("example.org")
|
2015-02-06 22:50:12 +01:00
|
|
|
|
2015-02-09 18:56:20 +01:00
|
|
|
|
2016-05-20 13:17:07 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def controller():
|
|
|
|
|
from gns3.controller import Controller
|
|
|
|
|
|
|
|
|
|
Controller._instance = None
|
|
|
|
|
c = Controller.instance()
|
|
|
|
|
c._http_client = MagicMock()
|
2023-10-31 15:40:50 +10:00
|
|
|
c._http_client.fullUrl.return_value = "http://localhost:3080"
|
2016-05-20 13:17:07 +02:00
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
2015-09-16 11:54:50 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def gns3vm_server():
|
|
|
|
|
|
|
|
|
|
from gns3.servers import Servers
|
|
|
|
|
|
2016-06-15 11:49:32 +02:00
|
|
|
Servers.instance()._settings["vm"]["auto_start"] = True
|
2015-09-16 11:54:50 +02:00
|
|
|
Servers.instance().initVMServer()
|
2016-06-15 11:49:32 +02:00
|
|
|
assert Servers.instance().vmServer() is not None
|
2015-09-16 11:54:50 +02:00
|
|
|
return Servers.instance().vmServer()
|
|
|
|
|
|
|
|
|
|
|
2015-01-28 16:45:29 +01:00
|
|
|
@pytest.fixture
|
|
|
|
|
def vpcs_device(local_server, project):
|
2015-01-29 19:26:50 +01:00
|
|
|
|
2016-07-10 20:33:19 -06:00
|
|
|
from gns3.modules.vpcs.vpcs_node import VPCSNode
|
2015-01-29 19:26:50 +01:00
|
|
|
from gns3.modules.vpcs import VPCS
|
|
|
|
|
|
2016-07-10 20:33:19 -06:00
|
|
|
device = VPCSNode(VPCS(), local_server, project)
|
2016-05-11 16:54:55 -06:00
|
|
|
device._node_id = str(uuid.uuid4())
|
2015-01-31 15:09:35 -07:00
|
|
|
device._settings = {"name": "VPCS 1", "script_file": "", "console": None, "startup_script": None}
|
|
|
|
|
device.setInitialized(True)
|
|
|
|
|
return device
|
2015-01-28 16:45:29 +01:00
|
|
|
|
2015-02-02 20:10:03 +01:00
|
|
|
|
2015-02-12 17:04:24 +01:00
|
|
|
@pytest.fixture
|
|
|
|
|
def iou_device(local_server, project):
|
|
|
|
|
|
|
|
|
|
from gns3.modules.iou.iou_device import IOUDevice
|
|
|
|
|
from gns3.modules.iou import IOU
|
|
|
|
|
|
|
|
|
|
device = IOUDevice(IOU(), local_server, project)
|
2016-06-15 13:47:43 +02:00
|
|
|
device._node_id = str(uuid.uuid4())
|
2015-02-12 17:04:24 +01:00
|
|
|
settings = device._settings
|
|
|
|
|
settings["name"] = "IOU 1"
|
|
|
|
|
device._settings = settings
|
|
|
|
|
device.setInitialized(True)
|
|
|
|
|
return device
|
|
|
|
|
|
|
|
|
|
|
2015-01-31 15:09:35 -07:00
|
|
|
@pytest.fixture
|
|
|
|
|
def virtualbox_vm(local_server, project):
|
|
|
|
|
|
|
|
|
|
from gns3.modules.virtualbox.virtualbox_vm import VirtualBoxVM
|
|
|
|
|
from gns3.modules.virtualbox import VirtualBox
|
|
|
|
|
|
|
|
|
|
vm = VirtualBoxVM(VirtualBox(), local_server, project)
|
2016-06-15 13:47:43 +02:00
|
|
|
vm._node_id = str(uuid.uuid4())
|
2015-01-31 15:09:35 -07:00
|
|
|
vm._settings = {"name": "VBOX1",
|
|
|
|
|
"vmname": "VBOX1",
|
|
|
|
|
"console": None,
|
|
|
|
|
"adapters": 0,
|
2015-03-13 17:13:36 -06:00
|
|
|
"ram": 0,
|
2015-02-06 17:31:13 -07:00
|
|
|
"use_any_adapter": False,
|
2015-01-31 15:09:35 -07:00
|
|
|
"adapter_type": "Intel PRO/1000 MT Desktop (82540EM)",
|
2016-11-03 18:56:39 +01:00
|
|
|
"headless": False}
|
2015-01-31 15:09:35 -07:00
|
|
|
vm.setInitialized(True)
|
|
|
|
|
return vm
|
2015-01-28 16:45:29 +01:00
|
|
|
|
2015-02-02 20:10:03 +01:00
|
|
|
|
2015-02-20 17:30:00 +01:00
|
|
|
@pytest.fixture
|
|
|
|
|
def qemu_vm(local_server, project):
|
|
|
|
|
|
|
|
|
|
from gns3.modules.qemu.qemu_vm import QemuVM
|
|
|
|
|
from gns3.modules.qemu import Qemu
|
|
|
|
|
|
|
|
|
|
vm = QemuVM(Qemu(), local_server, project)
|
2016-06-15 13:47:43 +02:00
|
|
|
vm._node_id = str(uuid.uuid4())
|
2015-02-20 17:30:00 +01:00
|
|
|
vm._settings = {"name": "QEMU1"}
|
|
|
|
|
vm.setInitialized(True)
|
|
|
|
|
return vm
|
|
|
|
|
|
2015-07-03 15:14:30 +02:00
|
|
|
|
2015-06-22 17:22:29 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def local_config():
|
|
|
|
|
from gns3.local_config import LocalConfig
|
2015-08-27 15:40:31 +02:00
|
|
|
|
2015-11-09 12:39:14 +01:00
|
|
|
(fd, config_path) = tempfile.mkstemp()
|
2015-08-27 15:40:31 +02:00
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
|
|
LocalConfig._instance = LocalConfig(config_file=config_path)
|
|
|
|
|
return LocalConfig.instance()
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 18:30:36 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def local_server_config():
|
|
|
|
|
from gns3.local_server_config import LocalServerConfig
|
|
|
|
|
|
|
|
|
|
(fd, config_path) = tempfile.mkstemp()
|
|
|
|
|
os.close(fd)
|
|
|
|
|
|
|
|
|
|
LocalServerConfig._instance = LocalServerConfig(config_file=config_path)
|
|
|
|
|
return LocalServerConfig.instance()
|
|
|
|
|
|
|
|
|
|
|
2023-08-16 00:30:02 +10:00
|
|
|
@pytest.fixture(autouse=True)
|
2015-08-27 15:40:31 +02:00
|
|
|
def run_around_tests(local_config, main_window):
|
|
|
|
|
"""
|
|
|
|
|
This setup a temporay environnement around tests
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from gns3.main_window import MainWindow
|
|
|
|
|
MainWindow._instance = main_window
|
|
|
|
|
yield
|
2015-02-20 17:30:00 +01:00
|
|
|
|
2015-07-03 15:14:30 +02:00
|
|
|
|
2015-01-29 15:01:50 +01:00
|
|
|
@pytest.fixture
|
|
|
|
|
def main_window():
|
|
|
|
|
"""
|
|
|
|
|
Get a mocked main window
|
|
|
|
|
"""
|
2015-09-09 11:36:06 +02:00
|
|
|
window = MagicMock()
|
2015-01-29 15:01:50 +01:00
|
|
|
|
2015-09-09 11:36:06 +02:00
|
|
|
uiGraphicsView = MagicMock()
|
2015-01-29 15:01:50 +01:00
|
|
|
uiGraphicsView.settings.return_value = {
|
|
|
|
|
"default_label_font": "TypeWriter,10,-1,5,75,0,0,0,0,0",
|
2018-06-10 17:53:00 +07:00
|
|
|
"default_label_color": "#000000",
|
|
|
|
|
"default_note_font": "TypeWriter,10,-1,5,75,0,0,0,0,0",
|
|
|
|
|
"default_note_color": "#000000"
|
2015-01-29 15:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.uiGraphicsView = uiGraphicsView
|
|
|
|
|
return window
|
|
|
|
|
|
|
|
|
|
|
2015-09-09 11:36:06 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def images_dir(tmpdir):
|
2015-09-15 15:36:26 +02:00
|
|
|
os.makedirs(os.path.join(str(tmpdir), "images", "QEMU"), exist_ok=True)
|
2015-12-10 18:17:51 +01:00
|
|
|
os.makedirs(os.path.join(str(tmpdir), "images", "IOS"), exist_ok=True)
|
2015-12-09 19:49:23 +01:00
|
|
|
os.makedirs(os.path.join(str(tmpdir), "images", "IOU"), exist_ok=True)
|
2015-09-15 15:36:26 +02:00
|
|
|
return os.path.join(str(tmpdir), "images")
|
2015-09-09 11:36:06 +02:00
|
|
|
|
|
|
|
|
|
2015-10-02 17:57:49 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def symbols_dir(tmpdir):
|
|
|
|
|
os.makedirs(os.path.join(str(tmpdir), "symbols"), exist_ok=True)
|
|
|
|
|
return os.path.join(str(tmpdir), "symbols")
|
|
|
|
|
|
|
|
|
|
|
2015-09-09 11:36:06 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def linux_microcore_img(images_dir):
|
|
|
|
|
"""
|
|
|
|
|
Create a fake image and return the path. The md5sum of the file will be 5d41402abc4b2a76b9719d911017c592
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
path = os.path.join(images_dir, "QEMU", "linux-microcore-3.4.1.img")
|
|
|
|
|
with open(path, 'w+') as f:
|
|
|
|
|
f.write("hello")
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
2015-12-09 19:49:23 +01:00
|
|
|
@pytest.fixture
|
|
|
|
|
def iou_l3(images_dir):
|
|
|
|
|
"""
|
|
|
|
|
Create a fake image and return the path. The md5sum of the file will be 5d41402abc4b2a76b9719d911017c592
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
path = os.path.join(images_dir, "IOU", "i86bi-linux-l3-adventerprisek9-15.4.1T.bin")
|
|
|
|
|
with open(path, 'w+') as f:
|
|
|
|
|
f.write("hello")
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
2015-12-10 18:17:51 +01:00
|
|
|
@pytest.fixture
|
|
|
|
|
def cisco_3745(images_dir):
|
|
|
|
|
"""
|
|
|
|
|
Create a fake image and return the path. The md5sum of the file will be 5d41402abc4b2a76b9719d911017c592
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
path = os.path.join(images_dir, "IOS", "c3745-adventerprisek9-mz.124-25d.image")
|
|
|
|
|
with open(path, 'w+') as f:
|
|
|
|
|
f.write("hello")
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
2015-01-29 19:26:50 +01:00
|
|
|
def pytest_configure(config):
|
|
|
|
|
"""
|
|
|
|
|
Use to detect in code if we are running from pytest
|
|
|
|
|
|
|
|
|
|
http://pytest.org/latest/example/simple.html#detect-if-running-from-within-a-pytest-run
|
|
|
|
|
"""
|
|
|
|
|
import sys
|
|
|
|
|
sys._called_from_test = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pytest_unconfigure(config):
|
|
|
|
|
del sys._called_from_test
|