1
0
mirror of https://github.com/GNS3/gns3-server synced 2025-02-17 18:42:00 +00:00

Fix pull of images

Fix #445
This commit is contained in:
Julien Duponchelle 2016-02-24 17:08:28 +01:00
parent 49f1931e95
commit c333e9451f
No known key found for this signature in database
GPG Key ID: F1E2485547D4595D
2 changed files with 44 additions and 30 deletions

View File

@ -150,7 +150,12 @@ class DockerVM(BaseVM):
def create(self): def create(self):
"""Creates the Docker container.""" """Creates the Docker container."""
image_infos = yield from self._get_image_informations() try:
image_infos = yield from self._get_image_informations()
except DockerHttp404Error:
log.info("Image %s is missing pulling it from docker hub", self._image)
yield from self.pull_image(self._image)
image_infos = yield from self._get_image_informations()
params = { params = {
"Hostname": self._name, "Hostname": self._name,
@ -173,11 +178,6 @@ class DockerVM(BaseVM):
if self._environment: if self._environment:
params.update({"Env": [e.strip() for e in self._environment.split("\n")]}) params.update({"Env": [e.strip() for e in self._environment.split("\n")]})
images = [i["image"] for i in (yield from self.manager.list_images())]
if self._image not in images:
log.info("Image %s is missing pulling it from docker hub", self._image)
yield from self.pull_image(self._image)
result = yield from self.manager.query("POST", "containers/create", data=params) result = yield from self.manager.query("POST", "containers/create", data=params)
self._cid = result['Id'] self._cid = result['Id']
log.info("Docker container '{name}' [{id}] created".format( log.info("Docker container '{name}' [{id}] created".format(

View File

@ -23,7 +23,7 @@ from tests.utils import asyncio_patch
from gns3server.ubridge.ubridge_error import UbridgeNamespaceError from gns3server.ubridge.ubridge_error import UbridgeNamespaceError
from gns3server.modules.docker.docker_vm import DockerVM from gns3server.modules.docker.docker_vm import DockerVM
from gns3server.modules.docker.docker_error import DockerError from gns3server.modules.docker.docker_error import *
from gns3server.modules.docker import Docker from gns3server.modules.docker import Docker
@ -161,33 +161,47 @@ def test_create_environment(loop, project, manager):
def test_create_image_not_available(loop, project, manager): def test_create_image_not_available(loop, project, manager):
call = 0
@asyncio.coroutine
def informations():
nonlocal call
if call == 0:
call += 1
raise DockerHttp404Error("missing")
else:
return {}
response = { response = {
"Id": "e90e34656806", "Id": "e90e34656806",
"Warnings": [] "Warnings": []
} }
with asyncio_patch("gns3server.modules.docker.Docker.list_images", return_value=[]) as mock_list_images:
with asyncio_patch("gns3server.modules.docker.DockerVM.pull_image", return_value=True) as mock_pull: vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock: vm._get_image_informations = MagicMock()
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu") vm._get_image_informations.side_effect = informations
loop.run_until_complete(asyncio.async(vm.create()))
mock.assert_called_with("POST", "containers/create", data={ with asyncio_patch("gns3server.modules.docker.DockerVM.pull_image", return_value=True) as mock_pull:
"Tty": True, with asyncio_patch("gns3server.modules.docker.Docker.query", return_value=response) as mock:
"OpenStdin": True, loop.run_until_complete(asyncio.async(vm.create()))
"StdinOnce": False, mock.assert_called_with("POST", "containers/create", data={
"HostConfig": "Tty": True,
{ "OpenStdin": True,
"CapAdd": ["ALL"], "StdinOnce": False,
"Binds": [], "HostConfig":
"Privileged": True {
}, "CapAdd": ["ALL"],
"Volumes": {}, "Binds": [],
"NetworkDisabled": True, "Privileged": True
"Name": "test", },
"Hostname": "test", "Volumes": {},
"Image": "ubuntu" "NetworkDisabled": True,
}) "Name": "test",
assert vm._cid == "e90e34656806" "Hostname": "test",
mock_pull.assert_called_with("ubuntu") "Image": "ubuntu"
})
assert vm._cid == "e90e34656806"
mock_pull.assert_called_with("ubuntu")
def test_get_container_state(loop, vm): def test_get_container_state(loop, vm):