diff --git a/gns3server/compute/docker/docker_vm.py b/gns3server/compute/docker/docker_vm.py index db1404c10..fc79be0be 100644 --- a/gns3server/compute/docker/docker_vm.py +++ b/gns3server/compute/docker/docker_vm.py @@ -68,6 +68,9 @@ class DockerVM(BaseNode): console_resolution="1024x768", console_http_port=80, console_http_path="/"): super().__init__(name, node_id, project, manager, console=console, aux=aux, allocate_aux=True, console_type=console_type) + # If no version is specified force latest + if ":" not in image: + image = "{}:latest".format(image) self._image = image self._start_command = start_command self._environment = environment @@ -541,11 +544,16 @@ class DockerVM(BaseNode): try: if self.console_type == "vnc": if self._x11vnc_process: - self._x11vnc_process.terminate() - self._xvfb_process.terminate() - yield from self._x11vnc_process.wait() - yield from self._xvfb_process.wait() - + try: + self._x11vnc_process.terminate() + yield from self._x11vnc_process.wait() + except ProcessLookupError: + pass + try: + self._xvfb_process.terminate() + yield from self._xvfb_process.wait() + except ProcessLookupError: + pass state = yield from self._get_container_state() if state == "paused" or state == "running": yield from self.stop() diff --git a/tests/compute/docker/test_docker_vm.py b/tests/compute/docker/test_docker_vm.py index 1be32d634..d242ee59b 100644 --- a/tests/compute/docker/test_docker_vm.py +++ b/tests/compute/docker/test_docker_vm.py @@ -41,7 +41,7 @@ def manager(port_manager): @pytest.fixture(scope="function") def vm(project, manager): - vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu") + vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") vm._cid = "e90e34656842" vm.allocate_aux = False return vm @@ -50,7 +50,7 @@ def vm(project, manager): def test_json(vm, project): assert vm.__json__() == { 'container_id': 'e90e34656842', - 'image': 'ubuntu', + 'image': 'ubuntu:latest', 'name': 'test', 'project_id': project.id, 'node_id': vm.id, @@ -84,7 +84,7 @@ def test_create(loop, project, manager): } with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock: - vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu") + vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") loop.run_until_complete(asyncio.async(vm.create())) mock.assert_called_with("POST", "containers/create", data={ "Tty": True, @@ -103,10 +103,47 @@ def test_create(loop, project, manager): "NetworkDisabled": True, "Name": "test", "Hostname": "test", - "Image": "ubuntu", + "Image": "ubuntu:latest", "Env": [ "GNS3_MAX_ETHERNET=eth0" - ], + ], + "Entrypoint": ["/gns3/init.sh"], + "Cmd": ["/bin/sh"] + }) + assert vm._cid == "e90e34656806" + + +def test_create_with_tag(loop, project, manager): + + response = { + "Id": "e90e34656806", + "Warnings": [] + } + with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: + with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock: + vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:16.04") + 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"], + "Binds": [ + "{}:/gns3:ro".format(get_resource("compute/docker/resources")), + "{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network")) + ], + "Privileged": True + }, + "Volumes": {}, + "NetworkDisabled": True, + "Name": "test", + "Hostname": "test", + "Image": "ubuntu:16.04", + "Env": [ + "GNS3_MAX_ETHERNET=eth0" + ], "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) @@ -144,11 +181,11 @@ def test_create_vnc(loop, project, manager): "NetworkDisabled": True, "Name": "test", "Hostname": "test", - "Image": "ubuntu", + "Image": "ubuntu:latest", "Env": [ "GNS3_MAX_ETHERNET=eth0", "DISPLAY=:42" - ], + ], "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) @@ -165,7 +202,7 @@ def test_create_start_cmd(loop, project, manager): } with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock: - vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu") + vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest") vm._start_command = "/bin/ls" loop.run_until_complete(asyncio.async(vm.create())) mock.assert_called_with("POST", "containers/create", data={ @@ -187,10 +224,10 @@ def test_create_start_cmd(loop, project, manager): "NetworkDisabled": True, "Name": "test", "Hostname": "test", - "Image": "ubuntu", + "Image": "ubuntu:latest", "Env": [ "GNS3_MAX_ETHERNET=eth0" - ] + ] }) assert vm._cid == "e90e34656806" @@ -223,12 +260,12 @@ def test_create_environment(loop, project, manager): "GNS3_MAX_ETHERNET=eth0", "YES=1", "NO=0" - ], + ], "Volumes": {}, "NetworkDisabled": True, "Name": "test", "Hostname": "test", - "Image": "ubuntu", + "Image": "ubuntu:latest", "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) @@ -256,7 +293,6 @@ def test_create_image_not_available(loop, project, manager): vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu") vm._get_image_information = MagicMock() vm._get_image_information.side_effect = information - 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: loop.run_until_complete(asyncio.async(vm.create())) @@ -277,15 +313,15 @@ def test_create_image_not_available(loop, project, manager): "NetworkDisabled": True, "Name": "test", "Hostname": "test", - "Image": "ubuntu", + "Image": "ubuntu:latest", "Env": [ "GNS3_MAX_ETHERNET=eth0" - ], + ], "Entrypoint": ["/gns3/init.sh"], "Cmd": ["/bin/sh"] }) assert vm._cid == "e90e34656806" - mock_pull.assert_called_with("ubuntu") + mock_pull.assert_called_with("ubuntu:latest") def test_get_container_state(loop, vm): @@ -494,7 +530,7 @@ def test_update(loop, vm): "NetworkDisabled": True, "Name": "test", "Hostname": "test", - "Image": "ubuntu", + "Image": "ubuntu:latest", "Env": [ "GNS3_MAX_ETHERNET=eth0" ], @@ -561,7 +597,7 @@ def test_update_running(loop, vm): "NetworkDisabled": True, "Name": "test", "Hostname": "test", - "Image": "ubuntu", + "Image": "ubuntu:latest", "Env": [ "GNS3_MAX_ETHERNET=eth0" ], @@ -814,7 +850,7 @@ def test_get_image_informations(project, manager, loop): with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock: vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu") loop.run_until_complete(asyncio.async(vm._get_image_information())) - mock.assert_called_with("GET", "images/ubuntu/json") + mock.assert_called_with("GET", "images/ubuntu:latest/json") def test_mount_binds(vm, tmpdir): diff --git a/tests/handlers/api/compute/test_docker.py b/tests/handlers/api/compute/test_docker.py index a981dac95..e10a92091 100644 --- a/tests/handlers/api/compute/test_docker.py +++ b/tests/handlers/api/compute/test_docker.py @@ -62,7 +62,7 @@ def test_docker_create(http_compute, project, base_params): assert response.json["name"] == "PC TEST 1" assert response.json["project_id"] == project.id assert response.json["container_id"] == "8bd8153ea8f5" - assert response.json["image"] == "nginx" + assert response.json["image"] == "nginx:latest" assert response.json["adapters"] == 2 assert response.json["environment"] == "YES=1\nNO=0" assert response.json["console_resolution"] == "1280x1024"