summaryrefslogtreecommitdiffstats
path: root/mod/onboardingapi/dcae_cli/util/tests/test_dmaap.py
blob: dabc737f36d78801a5264749ebd064ba69b664f0 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# ============LICENSE_START=======================================================
# 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.

"""
Tests for dmaap module
"""
import pytest
from dcae_cli.util import dmaap
from dcae_cli.util.exc import DcaeException


def test_validate_dmaap_map_schema_message_router():
    def pack_her_up(entry):
        return { "some-config-key": entry }

    good = {
        "type": "message_router",
        "aaf_username": "foo3",
        "aaf_password": "bar3",
        "dmaap_info": {
           "client_role":"com.dcae.member",
           "client_id":"1500462518108",
           "location":"mtc5",
            "topic_url":"https://dcae-msrt-ftl2.com:3905/events/com.dcae.dmaap.FTL2.TommyTestTopic2"
            }
        }
    dmaap.validate_dmaap_map_schema(pack_her_up(good))

    good_minimal = {
            "type": "message_router",
            "dmaap_info": {
                "topic_url":"https://dcae-msrt-ftl2.com:3905/events/com.dcae.dmaap.FTL2.TommyTestTopic2"
                }
            }
    dmaap.validate_dmaap_map_schema(pack_her_up(good_minimal))

    bad_extra = {
        "type": "message_router",
        "aaf_username": "foo3",
        "aaf_password": "bar3",
        "something_else": "boo",
        "dmaap_info": {
           "client_role":"com.dcae.member",
           "client_id":"1500462518108",
           "location":"mtc5",
           "topic_url":"https://dcae-msrt-ftl2.com:3905/events/com.dcae.dmaap.FTL2.TommyTestTopic2"
           }
        }
    dm = { "some-config-key": bad_extra }


    with pytest.raises(DcaeException):
        dmaap.validate_dmaap_map_schema(dm)

    bad_missing = {
        "type": "message_router",
        "aaf_username": "foo3",
        "aaf_password": "bar3",
        "dmaap_info": {
           "client_role":"com.dcae.member",
           "client_id":"1500462518108",
           "location":"mtc5"
           }
        }
    dm = { "some-config-key": bad_missing }

    with pytest.raises(DcaeException):
        dmaap.validate_dmaap_map_schema(dm)


def test_validate_dmaap_map_schema_data_router():
    def pack_her_up(entry):
        return { "some-config-key": entry }

    # Publishers
    good = {
        "type": "data_router",
        "dmaap_info": {
            "location": "mtc5",
            "publish_url": "http://some-publish-url/123",
            "log_url": "http://some-log-url/456",
            "username": "jane",
            "password": "abc"
            }
        }
    dmaap.validate_dmaap_map_schema(pack_her_up(good))

    good_minimal = {
            "type": "data_router",
            "dmaap_info": {
                "publish_url": "http://some-publish-url/123"
                }
            }
    dmaap.validate_dmaap_map_schema(pack_her_up(good_minimal))

    bad_extra = {
            "type": "data_router",
            "dmaap_info": {
                "publish_url": "http://some-publish-url/123",
                "unknown_key": "value"
                }
            }
    with pytest.raises(DcaeException):
        dmaap.validate_dmaap_map_schema(pack_her_up(bad_extra))

    # Subscribers
    good = {
        "type": "data_router",
        "dmaap_info": {
            "username": "drdeliver",
            "password": "1loveDataR0uter",
            "location": "loc00",
            "delivery_url": "https://example.com/whatever",
            "subscriber_id": "1550"
            }
        }
    dmaap.validate_dmaap_map_schema(pack_her_up(good))

    good_minimal = {
            "type": "data_router",
            "dmaap_info": {
                "delivery_url": "https://example.com/whatever"
                }
            }
    dmaap.validate_dmaap_map_schema(pack_her_up(good_minimal))

    bad_extra = {
            "type": "data_router",
            "dmaap_info": {
                "delivery_url": "https://example.com/whatever",
                "unknown_key": "value"
                }
            }
    with pytest.raises(DcaeException):
        dmaap.validate_dmaap_map_schema(pack_her_up(bad_extra))


