-
Notifications
You must be signed in to change notification settings - Fork 390
/
test_dsa.py
327 lines (279 loc) · 12.4 KB
/
test_dsa.py
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
import sys
import gevent
import uuid
sys.path.append("../config/common/tests")
from testtools.matchers import Equals, Contains, Not
from test_utils import *
import test_common
import test_case
from vnc_api.vnc_api import *
from vnc_api.gen.resource_xsd import *
from cfgm_common.exceptions import *
from gevent import sleep
import discoveryclient.client as client
EP_DELIM=','
PUBSUB_DELIM=' '
def parse_pubsub_ep(pubsub_str):
r = pubsub_str.split(EP_DELIM)
if len(r) < 4:
for i in range(4-len(r)):
r.append('')
return r
# '1.1.1.1/24' or '1.1.1.1'
def prefix_str_to_obj(prefix_str):
if '/' not in prefix_str:
prefix_str += '/32'
x = prefix_str.split('/')
if len(x) != 2:
return None
return SubnetType(x[0], int(x[1]))
def build_dsa_rule_entry(rule_str):
r = parse_pubsub_ep(rule_str)
r = rule_str.split(PUBSUB_DELIM) if rule_str else []
if len(r) < 2:
return None
# [0] is publisher-spec, [1] is subscriber-spec
pubspec = parse_pubsub_ep(r[0])
subspec = parse_pubsub_ep(r[1])
pfx_pub = prefix_str_to_obj(pubspec[0])
pfx_sub = prefix_str_to_obj(subspec[0])
if pfx_sub is None or pfx_sub is None:
return None
publisher = DiscoveryPubSubEndPointType(ep_prefix = pfx_pub,
ep_type = pubspec[1], ep_id = pubspec[2],
ep_version = pubspec[3])
subscriber = [DiscoveryPubSubEndPointType(ep_prefix = pfx_sub,
ep_type = subspec[1], ep_id = subspec[2],
ep_version = subspec[3])]
dsa_rule_entry = DiscoveryServiceAssignmentType(publisher, subscriber)
return dsa_rule_entry
def info_callback(info):
print 'In subscribe callback handler'
print '%s' % (info)
pass
class TestDsa(test_case.DsTestCase):
def test_bug_1548771(self):
dsa = DiscoveryServiceAssignment()
rule_entry = build_dsa_rule_entry('77.77.3.0/24,xmpp-server 77.77.0.0/16,contrail-vrouter-agent:0')
rule_uuid = uuid.uuid4()
dsa_rule1 = DsaRule(name = str(rule_uuid), parent_obj = dsa, dsa_rule_entry = rule_entry)
dsa_rule1.set_uuid(str(rule_uuid))
self._vnc_lib.dsa_rule_create(dsa_rule1)
rule_entry = build_dsa_rule_entry('77.77.3.0/24,dns-server 77.77.3.11/32,contrail-vrouter-agent:0')
rule_uuid = uuid.uuid4()
dsa_rule2 = DsaRule(name = str(rule_uuid), parent_obj = dsa, dsa_rule_entry = rule_entry)
dsa_rule2.set_uuid(str(rule_uuid))
self._vnc_lib.dsa_rule_create(dsa_rule2)
puburl = '/publish'
suburl = "/subscribe"
# publish 3 control nodes and dns servers
for service_type in ['xmpp-server', 'dns-server']:
for ipaddr in ["77.77.1.10", "77.77.2.10", "77.77.3.10"]:
payload = {
service_type: { "ip-addr" : ipaddr, "port" : "1111" },
'service-type' : '%s' % service_type,
'service-id' : '%s-%s' % (service_type, ipaddr),
'remote-addr': ipaddr,
}
(code, msg) = self._http_post(puburl, json.dumps(payload))
self.assertEqual(code, 200)
# Verify all services are published.
(code, msg) = self._http_get('/services.json')
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response['services']), 6)
# verify all agents see only 1 xmpp-server (rule #1)
service_type = 'xmpp-server'
expectedpub_set = set(["xmpp-server-77.77.3.10"])
for ipaddr in ["77.77.1.11", "77.77.2.11", "77.77.3.11"]:
payload = {
'service' : '%s' % service_type,
'client' : '%s-%s' % (service_type, ipaddr),
'instances' : 2,
'client-type' : 'contrail-vrouter-agent:0',
'remote-addr' : ipaddr,
}
(code, msg) = self._http_post(suburl, json.dumps(payload))
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response[service_type]), 1)
receivedpub_set = set([svc['@publisher-id'] for svc in response[service_type]])
self.assertEqual(expectedpub_set == receivedpub_set, True)
self._vnc_lib.dsa_rule_delete(id = dsa_rule1.get_uuid())
self._vnc_lib.dsa_rule_delete(id = dsa_rule2.get_uuid())
def test_bug_1540777(self):
dsa = DiscoveryServiceAssignment()
rule_entry = build_dsa_rule_entry('77.77.3.10/32,pulkit-pub 77.77.3.11/32,pulkit-sub')
rule_uuid = uuid.uuid4()
dsa_rule1 = DsaRule(name = str(rule_uuid), parent_obj = dsa, dsa_rule_entry = rule_entry)
dsa_rule1.set_uuid(str(rule_uuid))
self._vnc_lib.dsa_rule_create(dsa_rule1)
rule_entry = build_dsa_rule_entry('77.77.2.10/32,pulkit-pub 77.77.3.11/32,pulkit-sub')
rule_uuid = uuid.uuid4()
dsa_rule2 = DsaRule(name = str(rule_uuid), parent_obj = dsa, dsa_rule_entry = rule_entry)
dsa_rule2.set_uuid(str(rule_uuid))
self._vnc_lib.dsa_rule_create(dsa_rule2)
puburl = '/publish'
suburl = "/subscribe"
service_type = 'pulkit-pub'
# publish 3 control nodes - 2 subject to rules above
for ipaddr in ["77.77.1.10", "77.77.2.10", "77.77.3.10"]:
payload = {
service_type: { "ip-addr" : ipaddr, "port" : "1111" },
'service-type' : '%s' % service_type,
'service-id' : 'pulkit-pub-%s' % ipaddr,
'remote-addr': ipaddr,
}
(code, msg) = self._http_post(puburl, json.dumps(payload))
self.assertEqual(code, 200)
payload = {
'service' : '%s' % service_type,
'client' : 'discovery-ut',
'instances' : 3,
'client-type' : 'pulkit-sub',
'remote-addr' : '77.77.3.11',
}
# should see 2 publishers due to two rules
(code, msg) = self._http_post(suburl, json.dumps(payload))
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response[service_type]), 2)
expectedpub_set = set(["pulkit-pub-77.77.2.10", "pulkit-pub-77.77.3.10"])
receivedpub_set = set([svc['@publisher-id'] for svc in response[service_type]])
self.assertEqual(expectedpub_set == receivedpub_set, True)
self._vnc_lib.dsa_rule_delete(id = dsa_rule1.get_uuid())
self._vnc_lib.dsa_rule_delete(id = dsa_rule2.get_uuid())
def test_dsa_config(self):
# Assign DC1 control nodes to DC1 agents
rule_entry = build_dsa_rule_entry('1.1.1.0/24,Control-Node 1.1.1.0/24,Vrouter-Agent')
dsa = DiscoveryServiceAssignment()
rule_uuid = uuid.uuid4()
dsa_rule1 = DsaRule(name = str(rule_uuid), parent_obj = dsa, dsa_rule_entry = rule_entry)
dsa_rule1.set_uuid(str(rule_uuid))
self._vnc_lib.dsa_rule_create(dsa_rule1)
# Assign DC2 control nodes to DC1 agents
rule_entry = build_dsa_rule_entry('2.2.2.0/24,Control-Node 2.2.2.0/24,Vrouter-Agent')
rule_uuid = uuid.uuid4()
dsa_rule2 = DsaRule(name = str(rule_uuid), parent_obj = dsa, dsa_rule_entry = rule_entry)
dsa_rule2.set_uuid(str(rule_uuid))
self._vnc_lib.dsa_rule_create(dsa_rule2)
puburl = '/publish'
service_type = 'Control-Node'
# publish 4 control nodes - 2 in two data centers each
payload = {
'%s' % service_type: { "ip-addr" : "1.1.1.1", "port" : "1111" },
'service-type' : '%s' % service_type,
'service-id' : 'DC1-CN1',
'remote-addr': '1.1.1.1',
}
(code, msg) = self._http_post(puburl, json.dumps(payload))
self.assertEqual(code, 200)
payload = {
'%s' % service_type: { "ip-addr" : "1.1.1.2", "port" : "1112" },
'service-type' : '%s' % service_type,
'service-id' : 'DC1-CN2',
'remote-addr': '1.1.1.2',
}
(code, msg) = self._http_post(puburl, json.dumps(payload))
self.assertEqual(code, 200)
payload = {
'%s' % service_type: { "ip-addr" : "2.2.2.1", "port" : "2221" },
'service-type' : '%s' % service_type,
'service-id' : 'DC2-CN1',
'remote-addr': '2.2.2.1',
}
(code, msg) = self._http_post(puburl, json.dumps(payload))
self.assertEqual(code, 200)
payload = {
'%s' % service_type: { "ip-addr" : "2.2.2.2", "port" : "2222" },
'service-type' : '%s' % service_type,
'service-id' : 'DC2-CN2',
'remote-addr': '2.2.2.2',
}
(code, msg) = self._http_post(puburl, json.dumps(payload))
self.assertEqual(code, 200)
# Verify all services are published.
(code, msg) = self._http_get('/services.json')
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response['services']), 4)
# json subscribe request
suburl = "/subscribe"
payload = {
'service' : '%s' % service_type,
'client' : 'DC1-VA1',
'instances' : 0,
'client-type' : 'Vrouter-Agent',
'remote-addr' : '3.3.3.3',
'version' : '2.2',
}
# should see all 4 publishers for sub that is not in DC1 or DC2
(code, msg) = self._http_post(suburl, json.dumps(payload))
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response[service_type]), 4)
# Sub in DC1 - should see only DC1 services
payload['remote-addr'] = '1.1.1.3'
(code, msg) = self._http_post(suburl, json.dumps(payload))
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response[service_type]), 2)
for svc in response[service_type]:
self.assertEqual("DC1-CN" in svc['@publisher-id'], True)
# Sub in DC2 - should see only DC2 services
payload['remote-addr'] = '2.2.2.3'
(code, msg) = self._http_post(suburl, json.dumps(payload))
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response[service_type]), 2)
for svc in response[service_type]:
self.assertEqual("DC2-CN" in svc['@publisher-id'], True)
# Subscribe to IfmapServer from DC1, DC2 and DC3. There are no
# assignment rules applicable to IfmapServer. Thus clients from
# all DC should be able to subscribe to singtleton IfmapServer
service_type = 'IfmapServer'
payload = {
service_type: { "ip-addr" : "4.4.4.4", "port" : "4444" },
'service-type' : service_type,
'service-id' : 'Controller',
'remote-addr': '4.4.4.4',
}
(code, msg) = self._http_post(puburl, json.dumps(payload))
self.assertEqual(code, 200)
payload = {
'service' : '%s' % service_type,
'client' : 'DC1-VA1',
'instances' : 0,
'client-type' : 'Vrouter-Agent',
}
for remote in ['1.1.1.1', '2.2.2.2', '3.3.3.3']:
payload['remote-addr'] = remote
(code, msg) = self._http_post(suburl, json.dumps(payload))
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response[service_type]), 1)
# Delete service assignment rule.
# Subs from any DC should see all DC1+DC2 services
self._vnc_lib.dsa_rule_delete(id = dsa_rule1.uuid)
self._vnc_lib.dsa_rule_delete(id = dsa_rule2.uuid)
service_type = 'Control-Node'
payload = {
'service' : '%s' % service_type,
'client' : 'Dont Care',
'instances' : 0,
'client-type' : 'Vrouter-Agent',
}
# Sub in DC1 or DC2 should see DC1+DC2 services
expectedpub_set = set(["DC1-CN1", "DC1-CN2", "DC2-CN1", "DC2-CN2"])
for sub_ip in ['1.1.1.3', '2.2.2.3']:
payload['remote-addr'] = sub_ip
(code, msg) = self._http_post(suburl, json.dumps(payload))
self.assertEqual(code, 200)
response = json.loads(msg)
self.assertEqual(len(response[service_type]), 4)
receivedpub_set = set([svc['@publisher-id'] for svc in response[service_type]])
self.assertEqual(expectedpub_set == receivedpub_set, True)
#end class TestDsa