summaryrefslogtreecommitdiffstats
path: root/python-dockering/tests/test_core.py
blob: 76e027b3d32a7355f1101b07849bf8e731c45a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# org.onap.dcae
# ================================================================================
# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# 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.
# ============LICENSE_END=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.

import os
from functools import partial
import pytest
import docker
from dockering import core as doc
from dockering.exceptions import DockerError, DockerConnectionError


@pytest.mark.skip(reason="Need to automatically setup Docker engine and maybe Consul")
def test_create_client():
    # Bad - Could not connect to docker engine

    with pytest.raises(DockerConnectionError):
        doc.create_client("fake", 2376, reauth=True, logins=[{
            "registry": "nowhere", "username": "bob", "password": "123"}])


# TODO: Does pytest provide an env file?
CONSUL_HOST = os.environ.get("CONSUL_HOST")
EXTERNAL_IP = os.environ.get("EXTERNAL_IP")

@pytest.mark.skip(reason="Need to automatically setup Docker engine and maybe Consul")
def test_create_container():
    client = doc.create_client("127.0.0.1", 2376)

    scn = "unittest-registrator"
    consul_host = CONSUL_HOST
    # TODO: This may not work until we push the custom registrator into DockerHub
    image_name = "registrator:latest"
    envs = { "CONSUL_HOST": CONSUL_HOST,
             "EXTERNAL_IP": EXTERNAL_IP }
    volumes = {'/var/run/docker.sock': {'bind': '/tmp/docker.sock', 'mode': 'ro'}}

    hcp = doc.add_host_config_params_volumes(volumes=volumes)
    container = doc.create_container(client, image_name, scn, envs, hcp)

    # Container is a dict with "Id". Check if container name matches scn.

    try:
        inspect_result = client.inspect_container(scn)
        import pprint
        pprint.pprint(inspect_result)

        actual_mounts = inspect_result["Mounts"][0]
        assert actual_mounts["Destination"] == volumes.values()[0]["bind"]
        assert actual_mounts["Source"] == volumes.keys()[0]
    except Exception as e:
        raise e
    finally:
        # Execute teardown/cleanup
        try:
            doc.stop_then_remove_container(client, scn)
        except:
            print("Container removal failed")


def test_build_policy_update_cmd():
    assert ["/bin/sh", "/bin/foo", "policy", "{}"] == doc.build_policy_update_cmd("/bin/foo")
    assert ["/bin/foo", "policy", "{}"] == doc.build_policy_update_cmd("/bin/foo", use_sh=False)

    kwargs = { "bar": "baz" }

    assert ["/bin/foo", "policy", "{\"bar\": \"baz\"}"] == doc.build_policy_update_cmd(
            "/bin/foo", use_sh=False, **kwargs)

    assert ["/bin/foo", "policy", "{\"application_config\": {\"key\": \"hello world\"}}"] \
        == doc.build_policy_update_cmd("/bin/foo", use_sh=False,
                application_config={"key": "hello world"})