2015-10-14 16:10:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-06-16 04:29:03 +00:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2015-10-14 16:10:05 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2017-07-11 16:05:11 +00:00
|
|
|
import aiohttp
|
2016-09-27 16:01:50 +00:00
|
|
|
import asyncio
|
2015-10-14 16:10:05 +00:00
|
|
|
import pytest
|
|
|
|
import uuid
|
2016-09-27 16:01:50 +00:00
|
|
|
import sys
|
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-04-15 15:57:06 +00:00
|
|
|
from gns3server.compute.docker.docker_vm import DockerVM
|
2020-06-16 04:29:03 +00:00
|
|
|
from gns3server.compute.docker.docker_error import DockerError, DockerHttp404Error, DockerHttp304Error
|
2016-04-15 15:57:06 +00:00
|
|
|
from gns3server.compute.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
|
|
|
|
2017-07-20 13:37:56 +00:00
|
|
|
from unittest.mock import patch, MagicMock, call
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2016-10-24 19:39:35 +00:00
|
|
|
@pytest.fixture()
|
2020-06-16 04:29:03 +00:00
|
|
|
async def manager(loop, port_manager):
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
m = Docker.instance()
|
|
|
|
m.port_manager = port_manager
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2020-06-16 04:29:03 +00:00
|
|
|
async def vm(loop, compute_project, manager):
|
|
|
|
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest")
|
2015-10-14 16:10:05 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
def test_json(vm, compute_project):
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
assert vm.__json__() == {
|
|
|
|
'container_id': 'e90e34656842',
|
2016-05-19 11:09:07 +00:00
|
|
|
'image': 'ubuntu:latest',
|
2015-10-14 16:10:05 +00:00
|
|
|
'name': 'test',
|
2020-06-16 04:29:03 +00:00
|
|
|
'project_id': compute_project.id,
|
2016-05-11 17:35:36 +00:00
|
|
|
'node_id': vm.id,
|
2015-10-14 16:10:05 +00:00
|
|
|
'adapters': 1,
|
|
|
|
'console': vm.console,
|
2016-02-29 20:08:25 +00:00
|
|
|
'console_type': 'telnet',
|
2016-04-07 11:29:11 +00:00
|
|
|
'console_resolution': '1024x768',
|
2016-05-03 14:49:33 +00:00
|
|
|
'console_http_port': 80,
|
|
|
|
'console_http_path': '/',
|
2018-04-27 12:33:07 +00:00
|
|
|
'extra_hosts': None,
|
2019-04-22 08:53:38 +00:00
|
|
|
'extra_volumes': [],
|
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,
|
2016-05-17 17:51:06 +00:00
|
|
|
'node_directory': vm.working_dir,
|
2018-12-30 14:50:47 +00:00
|
|
|
'status': 'stopped',
|
|
|
|
'usage': ''
|
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
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create(compute_project, manager):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest")
|
|
|
|
await vm.create()
|
2015-10-14 16:10:05 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-24 16:08:16 +00:00
|
|
|
"Binds": [
|
2016-05-19 11:32:30 +00:00
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network"))
|
2016-03-24 16:08:16 +00:00
|
|
|
],
|
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-05-19 11:09:07 +00:00
|
|
|
"Image": "ubuntu:latest",
|
2016-05-06 17:07:18 +00:00
|
|
|
"Env": [
|
2016-06-15 08:12:32 +00:00
|
|
|
"container=docker",
|
2016-06-01 09:39:42 +00:00
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network"
|
2016-10-24 10:21:57 +00:00
|
|
|
],
|
2016-05-19 11:09:07 +00:00
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_with_tag(compute_project, manager):
|
2016-05-19 11:09:07 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2016-05-19 11:32:30 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:16.04")
|
|
|
|
await vm.create()
|
2015-10-14 16:10:05 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-24 16:08:16 +00:00
|
|
|
"Binds": [
|
2016-05-16 17:37:47 +00:00
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network"))
|
2016-03-24 16:08:16 +00:00
|
|
|
],
|
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-05-19 11:32:30 +00:00
|
|
|
"Image": "ubuntu:16.04",
|
2016-05-06 17:07:18 +00:00
|
|
|
"Env": [
|
2016-06-15 08:12:32 +00:00
|
|
|
"container=docker",
|
2016-06-01 09:39:42 +00:00
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network"
|
2016-10-24 10:21:57 +00:00
|
|
|
],
|
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"
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_vnc(compute_project, manager):
|
2016-02-29 20:08:25 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu", console_type="vnc", console=5900)
|
2016-02-29 20:08:25 +00:00
|
|
|
vm._start_vnc = MagicMock()
|
|
|
|
vm._display = 42
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
2016-02-29 20:08:25 +00:00
|
|
|
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-05-16 17:37:47 +00:00
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network")),
|
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",
|
2016-05-19 11:09:07 +00:00
|
|
|
"Image": "ubuntu:latest",
|
2016-05-06 17:07:18 +00:00
|
|
|
"Env": [
|
2016-06-15 08:12:32 +00:00
|
|
|
"container=docker",
|
2016-05-06 17:07:18 +00:00
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
2016-06-01 09:39:42 +00:00
|
|
|
"GNS3_VOLUMES=/etc/network",
|
2016-06-15 09:01:03 +00:00
|
|
|
"QT_GRAPHICSSYSTEM=native",
|
2016-05-06 17:07:18 +00:00
|
|
|
"DISPLAY=:42"
|
2016-10-24 10:21:57 +00:00
|
|
|
],
|
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"
|
2016-04-06 12:57:52 +00:00
|
|
|
assert vm._console_type == "vnc"
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_with_extra_hosts(compute_project, manager):
|
2018-04-25 14:03:01 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
extra_hosts = "test:199.199.199.1\ntest2:199.199.199.1"
|
2018-04-25 14:03:01 +00:00
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
|
|
|
|
2018-04-27 12:33:07 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2018-04-25 14:03:01 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu", extra_hosts=extra_hosts)
|
|
|
|
await vm.create()
|
2018-04-25 14:03:01 +00:00
|
|
|
called_kwargs = mock.call_args[1]
|
2018-04-27 12:33:07 +00:00
|
|
|
assert "GNS3_EXTRA_HOSTS=199.199.199.1\ttest\n199.199.199.1\ttest2" in called_kwargs["data"]["Env"]
|
2018-04-25 14:03:01 +00:00
|
|
|
assert vm._extra_hosts == extra_hosts
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_with_extra_hosts_wrong_format(compute_project, manager):
|
2018-04-27 12:33:07 +00:00
|
|
|
extra_hosts = "test"
|
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
|
|
|
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu", extra_hosts=extra_hosts)
|
2018-04-27 12:33:07 +00:00
|
|
|
with pytest.raises(DockerError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
2018-04-27 12:33:07 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_with_empty_extra_hosts(compute_project, manager):
|
2018-04-27 12:33:07 +00:00
|
|
|
extra_hosts = "test:\n"
|
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
|
|
|
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu", extra_hosts=extra_hosts)
|
|
|
|
await vm.create()
|
2018-04-27 12:33:07 +00:00
|
|
|
called_kwargs = mock.call_args[1]
|
|
|
|
assert len([ e for e in called_kwargs["data"]["Env"] if "GNS3_EXTRA_HOSTS" in e]) == 0
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_with_project_variables(compute_project, manager):
|
2018-05-04 12:34:44 +00:00
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
compute_project.variables = [
|
2018-05-04 12:34:44 +00:00
|
|
|
{"name": "VAR1"},
|
2018-05-09 09:25:55 +00:00
|
|
|
{"name": "VAR2", "value": "VAL1"},
|
|
|
|
{"name": "VAR3", "value": "2x${VAR2}"}
|
2018-05-04 12:34:44 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu")
|
|
|
|
await vm.create()
|
2018-05-04 12:34:44 +00:00
|
|
|
called_kwargs = mock.call_args[1]
|
|
|
|
assert "VAR1=" in called_kwargs["data"]["Env"]
|
|
|
|
assert "VAR2=VAL1" in called_kwargs["data"]["Env"]
|
2018-05-09 09:25:55 +00:00
|
|
|
assert "VAR3=2xVAL1" in called_kwargs["data"]["Env"]
|
2020-06-16 04:29:03 +00:00
|
|
|
compute_project.variables = None
|
2018-05-04 12:34:44 +00:00
|
|
|
|
2018-05-07 13:18:19 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_start_cmd(compute_project, manager):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest")
|
2015-10-14 16:10:05 +00:00
|
|
|
vm._start_command = "/bin/ls"
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
2015-10-14 16:10:05 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-24 16:08:16 +00:00
|
|
|
"Binds": [
|
2016-05-16 17:37:47 +00:00
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network"))
|
2016-03-24 16:08:16 +00:00
|
|
|
],
|
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-05-19 11:09:07 +00:00
|
|
|
"Image": "ubuntu:latest",
|
2016-05-06 17:07:18 +00:00
|
|
|
"Env": [
|
2016-06-15 08:12:32 +00:00
|
|
|
"container=docker",
|
2016-06-01 09:39:42 +00:00
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network"
|
2016-10-24 10:21:57 +00:00
|
|
|
]
|
2015-10-14 16:10:05 +00:00
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_environment(compute_project, manager):
|
2017-07-20 13:37:56 +00:00
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
Allow user to pass an environment. User can't override our
|
2017-07-20 13:37:56 +00:00
|
|
|
internal variables
|
|
|
|
"""
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
2017-07-20 13:37:56 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu")
|
2017-07-20 13:37:56 +00:00
|
|
|
vm.environment = "YES=1\nNO=0\nGNS3_MAX_ETHERNET=eth2"
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
2018-02-27 15:33:55 +00:00
|
|
|
assert mock.call_args[1]['data']['Env'] == [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network",
|
|
|
|
"YES=1",
|
|
|
|
"NO=0"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_environment_with_last_new_line_character(compute_project, manager):
|
2018-02-27 15:33:55 +00:00
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
Allow user to pass an environment. User can't override our
|
2018-02-27 15:33:55 +00:00
|
|
|
internal variables
|
|
|
|
"""
|
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu")
|
2018-02-27 15:33:55 +00:00
|
|
|
vm.environment = "YES=1\nNO=0\nGNS3_MAX_ETHERNET=eth2\n"
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
2018-02-27 15:33:55 +00:00
|
|
|
assert mock.call_args[1]['data']['Env'] == [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network",
|
|
|
|
"YES=1",
|
|
|
|
"NO=0"
|
|
|
|
]
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_image_not_available(compute_project, manager):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-02-24 16:08:28 +00:00
|
|
|
call = 0
|
2018-10-15 10:05:49 +00:00
|
|
|
async def information():
|
2016-02-24 16:08:28 +00:00
|
|
|
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
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu")
|
2016-05-14 01:28:53 +00:00
|
|
|
vm._get_image_information = MagicMock()
|
|
|
|
vm._get_image_information.side_effect = information
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM.pull_image", return_value=True) as mock_pull:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
2016-02-24 16:08:28 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-24 16:08:16 +00:00
|
|
|
"Binds": [
|
2016-05-16 17:37:47 +00:00
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network"))
|
2016-03-24 16:08:16 +00:00
|
|
|
],
|
2016-02-24 16:08:28 +00:00
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
2016-05-19 11:09:07 +00:00
|
|
|
"Image": "ubuntu:latest",
|
2016-05-06 17:07:18 +00:00
|
|
|
"Env": [
|
2016-06-15 08:12:32 +00:00
|
|
|
"container=docker",
|
2016-06-01 09:39:42 +00:00
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network"
|
2016-10-24 10:21:57 +00:00
|
|
|
],
|
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"
|
2016-05-19 11:09:07 +00:00
|
|
|
mock_pull.assert_called_with("ubuntu:latest")
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_user(compute_project, manager):
|
2019-04-05 10:06:35 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": [],
|
|
|
|
"Config" : {
|
|
|
|
"User" : "test",
|
|
|
|
},
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-04-05 10:06:35 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest")
|
|
|
|
await vm.create()
|
2019-04-05 10:06:35 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"User": "root",
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
|
|
|
"Binds": [
|
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network"))
|
2019-04-05 10:06:35 +00:00
|
|
|
],
|
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
|
|
|
"Image": "ubuntu:latest",
|
|
|
|
"Env": [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network",
|
|
|
|
"GNS3_USER=test"
|
|
|
|
],
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_with_extra_volumes_invalid_format_1(compute_project, manager):
|
2019-04-22 08:53:38 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["vol1"])
|
2019-04-22 08:53:38 +00:00
|
|
|
with pytest.raises(DockerError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
2019-04-22 08:53:38 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_extra_volumes_invalid_format_2(compute_project, manager):
|
2019-04-22 08:53:38 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-04-22 08:53:38 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/vol1", ""])
|
2019-04-22 08:53:38 +00:00
|
|
|
with pytest.raises(DockerError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
2019-04-22 08:53:38 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_extra_volumes_invalid_format_3(compute_project, manager):
|
2019-04-22 11:46:28 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/vol1/.."])
|
2019-04-22 11:46:28 +00:00
|
|
|
with pytest.raises(DockerError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.create()
|
|
|
|
|
2019-04-22 11:46:28 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_with_extra_volumes_duplicate_1_image(compute_project, manager):
|
2019-04-22 08:53:38 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": [],
|
|
|
|
"Config" : {
|
|
|
|
"Volumes" : {
|
|
|
|
"/vol/1": None
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-04-22 08:53:38 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/vol/1"])
|
|
|
|
await vm.create()
|
2019-06-05 08:39:44 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
|
|
|
"Binds": [
|
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network")),
|
|
|
|
"{}:/gns3volumes/vol/1".format(os.path.join(vm.working_dir, "vol", "1")),
|
|
|
|
],
|
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
|
|
|
"Image": "ubuntu:latest",
|
|
|
|
"Env": [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network:/vol/1"
|
|
|
|
],
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
2019-04-22 08:53:38 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_extra_volumes_duplicate_2_user(compute_project, manager):
|
2019-04-22 08:53:38 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": [],
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-04-22 08:53:38 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/vol/1", "/vol/1"])
|
|
|
|
await vm.create()
|
2019-06-05 08:39:44 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
|
|
|
"Binds": [
|
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network")),
|
|
|
|
"{}:/gns3volumes/vol/1".format(os.path.join(vm.working_dir, "vol", "1")),
|
|
|
|
],
|
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
|
|
|
"Image": "ubuntu:latest",
|
|
|
|
"Env": [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network:/vol/1"
|
|
|
|
],
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
2019-04-22 08:53:38 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_extra_volumes_duplicate_3_subdir(compute_project, manager):
|
2019-04-22 11:46:28 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": [],
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-04-22 11:46:28 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/vol/1/", "/vol"])
|
|
|
|
await vm.create()
|
2019-06-05 08:39:44 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
|
|
|
"Binds": [
|
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network")),
|
|
|
|
"{}:/gns3volumes/vol".format(os.path.join(vm.working_dir, "vol")),
|
|
|
|
],
|
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
|
|
|
"Image": "ubuntu:latest",
|
|
|
|
"Env": [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network:/vol"
|
|
|
|
],
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
2019-04-22 11:46:28 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_extra_volumes_duplicate_4_backslash(compute_project, manager):
|
2019-04-22 11:46:28 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": [],
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-04-22 11:46:28 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/vol//", "/vol"])
|
|
|
|
await vm.create()
|
2019-06-05 08:39:44 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
|
|
|
"Binds": [
|
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network")),
|
|
|
|
"{}:/gns3volumes/vol".format(os.path.join(vm.working_dir, "vol")),
|
|
|
|
],
|
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
|
|
|
"Image": "ubuntu:latest",
|
|
|
|
"Env": [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network:/vol"
|
|
|
|
],
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_extra_volumes_duplicate_5_subdir_issue_1595(compute_project, manager):
|
2019-06-05 08:39:44 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": [],
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-06-05 08:39:44 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/etc"])
|
|
|
|
await vm.create()
|
2019-06-05 08:39:44 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
|
|
|
"Binds": [
|
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
|
|
|
"{}:/gns3volumes/etc".format(os.path.join(vm.working_dir, "etc")),
|
|
|
|
],
|
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
|
|
|
"Image": "ubuntu:latest",
|
|
|
|
"Env": [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc"
|
|
|
|
],
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_extra_volumes_duplicate_6_subdir_issue_1595(compute_project, manager):
|
2019-06-05 08:39:44 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": [],
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-06-05 08:39:44 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/etc/test", "/etc"])
|
|
|
|
await vm.create()
|
2019-06-05 08:39:44 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
|
|
|
"Binds": [
|
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
|
|
|
"{}:/gns3volumes/etc".format(os.path.join(vm.working_dir, "etc")),
|
|
|
|
],
|
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
|
|
|
"Image": "ubuntu:latest",
|
|
|
|
"Env": [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc"
|
|
|
|
],
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
2019-04-22 11:46:28 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_create_with_extra_volumes(compute_project, manager):
|
2019-04-22 08:53:38 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": [],
|
|
|
|
"Config" : {
|
|
|
|
"Volumes" : {
|
|
|
|
"/vol/1": None
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2019-04-22 08:53:38 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu:latest", extra_volumes=["/vol/2"])
|
|
|
|
await vm.create()
|
2019-04-22 08:53:38 +00:00
|
|
|
mock.assert_called_with("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
|
|
|
"Binds": [
|
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network")),
|
2019-04-22 08:53:38 +00:00
|
|
|
"{}:/gns3volumes/vol/1".format(os.path.join(vm.working_dir, "vol", "1")),
|
|
|
|
"{}:/gns3volumes/vol/2".format(os.path.join(vm.working_dir, "vol", "2")),
|
|
|
|
],
|
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
|
|
|
"Image": "ubuntu:latest",
|
|
|
|
"Env": [
|
|
|
|
"container=docker",
|
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network:/vol/1:/vol/2"
|
|
|
|
],
|
|
|
|
"Entrypoint": ["/gns3/init.sh"],
|
|
|
|
"Cmd": ["/bin/sh"]
|
|
|
|
})
|
|
|
|
assert vm._cid == "e90e34656806"
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
async def test_get_container_state(vm):
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
|
|
|
assert await vm._get_container_state() == "running"
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
response["State"]["Running"] = False
|
|
|
|
response["State"]["Paused"] = True
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
|
|
|
assert await vm._get_container_state() == "paused"
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
response["State"]["Running"] = False
|
|
|
|
response["State"]["Paused"] = False
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
|
|
|
assert await vm._get_container_state() == "exited"
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_is_running(vm):
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
response = {
|
|
|
|
"State": {
|
|
|
|
"Running": False,
|
|
|
|
"Paused": False
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
|
|
|
assert await vm.is_running() is False
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
response["State"]["Running"] = True
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
|
|
|
assert await vm.is_running() is True
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_pause(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.pause()
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
mock.assert_called_with("POST", "containers/e90e34656842/pause")
|
2016-05-16 15:39:32 +00:00
|
|
|
assert vm.status == "suspended"
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_unpause(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.unpause()
|
2015-10-14 16:10:05 +00:00
|
|
|
mock.assert_called_with("POST", "containers/e90e34656842/unpause")
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_start(vm, manager, free_console_port):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.start()
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
|
2016-12-14 15:53:20 +00:00
|
|
|
vm._add_ubridge_connection.assert_called_once_with(nio, 0)
|
2016-03-01 13:53:43 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_start_namespace_failed(vm, manager, free_console_port):
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
assert vm.status != "started"
|
|
|
|
vm.adapters = 1
|
|
|
|
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
2016-02-11 14:49:28 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection", side_effect=UbridgeNamespaceError()) as mock_add_ubridge_connection:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_log", return_value='Hello not available') as mock_log:
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
with pytest.raises(DockerError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.start()
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
mock_query.assert_any_call("POST", "containers/e90e34656842/start")
|
2016-12-14 15:53:20 +00:00
|
|
|
mock_add_ubridge_connection.assert_called_once_with(nio, 0)
|
2016-02-11 14:49:28 +00:00
|
|
|
assert mock_start_ubridge.called
|
|
|
|
assert vm.status == "stopped"
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_start_without_nio(vm):
|
2016-02-09 13:22:37 +00:00
|
|
|
"""
|
|
|
|
If no nio exists we will create one.
|
|
|
|
"""
|
|
|
|
|
|
|
|
assert vm.status != "started"
|
|
|
|
vm.adapters = 1
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42):
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection") as mock_add_ubridge_connection:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._start_console") as mock_start_console:
|
2020-06-16 04:29:03 +00:00
|
|
|
await 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"
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_start_unpause(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="paused"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause", return_value="paused") as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.start()
|
2015-10-14 16:10:05 +00:00
|
|
|
assert mock.called
|
|
|
|
assert vm.status == "started"
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_restart(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.restart()
|
2015-10-14 16:10:05 +00:00
|
|
|
mock.assert_called_with("POST", "containers/e90e34656842/restart")
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_stop(vm):
|
|
|
|
|
2016-12-14 11:01:34 +00:00
|
|
|
mock = MagicMock()
|
|
|
|
vm._ubridge_hypervisor = mock
|
2015-10-14 16:10:05 +00:00
|
|
|
vm._ubridge_hypervisor.is_running.return_value = True
|
2016-05-31 19:08:41 +00:00
|
|
|
vm._fix_permissions = MagicMock()
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="running"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
2021-12-15 03:00:38 +00:00
|
|
|
vm._permissions_fixed = False
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.stop()
|
2015-10-14 16:10:05 +00:00
|
|
|
mock_query.assert_called_with("POST", "containers/e90e34656842/stop", params={"t": 5})
|
2016-12-14 11:01:34 +00:00
|
|
|
assert mock.stop.called
|
|
|
|
assert vm._ubridge_hypervisor is None
|
2016-05-31 19:08:41 +00:00
|
|
|
assert vm._fix_permissions.called
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_stop_paused_container(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="paused"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause") as mock_unpause:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.stop()
|
2015-10-14 16:10:05 +00:00
|
|
|
mock_query.assert_called_with("POST", "containers/e90e34656842/stop", params={"t": 5})
|
|
|
|
assert mock_unpause.called
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_update(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
2016-02-12 15:38:16 +00:00
|
|
|
|
|
|
|
original_console = vm.console
|
2016-04-05 14:33:40 +00:00
|
|
|
original_aux = vm.aux
|
2016-02-12 15:38:16 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock_query:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.update()
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-06-13 15:39:04 +00:00
|
|
|
mock_query.assert_any_call("DELETE", "containers/e90e34656842", params={"force": 1, "v": 1})
|
2015-10-14 16:10:05 +00:00
|
|
|
mock_query.assert_any_call("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-24 16:08:16 +00:00
|
|
|
"Binds": [
|
2016-05-16 17:37:47 +00:00
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network"))
|
2016-03-24 16:08:16 +00:00
|
|
|
],
|
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-05-19 11:09:07 +00:00
|
|
|
"Image": "ubuntu:latest",
|
2016-05-06 17:07:18 +00:00
|
|
|
"Env": [
|
2016-06-15 08:12:32 +00:00
|
|
|
"container=docker",
|
2016-06-01 09:39:42 +00:00
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network"
|
2016-05-06 17:07:18 +00:00
|
|
|
],
|
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
|
2016-04-05 14:33:40 +00:00
|
|
|
assert vm.aux == original_aux
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_update_vnc(vm):
|
2016-04-06 12:57:52 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
|
|
|
|
2016-10-24 10:21:57 +00:00
|
|
|
vm._console_type = "vnc"
|
|
|
|
vm._console = 5900
|
2016-04-06 12:57:52 +00:00
|
|
|
vm._display = "display"
|
|
|
|
original_console = vm.console
|
|
|
|
original_aux = vm.aux
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._start_vnc"):
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]):
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
2020-06-16 04:29:03 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response):
|
|
|
|
await vm.update()
|
2016-04-06 12:57:52 +00:00
|
|
|
|
|
|
|
assert vm.console == original_console
|
|
|
|
assert vm.aux == original_aux
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_update_running(vm):
|
2016-02-24 14:47:53 +00:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"Id": "e90e34656806",
|
|
|
|
"Warnings": []
|
|
|
|
}
|
|
|
|
|
|
|
|
original_console = vm.console
|
|
|
|
vm.start = MagicMock()
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="running"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock_query:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.update()
|
2016-02-24 14:47:53 +00:00
|
|
|
|
2016-06-13 15:39:04 +00:00
|
|
|
mock_query.assert_any_call("DELETE", "containers/e90e34656842", params={"force": 1, "v": 1})
|
2016-02-24 14:47:53 +00:00
|
|
|
mock_query.assert_any_call("POST", "containers/create", data={
|
|
|
|
"Tty": True,
|
|
|
|
"OpenStdin": True,
|
|
|
|
"StdinOnce": False,
|
|
|
|
"HostConfig":
|
|
|
|
{
|
|
|
|
"CapAdd": ["ALL"],
|
2016-03-24 16:08:16 +00:00
|
|
|
"Binds": [
|
2016-05-16 17:37:47 +00:00
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network"))
|
2016-03-24 16:08:16 +00:00
|
|
|
],
|
2016-02-24 14:47:53 +00:00
|
|
|
"Privileged": True
|
|
|
|
},
|
|
|
|
"Volumes": {},
|
|
|
|
"NetworkDisabled": True,
|
|
|
|
"Name": "test",
|
|
|
|
"Hostname": "test",
|
2016-05-19 11:09:07 +00:00
|
|
|
"Image": "ubuntu:latest",
|
2016-05-06 17:07:18 +00:00
|
|
|
"Env": [
|
2016-06-15 08:12:32 +00:00
|
|
|
"container=docker",
|
2016-06-01 09:39:42 +00:00
|
|
|
"GNS3_MAX_ETHERNET=eth0",
|
|
|
|
"GNS3_VOLUMES=/etc/network"
|
2016-05-06 17:07:18 +00:00
|
|
|
],
|
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
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_delete(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.delete()
|
2016-06-13 15:39:04 +00:00
|
|
|
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1, "v": 1})
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_close(vm, port_manager):
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
"lport": 4242,
|
|
|
|
"rport": 4343,
|
|
|
|
"rhost": "127.0.0.1"}
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = vm.manager.create_nio(nio)
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.close()
|
2016-06-13 15:39:04 +00:00
|
|
|
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1, "v": 1})
|
2016-02-29 20:08:25 +00:00
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
assert vm._closed is True
|
|
|
|
assert "4242" not in port_manager.udp_ports
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_close_vnc(vm):
|
2016-02-29 20:08:25 +00:00
|
|
|
|
|
|
|
vm._console_type = "vnc"
|
|
|
|
vm._x11vnc_process = MagicMock()
|
|
|
|
vm._xvfb_process = MagicMock()
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.close()
|
2016-06-13 15:39:04 +00:00
|
|
|
mock_query.assert_called_with("DELETE", "containers/e90e34656842", params={"force": 1, "v": 1})
|
2016-02-29 20:08:25 +00:00
|
|
|
|
|
|
|
assert vm._closed is True
|
|
|
|
assert vm._xvfb_process.terminate.called
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_get_namespace(vm):
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
response = {
|
|
|
|
"State": {
|
|
|
|
"Pid": 42
|
|
|
|
}
|
|
|
|
}
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock_query:
|
2020-06-16 04:29:03 +00:00
|
|
|
assert await vm._get_namespace() == 42
|
2015-10-14 16:10:05 +00:00
|
|
|
mock_query.assert_called_with("GET", "containers/e90e34656842/json")
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_add_ubridge_connection(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
"lport": 4242,
|
|
|
|
"rport": 4343,
|
|
|
|
"rhost": "127.0.0.1"}
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = vm.manager.create_nio(nio)
|
2019-04-01 13:58:18 +00:00
|
|
|
nio.start_packet_capture("/tmp/capture.pcap")
|
2015-10-14 16:10:05 +00:00
|
|
|
vm._ubridge_hypervisor = MagicMock()
|
2016-12-14 15:53:20 +00:00
|
|
|
vm._namespace = 42
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._add_ubridge_connection(nio, 0)
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
calls = [
|
|
|
|
call.send('bridge create bridge0'),
|
2016-10-24 10:35:50 +00:00
|
|
|
call.send("bridge add_nio_tap bridge0 tap-gns3-e0"),
|
|
|
|
call.send('docker move_to_ns tap-gns3-e0 42 eth0'),
|
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')
|
|
|
|
]
|
2017-07-12 15:39:19 +00:00
|
|
|
assert 'bridge0' in vm._bridges
|
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)
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_add_ubridge_connection_none_nio(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-12-14 15:53:20 +00:00
|
|
|
vm._namespace = 42
|
2016-02-11 14:49:28 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._add_ubridge_connection(nio, 0)
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
calls = [
|
2016-10-24 10:35:50 +00:00
|
|
|
call.send('bridge create bridge0'),
|
|
|
|
call.send("bridge add_nio_tap bridge0 tap-gns3-e0"),
|
|
|
|
call.send('docker move_to_ns tap-gns3-e0 42 eth0'),
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
]
|
2017-07-12 15:39:19 +00:00
|
|
|
assert 'bridge0' in vm._bridges
|
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)
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_add_ubridge_connection_invalid_adapter_number(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
"lport": 4242,
|
|
|
|
"rport": 4343,
|
|
|
|
"rhost": "127.0.0.1"}
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = vm.manager.create_nio(nio)
|
2015-10-14 16:10:05 +00:00
|
|
|
with pytest.raises(DockerError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._add_ubridge_connection(nio, 12)
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_add_ubridge_connection_no_free_interface(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
"lport": 4242,
|
|
|
|
"rport": 4343,
|
|
|
|
"rhost": "127.0.0.1"}
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = vm.manager.create_nio(nio)
|
2015-10-14 16:10:05 +00:00
|
|
|
with pytest.raises(DockerError):
|
|
|
|
|
2016-05-18 09:23:45 +00:00
|
|
|
# We create fake ethernet interfaces for docker
|
2016-12-13 15:05:38 +00:00
|
|
|
interfaces = ["tap-gns3-e{}".format(index) for index in range(4096)]
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
with patch("psutil.net_if_addrs", return_value=interfaces):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._add_ubridge_connection(nio, 0)
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_adapter_add_nio_binding_1(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2017-07-12 15:39:19 +00:00
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
"lport": 4242,
|
|
|
|
"rport": 4343,
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
nio = vm.manager.create_nio(nio)
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
|
|
|
assert vm._ethernet_adapters[0].get_nio(0) == nio
|
2017-07-12 15:39:19 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_adapter_udpate_nio_binding_bridge_not_started(vm):
|
2017-07-12 15:39:19 +00:00
|
|
|
|
|
|
|
vm._ubridge_apply_filters = AsyncioMagicMock()
|
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
"lport": 4242,
|
|
|
|
"rport": 4343,
|
|
|
|
"rhost": "127.0.0.1"}
|
|
|
|
nio = vm.manager.create_nio(nio)
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="running"):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
|
|
|
await vm.adapter_update_nio_binding(0, nio)
|
2017-07-12 15:39:19 +00:00
|
|
|
assert vm._ubridge_apply_filters.called is False
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_adapter_add_nio_binding_invalid_adapter(vm):
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
"lport": 4242,
|
|
|
|
"rport": 4343,
|
|
|
|
"rhost": "127.0.0.1"}
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = vm.manager.create_nio(nio)
|
2015-10-14 16:10:05 +00:00
|
|
|
with pytest.raises(DockerError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(12, nio)
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_adapter_remove_nio_binding(vm):
|
2015-10-14 16:10:05 +00:00
|
|
|
|
2016-12-14 15:53:20 +00:00
|
|
|
vm.ubridge = MagicMock()
|
|
|
|
vm.ubridge.is_running.return_value = True
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
nio = {"type": "nio_udp",
|
|
|
|
"lport": 4242,
|
|
|
|
"rport": 4343,
|
|
|
|
"rhost": "127.0.0.1"}
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = vm.manager.create_nio(nio)
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
2016-12-14 15:53:20 +00:00
|
|
|
|
|
|
|
with asyncio_patch("gns3server.compute.docker.DockerVM._ubridge_send") as delete_ubridge_mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_remove_nio_binding(0)
|
2015-10-14 16:10:05 +00:00
|
|
|
assert vm._ethernet_adapters[0].get_nio(0) is None
|
2016-12-14 15:53:20 +00:00
|
|
|
delete_ubridge_mock.assert_any_call('bridge stop bridge0')
|
|
|
|
delete_ubridge_mock.assert_any_call('bridge remove_nio_udp bridge0 4242 127.0.0.1 4343')
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_adapter_remove_nio_binding_invalid_adapter(vm):
|
|
|
|
|
2015-10-14 16:10:05 +00:00
|
|
|
with pytest.raises(DockerError):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_remove_nio_binding(12)
|
2015-10-14 16:10:05 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_start_capture(vm, tmpdir, manager, free_console_port):
|
2016-02-09 15:07:33 +00:00
|
|
|
|
|
|
|
output_file = str(tmpdir / "test.pcap")
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
|
|
|
await vm.start_capture(0, output_file)
|
2016-02-09 15:07:33 +00:00
|
|
|
assert vm._ethernet_adapters[0].get_nio(0).capturing
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_stop_capture(vm, tmpdir, manager, free_console_port):
|
2016-02-09 15:07:33 +00:00
|
|
|
|
|
|
|
output_file = str(tmpdir / "test.pcap")
|
2016-06-25 00:35:39 +00:00
|
|
|
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.adapter_add_nio_binding(0, nio)
|
|
|
|
await vm.start_capture(0, output_file)
|
2016-02-09 15:07:33 +00:00
|
|
|
assert vm._ethernet_adapters[0].get_nio(0).capturing
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm.stop_capture(0)
|
2016-02-09 15:07:33 +00:00
|
|
|
assert vm._ethernet_adapters[0].get_nio(0).capturing is False
|
2016-02-11 14:49:28 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_get_log(vm):
|
2018-10-15 10:05:49 +00:00
|
|
|
|
|
|
|
async def read():
|
2016-02-11 14:49:28 +00:00
|
|
|
return b'Hello\nWorld'
|
|
|
|
|
|
|
|
mock_query = MagicMock()
|
|
|
|
mock_query.read = read
|
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.http_query", return_value=mock_query) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._get_log()
|
2016-02-11 14:49:28 +00:00
|
|
|
mock.assert_called_with("GET", "containers/e90e34656842/logs", params={"stderr": 1, "stdout": 1}, data={})
|
2016-02-12 10:57:56 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_get_image_information(compute_project, manager):
|
|
|
|
|
|
|
|
response = {}
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
|
2020-06-16 04:29:03 +00:00
|
|
|
vm = DockerVM("test", str(uuid.uuid4()), compute_project, manager, "ubuntu")
|
|
|
|
await vm._get_image_information()
|
2016-05-19 11:09:07 +00:00
|
|
|
mock.assert_called_with("GET", "images/ubuntu:latest/json")
|
2016-02-12 10:57:56 +00:00
|
|
|
|
2016-10-24 10:21:57 +00:00
|
|
|
|
2016-09-27 16:01:50 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_mount_binds(vm):
|
|
|
|
|
2016-02-12 10:57:56 +00:00
|
|
|
image_infos = {
|
2017-05-23 08:18:30 +00:00
|
|
|
"Config": {
|
2016-02-12 10:57:56 +00:00
|
|
|
"Volumes": {
|
|
|
|
"/test/experimental": {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dst = os.path.join(vm.working_dir, "test/experimental")
|
|
|
|
assert vm._mount_binds(image_infos) == [
|
2016-05-16 17:37:47 +00:00
|
|
|
"{}:/gns3:ro".format(get_resource("compute/docker/resources")),
|
2019-06-05 08:39:44 +00:00
|
|
|
"{}:/gns3volumes/etc/network".format(os.path.join(vm.working_dir, "etc", "network")),
|
2016-06-06 10:39:29 +00:00
|
|
|
"{}:/gns3volumes{}".format(dst, "/test/experimental")
|
2016-02-12 10:57:56 +00:00
|
|
|
]
|
|
|
|
|
2016-05-31 19:08:41 +00:00
|
|
|
assert vm._volumes == ["/etc/network", "/test/experimental"]
|
2016-02-12 10:57:56 +00:00
|
|
|
assert os.path.exists(dst)
|
2016-02-29 20:08:25 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_start_vnc(vm):
|
|
|
|
|
2016-04-07 11:29:11 +00:00
|
|
|
vm.console_resolution = "1280x1024"
|
2018-11-11 15:25:23 +00:00
|
|
|
with patch("shutil.which", return_value="/bin/Xtigervnc"):
|
2016-04-15 15:57:06 +00:00
|
|
|
with asyncio_patch("gns3server.compute.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:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._start_vnc()
|
2016-02-29 20:08:25 +00:00
|
|
|
assert vm._display is not None
|
2022-06-07 16:05:24 +00:00
|
|
|
assert mock_exec.call_args[0] == ("/bin/Xtigervnc", "-extension", "MIT-SHM", "-geometry", vm.console_resolution, "-depth", "16", "-interface", "127.0.0.1", "-rfbport", str(vm.console), "-AlwaysShared", "-SecurityTypes", "None", ":{}".format(vm._display))
|
2016-02-29 20:08:25 +00:00
|
|
|
mock_wait.assert_called_with("/tmp/.X11-unix/X{}".format(vm._display))
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_start_vnc_missing(vm):
|
|
|
|
|
|
|
|
with patch("shutil.which", return_value=None):
|
|
|
|
with pytest.raises(DockerError):
|
|
|
|
await vm._start_vnc()
|
2016-03-01 13:53:43 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_start_aux(vm):
|
2016-03-01 13:53:43 +00:00
|
|
|
|
|
|
|
with asyncio_patch("asyncio.subprocess.create_subprocess_exec", return_value=MagicMock()) as mock_exec:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._start_aux()
|
2017-05-03 08:30:43 +00:00
|
|
|
mock_exec.assert_called_with('docker', 'exec', '-i', 'e90e34656842', '/gns3/bin/busybox', 'script', '-qfc', 'while true; do TERM=vt100 /gns3/bin/busybox sh; done', '/dev/null', stderr=asyncio.subprocess.STDOUT, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE)
|
2016-03-24 16:08:16 +00:00
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_create_network_interfaces(vm):
|
2016-03-24 16:08:16 +00:00
|
|
|
|
|
|
|
vm.adapters = 5
|
|
|
|
network_config = vm._create_network_config()
|
|
|
|
assert os.path.exists(os.path.join(network_config, "interfaces"))
|
|
|
|
assert os.path.exists(os.path.join(network_config, "if-up.d"))
|
|
|
|
|
|
|
|
with open(os.path.join(network_config, "interfaces")) as f:
|
|
|
|
content = f.read()
|
|
|
|
assert "eth0" in content
|
|
|
|
assert "eth4" in content
|
|
|
|
assert "eth5" not in content
|
2016-05-31 19:08:41 +00:00
|
|
|
|
2016-10-24 10:21:57 +00:00
|
|
|
|
2016-09-27 16:01:50 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_fix_permission(vm):
|
|
|
|
|
2016-05-31 19:08:41 +00:00
|
|
|
vm._volumes = ["/etc"]
|
2017-07-26 10:41:06 +00:00
|
|
|
vm._get_container_state = AsyncioMagicMock(return_value="running")
|
2016-05-31 19:08:41 +00:00
|
|
|
process = MagicMock()
|
|
|
|
with asyncio_patch("asyncio.subprocess.create_subprocess_exec", return_value=process) as mock_exec:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._fix_permissions()
|
2016-06-01 18:09:15 +00:00
|
|
|
mock_exec.assert_called_with('docker', 'exec', 'e90e34656842', '/gns3/bin/busybox', 'sh', '-c', '(/gns3/bin/busybox find "/etc" -depth -print0 | /gns3/bin/busybox xargs -0 /gns3/bin/busybox stat -c \'%a:%u:%g:%n\' > "/etc/.gns3_perms") && /gns3/bin/busybox chmod -R u+rX "/etc" && /gns3/bin/busybox chown {}:{} -R "/etc"'.format(os.getuid(), os.getgid()))
|
2016-05-31 19:08:41 +00:00
|
|
|
assert process.wait.called
|
2017-07-06 08:13:00 +00:00
|
|
|
|
|
|
|
|
2017-07-26 10:41:06 +00:00
|
|
|
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_fix_permission_not_running(vm):
|
|
|
|
|
2017-07-26 10:41:06 +00:00
|
|
|
vm._volumes = ["/etc"]
|
|
|
|
vm._get_container_state = AsyncioMagicMock(return_value="stopped")
|
|
|
|
process = MagicMock()
|
|
|
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_start:
|
|
|
|
with asyncio_patch("asyncio.subprocess.create_subprocess_exec", return_value=process) as mock_exec:
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._fix_permissions()
|
2017-07-26 10:41:06 +00:00
|
|
|
mock_exec.assert_called_with('docker', 'exec', 'e90e34656842', '/gns3/bin/busybox', 'sh', '-c', '(/gns3/bin/busybox find "/etc" -depth -print0 | /gns3/bin/busybox xargs -0 /gns3/bin/busybox stat -c \'%a:%u:%g:%n\' > "/etc/.gns3_perms") && /gns3/bin/busybox chmod -R u+rX "/etc" && /gns3/bin/busybox chown {}:{} -R "/etc"'.format(os.getuid(), os.getgid()))
|
|
|
|
assert mock_start.called
|
|
|
|
assert process.wait.called
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
async def test_read_console_output_with_binary_mode(vm):
|
|
|
|
|
2017-07-06 08:13:00 +00:00
|
|
|
class InputStreamMock(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.sent = False
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def receive(self):
|
2017-07-06 08:13:00 +00:00
|
|
|
if not self.sent:
|
|
|
|
self.sent = True
|
2018-11-30 10:59:02 +00:00
|
|
|
return MagicMock(type=aiohttp.WSMsgType.BINARY, data=b"test")
|
2017-07-06 08:13:00 +00:00
|
|
|
else:
|
2018-11-30 10:59:02 +00:00
|
|
|
return MagicMock(type=aiohttp.WSMsgType.CLOSE)
|
2017-07-06 08:13:00 +00:00
|
|
|
|
2018-11-30 10:59:02 +00:00
|
|
|
async def close(self):
|
2017-07-06 08:13:00 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
input_stream = InputStreamMock()
|
|
|
|
output_stream = MagicMock()
|
|
|
|
|
|
|
|
with asyncio_patch('gns3server.compute.docker.docker_vm.DockerVM.stop'):
|
2020-06-16 04:29:03 +00:00
|
|
|
await vm._read_console_output(input_stream, output_stream)
|
2017-07-06 08:13:00 +00:00
|
|
|
output_stream.feed_data.assert_called_once_with(b"test")
|