mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Refactoring to use a common node class for all VMs and other (future) objects.
This commit is contained in:
1 parent
9dca7dfe4a
commit
5a76f81271
102 files changed
+1654
-1672
No files matched your search
@@ -75,16 +75,16 @@ def test_isEnabled(controller):
|
||||
|
||||
|
||||
def test_addCompute(controller, controller_config_path, async_run):
|
||||
async_run(controller.addCompute("test1"))
|
||||
async_run(controller.add_compute("test1"))
|
||||
assert len(controller.computes) == 1
|
||||
async_run(controller.addCompute("test1"))
|
||||
async_run(controller.add_compute("test1"))
|
||||
assert len(controller.computes) == 1
|
||||
async_run(controller.addCompute("test2"))
|
||||
async_run(controller.add_compute("test2"))
|
||||
assert len(controller.computes) == 2
|
||||
|
||||
|
||||
def test_addComputeConfigFile(controller, controller_config_path, async_run):
|
||||
async_run(controller.addCompute("test1"))
|
||||
async_run(controller.add_compute("test1"))
|
||||
assert len(controller.computes) == 1
|
||||
with open(controller_config_path) as f:
|
||||
data = json.load(f)
|
||||
@@ -101,7 +101,7 @@ def test_addComputeConfigFile(controller, controller_config_path, async_run):
|
||||
|
||||
|
||||
def test_getCompute(controller, async_run):
|
||||
compute = async_run(controller.addCompute("test1"))
|
||||
compute = async_run(controller.add_compute("test1"))
|
||||
|
||||
assert controller.getCompute("test1") == compute
|
||||
with pytest.raises(aiohttp.web.HTTPNotFound):
|
||||
@@ -145,9 +145,9 @@ def test_getProject(controller, async_run):
|
||||
uuid1 = str(uuid.uuid4())
|
||||
|
||||
project = async_run(controller.addProject(project_id=uuid1))
|
||||
assert controller.getProject(uuid1) == project
|
||||
assert controller.get_project(uuid1) == project
|
||||
with pytest.raises(aiohttp.web.HTTPNotFound):
|
||||
assert controller.getProject("dsdssd")
|
||||
assert controller.get_project("dsdssd")
|
||||
|
||||
|
||||
def test_emit(controller, async_run):
|
||||
|
||||
@@ -22,7 +22,7 @@ from unittest.mock import MagicMock
|
||||
|
||||
|
||||
from gns3server.controller.link import Link
|
||||
from gns3server.controller.vm import VM
|
||||
from gns3server.controller.node import Node
|
||||
from gns3server.controller.compute import Compute
|
||||
from gns3server.controller.project import Project
|
||||
|
||||
@@ -41,23 +41,23 @@ def compute():
|
||||
|
||||
@pytest.fixture
|
||||
def link(async_run, project, compute):
|
||||
vm1 = VM(project, compute)
|
||||
vm2 = VM(project, compute)
|
||||
node1 = Node(project, compute)
|
||||
node2 = Node(project, compute)
|
||||
|
||||
link = Link(project)
|
||||
async_run(link.addVM(vm1, 0, 4))
|
||||
async_run(link.addVM(vm2, 1, 3))
|
||||
async_run(link.add_node(node1, 0, 4))
|
||||
async_run(link.add_node(node2, 1, 3))
|
||||
return link
|
||||
|
||||
|
||||
def test_addVM(async_run, project, compute):
|
||||
vm1 = VM(project, compute)
|
||||
def test_addNode(async_run, project, compute):
|
||||
node1 = Node(project, compute)
|
||||
|
||||
link = Link(project)
|
||||
async_run(link.addVM(vm1, 0, 4))
|
||||
assert link._vms == [
|
||||
async_run(link.add_node(node1, 0, 4))
|
||||
assert link._nodes == [
|
||||
{
|
||||
"vm": vm1,
|
||||
"node": node1,
|
||||
"adapter_number": 0,
|
||||
"port_number": 4
|
||||
}
|
||||
@@ -65,22 +65,22 @@ def test_addVM(async_run, project, compute):
|
||||
|
||||
|
||||
def test_json(async_run, project, compute):
|
||||
vm1 = VM(project, compute)
|
||||
vm2 = VM(project, compute)
|
||||
node1 = Node(project, compute)
|
||||
node2 = Node(project, compute)
|
||||
|
||||
link = Link(project)
|
||||
async_run(link.addVM(vm1, 0, 4))
|
||||
async_run(link.addVM(vm2, 1, 3))
|
||||
async_run(link.add_node(node1, 0, 4))
|
||||
async_run(link.add_node(node2, 1, 3))
|
||||
assert link.__json__() == {
|
||||
"link_id": link.id,
|
||||
"vms": [
|
||||
"nodes": [
|
||||
{
|
||||
"vm_id": vm1.id,
|
||||
"node_id": node1.id,
|
||||
"adapter_number": 0,
|
||||
"port_number": 4
|
||||
},
|
||||
{
|
||||
"vm_id": vm2.id,
|
||||
"node_id": node2.id,
|
||||
"adapter_number": 1,
|
||||
"port_number": 3
|
||||
}
|
||||
@@ -109,10 +109,10 @@ def test_start_streaming_pcap(link, async_run, tmpdir, project):
|
||||
|
||||
|
||||
def test_default_capture_file_name(project, compute, async_run):
|
||||
vm1 = VM(project, compute, name="Hello@")
|
||||
vm2 = VM(project, compute, name="w0.rld")
|
||||
node1 = Node(project, compute, name="Hello@")
|
||||
node2 = Node(project, compute, name="w0.rld")
|
||||
|
||||
link = Link(project)
|
||||
async_run(link.addVM(vm1, 0, 4))
|
||||
async_run(link.addVM(vm2, 1, 3))
|
||||
async_run(link.add_node(node1, 0, 4))
|
||||
async_run(link.add_node(node2, 1, 3))
|
||||
assert link.default_capture_file_name() == "Hello_0-4_to_w0rld_1-3.pcap"
|
||||
@@ -0,0 +1,171 @@
|
||||
#!/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 uuid
|
||||
import asyncio
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
from tests.utils import AsyncioMagicMock
|
||||
|
||||
from gns3server.controller.node import Node
|
||||
from gns3server.controller.project import Project
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def compute():
|
||||
s = AsyncioMagicMock()
|
||||
s.id = "http://test.com:42"
|
||||
return s
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def node(compute):
|
||||
project = Project(str(uuid.uuid4()))
|
||||
node = Node(project, compute,
|
||||
name="demo",
|
||||
node_id=str(uuid.uuid4()),
|
||||
node_type="vpcs",
|
||||
console_type="vnc",
|
||||
properties={"startup_script": "echo test"})
|
||||
return node
|
||||
|
||||
|
||||
def test_json(node, compute):
|
||||
assert node.__json__() == {
|
||||
"compute_id": compute.id,
|
||||
"project_id": node.project.id,
|
||||
"node_id": node.id,
|
||||
"node_type": node.node_type,
|
||||
"name": "demo",
|
||||
"console": node.console,
|
||||
"console_type": node.console_type,
|
||||
"properties": node.properties
|
||||
}
|
||||
|
||||
|
||||
def test_init_without_uuid(project, compute):
|
||||
node = Node(project, compute,
|
||||
node_type="vpcs",
|
||||
console_type="vnc")
|
||||
assert node.id is not None
|
||||
|
||||
|
||||
def test_create(node, compute, project, async_run):
|
||||
node._console = 2048
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
async_run(node.create())
|
||||
data = {
|
||||
"console": 2048,
|
||||
"console_type": "vnc",
|
||||
"node_id": node.id,
|
||||
"startup_script": "echo test",
|
||||
"name": "demo"
|
||||
}
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/nodes".format(node.project.id), data=data)
|
||||
assert node._console == 2048
|
||||
assert node._properties == {"startup_script": "echo test"}
|
||||
|
||||
|
||||
def test_update(node, compute, project, async_run):
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
compute.put = AsyncioMagicMock(return_value=response)
|
||||
|
||||
async_run(node.update(console=2048, console_type="vnc", properties={"startup_script": "echo test"}, name="demo"))
|
||||
data = {
|
||||
"console": 2048,
|
||||
"console_type": "vnc",
|
||||
"startup_script": "echo test",
|
||||
"name": "demo"
|
||||
}
|
||||
compute.put.assert_called_with("/projects/{}/vpcs/nodes/{}".format(node.project.id, node.id), data=data)
|
||||
assert node._console == 2048
|
||||
assert node._properties == {"startup_script": "echo test"}
|
||||
|
||||
|
||||
def test_start(node, compute, project, async_run):
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
async_run(node.start())
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/start".format(node.project.id, node.id))
|
||||
|
||||
|
||||
def test_stop(node, compute, project, async_run):
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
async_run(node.stop())
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/stop".format(node.project.id, node.id))
|
||||
|
||||
|
||||
def test_suspend(node, compute, project, async_run):
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
async_run(node.suspend())
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/suspend".format(node.project.id, node.id))
|
||||
|
||||
|
||||
def test_reload(node, compute, project, async_run):
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
async_run(node.reload())
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/reload".format(node.project.id, node.id))
|
||||
|
||||
|
||||
def test_create_without_console(node, compute, project, async_run):
|
||||
"""
|
||||
None properties should be send. Because it can mean the emulator doesn"t support it
|
||||
"""
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048, "test_value": "success"}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
async_run(node.create())
|
||||
data = {
|
||||
"console_type": "vnc",
|
||||
"node_id": node.id,
|
||||
"startup_script": "echo test",
|
||||
"name": "demo"
|
||||
}
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/nodes".format(node.project.id), data=data)
|
||||
assert node._console == 2048
|
||||
assert node._properties == {"test_value": "success", "startup_script": "echo test"}
|
||||
|
||||
|
||||
def test_delete(node, compute, async_run):
|
||||
async_run(node.destroy())
|
||||
compute.delete.assert_called_with("/projects/{}/vpcs/nodes/{}".format(node.project.id, node.id))
|
||||
|
||||
|
||||
def test_post(node, compute, async_run):
|
||||
async_run(node.post("/test", {"a": "b"}))
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/nodes/{}/test".format(node.project.id, node.id), data={"a": "b"})
|
||||
|
||||
|
||||
def test_delete(node, compute, async_run):
|
||||
async_run(node.delete("/test"))
|
||||
compute.delete.assert_called_with("/projects/{}/vpcs/nodes/{}/test".format(node.project.id, node.id))
|
||||
@@ -77,10 +77,10 @@ def test_addVM(async_run):
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
vm = async_run(project.addVM(compute, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
vm = async_run(project.add_node(compute, None, name="test", node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
|
||||
compute.post.assert_called_with('/projects/{}/vpcs/vms'.format(project.id),
|
||||
data={'vm_id': vm.id,
|
||||
compute.post.assert_called_with('/projects/{}/vpcs/nodes'.format(project.id),
|
||||
data={'node_id': vm.id,
|
||||
'console_type': 'telnet',
|
||||
'startup_config': 'test.cfg',
|
||||
'name': 'test'})
|
||||
@@ -94,11 +94,11 @@ def test_getVM(async_run):
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
vm = async_run(project.addVM(compute, None, name="test", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
assert project.getVM(vm.id) == vm
|
||||
vm = async_run(project.add_node(compute, None, name="test", node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
assert project.get_node(vm.id) == vm
|
||||
|
||||
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
|
||||
project.getVM("test")
|
||||
project.get_node("test")
|
||||
|
||||
|
||||
def test_addLink(async_run):
|
||||
@@ -109,12 +109,12 @@ def test_addLink(async_run):
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
vm1 = async_run(project.addVM(compute, None, name="test1", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
vm2 = async_run(project.addVM(compute, None, name="test2", vm_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
link = async_run(project.addLink())
|
||||
async_run(link.addVM(vm1, 3, 1))
|
||||
async_run(link.addVM(vm2, 4, 2))
|
||||
assert len(link._vms) == 2
|
||||
vm1 = async_run(project.add_node(compute, None, name="test1", node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
vm2 = async_run(project.add_node(compute, None, name="test2", node_type="vpcs", properties={"startup_config": "test.cfg"}))
|
||||
link = async_run(project.add_link())
|
||||
async_run(link.add_node(vm1, 3, 1))
|
||||
async_run(link.add_node(vm2, 4, 2))
|
||||
assert len(link._nodes) == 2
|
||||
|
||||
|
||||
def test_getLink(async_run):
|
||||
@@ -125,11 +125,11 @@ def test_getLink(async_run):
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
link = async_run(project.addLink())
|
||||
assert project.getLink(link.id) == link
|
||||
link = async_run(project.add_link())
|
||||
assert project.get_link(link.id) == link
|
||||
|
||||
with pytest.raises(aiohttp.web_exceptions.HTTPNotFound):
|
||||
project.getLink("test")
|
||||
project.get_link("test")
|
||||
|
||||
|
||||
def test_emit(async_run):
|
||||
|
||||
@@ -22,9 +22,8 @@ from unittest.mock import MagicMock
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
from gns3server.controller.project import Project
|
||||
from gns3server.controller.compute import Compute
|
||||
from gns3server.controller.udp_link import UDPLink
|
||||
from gns3server.controller.vm import VM
|
||||
from gns3server.controller.node import Node
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -36,12 +35,12 @@ def test_create(async_run, project):
|
||||
compute1 = MagicMock()
|
||||
compute2 = MagicMock()
|
||||
|
||||
vm1 = VM(project, compute1, vm_type="vpcs")
|
||||
vm2 = VM(project, compute2, vm_type="vpcs")
|
||||
node1 = Node(project, compute1, node_type="vpcs")
|
||||
node2 = Node(project, compute2, node_type="vpcs")
|
||||
|
||||
link = UDPLink(project)
|
||||
async_run(link.addVM(vm1, 0, 4))
|
||||
async_run(link.addVM(vm2, 3, 1))
|
||||
async_run(link.add_node(node1, 0, 4))
|
||||
async_run(link.add_node(node2, 3, 1))
|
||||
|
||||
@asyncio.coroutine
|
||||
def compute1_callback(path, data={}):
|
||||
@@ -69,13 +68,13 @@ def test_create(async_run, project):
|
||||
compute2.host = "example.org"
|
||||
async_run(link.create())
|
||||
|
||||
compute1.post.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/0/ports/4/nio".format(project.id, vm1.id), data={
|
||||
compute1.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id), data={
|
||||
"lport": 1024,
|
||||
"rhost": compute2.host,
|
||||
"rport": 2048,
|
||||
"type": "nio_udp"
|
||||
})
|
||||
compute2.post.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/3/ports/1/nio".format(project.id, vm2.id), data={
|
||||
compute2.post.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id), data={
|
||||
"lport": 2048,
|
||||
"rhost": compute1.host,
|
||||
"rport": 1024,
|
||||
@@ -87,17 +86,17 @@ def test_delete(async_run, project):
|
||||
compute1 = MagicMock()
|
||||
compute2 = MagicMock()
|
||||
|
||||
vm1 = VM(project, compute1, vm_type="vpcs")
|
||||
vm2 = VM(project, compute2, vm_type="vpcs")
|
||||
node1 = Node(project, compute1, node_type="vpcs")
|
||||
node2 = Node(project, compute2, node_type="vpcs")
|
||||
|
||||
link = UDPLink(project)
|
||||
async_run(link.addVM(vm1, 0, 4))
|
||||
async_run(link.addVM(vm2, 3, 1))
|
||||
async_run(link.add_node(node1, 0, 4))
|
||||
async_run(link.add_node(node2, 3, 1))
|
||||
|
||||
async_run(link.delete())
|
||||
|
||||
compute1.delete.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/0/ports/4/nio".format(project.id, vm1.id))
|
||||
compute2.delete.assert_any_call("/projects/{}/vpcs/vms/{}/adapters/3/ports/1/nio".format(project.id, vm2.id))
|
||||
compute1.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/0/ports/4/nio".format(project.id, node1.id))
|
||||
compute2.delete.assert_any_call("/projects/{}/vpcs/nodes/{}/adapters/3/ports/1/nio".format(project.id, node2.id))
|
||||
|
||||
|
||||
def test_choose_capture_side(async_run, project):
|
||||
@@ -108,51 +107,51 @@ def test_choose_capture_side(async_run, project):
|
||||
compute2 = MagicMock()
|
||||
compute2.id = "local"
|
||||
|
||||
vm_vpcs = VM(project, compute1, vm_type="vpcs")
|
||||
vm_iou = VM(project, compute2, vm_type="iou")
|
||||
node_vpcs = Node(project, compute1, node_type="vpcs")
|
||||
node_iou = Node(project, compute2, node_type="iou")
|
||||
|
||||
link = UDPLink(project)
|
||||
async_run(link.addVM(vm_vpcs, 0, 4))
|
||||
async_run(link.addVM(vm_iou, 3, 1))
|
||||
async_run(link.add_node(node_vpcs, 0, 4))
|
||||
async_run(link.add_node(node_iou, 3, 1))
|
||||
|
||||
assert link._choose_capture_side()["vm"] == vm_iou
|
||||
assert link._choose_capture_side()["node"] == node_iou
|
||||
|
||||
vm_vpcs = VM(project, compute1, vm_type="vpcs")
|
||||
vm_vpcs2 = VM(project, compute1, vm_type="vpcs")
|
||||
node_vpcs = Node(project, compute1, node_type="vpcs")
|
||||
node_vpcs2 = Node(project, compute1, node_type="vpcs")
|
||||
|
||||
link = UDPLink(project)
|
||||
async_run(link.addVM(vm_vpcs, 0, 4))
|
||||
async_run(link.addVM(vm_vpcs2, 3, 1))
|
||||
async_run(link.add_node(node_vpcs, 0, 4))
|
||||
async_run(link.add_node(node_vpcs2, 3, 1))
|
||||
|
||||
# VPCS doesn't support capture
|
||||
with pytest.raises(aiohttp.web.HTTPConflict):
|
||||
link._choose_capture_side()["vm"]
|
||||
link._choose_capture_side()["node"]
|
||||
|
||||
# Capture should run on the local node
|
||||
vm_iou = VM(project, compute1, vm_type="iou")
|
||||
vm_iou2 = VM(project, compute2, vm_type="iou")
|
||||
node_iou = Node(project, compute1, node_type="iou")
|
||||
node_iou2 = Node(project, compute2, node_type="iou")
|
||||
|
||||
link = UDPLink(project)
|
||||
async_run(link.addVM(vm_iou, 0, 4))
|
||||
async_run(link.addVM(vm_iou2, 3, 1))
|
||||
async_run(link.add_node(node_iou, 0, 4))
|
||||
async_run(link.add_node(node_iou2, 3, 1))
|
||||
|
||||
assert link._choose_capture_side()["vm"] == vm_iou2
|
||||
assert link._choose_capture_side()["node"] == node_iou2
|
||||
|
||||
|
||||
def test_capture(async_run, project):
|
||||
compute1 = MagicMock()
|
||||
|
||||
vm_vpcs = VM(project, compute1, vm_type="vpcs", name="V1")
|
||||
vm_iou = VM(project, compute1, vm_type="iou", name="I1")
|
||||
node_vpcs = Node(project, compute1, node_type="vpcs", name="V1")
|
||||
node_iou = Node(project, compute1, node_type="iou", name="I1")
|
||||
|
||||
link = UDPLink(project)
|
||||
async_run(link.addVM(vm_vpcs, 0, 4))
|
||||
async_run(link.addVM(vm_iou, 3, 1))
|
||||
async_run(link.add_node(node_vpcs, 0, 4))
|
||||
async_run(link.add_node(node_iou, 3, 1))
|
||||
|
||||
capture = async_run(link.start_capture())
|
||||
assert link.capturing
|
||||
|
||||
compute1.post.assert_any_call("/projects/{}/iou/vms/{}/adapters/3/ports/1/start_capture".format(project.id, vm_iou.id), data={
|
||||
compute1.post.assert_any_call("/projects/{}/iou/nodes/{}/adapters/3/ports/1/start_capture".format(project.id, node_iou.id), data={
|
||||
"capture_file_name": link.default_capture_file_name(),
|
||||
"data_link_type": "DLT_EN10MB"
|
||||
})
|
||||
@@ -160,18 +159,18 @@ def test_capture(async_run, project):
|
||||
capture = async_run(link.stop_capture())
|
||||
assert link.capturing is False
|
||||
|
||||
compute1.post.assert_any_call("/projects/{}/iou/vms/{}/adapters/3/ports/1/stop_capture".format(project.id, vm_iou.id))
|
||||
compute1.post.assert_any_call("/projects/{}/iou/nodes/{}/adapters/3/ports/1/stop_capture".format(project.id, node_iou.id))
|
||||
|
||||
|
||||
def test_read_pcap_from_source(project, async_run):
|
||||
compute1 = MagicMock()
|
||||
|
||||
link = UDPLink(project)
|
||||
async_run(link.addVM(compute1, 0, 4))
|
||||
async_run(link.addVM(compute1, 3, 1))
|
||||
async_run(link.add_node(compute1, 0, 4))
|
||||
async_run(link.add_node(compute1, 3, 1))
|
||||
|
||||
capture = async_run(link.start_capture())
|
||||
assert link._capture_vm is not None
|
||||
assert link._capture_node is not None
|
||||
|
||||
async_run(link.read_pcap_from_source())
|
||||
link._capture_vm["vm"].compute.streamFile.assert_called_with(project, "tmp/captures/" + link._capture_file_name)
|
||||
link._capture_node["node"].compute.streamFile.assert_called_with(project, "tmp/captures/" + link._capture_file_name)
|
||||
@@ -1,171 +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 uuid
|
||||
import asyncio
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
from tests.utils import AsyncioMagicMock
|
||||
|
||||
from gns3server.controller.vm import VM
|
||||
from gns3server.controller.project import Project
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def compute():
|
||||
s = AsyncioMagicMock()
|
||||
s.id = "http://test.com:42"
|
||||
return s
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def vm(compute):
|
||||
project = Project(str(uuid.uuid4()))
|
||||
vm = VM(project, compute,
|
||||
name="demo",
|
||||
vm_id=str(uuid.uuid4()),
|
||||
vm_type="vpcs",
|
||||
console_type="vnc",
|
||||
properties={"startup_script": "echo test"})
|
||||
return vm
|
||||
|
||||
|
||||
def test_json(vm, compute):
|
||||
assert vm.__json__() == {
|
||||
"compute_id": compute.id,
|
||||
"project_id": vm.project.id,
|
||||
"vm_id": vm.id,
|
||||
"vm_type": vm.vm_type,
|
||||
"name": "demo",
|
||||
"console": vm.console,
|
||||
"console_type": vm.console_type,
|
||||
"properties": vm.properties
|
||||
}
|
||||
|
||||
|
||||
def test_init_without_uuid(project, compute):
|
||||
vm = VM(project, compute,
|
||||
vm_type="vpcs",
|
||||
console_type="vnc")
|
||||
assert vm.id is not None
|
||||
|
||||
|
||||
def test_create(vm, compute, project, async_run):
|
||||
vm._console = 2048
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
async_run(vm.create())
|
||||
data = {
|
||||
"console": 2048,
|
||||
"console_type": "vnc",
|
||||
"vm_id": vm.id,
|
||||
"startup_script": "echo test",
|
||||
"name": "demo"
|
||||
}
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)
|
||||
assert vm._console == 2048
|
||||
assert vm._properties == {"startup_script": "echo test"}
|
||||
|
||||
|
||||
def test_update(vm, compute, project, async_run):
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
compute.put = AsyncioMagicMock(return_value=response)
|
||||
|
||||
async_run(vm.update(console=2048, console_type="vnc", properties={"startup_script": "echo test"}, name="demo"))
|
||||
data = {
|
||||
"console": 2048,
|
||||
"console_type": "vnc",
|
||||
"startup_script": "echo test",
|
||||
"name": "demo"
|
||||
}
|
||||
compute.put.assert_called_with("/projects/{}/vpcs/vms/{}".format(vm.project.id, vm.id), data=data)
|
||||
assert vm._console == 2048
|
||||
assert vm._properties == {"startup_script": "echo test"}
|
||||
|
||||
|
||||
def test_start(vm, compute, project, async_run):
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
async_run(vm.start())
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/start".format(vm.project.id, vm.id))
|
||||
|
||||
|
||||
def test_stop(vm, compute, project, async_run):
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
async_run(vm.stop())
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/stop".format(vm.project.id, vm.id))
|
||||
|
||||
|
||||
def test_suspend(vm, compute, project, async_run):
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
async_run(vm.suspend())
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/suspend".format(vm.project.id, vm.id))
|
||||
|
||||
|
||||
def test_reload(vm, compute, project, async_run):
|
||||
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
async_run(vm.reload())
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/reload".format(vm.project.id, vm.id))
|
||||
|
||||
|
||||
def test_create_without_console(vm, compute, project, async_run):
|
||||
"""
|
||||
None properties should be send. Because it can mean the emulator doesn"t support it
|
||||
"""
|
||||
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048, "test_value": "success"}
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
async_run(vm.create())
|
||||
data = {
|
||||
"console_type": "vnc",
|
||||
"vm_id": vm.id,
|
||||
"startup_script": "echo test",
|
||||
"name": "demo"
|
||||
}
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/vms".format(vm.project.id), data=data)
|
||||
assert vm._console == 2048
|
||||
assert vm._properties == {"test_value": "success", "startup_script": "echo test"}
|
||||
|
||||
|
||||
def test_delete(vm, compute, async_run):
|
||||
async_run(vm.destroy())
|
||||
compute.delete.assert_called_with("/projects/{}/vpcs/vms/{}".format(vm.project.id, vm.id))
|
||||
|
||||
|
||||
def test_post(vm, compute, async_run):
|
||||
async_run(vm.post("/test", {"a": "b"}))
|
||||
compute.post.assert_called_with("/projects/{}/vpcs/vms/{}/test".format(vm.project.id, vm.id), data={"a": "b"})
|
||||
|
||||
|
||||
def test_delete(vm, compute, async_run):
|
||||
async_run(vm.delete("/test"))
|
||||
compute.delete.assert_called_with("/projects/{}/vpcs/vms/{}/test".format(vm.project.id, vm.id))
|
||||
Reference in new issue
Block a user