Some spring cleanup for Docker VM.

This commit is contained in:
grossmj 2016-05-13 19:28:53 -06:00
parent f1bc2f22c3
commit 51738e19c3
2 changed files with 25 additions and 23 deletions

View File

@ -27,16 +27,20 @@ import aiohttp
import json import json
import os import os
from .docker_error import * from gns3server.utils.asyncio.telnet_server import AsyncioTelnetServer
from gns3server.utils.asyncio.raw_command_server import AsyncioRawCommandServer
from gns3server.utils.asyncio import wait_for_file_creation
from gns3server.utils.get_resource import get_resource
from gns3server.ubridge.ubridge_error import UbridgeError, UbridgeNamespaceError
from ..base_node import BaseNode from ..base_node import BaseNode
from ..adapters.ethernet_adapter import EthernetAdapter from ..adapters.ethernet_adapter import EthernetAdapter
from ..nios.nio_udp import NIOUDP from ..nios.nio_udp import NIOUDP
from ...utils.asyncio.telnet_server import AsyncioTelnetServer from .docker_error import (
from ...utils.asyncio.raw_command_server import AsyncioRawCommandServer DockerError,
from ...utils.asyncio import wait_for_file_creation DockerHttp304Error,
from ...utils.get_resource import get_resource DockerHttp404Error
from ...ubridge.ubridge_error import UbridgeError, UbridgeNamespaceError )
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -180,9 +184,9 @@ class DockerVM(BaseNode):
return "exited" return "exited"
@asyncio.coroutine @asyncio.coroutine
def _get_image_informations(self): def _get_image_information(self):
""" """
:returns: Dictionnary informations about the container image :returns: Dictionary information about the container image
""" """
result = yield from self.manager.query("GET", "images/{}/json".format(self._image)) result = yield from self.manager.query("GET", "images/{}/json".format(self._image))
return result return result
@ -191,9 +195,7 @@ class DockerVM(BaseNode):
""" """
:returns: Return the path that we need to map to local folders :returns: Return the path that we need to map to local folders
""" """
binds = [] binds = ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))]
binds.append("{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")))
# We mount our own etc/network # We mount our own etc/network
network_config = self._create_network_config() network_config = self._create_network_config()
@ -247,11 +249,11 @@ class DockerVM(BaseNode):
"""Creates the Docker container.""" """Creates the Docker container."""
try: try:
image_infos = yield from self._get_image_informations() image_infos = yield from self._get_image_information()
except DockerHttp404Error: except DockerHttp404Error:
log.info("Image %s is missing pulling it from docker hub", self._image) log.info("Image %s is missing pulling it from docker hub", self._image)
yield from self.pull_image(self._image) yield from self.pull_image(self._image)
image_infos = yield from self._get_image_informations() image_infos = yield from self._get_image_information()
params = { params = {
"Hostname": self._name, "Hostname": self._name,
@ -328,8 +330,7 @@ class DockerVM(BaseNode):
else: else:
yield from self._clean_servers() yield from self._clean_servers()
result = yield from self.manager.query("POST", "containers/{}/start".format(self._cid)) yield from self.manager.query("POST", "containers/{}/start".format(self._cid))
namespace = yield from self._get_namespace() namespace = yield from self._get_namespace()
yield from self._start_ubridge() yield from self._start_ubridge()
@ -398,7 +399,7 @@ class DockerVM(BaseNode):
""" """
log.debug("Forward HTTP for %s to %d", self.name, self._console_http_port) log.debug("Forward HTTP for %s to %d", self.name, self._console_http_port)
command = ["docker", "exec", "-i", self._cid, "/gns3/bin/busybox", "nc", "127.0.0.1", str(self._console_http_port)] command = ["docker", "exec", "-i", self._cid, "/gns3/bin/busybox", "nc", "127.0.0.1", str(self._console_http_port)]
# We replace the port in the server answer otherwise somelink could be broke # We replace the port in the server answer otherwise some link could be broken
server = AsyncioRawCommandServer(command, replaces=[ server = AsyncioRawCommandServer(command, replaces=[
( (
'{}'.format(self._console_http_port).encode(), '{}'.format(self._console_http_port).encode(),
@ -443,8 +444,9 @@ class DockerVM(BaseNode):
@asyncio.coroutine @asyncio.coroutine
def _read_console_output(self, ws, out): def _read_console_output(self, ws, out):
""" """
Read websocket and forward it to the telnet Read Websocket and forward it to the telnet
:params ws: Websocket connection
:param ws: Websocket connection
:param out: Output stream :param out: Output stream
""" """

View File

@ -239,7 +239,7 @@ def test_create_image_not_available(loop, project, manager):
call = 0 call = 0
@asyncio.coroutine @asyncio.coroutine
def informations(): def information():
nonlocal call nonlocal call
if call == 0: if call == 0:
call += 1 call += 1
@ -253,8 +253,8 @@ def test_create_image_not_available(loop, project, manager):
} }
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu") vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu")
vm._get_image_informations = MagicMock() vm._get_image_information = MagicMock()
vm._get_image_informations.side_effect = informations 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.DockerVM.pull_image", return_value=True) as mock_pull:
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock: with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock:
@ -812,7 +812,7 @@ def test_get_image_informations(project, manager, loop):
} }
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value=response) as mock: 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")
loop.run_until_complete(asyncio.async(vm._get_image_informations())) 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/json")