aboutsummaryrefslogtreecommitdiffstats
path: root/python_moonutilities/tests/unit_python/test_configuration.py
blob: d5d83f7a0c15d49cb7fcfb0f85a65cced95bdb88 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import mock_repo.components_utilities as comp_util
import pytest
import requests_mock


def test_get_configuration_success():
    from python_moonutilities import configuration
    assert configuration.get_configuration("components/port_start")["components/port_start"] == comp_util.CONF["components"]["port_start"]


@requests_mock.Mocker(kw='mock')
def test_get_configuration_mutliple_list_success(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start',
                       json=[
                           {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")},
                           {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}
                             ]
                       )

    assert len(configuration.get_configuration("components/port_start")) == 2


@requests_mock.Mocker(kw='mock')
def test_get_configuration_mutliple_list_failure(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start',
                       json=[
                           {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")},
                           {'invalidKey': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}
                             ]
                       )
    with pytest.raises(Exception) as exception_info:
        configuration.get_configuration("components/port_start")
    assert str(exception_info.value) == '500: Consul Content error'


@requests_mock.Mocker(kw='mock')
def test_get_configuration_not_found(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start_wrong', json=[
    ], status_code=500)
    with pytest.raises(Exception) as exception_info:
        configuration.get_configuration("components/port_start_wrong")
    assert str(exception_info.value) == '500: Consul error'


@requests_mock.Mocker(kw='mock')
def test_get_configuration_invalid_response(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[
        {"port_start":'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}
    ])
    with pytest.raises(Exception) as exception_info:
        configuration.get_configuration("components/port_start")
    assert str(exception_info.value) == '500: Consul Content error'


################################ increment_port ####################################
@requests_mock.Mocker(kw='mock')
def test_put_increment_port_invalidkey_failure(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[
        {'Key': 'invalidkey', 'Value': comp_util.get_b64_conf("components/port_start")}
    ], status_code=200)
    with pytest.raises(Exception) as exception_info:
        configuration.increment_port()
    assert str(exception_info.value) == '500: Consul Content error'


@requests_mock.Mocker(kw='mock')
def test_put_increment_port_failure(**kwargs):
    from python_moonutilities import configuration
    kwargs['mock'].put('http://consul:8500/v1/kv/components/port_start', json=[], status_code=400)
    kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[
        {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}
    ], status_code=200)
    with pytest.raises(Exception) as exception_info:
        configuration.increment_port()
    assert str(exception_info.value) == '500: Consul Content error'


def test_increment_port_success():
    from python_moonutilities import configuration
    cur_port = comp_util.CONF["components"]["port_start"]
    incremented_port = configuration.increment_port()
    assert incremented_port == cur_port + 1


################################ plugin ####################################
def test_get_plugins_success():
    from python_moonutilities import configuration
    plugin = configuration.get_plugins()
    assert plugin is not None

def test_get_plugins_failure(no_requests):
    from python_moonutilities import configuration
    no_requests.register_uri(
        'GET', 'http://consul:8500/v1/kv/components/pipeline',
        json=[{'Key': 'components/pipeline', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='}]
    )
    with pytest.raises(Exception) as exception_info:
        configuration.get_plugins()
    assert str(exception_info.value) == '500: Consul Content error'
################################ component ####################################
def test_get_components():
    from python_moonutilities import configuration
    assert isinstance(configuration.get_components(), dict)


@requests_mock.Mocker(kw='mock')
def test_get_components_mutliple_list_success(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true',
                       json=[
                           {'Key': 'components/c1', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='},
                           {'Key': 'components/c2', 'Value': 'eyJjb250YWluZXIiOiAid3Vrb25nc3VuL21vb25fYXV0aHo6djQuMyIsICJwb3J0IjogODA4MX0='}
                             ]
                       )

    res = configuration.get_components()
    assert bool(res)


@requests_mock.Mocker(kw='mock')
def test_get_components_mutliple_list_failure(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true',
                       json=[
                           {'Key': 'components/c1', 'Value': "eyJjb250YWluZXIiOiAid3Vrb25"},
                           {'invalidKey': 'components/c2', 'Value': "eyJjb250YWluZXIiOiAid3Vrb25"}
                             ]
                       )
    with pytest.raises(Exception) as exception_info:
        configuration.get_components()
    assert str(exception_info.value) == '500: Consul Content error'


@requests_mock.Mocker(kw='mock')
def test_get_components_not_found(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', json=[
    ], status_code=500)
    with pytest.raises(Exception) as exception_info:
        configuration.get_components()
    assert str(exception_info.value) == '400: Consul error'


@requests_mock.Mocker(kw='mock')
def test_get_components_invalid_response(**kwargs):
    from python_moonutilities import configuration

    kwargs['mock'].get('http://consul:8500/v1/kv/components?recurse=true', json=[
        {"invalidKey":'invalid', 'Value': "jb250"}
    ])
    with pytest.raises(Exception) as exception_info:
        configuration.get_components()
    assert str(exception_info.value) == '500: Consul Content error'