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
|
# 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 six, json, logging
# http://stackoverflow.com/questions/9623114/check-if-two-unordered-lists-are-equal
from collections import Counter
from functools import partial
import pytest
import requests
from discovery_client import discovery as dis
def test_get_connection_types():
config = { "x": "say something", "y": 123, "z": "{{some-analytics}}" }
expected = [(("z", ), "some-analytics"), ]
actual = dis._get_connection_types(config)
assert Counter(expected) == Counter(actual)
# Whitespaces ok
config = { "x": "say something", "y": 123, "z": "{{ some-analytics }}" }
expected = [(("z", ), "some-analytics"), ]
actual = dis._get_connection_types(config)
assert Counter(expected) == Counter(actual)
# Paul wanted the ability to include version so match on more than just one
# subfield
config = { "x": "say something", "y": 123, "z": "{{1-0-0.some-analytics}}" }
expected = [(("z", ), "1-0-0.some-analytics"), ]
actual = dis._get_connection_types(config)
assert Counter(expected) == Counter(actual)
# Need double parantheses
config = { "x": "say something", "y": 123, "z": "{some-analytics}" }
actual = dis._get_connection_types(config)
assert Counter([]) == Counter(actual)
# Nested in dict dict
config = { "x": "say something", "y": 123,
"z": { "aa": { "bbb": "{{some-analytics}}" } } }
expected = [(("z", "aa", "bbb"), "some-analytics"), ]
actual = dis._get_connection_types(config)
assert Counter(expected) == Counter(actual)
# Nested in list dict
config = { "x": "say something", "y": 123,
"z": [ "no-op", { "bbb": "{{some-analytics}}" } ] }
expected = [(("z", 1, "bbb"), "some-analytics"), ]
actual = dis._get_connection_types(config)
assert Counter(expected) == Counter(actual)
# Force strings to be unicode, test for Python2 compatibility
config = { "x": six.u("say something"), "y": 123,
"z": six.u("{{some-analytics}}") }
expected = [(("z", ), "some-analytics"), ]
actual = dis._get_connection_types(config)
assert Counter(expected) == Counter(actual)
def test_resolve_connection_types():
upstream = "b243b0b8-8a24-4f88-add7-9b530c578149.laika.foobar.rework-central.dcae.ecomp.com"
downstream = "839b0b31-f13d-4bfc-9adf-450d34071304.laika.foobar.rework-central.dcae.ecomp.com"
connection_types = [("downstream-laika", "laika"),]
relationships = [downstream]
expected = [("downstream-laika", [downstream])]
actual = dis._resolve_connection_types(upstream, connection_types, relationships)
assert sorted(actual) == sorted(expected)
# NOTE: Removed test that tested the scenario where the name stems don't
# match up. This name stem matching was causing grief to others so lifted the
# constraint.
def test_resolve_name_for_platform():
def fake_lookup(fixture, service_name):
if service_name == fixture["ServiceName"]:
return [fixture]
# Good case. Grabbed from Consul call
fixture = { 'Node': 'agent-one', 'ModifyIndex': 2892, 'Address': '127.0.0.1',
'ServiceName': 'e9526e08-f9b8-42c4-99a3-443cd4deeac1.platform-laika.foobar.rework-central.dcae.ecomp.com',
'ServicePort': 12708, 'CreateIndex': 2825, 'ServiceAddress': '196.207.143.67',
'ServiceTags': [], 'ServiceEnableTagOverride': False,
'ServiceID': 'e9526e08-f9b8-42c4-99a3-443cd4deeac1.platform-laika.foobar.rework-central.dcae.ecomp.com'}
expected = ["{0}:{1}".format(fixture["ServiceAddress"],
fixture["ServicePort"])]
assert dis._resolve_name(partial(fake_lookup, fixture), fixture["ServiceName"]) == expected
# Fail case. When Registrator is misconfigured and ServiceAddress is not set
fixture = { 'Node': 'agent-one', 'ModifyIndex': 2892, 'Address': '127.0.0.1',
'ServiceName': 'e9526e08-f9b8-42c4-99a3-443cd4deeac1.platform-laika.foobar.rework-central.dcae.ecomp.com',
'ServicePort': 12708, 'CreateIndex': 2825, 'ServiceAddress': '',
'ServiceTags': [], 'ServiceEnableTagOverride': False,
'ServiceID': 'e9526e08-f9b8-42c4-99a3-443cd4deeac1.platform-laika.foobar.rework-central.dcae.ecomp.com'}
with pytest.raises(dis.DiscoveryResolvingNameError):
dis._resolve_name(partial(fake_lookup, fixture), fixture["ServiceName"])
# Fail case. When lookup just blows up for some reason
def fake_lookup_blows(service_name):
raise RuntimeError("Thar she blows")
with pytest.raises(dis.DiscoveryResolvingNameError):
dis._resolve_name(fake_lookup_blows, fixture["ServiceName"])
def test_resolve_name_for_docker():
def fake_lookup(fixture, service_name):
if service_name == fixture["ServiceName"]:
return [fixture]
# Good case. Grabbed from Consul call
fixture = { 'Node': 'agent-one', 'ModifyIndex': 2892, 'Address': '127.0.0.1',
'ServiceName': 'e9526e08-f9b8-42c4-99a3-443cd4deeac1.laika.foobar.rework-central.dcae.ecomp.com',
'ServicePort': 12708, 'CreateIndex': 2825, 'ServiceAddress': '196.207.143.67',
'ServiceTags': [], 'ServiceEnableTagOverride': False,
'ServiceID': 'e9526e08-f9b8-42c4-99a3-443cd4deeac1.laika.foobar.rework-central.dcae.ecomp.com'}
expected = ["{0}:{1}".format(fixture["ServiceAddress"],
fixture["ServicePort"])]
assert dis._resolve_name(partial(fake_lookup, fixture), fixture["ServiceName"]) == expected
# Fail case. When Registrator is misconfigured and ServiceAddress is not set
fixture = { 'Node': 'agent-one', 'ModifyIndex': 2892, 'Address': '127.0.0.1',
'ServiceName': 'e9526e08-f9b8-42c4-99a3-443cd4deeac1.laika.foobar.rework-central.dcae.ecomp.com',
'ServicePort': 12708, 'CreateIndex': 2825, 'ServiceAddress': '',
'ServiceTags': [], 'ServiceEnableTagOverride': False,
'ServiceID': 'e9526e08-f9b8-42c4-99a3-443cd4deeac1.laika.foobar.rework-central.dcae.ecomp.com'}
with pytest.raises(dis.DiscoveryResolvingNameError):
dis._resolve_name(partial(fake_lookup, fixture), fixture["ServiceName"])
# Fail case. When lookup just blows up for some reason
def fake_lookup_blows(service_name):
raise RuntimeError("Thar she blows")
with pytest.raises(dis.DiscoveryResolvingNameError):
dis._resolve_name(fake_lookup_blows, fixture["ServiceName"])
def test_resolve_name_for_cdap(monkeypatch):
def fake_lookup(fixture, service_name):
if service_name == fixture["ServiceName"]:
return [fixture]
# Good case. Handle CDAP apps
fixture = {
"Node":"agent-one", "Address":"10.170.2.17",
"ServiceID": "00b6210b71e445cdaadf76e620ebffcfhelloworldcdapappfoobardcaereworkdcaeecompcom",
"ServiceName": "00b6210b71e445cdaadf76e620ebffcfhelloworldcdapappfoobardcaereworkdcaeecompcom",
"ServiceTags":[],
"ServiceAddress": "196.207.143.116",
"ServicePort": 7777, "ServiceEnableTagOverride": False, "CreateIndex": 144733, "ModifyIndex":145169 }
class FakeRequestsResponse(object):
def __init__(self, url, broker_json):
self.url = url
self.broker_json = broker_json
def raise_for_status(self):
expected_broker_url = "http://{0}:{1}/application/{2}".format(
fixture["ServiceAddress"], fixture["ServicePort"],
fixture["ServiceName"])
if self.url == expected_broker_url:
return True
else:
raise RuntimeError("Mismatching address")
def json(self):
return self.broker_json
# Simulate the call to the CDAP broker
broker_json = {
"appname":"00b6210b71e445cdaadf76e620ebffcfhelloworldcdapappfoobardcaereworkdcaeecompcom",
"healthcheckurl":"http://196.207.143.116:7777/application/00b6210b71e445cdaadf76e620ebffcfhelloworldcdapappfoobardcaereworkdcaeecompcom/healthcheck",
"metricsurl":"http://196.207.143.116:7777/application/00b6210b71e445cdaadf76e620ebffcfhelloworldcdapappfoobardcaereworkdcaeecompcom/metrics",
"url":"http://196.207.143.116:7777/application/00b6210b71e445cdaadf76e620ebffcfhelloworldcdapappfoobardcaereworkdcaeecompcom",
"connectionurl":"http://196.207.160.159:10000/v3/namespaces/default/streams/foo",
"serviceendpoints": "something" }
monkeypatch.setattr(requests, "get", lambda url: FakeRequestsResponse(url, broker_json))
expected = [{ key: broker_json[key]
for key in ["connectionurl", "serviceendpoints"] }]
assert dis._resolve_name(partial(fake_lookup, fixture), fixture["ServiceName"]) == expected
def test_resolve_configuration_dict(monkeypatch):
service_name = "123.current-node-type.some-service.some-location.com"
target_service_name = "456.target-node-type.some-service.some-location.com"
# Fake the Consul calls
def fake_get_relationship(ch, service_name):
return [ target_service_name ]
monkeypatch.setattr(dis, "_get_relationships_from_consul",
fake_get_relationship)
fixture = [{ 'Node': 'agent-one', 'ModifyIndex': 2892, 'Address': '127.0.0.1',
'ServiceName': target_service_name,
'ServicePort': 12708, 'CreateIndex': 2825, 'ServiceAddress': '196.207.143.67',
'ServiceTags': [], 'ServiceEnableTagOverride': False,
'ServiceID': target_service_name }]
def fake_lookup(ch, service_name):
return fixture
monkeypatch.setattr(dis, "_lookup_with_consul", fake_lookup)
# Simple config case
test_config = { "target-node": "{{ target-node-type }}", "other-param": 123 }
expected = dict(test_config)
expected["target-node"] = ["196.207.143.67:12708"]
actual = dis._resolve_configuration_dict(None, service_name, test_config)
assert Counter(actual) == Counter(expected)
# Nested config case
test_config = { "output_formats": { "target-node": "{{ target-node-type }}" },
"other-param": 123 }
expected = dict(test_config)
expected["output_formats"]["target-node"] = "196.207.143.67:12708"
actual = dis._resolve_configuration_dict(None, service_name, test_config)
assert Counter(actual) == Counter(expected)
def test_get_consul_host(monkeypatch):
with pytest.raises(dis.DiscoveryInitError):
dis.get_consul_hostname()
monkeypatch.setenv("CONSUL_HOST", "i-am-consul-host")
assert "i-am-consul-host" == dis.get_consul_hostname()
assert "no-i-am" == dis.get_consul_hostname("no-i-am")
|