2015-02-18 15:13:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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 uuid
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
|
|
from gns3server.modules.vpcs import VPCS
|
2015-04-30 14:43:30 +00:00
|
|
|
from gns3server.modules.qemu import Qemu
|
2015-02-18 15:13:09 +00:00
|
|
|
|
|
|
|
|
2015-04-13 12:33:13 +00:00
|
|
|
@pytest.fixture(scope="function")
|
2015-04-30 14:43:30 +00:00
|
|
|
def qemu(port_manager):
|
|
|
|
Qemu._instance = None
|
|
|
|
qemu = Qemu.instance()
|
|
|
|
qemu.port_manager = port_manager
|
|
|
|
return qemu
|
2015-04-13 12:33:13 +00:00
|
|
|
|
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
def test_create_vm_new_topology(loop, project, port_manager):
|
2015-04-15 12:31:40 +00:00
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
VPCS._instance = None
|
|
|
|
vpcs = VPCS.instance()
|
|
|
|
vpcs.port_manager = port_manager
|
2015-02-18 15:13:09 +00:00
|
|
|
vm_id = str(uuid.uuid4())
|
|
|
|
vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id))
|
|
|
|
assert vm in project.vms
|
|
|
|
|
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
def test_create_twice_same_vm_new_topology(loop, project, port_manager):
|
|
|
|
|
2015-03-02 14:35:36 +00:00
|
|
|
project._vms = set()
|
2015-04-30 14:43:30 +00:00
|
|
|
VPCS._instance = None
|
|
|
|
vpcs = VPCS.instance()
|
|
|
|
vpcs.port_manager = port_manager
|
2015-03-02 14:35:36 +00:00
|
|
|
vm_id = str(uuid.uuid4())
|
|
|
|
vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id, console=2222))
|
|
|
|
assert vm in project.vms
|
|
|
|
assert len(project.vms) == 1
|
|
|
|
vm = loop.run_until_complete(vpcs.create_vm("PC 2", project.id, vm_id, console=2222))
|
|
|
|
assert len(project.vms) == 1
|
|
|
|
|
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
def test_create_vm_new_topology_without_uuid(loop, project, port_manager):
|
|
|
|
|
|
|
|
VPCS._instance = None
|
|
|
|
vpcs = VPCS.instance()
|
|
|
|
vpcs.port_manager = port_manager
|
2015-02-18 15:13:09 +00:00
|
|
|
vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, None))
|
|
|
|
assert vm in project.vms
|
|
|
|
assert len(vm.id) == 36
|
|
|
|
|
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
def test_create_vm_old_topology(loop, project, tmpdir, port_manager):
|
2015-02-18 15:13:09 +00:00
|
|
|
|
2015-03-14 19:16:27 +00:00
|
|
|
with patch("gns3server.modules.project.Project.is_local", return_value=True):
|
2015-02-18 15:13:09 +00:00
|
|
|
# Create an old topology directory
|
|
|
|
project_dir = str(tmpdir / "testold")
|
|
|
|
vm_dir = os.path.join(project_dir, "testold-files", "vpcs", "pc-1")
|
|
|
|
project.path = project_dir
|
2015-03-16 11:08:23 +00:00
|
|
|
project.name = "testold"
|
2015-02-18 15:13:09 +00:00
|
|
|
os.makedirs(vm_dir, exist_ok=True)
|
|
|
|
with open(os.path.join(vm_dir, "startup.vpc"), "w+") as f:
|
|
|
|
f.write("1")
|
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
VPCS._instance = None
|
|
|
|
vpcs = VPCS.instance()
|
|
|
|
vpcs.port_manager = port_manager
|
2015-02-18 15:13:09 +00:00
|
|
|
vm_id = 1
|
|
|
|
vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id))
|
|
|
|
assert len(vm.id) == 36
|
|
|
|
|
2015-02-25 17:23:41 +00:00
|
|
|
assert os.path.exists(os.path.join(project_dir, "testold-files")) is False
|
|
|
|
|
|
|
|
vm_dir = os.path.join(project_dir, "project-files", "vpcs", vm.id)
|
|
|
|
with open(os.path.join(vm_dir, "startup.vpc")) as f:
|
|
|
|
assert f.read() == "1"
|
2015-04-14 16:46:55 +00:00
|
|
|
|
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
def test_get_abs_image_path(qemu, tmpdir):
|
|
|
|
os.makedirs(str(tmpdir / "QEMU"))
|
2015-04-14 16:46:55 +00:00
|
|
|
path1 = str(tmpdir / "test1.bin")
|
|
|
|
open(path1, 'w+').close()
|
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
path2 = str(tmpdir / "QEMU" / "test2.bin")
|
2015-04-14 16:46:55 +00:00
|
|
|
open(path2, 'w+').close()
|
|
|
|
|
|
|
|
with patch("gns3server.config.Config.get_section_config", return_value={"images_path": str(tmpdir)}):
|
2015-04-30 14:43:30 +00:00
|
|
|
assert qemu.get_abs_image_path(path1) == path1
|
2015-05-04 08:57:08 +00:00
|
|
|
assert qemu.get_abs_image_path("test1.bin") == path1
|
2015-04-30 14:43:30 +00:00
|
|
|
assert qemu.get_abs_image_path(path2) == path2
|
|
|
|
assert qemu.get_abs_image_path("test2.bin") == path2
|
|
|
|
assert qemu.get_abs_image_path("../test1.bin") == path1
|
2015-04-14 16:46:55 +00:00
|
|
|
|
2015-05-04 08:57:08 +00:00
|
|
|
# We look at first in new location
|
|
|
|
path2 = str(tmpdir / "QEMU" / "test1.bin")
|
|
|
|
open(path2, 'w+').close()
|
|
|
|
assert qemu.get_abs_image_path("test1.bin") == path2
|
|
|
|
|
2015-04-14 16:46:55 +00:00
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
def test_get_relative_image_path(qemu, tmpdir):
|
|
|
|
os.makedirs(str(tmpdir / "QEMU"))
|
2015-04-14 16:46:55 +00:00
|
|
|
path1 = str(tmpdir / "test1.bin")
|
|
|
|
open(path1, 'w+').close()
|
|
|
|
|
2015-04-30 14:43:30 +00:00
|
|
|
path2 = str(tmpdir / "QEMU" / "test2.bin")
|
2015-04-14 16:46:55 +00:00
|
|
|
open(path2, 'w+').close()
|
|
|
|
|
|
|
|
with patch("gns3server.config.Config.get_section_config", return_value={"images_path": str(tmpdir)}):
|
2015-04-30 14:43:30 +00:00
|
|
|
assert qemu.get_relative_image_path(path1) == path1
|
2015-05-04 08:57:08 +00:00
|
|
|
assert qemu.get_relative_image_path("test1.bin") == path1
|
2015-04-30 14:43:30 +00:00
|
|
|
assert qemu.get_relative_image_path(path2) == "test2.bin"
|
|
|
|
assert qemu.get_relative_image_path("test2.bin") == "test2.bin"
|
|
|
|
assert qemu.get_relative_image_path("../test1.bin") == path1
|