Commit d93bc5c3 authored by Daniele Venzano's avatar Daniele Venzano

Add unit tests for the new docker backend

parent 853a7a51
[DEFAULT]
use_tls: no
tls_cert: /mnt/test/cert.pem
tls_key: /mnt/test/key.pem
tls_ca: /mnt/test/ca.pem
[bf5]
docker_address: 192.168.47.5:2375
external_address: 192.168.47.5
use_tls: yes
[bf7]
docker_address: 192.168.47.7:2375
external_address: 192.168.47.7
use_tls: yes
[bf8]
docker_address: 192.168.47.8:2375
external_address: 192.168.47.8
use_tls: yes
[bf9]
docker_address: 192.168.47.9:2375
external_address: 192.168.47.9
use_tls: yes
[bf10]
docker_address: 192.168.47.10:2375
external_address: 192.168.47.10
use_tls: yes
[bf12]
docker_address: 192.168.47.12:2375
external_address: 192.168.47.12
use_tls: yes
[bf13]
docker_address: 192.168.47.13:2375
external_address: 192.168.47.13
use_tls: yes
[bf14]
docker_address: 192.168.47.14:2375
external_address: 192.168.47.14
use_tls: yes
[bf15]
docker_address: 192.168.47.15:2375
external_address: 192.168.47.15
use_tls: yes
[bf16]
docker_address: 192.168.47.16:2375
external_address: 192.168.47.16
use_tls: yes
[bf17]
docker_address: 192.168.47.17:2375
external_address: 192.168.47.17
use_tls: yes
[bf18]
docker_address: 192.168.47.18:2375
external_address: 192.168.47.18
use_tls: yes
[bf19]
docker_address: 192.168.47.19:2375
external_address: 192.168.47.19
use_tls: yes
[bf20]
docker_address: 192.168.47.20:2375
external_address: 192.168.47.20
use_tls: yes
[bf21]
docker_address: 192.168.47.21:2375
external_address: 192.168.47.21
use_tls: yes
[bf22]
docker_address: 192.168.47.22:2375
external_address: 192.168.47.22
use_tls: yes
......@@ -43,13 +43,18 @@ except AttributeError:
class DockerClient:
"""The client class that wraps the Docker API."""
def __init__(self, docker_config: DockerHostConfig) -> None:
def __init__(self, docker_config: DockerHostConfig, mock_client=None) -> None:
self.name = docker_config.name
if not docker_config.tls:
tls = None
else:
tls = docker.tls.TLSConfig(client_cert=(docker_config.tls_cert, docker_config.tls_key), verify=docker_config.tls_ca)
# Simplify testing
if mock_client is not None:
self.cli = mock_client
return
try:
self.cli = docker.DockerClient(base_url=docker_config.address, version="auto", tls=tls)
except docker.errors.DockerException as e:
......@@ -173,7 +178,7 @@ class DockerClient:
info["running"] = False
info['ports'] = {}
if container.attrs['NetworkSettings']['Ports'] is not None:
if 'Ports' in container.attrs['NetworkSettings'] and container.attrs['NetworkSettings']['Ports'] is not None:
for port in container.attrs['NetworkSettings']['Ports']:
if container.attrs['NetworkSettings']['Ports'][port] is not None:
info['ports'][port] = container.attrs['NetworkSettings']['Ports'][port][0]['HostPort']
......@@ -190,7 +195,7 @@ class DockerClient:
def inspect_container(self, docker_id: str) -> Dict[str, Any]:
"""Retrieve information about a running container."""
try:
cont = self.cli.container.get(docker_id)
cont = self.cli.containers.get(docker_id)
except Exception as e:
raise ZoeException(str(e))
return self._container_summary(cont)
......
......@@ -37,7 +37,7 @@ class DockerEngineBackend(zoe_master.backends.base.BaseBackend):
"""Zoe backend implementation for old-style stand-alone Docker Swarm."""
def __init__(self, opts):
super().__init__(opts)
self.docker_config = DockerConfig().read_config()
self.docker_config = DockerConfig(get_conf().backend_docker_config_file).read_config()
def _get_config(self, host) -> DockerHostConfig:
for conf in self.docker_config:
......
......@@ -19,8 +19,6 @@ import configparser
import logging
from typing import List
from zoe_lib.config import get_conf
log = logging.getLogger(__name__)
......@@ -39,8 +37,8 @@ class DockerHostConfig:
class DockerConfig:
"""A class that holds the configuration for the Docker Engine backend."""
def __init__(self):
self.conffile = get_conf().backend_docker_config_file
def __init__(self, config_file):
self.conffile = config_file
def read_config(self) -> List[DockerHostConfig]:
"""Parse the configuration file."""
......
This diff is collapsed.
# Copyright (c) 2017, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests"""
from zoe_master.backends.docker import config
class TestDockerEngineBackendConfig:
"""Docker configuration parsing tests."""
def test_new_docker_host_config(self):
"""Test the DockerHostConfig object."""
config.DockerHostConfig()
def test_parsing_config_file(self):
"""Test Docker backend config parsing."""
hosts = config.DockerConfig(config_file='tests/sample_docker.conf').read_config()
assert len(hosts) == 16
......@@ -46,7 +46,7 @@ class DockerStateSynchronizer(threading.Thread):
self.setDaemon(True)
self._platform_stats = ClusterStats()
self.host_checkers = []
for docker_host in DockerConfig().read_config():
for docker_host in DockerConfig(get_conf().backend_docker_config_file).read_config():
th = threading.Thread(target=self._host_subthread, args=(docker_host,), name='synchro_' + docker_host.name, daemon=True)
th.start()
self.host_checkers.append((th, docker_host))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment