77
81
class OpenDaylightMechanismTestPortsV2(test_plugin.TestPortsV2,
78
82
OpenDaylightTestCase):
86
class AuthMatcher(object):
87
def __eq__(self, obj):
88
return (obj.username == config.cfg.CONF.ml2_odl.username and
89
obj.password == config.cfg.CONF.ml2_odl.password)
92
class OpenDaylightMechanismDriverTestCase(base.BaseTestCase):
95
super(OpenDaylightMechanismDriverTestCase, self).setUp()
96
config.cfg.CONF.set_override('mechanism_drivers',
97
['logger', 'opendaylight'], 'ml2')
98
config.cfg.CONF.set_override('url', 'http://127.0.0.1:9999', 'ml2_odl')
99
config.cfg.CONF.set_override('username', 'someuser', 'ml2_odl')
100
config.cfg.CONF.set_override('password', 'somepass', 'ml2_odl')
101
self.mech = mechanism_odl.OpenDaylightMechanismDriver()
102
self.mech.initialize()
105
def _get_mock_delete_resource_context():
106
current = {'id': '00000000-1111-2222-3333-444444444444'}
107
context = mock.Mock(current=current)
110
_status_code_msgs = {
112
401: '401 Client Error: Unauthorized',
113
403: '403 Client Error: Forbidden',
114
404: '404 Client Error: Not Found',
115
409: '409 Client Error: Conflict',
116
501: '501 Server Error: Not Implemented'
120
def _get_mock_request_response(cls, status_code):
121
response = mock.Mock(status_code=status_code)
122
response.raise_for_status = mock.Mock() if status_code < 400 else (
123
mock.Mock(side_effect=requests.exceptions.HTTPError(
124
cls._status_code_msgs[status_code])))
127
def _test_delete_resource_postcommit(self, object_type, status_code,
129
self.mech.out_of_sync = False
130
method = getattr(self.mech, 'delete_%s_postcommit' % object_type)
131
context = self._get_mock_delete_resource_context()
132
request_response = self._get_mock_request_response(status_code)
133
with mock.patch('requests.request',
134
return_value=request_response) as mock_method:
135
if exc_class is not None:
136
self.assertRaises(exc_class, method, context)
139
url = '%s/%ss/%s' % (config.cfg.CONF.ml2_odl.url, object_type,
140
context.current['id'])
141
mock_method.assert_called_once_with(
142
'delete', url=url, headers={'Content-Type': 'application/json'},
143
data=None, auth=AuthMatcher(),
144
timeout=config.cfg.CONF.ml2_odl.timeout)
146
def test_delete_network_postcommit(self):
147
self._test_delete_resource_postcommit('network',
148
requests.codes.no_content)
149
for status_code in (requests.codes.unauthorized,
150
requests.codes.not_found,
151
requests.codes.conflict):
152
self._test_delete_resource_postcommit(
153
'network', status_code, requests.exceptions.HTTPError)
155
def test_delete_subnet_postcommit(self):
156
self._test_delete_resource_postcommit('subnet',
157
requests.codes.no_content)
158
for status_code in (requests.codes.unauthorized,
159
requests.codes.not_found,
160
requests.codes.conflict,
161
requests.codes.not_implemented):
162
self._test_delete_resource_postcommit(
163
'subnet', status_code, requests.exceptions.HTTPError)
165
def test_delete_port_postcommit(self):
166
self._test_delete_resource_postcommit('port',
167
requests.codes.no_content)
168
for status_code in (requests.codes.unauthorized,
169
requests.codes.forbidden,
170
requests.codes.not_found,
171
requests.codes.not_implemented):
172
self._test_delete_resource_postcommit(
173
'port', status_code, requests.exceptions.HTTPError)