def test_validate_dmaap_map_entries():

    # Success

    dmaap_map = { "mr_pub_fun": { "foo": "bar" }, "mr_sub_fun": { "baz": "duh"} }
    mr_config_keys = [ "mr_pub_fun", "mr_sub_fun" ]
    dr_config_keys = []

    assert dmaap.validate_dmaap_map_entries(dmaap_map, mr_config_keys, dr_config_keys) == True

    # Not supposed to be empty

    dmaap_map = {}

    assert dmaap.validate_dmaap_map_entries(dmaap_map, mr_config_keys, dr_config_keys) == False

    # Too many in dmaap map

    # NOTE: This scenario has been changed to be a success case per Tommy who
    # believes that having extra keys in the dmaap_map is harmless. People would
    # want to have a master dmaap_map that has a superset of connections used
    # across many components.

    dmaap_map = { "mr_pub_fun": { "foo": "bar" }, "mr_sub_fun": { "baz": "duh"} }
    mr_config_keys = [ "mr_pub_fun" ]
    dr_config_keys = []

    assert dmaap.validate_dmaap_map_entries(dmaap_map, mr_config_keys, dr_config_keys) == True

    # Too little in dmaap map

    dmaap_map = { "mr_pub_fun": { "foo": "bar" }, "mr_sub_fun": { "baz": "duh"} }
    mr_config_keys = [ "mr_pub_fun", "mr_sub_fun", "mr_xxx" ]
    dr_config_keys = []

    assert dmaap.validate_dmaap_map_entries(dmaap_map, mr_config_keys, dr_config_keys) == False


def test_apply_defaults_dmaap_map():
    good = {
        "type": "message_router",
        "aaf_username": "foo3",
        "aaf_password": "bar3",
        "dmaap_info": {
           "client_role":"com.dcae.member",
           "client_id":"1500462518108",
           "location":"mtc5",
            "topic_url":"https://dcae-msrt-ftl2.com:3905/events/com.dcae.dmaap.FTL2.TommyTestTopic2"
            }
        }
    dm = { "some-config-key": good }

    assert dmaap.apply_defaults_dmaap_map(dm) == dm

    minimal = {
        "type": "message_router",
        "dmaap_info": {
            "topic_url":"https://dcae-msrt-ftl2.com:3905/events/com.dcae.dmaap.FTL2.TommyTestTopic2"
            }
        }
    dm = { "some-config-key": minimal }

    result = dmaap.apply_defaults_dmaap_map(dm)
    assert result == {'some-config-key': {'aaf_username': None, 
        'aaf_password': None, 'dmaap_info': {'client_role': None,
            'topic_url': 'https://dcae-msrt-ftl2.com:3905/events/com.dcae.dmaap.FTL2.TommyTestTopic2', 'client_id': None, 'location': None},
        'type': 'message_router'}}


def test_update_delivery_urls():
    def get_route_with_slash(config_key):
        return "/eden"

    dmaap_map = {"spade-key": {"type": "data_router", "dmaap_info": {"delivery_url": "bleh","username": "dolittle"}},
            "clover-key": {"type": "data_router", "dmaap_info": {"publish_url": "manyfoos",
                "username": "chickenlittle"}}}

    dmaap_map = dmaap.update_delivery_urls(get_route_with_slash, "http://some-host.io", dmaap_map)

    expected = {'spade-key': {"type": "data_router", 'dmaap_info': {'delivery_url': 'http://some-host.io/eden', 
        'username': 'dolittle'}}, 'clover-key': {"type": "data_router", 'dmaap_info': {'publish_url': 'manyfoos', 
            'username': 'chickenlittle'}}}
    assert expected == dmaap_map

    def get_route_no_slash(config_key):
        return "eden"

    dmaap_map = dmaap.update_delivery_urls(get_route_no_slash, "http://some-host.io", dmaap_map)
    assert expected == dmaap_map

    # Case when there is nothing to update
    dmaap_map = {"clover-key": {"type": "data_router", "dmaap_info": {"publish_url": "manyfoos",
                "username": "chickenlittle"}}}

    assert dmaap_map == dmaap.update_delivery_urls(get_route_no_slash, "http://some-host.io", 
            dmaap_map)


def test_list_delivery_urls():
    dmaap_map = {"spade-key": {"type": "data_router", "dmaap_info": {"delivery_url": "bleh","username": "dolittle"}},
            "clover-key": {"type": "data_router", "dmaap_info": {"publish_url": "manyfoos",
                "username": "chickenlittle"}}}

    result = dmaap.list_delivery_urls(dmaap_map)
    assert result == [('spade-key', 'bleh')]