1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-29 18:28:11 +00:00

Force tag latest for docker image if no tag is specified

Fix #533
This commit is contained in:
Julien Duponchelle 2016-05-19 13:09:07 +02:00
parent 871fea33e0
commit a741662636
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
3 changed files with 72 additions and 32 deletions

View File

@ -65,6 +65,9 @@ class DockerVM(BaseVM):
console_resolution="1024x768", console_http_port=80, console_http_path="/"): console_resolution="1024x768", console_http_port=80, console_http_path="/"):
super().__init__(name, vm_id, project, manager, console=console, aux=aux, allocate_aux=True, console_type=console_type) super().__init__(name, vm_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._image = image
self._start_command = start_command self._start_command = start_command
self._environment = environment self._environment = environment

View File

@ -62,7 +62,7 @@ def test_docker_create(server, project, base_params):
assert response.json["name"] == "PC TEST 1" assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id assert response.json["project_id"] == project.id
assert response.json["container_id"] == "8bd8153ea8f5" 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["adapters"] == 2
assert response.json["environment"] == "YES=1\nNO=0" assert response.json["environment"] == "YES=1\nNO=0"
assert response.json["console_resolution"] == "1280x1024" assert response.json["console_resolution"] == "1280x1024"

View File

@ -41,7 +41,7 @@ def manager(port_manager):
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def vm(project, manager): 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._cid = "e90e34656842"
vm.allocate_aux = False vm.allocate_aux = False
return vm return vm
@ -50,7 +50,7 @@ def vm(project, manager):
def test_json(vm, project): def test_json(vm, project):
assert vm.__json__() == { assert vm.__json__() == {
'container_id': 'e90e34656842', 'container_id': 'e90e34656842',
'image': 'ubuntu', 'image': 'ubuntu:latest',
'name': 'test', 'name': 'test',
'project_id': project.id, 'project_id': project.id,
'vm_id': vm.id, 'vm_id': vm.id,
@ -81,9 +81,9 @@ def test_create(loop, project, manager):
"Id": "e90e34656806", "Id": "e90e34656806",
"Warnings": [] "Warnings": []
} }
with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: with asyncio_patch("gns3server.modules.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())) loop.run_until_complete(asyncio.async(vm.create()))
mock.assert_called_with("POST", "containers/create", data={ mock.assert_called_with("POST", "containers/create", data={
"Tty": True, "Tty": True,
@ -102,10 +102,47 @@ def test_create(loop, project, manager):
"NetworkDisabled": True, "NetworkDisabled": True,
"Name": "test", "Name": "test",
"Hostname": "test", "Hostname": "test",
"Image": "ubuntu", "Image": "ubuntu:latest",
"Env": [ "Env": [
"GNS3_MAX_ETHERNET=eth0" "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.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock:
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest: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("modules/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:latest:16.04",
"Env": [
"GNS3_MAX_ETHERNET=eth0"
],
"Entrypoint": ["/gns3/init.sh"], "Entrypoint": ["/gns3/init.sh"],
"Cmd": ["/bin/sh"] "Cmd": ["/bin/sh"]
}) })
@ -119,9 +156,9 @@ def test_create_vnc(loop, project, manager):
"Warnings": [] "Warnings": []
} }
with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock:
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", console_type="vnc", console=5900) vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest", console_type="vnc", console=5900)
vm._start_vnc = MagicMock() vm._start_vnc = MagicMock()
vm._display = 42 vm._display = 42
loop.run_until_complete(asyncio.async(vm.create())) loop.run_until_complete(asyncio.async(vm.create()))
@ -143,11 +180,11 @@ def test_create_vnc(loop, project, manager):
"NetworkDisabled": True, "NetworkDisabled": True,
"Name": "test", "Name": "test",
"Hostname": "test", "Hostname": "test",
"Image": "ubuntu", "Image": "ubuntu:latest",
"Env": [ "Env": [
"GNS3_MAX_ETHERNET=eth0", "GNS3_MAX_ETHERNET=eth0",
"DISPLAY=:42" "DISPLAY=:42"
], ],
"Entrypoint": ["/gns3/init.sh"], "Entrypoint": ["/gns3/init.sh"],
"Cmd": ["/bin/sh"] "Cmd": ["/bin/sh"]
}) })
@ -162,9 +199,9 @@ def test_create_start_cmd(loop, project, manager):
"Id": "e90e34656806", "Id": "e90e34656806",
"Warnings": [] "Warnings": []
} }
with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: with asyncio_patch("gns3server.modules.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" vm._start_command = "/bin/ls"
loop.run_until_complete(asyncio.async(vm.create())) loop.run_until_complete(asyncio.async(vm.create()))
mock.assert_called_with("POST", "containers/create", data={ mock.assert_called_with("POST", "containers/create", data={
@ -186,10 +223,10 @@ def test_create_start_cmd(loop, project, manager):
"NetworkDisabled": True, "NetworkDisabled": True,
"Name": "test", "Name": "test",
"Hostname": "test", "Hostname": "test",
"Image": "ubuntu", "Image": "ubuntu:latest",
"Env": [ "Env": [
"GNS3_MAX_ETHERNET=eth0" "GNS3_MAX_ETHERNET=eth0"
] ]
}) })
assert vm._cid == "e90e34656806" assert vm._cid == "e90e34656806"
@ -200,9 +237,9 @@ def test_create_environment(loop, project, manager):
"Id": "e90e34656806", "Id": "e90e34656806",
"Warnings": [] "Warnings": []
} }
with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: with asyncio_patch("gns3server.modules.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.environment = "YES=1\nNO=0" vm.environment = "YES=1\nNO=0"
loop.run_until_complete(asyncio.async(vm.create())) loop.run_until_complete(asyncio.async(vm.create()))
mock.assert_called_with("POST", "containers/create", data={ mock.assert_called_with("POST", "containers/create", data={
@ -222,12 +259,12 @@ def test_create_environment(loop, project, manager):
"GNS3_MAX_ETHERNET=eth0", "GNS3_MAX_ETHERNET=eth0",
"YES=1", "YES=1",
"NO=0" "NO=0"
], ],
"Volumes": {}, "Volumes": {},
"NetworkDisabled": True, "NetworkDisabled": True,
"Name": "test", "Name": "test",
"Hostname": "test", "Hostname": "test",
"Image": "ubuntu", "Image": "ubuntu:latest",
"Entrypoint": ["/gns3/init.sh"], "Entrypoint": ["/gns3/init.sh"],
"Cmd": ["/bin/sh"] "Cmd": ["/bin/sh"]
}) })
@ -252,7 +289,7 @@ def test_create_image_not_available(loop, project, manager):
"Warnings": [] "Warnings": []
} }
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu") vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu:latest")
vm._get_image_informations = MagicMock() vm._get_image_informations = MagicMock()
vm._get_image_informations.side_effect = informations vm._get_image_informations.side_effect = informations
@ -276,15 +313,15 @@ def test_create_image_not_available(loop, project, manager):
"NetworkDisabled": True, "NetworkDisabled": True,
"Name": "test", "Name": "test",
"Hostname": "test", "Hostname": "test",
"Image": "ubuntu", "Image": "ubuntu:latest",
"Env": [ "Env": [
"GNS3_MAX_ETHERNET=eth0" "GNS3_MAX_ETHERNET=eth0"
], ],
"Entrypoint": ["/gns3/init.sh"], "Entrypoint": ["/gns3/init.sh"],
"Cmd": ["/bin/sh"] "Cmd": ["/bin/sh"]
}) })
assert vm._cid == "e90e34656806" assert vm._cid == "e90e34656806"
mock_pull.assert_called_with("ubuntu") mock_pull.assert_called_with("ubuntu:latest")
def test_get_container_state(loop, vm): def test_get_container_state(loop, vm):
@ -470,7 +507,7 @@ def test_update(loop, vm):
original_console = vm.console original_console = vm.console
original_aux = vm.aux original_aux = vm.aux
with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.DockerVM._get_container_state", return_value="stopped"): with asyncio_patch("gns3server.modules.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock_query: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock_query:
loop.run_until_complete(asyncio.async(vm.update())) loop.run_until_complete(asyncio.async(vm.update()))
@ -493,7 +530,7 @@ def test_update(loop, vm):
"NetworkDisabled": True, "NetworkDisabled": True,
"Name": "test", "Name": "test",
"Hostname": "test", "Hostname": "test",
"Image": "ubuntu", "Image": "ubuntu:latest",
"Env": [ "Env": [
"GNS3_MAX_ETHERNET=eth0" "GNS3_MAX_ETHERNET=eth0"
], ],
@ -518,7 +555,7 @@ def test_update_vnc(loop, vm):
original_aux = vm.aux original_aux = vm.aux
with asyncio_patch("gns3server.modules.docker.DockerVM._start_vnc"): with asyncio_patch("gns3server.modules.docker.DockerVM._start_vnc"):
with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.DockerVM._get_container_state", return_value="stopped"): with asyncio_patch("gns3server.modules.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock_query: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock_query:
loop.run_until_complete(asyncio.async(vm.update())) loop.run_until_complete(asyncio.async(vm.update()))
@ -537,7 +574,7 @@ def test_update_running(loop, vm):
original_console = vm.console original_console = vm.console
vm.start = MagicMock() vm.start = MagicMock()
with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images: with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[{"image": "ubuntu:latest"}]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.DockerVM._get_container_state", return_value="running"): with asyncio_patch("gns3server.modules.docker.DockerVM._get_container_state", return_value="running"):
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock_query: with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock_query:
loop.run_until_complete(asyncio.async(vm.update())) loop.run_until_complete(asyncio.async(vm.update()))
@ -560,7 +597,7 @@ def test_update_running(loop, vm):
"NetworkDisabled": True, "NetworkDisabled": True,
"Name": "test", "Name": "test",
"Hostname": "test", "Hostname": "test",
"Image": "ubuntu", "Image": "ubuntu:latest",
"Env": [ "Env": [
"GNS3_MAX_ETHERNET=eth0" "GNS3_MAX_ETHERNET=eth0"
], ],
@ -770,8 +807,8 @@ def test_pull_image(loop, vm):
mock_query.content.return_value = Response() mock_query.content.return_value = Response()
with asyncio_patch("gns3server.modules.docker.Docker.http_query", return_value=mock_query) as mock: with asyncio_patch("gns3server.modules.docker.Docker.http_query", return_value=mock_query) as mock:
images = loop.run_until_complete(asyncio.async(vm.pull_image("ubuntu"))) images = loop.run_until_complete(asyncio.async(vm.pull_image("ubuntu:latest")))
mock.assert_called_with("POST", "images/create", params={"fromImage": "ubuntu"}) mock.assert_called_with("POST", "images/create", params={"fromImage": "ubuntu:latest"})
def test_start_capture(vm, tmpdir, manager, free_console_port, loop): def test_start_capture(vm, tmpdir, manager, free_console_port, loop):
@ -811,9 +848,9 @@ def test_get_image_informations(project, manager, loop):
response = { response = {
} }
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: with asyncio_patch("gns3server.modules.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._get_image_informations())) loop.run_until_complete(asyncio.async(vm._get_image_informations()))
mock.assert_called_with("GET", "images/ubuntu/json") mock.assert_called_with("GET", "images/ubuntu:latest/json")
def test_mount_binds(vm, tmpdir): def test_mount_binds(vm, tmpdir):