forked from apel/ssm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_brokers.py
131 lines (106 loc) · 5.01 KB
/
test_brokers.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
# Copyright 2019 UK Research and Innovation
#
# 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.
from __future__ import print_function
import unittest
import mock
from ssm import brokers
class Test(unittest.TestCase):
def test_parse_stomp_url(self):
wrong_url = 'this is not a correct url'
try:
brokers.parse_stomp_url(wrong_url)
self.fail('Appeared to parse a fake URL.')
except (IndexError, ValueError):
# Expected exception
pass
http_url = 'http://not.a.stomp.url:8080'
self.assertRaises(ValueError, brokers.parse_stomp_url, http_url)
self.assertRaises(ValueError, brokers.parse_stomp_url,
'stomp://invalid.port.number:abc')
stomp_url = 'stomp://stomp.cern.ch:6262'
try:
brokers.parse_stomp_url(stomp_url)
except Exception:
self.fail('Could not parse a valid stomp URL: %s' % stomp_url)
stomp_ssl_url = 'stomp+ssl://stomp.cern.ch:61262'
try:
brokers.parse_stomp_url(stomp_ssl_url)
except Exception:
self.fail('Could not parse a valid stomp+ssl URL: %s' % stomp_url)
def test_fetch_brokers(self):
"""Check the handling of responses from a mocked BDII."""
bdii = 'ldap://no-bdii.utopia.ch:2170'
network = 'PROD'
sbg = brokers.StompBrokerGetter(bdii)
# So that there are no external LDAP calls, mock out the LDAP seach.
with mock.patch('ldap.ldapobject.SimpleLDAPObject.search_s',
side_effect=self._mocked_search):
bs = sbg.get_broker_hosts_and_ports(brokers.STOMP_SERVICE, network)
if len(bs) < 1:
self.fail('No brokers found in the BDII.')
host, port = bs[0]
if not str(port).isdigit():
self.fail('Got a non-integer port from fetch_brokers()')
if '.' not in host:
self.fail("Didn't get a hostname from fetch_brokers()")
# Check that no brokers are returned from the TEST-NWOB network.
test_network = 'TEST-NWOB'
# So that there are no external LDAP calls, mock out the LDAP seach.
with mock.patch('ldap.ldapobject.SimpleLDAPObject.search_s',
side_effect=self._mocked_search):
test_bs = sbg.get_broker_hosts_and_ports(brokers.STOMP_SERVICE,
test_network)
self.assertEqual(len(test_bs), 0, "Test brokers found in error.")
def _mocked_search(*args, **kwargs):
"""Return values to mocked search call based on input."""
if (
'(&(objectClass=GlueService)(GlueServiceType=msg.broker.stomp))'
) in args:
return [(
'GlueServiceUniqueID=mq.cro-ngi.hr_msg.broker.stomp_3523291347'
',Mds-Vo-name=egee.srce.hr,Mds-Vo-name=local,o=grid',
{'GlueServiceUniqueID':
['mq.cro-ngi.hr_msg.broker.stomp_3523291347'],
'GlueServiceEndpoint': ['stomp://mq.cro-ngi.hr:6163/']}),
(
'GlueServiceUniqueID=broker-prod1.argo.grnet.gr_msg.broker.sto'
'mp_175215210,Mds-Vo-name=HG-06-EKT,Mds-Vo-name=local,o=grid',
{'GlueServiceUniqueID':
['broker-prod1.argo.grnet.gr_msg.broker.stomp_175215210'],
'GlueServiceEndpoint':
['stomp://broker-prod1.argo.grnet.gr:6163/']}
)]
elif (
'(&(GlueServiceDataKey=cluster)(GlueChunkKey=GlueServiceUniqueID='
'mq.cro-ngi.hr_msg.broker.stomp_3523291347))'
) in args:
return [(
'GlueServiceDataKey=cluster,GlueServiceUniqueID=mq.cro-ngi.hr_'
'msg.broker.stomp_3523291347,Mds-Vo-name=egee.srce.hr,Mds-Vo-n'
'ame=local,o=grid', {'GlueServiceDataValue': ['PROD']}
)]
elif (
'(&(GlueServiceDataKey=cluster)(GlueChunkKey=GlueServiceUniqueID='
'broker-prod1.argo.grnet.gr_msg.broker.stomp_175215210))'
) in args:
return [(
'GlueServiceDataKey=cluster,GlueServiceUniqueID=broker-prod1.a'
'rgo.grnet.gr_msg.broker.stomp_175215210,Mds-Vo-name=HG-06-EKT'
',Mds-Vo-name=local,o=grid', {'GlueServiceDataValue': ['PROD']}
)]
else:
# This will tell mock to use the normal return value
return mock.DEFAULT
if __name__ == '__main__':
unittest.main()