2015-10-14 16:10:05 +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 pytest
|
|
|
|
|
import uuid
|
|
|
|
|
import asyncio
|
2016-02-12 10:57:56 +00:00
|
|
|
|
import os
|
2016-03-01 13:53:43 +00:00
|
|
|
|
from tests.utils import asyncio_patch, AsyncioMagicMock
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
2016-02-11 14:49:28 +00:00
|
|
|
|
from gns3server.ubridge.ubridge_error import UbridgeNamespaceError
|
2016-03-08 15:12:46 +00:00
|
|
|
|
from gns3server.hypervisor.docker.docker_vm import DockerVM
|
|
|
|
|
from gns3server.hypervisor.docker.docker_error import *
|
|
|
|
|
from gns3server.hypervisor.docker import Docker
|
2016-03-01 17:38:03 +00:00
|
|
|
|
from gns3server.utils.get_resource import get_resource
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
|
from unittest.mock import patch, MagicMock, PropertyMock, call
|
|
|
|
|
from gns3server.config import Config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
|
def manager(port_manager):
|
|
|
|
|
m = Docker.instance()
|
|
|
|
|
m.port_manager = port_manager
|
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def vm(project, manager):
|
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
|
|
|
|
|
vm._cid = "e90e34656842"
|
2016-03-01 13:53:43 +00:00
|
|
|
|
vm.allocate_aux = False
|
2015-10-14 16:10:05 +00:00
|
|
|
|
return vm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_json(vm, project):
|
|
|
|
|
assert vm.__json__() == {
|
|
|
|
|
'container_id': 'e90e34656842',
|
|
|
|
|
'image': 'ubuntu',
|
|
|
|
|
'name': 'test',
|
|
|
|
|
'project_id': project.id,
|
|
|
|
|
'vm_id': vm.id,
|
|
|
|
|
'adapters': 1,
|
|
|
|
|
'console': vm.console,
|
2016-02-29 20:08:25 +00:00
|
|
|
|
'console_type': 'telnet',
|
2016-02-29 09:38:30 +00:00
|
|
|
|
'aux': vm.aux,
|
2015-10-14 16:10:05 +00:00
|
|
|
|
'start_command': vm.start_command,
|
2016-02-12 10:57:56 +00:00
|
|
|
|
'environment': vm.environment,
|
|
|
|
|
'vm_directory': vm.working_dir
|
2015-10-14 16:10:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-19 16:01:28 +00:00
|
|
|
|
def test_start_command(vm):
|
|
|
|
|
|
|
|
|
|
vm.start_command = "hello"
|
|
|
|
|
assert vm.start_command == "hello"
|
|
|
|
|
vm.start_command = " "
|
|
|
|
|
assert vm.start_command is None
|
|
|
|
|
|
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
|
def test_create(loop, project, manager):
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
|
"Warnings": []
|
|
|
|
|
}
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.create()))
|
|
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
|
"Tty": True,
|
|
|
|
|
"OpenStdin": True,
|
|
|
|
|
"StdinOnce": False,
|
|
|
|
|
"HostConfig":
|
|
|
|
|
{
|
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-08 15:12:46 +00:00
|
|
|
|
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
|
2015-10-14 16:10:05 +00:00
|
|
|
|
"Privileged": True
|
|
|
|
|
},
|
2016-02-12 10:57:56 +00:00
|
|
|
|
"Volumes": {},
|
2015-10-14 16:10:05 +00:00
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
|
"Name": "test",
|
2016-02-23 18:22:35 +00:00
|
|
|
|
"Hostname": "test",
|
2016-02-29 20:08:25 +00:00
|
|
|
|
"Image": "ubuntu",
|
2016-03-01 17:38:03 +00:00
|
|
|
|
"Env": [],
|
2016-03-04 08:08:40 +00:00
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
|
"Cmd": ["/bin/sh"]
|
2016-02-29 20:08:25 +00:00
|
|
|
|
})
|
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_vnc(loop, project, manager):
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
|
"Warnings": []
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2016-02-29 20:08:25 +00:00
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", console_type="vnc")
|
|
|
|
|
vm._start_vnc = MagicMock()
|
|
|
|
|
vm._display = 42
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.create()))
|
|
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
|
"Tty": True,
|
|
|
|
|
"OpenStdin": True,
|
|
|
|
|
"StdinOnce": False,
|
|
|
|
|
"HostConfig":
|
|
|
|
|
{
|
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-01 17:38:03 +00:00
|
|
|
|
"Binds": [
|
2016-03-08 15:12:46 +00:00
|
|
|
|
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
|
2016-03-01 17:38:03 +00:00
|
|
|
|
'/tmp/.X11-unix/:/tmp/.X11-unix/'
|
|
|
|
|
],
|
2016-02-29 20:08:25 +00:00
|
|
|
|
"Privileged": True
|
|
|
|
|
},
|
|
|
|
|
"Volumes": {},
|
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
|
"Name": "test",
|
|
|
|
|
"Hostname": "test",
|
|
|
|
|
"Image": "ubuntu",
|
2016-03-01 17:38:03 +00:00
|
|
|
|
"Env": ['DISPLAY=:42'],
|
2016-03-04 08:08:40 +00:00
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
|
"Cmd": ["/bin/sh"]
|
2015-10-14 16:10:05 +00:00
|
|
|
|
})
|
2016-02-29 20:08:25 +00:00
|
|
|
|
assert vm._start_vnc.called
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_start_cmd(loop, project, manager):
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
|
"Warnings": []
|
|
|
|
|
}
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
|
|
|
|
|
vm._start_command = "/bin/ls"
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.create()))
|
|
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
|
"Tty": True,
|
|
|
|
|
"OpenStdin": True,
|
|
|
|
|
"StdinOnce": False,
|
|
|
|
|
"HostConfig":
|
|
|
|
|
{
|
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-08 15:12:46 +00:00
|
|
|
|
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
|
2015-10-14 16:10:05 +00:00
|
|
|
|
"Privileged": True
|
|
|
|
|
},
|
2016-02-12 10:57:56 +00:00
|
|
|
|
"Volumes": {},
|
2016-03-04 08:08:40 +00:00
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
|
"Cmd": ["/bin/ls"],
|
2015-10-14 16:10:05 +00:00
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
|
"Name": "test",
|
2016-02-23 18:22:35 +00:00
|
|
|
|
"Hostname": "test",
|
2016-02-29 20:08:25 +00:00
|
|
|
|
"Image": "ubuntu",
|
|
|
|
|
"Env": []
|
2015-10-14 16:10:05 +00:00
|
|
|
|
})
|
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_environment(loop, project, manager):
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
|
"Warnings": []
|
|
|
|
|
}
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
|
|
|
|
|
vm.environment = "YES=1\nNO=0"
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.create()))
|
|
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
|
"Tty": True,
|
|
|
|
|
"OpenStdin": True,
|
|
|
|
|
"StdinOnce": False,
|
|
|
|
|
"HostConfig":
|
|
|
|
|
{
|
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-08 15:12:46 +00:00
|
|
|
|
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
|
2015-10-14 16:10:05 +00:00
|
|
|
|
"Privileged": True
|
|
|
|
|
},
|
2016-02-12 10:57:56 +00:00
|
|
|
|
"Env": ["YES=1", "NO=0"],
|
|
|
|
|
"Volumes": {},
|
2015-10-14 16:10:05 +00:00
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
|
"Name": "test",
|
2016-02-23 18:22:35 +00:00
|
|
|
|
"Hostname": "test",
|
2016-03-01 17:38:03 +00:00
|
|
|
|
"Image": "ubuntu",
|
2016-03-04 08:08:40 +00:00
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
|
"Cmd": ["/bin/sh"]
|
2015-10-14 16:10:05 +00:00
|
|
|
|
})
|
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_image_not_available(loop, project, manager):
|
|
|
|
|
|
2016-02-24 16:08:28 +00:00
|
|
|
|
call = 0
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def informations():
|
|
|
|
|
nonlocal call
|
|
|
|
|
if call == 0:
|
|
|
|
|
call += 1
|
|
|
|
|
raise DockerHttp404Error("missing")
|
|
|
|
|
else:
|
|
|
|
|
return {}
|
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
|
response = {
|
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
|
"Warnings": []
|
|
|
|
|
}
|
2016-02-24 16:08:28 +00:00
|
|
|
|
|
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
|
|
|
|
|
vm._get_image_informations = MagicMock()
|
|
|
|
|
vm._get_image_informations.side_effect = informations
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM.pull_image", return_value=True) as mock_pull:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2016-02-24 16:08:28 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.create()))
|
|
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
|
"Tty": True,
|
|
|
|
|
"OpenStdin": True,
|
|
|
|
|
"StdinOnce": False,
|
|
|
|
|
"HostConfig":
|
|
|
|
|
{
|
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-08 15:12:46 +00:00
|
|
|
|
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
|
2016-02-24 16:08:28 +00:00
|
|
|
|
"Privileged": True
|
|
|
|
|
},
|
|
|
|
|
"Volumes": {},
|
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
|
"Name": "test",
|
|
|
|
|
"Hostname": "test",
|
2016-02-29 20:08:25 +00:00
|
|
|
|
"Image": "ubuntu",
|
2016-03-01 17:38:03 +00:00
|
|
|
|
"Env": [],
|
2016-03-04 08:08:40 +00:00
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
|
"Cmd": ["/bin/sh"]
|
2016-02-24 16:08:28 +00:00
|
|
|
|
})
|
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
mock_pull.assert_called_with("ubuntu")
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_container_state(loop, vm):
|
|
|
|
|
response = {
|
|
|
|
|
"State": {
|
|
|
|
|
"Error": "",
|
|
|
|
|
"ExitCode": 9,
|
|
|
|
|
"FinishedAt": "2015-01-06T15:47:32.080254511Z",
|
|
|
|
|
"OOMKilled": False,
|
|
|
|
|
"Paused": False,
|
|
|
|
|
"Pid": 0,
|
|
|
|
|
"Restarting": False,
|
|
|
|
|
"Running": True,
|
|
|
|
|
"StartedAt": "2015-01-06T15:47:32.072697474Z"
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert loop.run_until_complete(asyncio.async(vm._get_container_state())) == "running"
|
|
|
|
|
|
|
|
|
|
response["State"]["Running"] = False
|
|
|
|
|
response["State"]["Paused"] = True
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert loop.run_until_complete(asyncio.async(vm._get_container_state())) == "paused"
|
|
|
|
|
|
|
|
|
|
response["State"]["Running"] = False
|
|
|
|
|
response["State"]["Paused"] = False
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert loop.run_until_complete(asyncio.async(vm._get_container_state())) == "exited"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_running(loop, vm):
|
|
|
|
|
response = {
|
|
|
|
|
"State": {
|
|
|
|
|
"Running": False,
|
|
|
|
|
"Paused": False
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert loop.run_until_complete(asyncio.async(vm.is_running())) is False
|
|
|
|
|
|
|
|
|
|
response["State"]["Running"] = True
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert loop.run_until_complete(asyncio.async(vm.is_running())) is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_pause(loop, vm):
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.pause()))
|
|
|
|
|
|
|
|
|
|
mock.assert_called_with("POST", "containers/e90e34656842/pause")
|
|
|
|
|
assert vm.status == "paused"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_unpause(loop, vm):
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.unpause()))
|
|
|
|
|
|
|
|
|
|
mock.assert_called_with("POST", "containers/e90e34656842/unpause")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_start(loop, vm, manager, free_console_port):
|
|
|
|
|
|
|
|
|
|
assert vm.status != "started"
|
|
|
|
|
vm.adapters = 1
|
|
|
|
|
|
2016-03-01 13:53:43 +00:00
|
|
|
|
vm.allocate_aux = True
|
|
|
|
|
vm._start_aux = AsyncioMagicMock()
|
|
|
|
|
|
|
|
|
|
vm._get_container_state = AsyncioMagicMock(return_value="stopped")
|
|
|
|
|
vm._start_ubridge = AsyncioMagicMock()
|
|
|
|
|
vm._get_namespace = AsyncioMagicMock(return_value=42)
|
|
|
|
|
vm._add_ubridge_connection = AsyncioMagicMock()
|
|
|
|
|
vm._start_console = AsyncioMagicMock()
|
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
|
nio = manager.create_nio(0, {"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
|
2016-03-01 13:53:43 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
|
2016-03-01 13:53:43 +00:00
|
|
|
|
vm._add_ubridge_connection.assert_called_once_with(nio, 0, 42)
|
|
|
|
|
assert vm._start_ubridge.called
|
|
|
|
|
assert vm._start_console.called
|
|
|
|
|
assert vm._start_aux.called
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert vm.status == "started"
|
|
|
|
|
|
|
|
|
|
|
2016-02-11 14:49:28 +00:00
|
|
|
|
def test_start_namespace_failed(loop, vm, manager, free_console_port):
|
|
|
|
|
|
|
|
|
|
assert vm.status != "started"
|
|
|
|
|
vm.adapters = 1
|
|
|
|
|
|
|
|
|
|
nio = manager.create_nio(0, {"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._start_ubridge") as mock_start_ubridge:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._add_ubridge_connection", side_effect=UbridgeNamespaceError()) as mock_add_ubridge_connection:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_log", return_value='Hello not available') as mock_log:
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
|
|
with pytest.raises(DockerError):
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
|
|
|
|
|
|
|
|
|
mock_query.assert_any_call("POST", "containers/e90e34656842/start")
|
|
|
|
|
mock_add_ubridge_connection.assert_called_once_with(nio, 0, 42)
|
|
|
|
|
assert mock_start_ubridge.called
|
|
|
|
|
assert vm.status == "stopped"
|
|
|
|
|
|
|
|
|
|
|
2016-02-09 13:22:37 +00:00
|
|
|
|
def test_start_without_nio(loop, vm, manager, free_console_port):
|
|
|
|
|
"""
|
|
|
|
|
If no nio exists we will create one.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
assert vm.status != "started"
|
|
|
|
|
vm.adapters = 1
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._start_ubridge") as mock_start_ubridge:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._add_ubridge_connection") as mock_add_ubridge_connection:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._start_console") as mock_start_console:
|
2016-02-11 14:49:28 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
2016-02-09 13:22:37 +00:00
|
|
|
|
|
|
|
|
|
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
|
|
|
|
|
assert mock_add_ubridge_connection.called
|
|
|
|
|
assert mock_start_ubridge.called
|
|
|
|
|
assert mock_start_console.called
|
|
|
|
|
assert vm.status == "started"
|
|
|
|
|
|
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
|
def test_start_unpause(loop, vm, manager, free_console_port):
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="paused"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM.unpause", return_value="paused") as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.start()))
|
|
|
|
|
assert mock.called
|
|
|
|
|
assert vm.status == "started"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_restart(loop, vm):
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.restart()))
|
|
|
|
|
|
|
|
|
|
mock.assert_called_with("POST", "containers/e90e34656842/restart")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stop(loop, vm):
|
|
|
|
|
vm._ubridge_hypervisor = MagicMock()
|
|
|
|
|
vm._ubridge_hypervisor.is_running.return_value = True
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="running"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.stop()))
|
|
|
|
|
mock_query.assert_called_with("POST", "containers/e90e34656842/stop", params={"t": 5})
|
|
|
|
|
assert vm._ubridge_hypervisor.stop.called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stop_paused_container(loop, vm):
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="paused"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM.unpause") as mock_unpause:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.stop()))
|
|
|
|
|
mock_query.assert_called_with("POST", "containers/e90e34656842/stop", params={"t": 5})
|
|
|
|
|
assert mock_unpause.called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update(loop, vm):
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
|
"Warnings": []
|
|
|
|
|
}
|
2016-02-12 15:38:16 +00:00
|
|
|
|
|
|
|
|
|
original_console = vm.console
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock_query:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.update()))
|
|
|
|
|
|
|
|
|
|
mock_query.assert_any_call("DELETE", "containers/e90e34656842", params={"force": 1})
|
|
|
|
|
mock_query.assert_any_call("POST", "containers/create", data={
|
|
|
|
|
"Tty": True,
|
|
|
|
|
"OpenStdin": True,
|
|
|
|
|
"StdinOnce": False,
|
|
|
|
|
"HostConfig":
|
|
|
|
|
{
|
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-08 15:12:46 +00:00
|
|
|
|
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
|
2015-10-14 16:10:05 +00:00
|
|
|
|
"Privileged": True
|
|
|
|
|
},
|
2016-02-12 10:57:56 +00:00
|
|
|
|
"Volumes": {},
|
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
|
"Name": "test",
|
2016-02-23 18:22:35 +00:00
|
|
|
|
"Hostname": "test",
|
2016-02-29 20:08:25 +00:00
|
|
|
|
"Image": "ubuntu",
|
2016-03-01 17:38:03 +00:00
|
|
|
|
"Env": [],
|
2016-03-04 08:08:40 +00:00
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
|
"Cmd": ["/bin/sh"]
|
2015-10-14 16:10:05 +00:00
|
|
|
|
})
|
2016-02-12 15:38:16 +00:00
|
|
|
|
assert vm.console == original_console
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
|
2016-02-24 14:47:53 +00:00
|
|
|
|
def test_update_running(loop, vm):
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
|
"Warnings": []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
original_console = vm.console
|
|
|
|
|
vm.start = MagicMock()
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="running"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock_query:
|
2016-02-24 14:47:53 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.update()))
|
|
|
|
|
|
|
|
|
|
mock_query.assert_any_call("DELETE", "containers/e90e34656842", params={"force": 1})
|
|
|
|
|
mock_query.assert_any_call("POST", "containers/create", data={
|
|
|
|
|
"Tty": True,
|
|
|
|
|
"OpenStdin": True,
|
|
|
|
|
"StdinOnce": False,
|
|
|
|
|
"HostConfig":
|
|
|
|
|
{
|
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-08 15:12:46 +00:00
|
|
|
|
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
|
2016-02-24 14:47:53 +00:00
|
|
|
|
"Privileged": True
|
|
|
|
|
},
|
|
|
|
|
"Volumes": {},
|
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
|
"Name": "test",
|
|
|
|
|
"Hostname": "test",
|
2016-02-29 20:08:25 +00:00
|
|
|
|
"Image": "ubuntu",
|
2016-03-01 17:38:03 +00:00
|
|
|
|
"Env": [],
|
2016-03-04 08:08:40 +00:00
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
|
"Cmd": ["/bin/sh"]
|
2016-02-24 14:47:53 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert vm.console == original_console
|
|
|
|
|
assert vm.start.called
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 20:08:25 +00:00
|
|
|
|
def test_delete(loop, vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
|
2016-02-29 20:08:25 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.delete()))
|
2015-10-14 16:10:05 +00:00
|
|
|
|
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_close(loop, vm, port_manager):
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
|
"lport": 4242,
|
|
|
|
|
"rport": 4343,
|
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
|
nio = vm.manager.create_nio(0, nio)
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
|
2016-02-29 20:08:25 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.close()))
|
|
|
|
|
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1})
|
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert vm._closed is True
|
|
|
|
|
assert "4242" not in port_manager.udp_ports
|
|
|
|
|
|
|
|
|
|
|
2016-02-29 20:08:25 +00:00
|
|
|
|
def test_close_vnc(loop, vm, port_manager):
|
|
|
|
|
|
|
|
|
|
vm._console_type = "vnc"
|
|
|
|
|
vm._x11vnc_process = MagicMock()
|
|
|
|
|
vm._xvfb_process = MagicMock()
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query") as mock_query:
|
2016-02-29 20:08:25 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.close()))
|
|
|
|
|
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1})
|
|
|
|
|
|
|
|
|
|
assert vm._closed is True
|
|
|
|
|
assert vm._xvfb_process.terminate.called
|
|
|
|
|
|
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
|
def test_get_namespace(loop, vm):
|
|
|
|
|
response = {
|
|
|
|
|
"State": {
|
|
|
|
|
"Pid": 42
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock_query:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
assert loop.run_until_complete(asyncio.async(vm._get_namespace())) == 42
|
|
|
|
|
mock_query.assert_called_with("GET", "containers/e90e34656842/json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_ubridge_connection(loop, vm):
|
|
|
|
|
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
|
"lport": 4242,
|
|
|
|
|
"rport": 4343,
|
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
|
nio = vm.manager.create_nio(0, nio)
|
2016-02-09 13:22:37 +00:00
|
|
|
|
nio.startPacketCapture("/tmp/capture.pcap")
|
2015-10-14 16:10:05 +00:00
|
|
|
|
vm._ubridge_hypervisor = MagicMock()
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm._add_ubridge_connection(nio, 0, 42)))
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
calls = [
|
2016-03-01 14:33:30 +00:00
|
|
|
|
call.send("docker create_veth veth-gns3-ext0 veth-gns3-int0"),
|
|
|
|
|
call.send('docker move_to_ns veth-gns3-int0 42 eth0'),
|
2015-10-14 16:10:05 +00:00
|
|
|
|
call.send('bridge create bridge0'),
|
2016-03-01 14:33:30 +00:00
|
|
|
|
call.send('bridge add_nio_linux_raw bridge0 veth-gns3-ext0'),
|
2015-10-14 16:10:05 +00:00
|
|
|
|
call.send('bridge add_nio_udp bridge0 4242 127.0.0.1 4343'),
|
2016-02-09 13:22:37 +00:00
|
|
|
|
call.send('bridge start_capture bridge0 "/tmp/capture.pcap"'),
|
2015-10-14 16:10:05 +00:00
|
|
|
|
call.send('bridge start bridge0')
|
|
|
|
|
]
|
|
|
|
|
# We need to check any_order ortherwise mock is confused by asyncio
|
|
|
|
|
vm._ubridge_hypervisor.assert_has_calls(calls, any_order=True)
|
|
|
|
|
|
|
|
|
|
|
2016-02-09 13:22:37 +00:00
|
|
|
|
def test_add_ubridge_connection_none_nio(loop, vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
2016-02-09 13:22:37 +00:00
|
|
|
|
nio = None
|
2015-10-14 16:10:05 +00:00
|
|
|
|
vm._ubridge_hypervisor = MagicMock()
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm._add_ubridge_connection(nio, 0, 42)))
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
calls = [
|
2016-03-01 14:33:30 +00:00
|
|
|
|
call.send("docker create_veth veth-gns3-ext0 veth-gns3-int0"),
|
|
|
|
|
call.send('docker move_to_ns veth-gns3-int0 42 eth0'),
|
2015-10-14 16:10:05 +00:00
|
|
|
|
]
|
|
|
|
|
# We need to check any_order ortherwise mock is confused by asyncio
|
|
|
|
|
vm._ubridge_hypervisor.assert_has_calls(calls, any_order=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_ubridge_connection_invalid_adapter_number(loop, vm):
|
|
|
|
|
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
|
"lport": 4242,
|
|
|
|
|
"rport": 4343,
|
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
|
nio = vm.manager.create_nio(0, nio)
|
|
|
|
|
with pytest.raises(DockerError):
|
2016-02-11 14:49:28 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm._add_ubridge_connection(nio, 12, 42)))
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_ubridge_connection_no_free_interface(loop, vm):
|
|
|
|
|
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
|
"lport": 4242,
|
|
|
|
|
"rport": 4343,
|
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
|
nio = vm.manager.create_nio(0, nio)
|
|
|
|
|
with pytest.raises(DockerError):
|
|
|
|
|
|
|
|
|
|
# We create fake ethernet interfaces for docker
|
2016-03-01 14:33:30 +00:00
|
|
|
|
interfaces = ["veth-gns3-ext{}".format(index) for index in range(128)]
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
with patch("psutil.net_if_addrs", return_value=interfaces):
|
2016-02-11 14:49:28 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm._add_ubridge_connection(nio, 0, 42)))
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_ubridge_connection(loop, vm):
|
|
|
|
|
|
|
|
|
|
vm._ubridge_hypervisor = MagicMock()
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
|
"lport": 4242,
|
|
|
|
|
"rport": 4343,
|
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
|
nio = vm.manager.create_nio(0, nio)
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm._add_ubridge_connection(nio, 0, 42)))
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm._delete_ubridge_connection(0)))
|
|
|
|
|
|
|
|
|
|
calls = [
|
|
|
|
|
call.send("bridge delete bridge0"),
|
2016-03-01 14:33:30 +00:00
|
|
|
|
call.send('docker delete_veth veth-gns3-ext0')
|
2015-10-14 16:10:05 +00:00
|
|
|
|
]
|
|
|
|
|
vm._ubridge_hypervisor.assert_has_calls(calls, any_order=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_adapter_add_nio_binding(vm, loop):
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
|
"lport": 4242,
|
|
|
|
|
"rport": 4343,
|
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
|
nio = vm.manager.create_nio(0, nio)
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
|
|
|
|
|
assert vm._ethernet_adapters[0].get_nio(0) == nio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_adapter_add_nio_binding_invalid_adapter(vm, loop):
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
|
"lport": 4242,
|
|
|
|
|
"rport": 4343,
|
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
|
nio = vm.manager.create_nio(0, nio)
|
|
|
|
|
with pytest.raises(DockerError):
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(12, nio)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_adapter_remove_nio_binding(vm, loop):
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
|
"lport": 4242,
|
|
|
|
|
"rport": 4343,
|
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
|
nio = vm.manager.create_nio(0, nio)
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._delete_ubridge_connection") as delete_ubridge_mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_remove_nio_binding(0)))
|
|
|
|
|
assert vm._ethernet_adapters[0].get_nio(0) is None
|
|
|
|
|
delete_ubridge_mock.assert_called_with(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_adapter_remove_nio_binding_invalid_adapter(vm, loop):
|
|
|
|
|
with pytest.raises(DockerError):
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_remove_nio_binding(12)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_pull_image(loop, vm):
|
|
|
|
|
class Response:
|
|
|
|
|
"""
|
|
|
|
|
Simulate a response splitted in multiple packets
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self._read = -1
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def read(self, size):
|
|
|
|
|
self._read += 1
|
|
|
|
|
if self._read == 0:
|
|
|
|
|
return b'{"progress": "0/100",'
|
|
|
|
|
elif self._read == 1:
|
|
|
|
|
return '"id": 42}'
|
|
|
|
|
else:
|
|
|
|
|
None
|
|
|
|
|
|
|
|
|
|
mock_query = MagicMock()
|
|
|
|
|
mock_query.content.return_value = Response()
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.http_query", return_value=mock_query) as mock:
|
2015-10-14 16:10:05 +00:00
|
|
|
|
images = loop.run_until_complete(asyncio.async(vm.pull_image("ubuntu")))
|
|
|
|
|
mock.assert_called_with("POST", "images/create", params={"fromImage": "ubuntu"})
|
2016-02-09 15:07:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_start_capture(vm, tmpdir, manager, free_console_port, loop):
|
|
|
|
|
|
|
|
|
|
output_file = str(tmpdir / "test.pcap")
|
|
|
|
|
nio = manager.create_nio(0, {"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.start_capture(0, output_file)))
|
|
|
|
|
assert vm._ethernet_adapters[0].get_nio(0).capturing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stop_capture(vm, tmpdir, manager, free_console_port, loop):
|
|
|
|
|
|
|
|
|
|
output_file = str(tmpdir / "test.pcap")
|
|
|
|
|
nio = manager.create_nio(0, {"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio)))
|
|
|
|
|
loop.run_until_complete(vm.start_capture(0, output_file))
|
|
|
|
|
assert vm._ethernet_adapters[0].get_nio(0).capturing
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm.stop_capture(0)))
|
|
|
|
|
assert vm._ethernet_adapters[0].get_nio(0).capturing is False
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_log(loop, vm):
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def read():
|
|
|
|
|
return b'Hello\nWorld'
|
|
|
|
|
|
|
|
|
|
mock_query = MagicMock()
|
|
|
|
|
mock_query.read = read
|
|
|
|
|
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.http_query", return_value=mock_query) as mock:
|
2016-02-11 14:49:28 +00:00
|
|
|
|
images = loop.run_until_complete(asyncio.async(vm._get_log()))
|
|
|
|
|
mock.assert_called_with("GET", "containers/e90e34656842/logs", params={"stderr": 1, "stdout": 1}, data={})
|
2016-02-12 10:57:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_image_informations(project, manager, loop):
|
|
|
|
|
response = {
|
|
|
|
|
}
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
|
2016-02-12 10:57:56 +00:00
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm._get_image_informations()))
|
|
|
|
|
mock.assert_called_with("GET", "images/ubuntu/json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_mount_binds(vm, tmpdir):
|
|
|
|
|
image_infos = {
|
|
|
|
|
"ContainerConfig": {
|
|
|
|
|
"Volumes": {
|
|
|
|
|
"/test/experimental": {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dst = os.path.join(vm.working_dir, "test/experimental")
|
|
|
|
|
assert vm._mount_binds(image_infos) == [
|
2016-03-08 15:12:46 +00:00
|
|
|
|
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
|
2016-02-12 10:57:56 +00:00
|
|
|
|
"{}:{}".format(dst, "/test/experimental")
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
assert os.path.exists(dst)
|
2016-02-29 20:08:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_start_vnc(vm, loop):
|
|
|
|
|
with patch("shutil.which", return_value="/bin/x"):
|
2016-03-08 15:12:46 +00:00
|
|
|
|
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.wait_for_file_creation") as mock_wait:
|
2016-02-29 20:08:25 +00:00
|
|
|
|
with asyncio_patch("asyncio.create_subprocess_exec") as mock_exec:
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm._start_vnc()))
|
|
|
|
|
assert vm._display is not None
|
|
|
|
|
mock_exec.assert_any_call("Xvfb", "-nolisten", "tcp", ":{}".format(vm._display), "-screen", "0", "1024x768x16")
|
|
|
|
|
mock_exec.assert_any_call("x11vnc", "-forever", "-nopw", "-display", "WAIT:{}".format(vm._display), "-rfbport", str(vm.console), "-noncache", "-listen", "127.0.0.1")
|
|
|
|
|
mock_wait.assert_called_with("/tmp/.X11-unix/X{}".format(vm._display))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_start_vnc_xvfb_missing(vm, loop):
|
|
|
|
|
with pytest.raises(DockerError):
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm._start_vnc()))
|
2016-03-01 13:53:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_start_aux(vm, loop):
|
|
|
|
|
|
|
|
|
|
with asyncio_patch("asyncio.subprocess.create_subprocess_exec", return_value=MagicMock()) as mock_exec:
|
|
|
|
|
loop.run_until_complete(asyncio.async(vm._start_aux()))
|