summaryrefslogtreecommitdiffstats
path: root/onap-dcae-cbs-docker-client/tests/test_client.py
blob: b1589c270a9bfaf85cdaa6800560e7ff6cc3fd11 (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
# ================================================================================
# Copyright (c) 2017-2019 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=========================================================
import pytest
from onap_dcae_cbs_docker_client.client import get_config, get_all
from onap_dcae_cbs_docker_client.exceptions import CantGetConfig, CBSUnreachable, ENVsMissing


def test_config(monkeypatch, monkeyed_requests_get):
    monkeypatch.setattr("requests.get", monkeyed_requests_get)
    assert get_config() == {"key_to_your_heart": 666}


def test_all(monkeypatch, monkeyed_requests_get):
    monkeypatch.setattr("requests.get", monkeyed_requests_get)
    assert get_all() == {
        "config": {"key_to_your_heart": 666},
        "dti": {"some amazing": "dti stuff"},
        "policies": {"event": {"foo": "bar"}, "items": [{"foo2": "bar2"}]},
        "otherkey": {"foo3": "bar3"},
    }


def test_bad_hostname(monkeypatch, monkeyed_requests_get_404):
    monkeypatch.setattr("requests.get", monkeyed_requests_get_404)
    with pytest.raises(CantGetConfig):
        get_config()
    with pytest.raises(CantGetConfig):
        get_all()

    try:
        get_config()
    except CantGetConfig as e:
        assert e.code == 404
        assert e.text == ""


def test_unreachable(monkeypatch, monkeyed_requests_get_unreachable):
    monkeypatch.setattr("requests.get", monkeyed_requests_get_unreachable)
    with pytest.raises(CBSUnreachable):
        get_config()
    with pytest.raises(CBSUnreachable):
        get_all()


def test_badenv(monkeypatch):
    monkeypatch.delenv("CONFIG_BINDING_SERVICE")
    with pytest.raises(ENVsMissing):
        get_config()
    with pytest.raises(ENVsMissing):
        get_all()