def do_single_request(self, method, action, body=None): action = cfg.CONF.OFC.path_prefix + action LOG.debug("Client request: %(host)s:%(port)s " "%(method)s %(action)s [%(body)s]", {'host': self.host, 'port': self.port, 'method': method, 'action': action, 'body': body}) if isinstance(body, dict): body = jsonutils.dumps(body) try: res = self._get_response(method, action, body) data = res.text LOG.debug("OFC returns [%(status)s:%(data)s]", {'status': res.status_code, 'data': data}) # Try to decode JSON data if possible. try: data = jsonutils.loads(data) except (ValueError, TypeError): pass if res.status_code in (requests.codes.OK, requests.codes.CREATED, requests.codes.ACCEPTED, requests.codes.NO_CONTENT): return data elif res.status_code == requests.codes.SERVICE_UNAVAILABLE: retry_after = res.headers.get('retry-after') LOG.warning(_LW("OFC returns ServiceUnavailable " "(retry-after=%s)"), retry_after) raise nexc.OFCServiceUnavailable(retry_after=retry_after) elif res.status_code == requests.codes.NOT_FOUND: LOG.info(_LI("Specified resource %s does not exist on OFC "), action) raise nexc.OFCResourceNotFound(resource=action) else: LOG.warning(_LW("Operation on OFC failed: " "status=%(status)s, detail=%(detail)s"), {'status': res.status_code, 'detail': data}) params = {'reason': _("Operation on OFC failed"), 'status': res.status_code} if isinstance(data, dict): params['err_code'] = data.get('err_code') params['err_msg'] = data.get('err_msg') else: params['err_msg'] = data raise nexc.OFCException(**params) except requests.exceptions.RequestException as e: reason = _("Failed to connect OFC : %s") % e LOG.error(reason) raise nexc.OFCException(reason=reason)
def test_delete_port_with_ofc_deletion_failure(self): self.ofc.set_raise_exc('delete_ofc_port', nexc.OFCException(reason='hoge')) with self.port() as port: port_id = port['port']['id'] portinfo = {'id': port_id, 'port_no': 123} self.rpcapi_update_ports(added=[portinfo]) self._delete('ports', port_id, expected_code=webob.exc.HTTPInternalServerError.code) port_ref = self._show('ports', port_id) self.assertEqual(port_ref['port']['status'], 'ERROR') self.ofc.set_raise_exc('delete_ofc_port', None) self._delete('ports', port['port']['id']) ctx = mock.ANY port = mock.ANY expected = [ mock.call.exists_ofc_port(ctx, port_id), mock.call.create_ofc_port(ctx, port_id, port), mock.call.exists_ofc_port(ctx, port_id), mock.call.delete_ofc_port(ctx, port_id, port), mock.call.exists_ofc_port(ctx, port_id), mock.call.delete_ofc_port(ctx, port_id, port) ] self.ofc.assert_has_calls(expected) self.assertEqual(self.ofc.delete_ofc_port.call_count, 2)
def test_delete_network_with_ofc_deletion_failure(self): self.ofc.set_raise_exc('delete_ofc_network', nexc.OFCException(reason='hoge')) with self.network() as net: net_id = net['network']['id'] self._delete('networks', net_id, expected_code=webob.exc.HTTPInternalServerError.code) net_ref = self._show('networks', net_id) self.assertEqual(net_ref['network']['status'], 'ERROR') self.ofc.set_raise_exc('delete_ofc_network', None) self._delete('networks', net['network']['id']) ctx = mock.ANY tenant = mock.ANY net_name = mock.ANY net = mock.ANY expected = [ mock.call.create_ofc_network(ctx, tenant, net_id, net_name), mock.call.exists_ofc_network(ctx, net_id), mock.call.delete_ofc_network(ctx, net_id, net), mock.call.exists_ofc_network(ctx, net_id), mock.call.delete_ofc_network(ctx, net_id, net), ] self.ofc.assert_has_calls(expected) self.assertEqual(self.ofc.delete_ofc_network.call_count, 2)
def test_delete_pf_with_ofc_deletion_failure(self): self.ofc.set_raise_exc('delete_ofc_packet_filter', nexc.OFCException(reason='hoge')) with self.packet_filter_on_network() as pf: pf_id = pf['packet_filter']['id'] self._delete('packet_filters', pf_id, expected_code=webob.exc.HTTPInternalServerError.code) pf_ref = self._show('packet_filters', pf_id) self.assertEqual(pf_ref['packet_filter']['status'], 'ERROR') self.ofc.set_raise_exc('delete_ofc_packet_filter', None) # Then, self._delete('packet_filters', pf_id) will success. ctx = mock.ANY pf_dict = mock.ANY expected = [ mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.create_ofc_packet_filter(ctx, pf_id, pf_dict), mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.delete_ofc_packet_filter(ctx, pf_id), mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.delete_ofc_packet_filter(ctx, pf_id), ] self.ofc.assert_has_calls(expected) self.assertEqual(self.ofc.delete_ofc_packet_filter.call_count, 2)
def test_activate_pf_while_exists_on_ofc(self): ctx = mock.ANY with self.packet_filter_on_network() as pf: pf_id = pf['packet_filter']['id'] self.ofc.set_raise_exc('delete_ofc_packet_filter', nexc.OFCException(reason='hoge')) # This update request will make plugin reactivate pf. data = {'packet_filter': {'priority': 1000}} self._update('packet_filters', pf_id, data, expected_code=webob.exc.HTTPInternalServerError.code) self.ofc.set_raise_exc('delete_ofc_packet_filter', None) ctx = mock.ANY pf_dict = mock.ANY expected = [ mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.create_ofc_packet_filter(ctx, pf_id, pf_dict), mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.delete_ofc_packet_filter(ctx, pf_id), mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.delete_ofc_packet_filter(ctx, pf_id), ] self.ofc.assert_has_calls(expected) self.assertEqual(self.ofc.delete_ofc_packet_filter.call_count, 2)
def test_create_pf_with_ofc_creation_failure(self): self.ofc.set_raise_exc('create_ofc_packet_filter', nexc.OFCException(reason='hoge')) with self.packet_filter_on_network() as pf: pf_id = pf['packet_filter']['id'] pf_ref = self._show('packet_filters', pf_id) self.assertEqual(pf_ref['packet_filter']['status'], 'ERROR') self.ofc.set_raise_exc('create_ofc_packet_filter', None) # Retry activate packet_filter (even if there is no change). data = {'packet_filter': {}} self._update('packet_filters', pf_id, data) pf_ref = self._show('packet_filters', pf_id) self.assertEqual(pf_ref['packet_filter']['status'], 'ACTIVE') ctx = mock.ANY pf_dict = mock.ANY expected = [ mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.create_ofc_packet_filter(ctx, pf_id, pf_dict), mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.create_ofc_packet_filter(ctx, pf_id, pf_dict), ] self.ofc.assert_has_calls(expected) self.assertEqual(self.ofc.create_ofc_packet_filter.call_count, 2)
def test_delete_pf_with_error_status(self): self.ofc.set_raise_exc('create_ofc_packet_filter', nexc.OFCException(reason='fake')) with self.packet_filter_on_network() as pf: pf_id = pf['packet_filter']['id'] pf_ref = self._show('packet_filters', pf_id) self.assertEqual(pf_ref['packet_filter']['status'], 'ERROR') ctx = mock.ANY pf_dict = mock.ANY expected = [ mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.create_ofc_packet_filter(ctx, pf_id, pf_dict), mock.call.exists_ofc_packet_filter(ctx, pf_id), ] self.ofc.assert_has_calls(expected) self.assertEqual(1, self.ofc.create_ofc_packet_filter.call_count) self.assertEqual(0, self.ofc.delete_ofc_packet_filter.call_count)
def test_delete_network_with_deactivating_auto_delete_port_failure(self): self.ofc.set_raise_exc('delete_ofc_port', nexc.OFCException(reason='hoge')) with self.network() as net: net_id = net['network']['id'] device_owner = db_base_plugin_v2.AUTO_DELETE_PORT_OWNERS[0] port = self._make_port(self.fmt, net_id, device_owner=device_owner) port_id = port['port']['id'] portinfo = {'id': port_id, 'port_no': 123} self.rpcapi_update_ports(added=[portinfo]) self._delete('networks', net_id, expected_code=webob.exc.HTTPInternalServerError.code) net_ref = self._show('networks', net_id) self.assertEqual(net_ref['network']['status'], 'ACTIVE') port_ref = self._show('ports', port_id) self.assertEqual(port_ref['port']['status'], 'ERROR') self.ofc.set_raise_exc('delete_ofc_port', None) self._delete('networks', net_id) ctx = mock.ANY tenant = mock.ANY net_name = mock.ANY net = mock.ANY port = mock.ANY expected = [ mock.call.create_ofc_network(ctx, tenant, net_id, net_name), mock.call.exists_ofc_port(ctx, port_id), mock.call.create_ofc_port(ctx, port_id, port), mock.call.exists_ofc_port(ctx, port_id), mock.call.delete_ofc_port(ctx, port_id, port), mock.call.exists_ofc_port(ctx, port_id), mock.call.delete_ofc_port(ctx, port_id, port), mock.call.exists_ofc_network(ctx, net_id), mock.call.delete_ofc_network(ctx, net_id, net) ] self.ofc.assert_has_calls(expected) self.assertEqual(self.ofc.delete_ofc_network.call_count, 1)
def test_update_port_with_ofc_creation_failure(self): with self.port(admin_state_up=False) as port: port_id = port['port']['id'] portinfo = {'id': port_id, 'port_no': 123} self.rpcapi_update_ports(added=[portinfo]) self.ofc.set_raise_exc('create_ofc_port', nexc.OFCException(reason='hoge')) body = {'port': {'admin_state_up': True}} res = self._update('ports', port_id, body) self.assertEqual(res['port']['status'], 'ERROR') port_ref = self._show('ports', port_id) self.assertEqual(port_ref['port']['status'], 'ERROR') body = {'port': {'admin_state_up': False}} res = self._update('ports', port_id, body) self.assertEqual(res['port']['status'], 'ERROR') port_ref = self._show('ports', port_id) self.assertEqual(port_ref['port']['status'], 'ERROR') self.ofc.set_raise_exc('create_ofc_port', None) body = {'port': {'admin_state_up': True}} res = self._update('ports', port_id, body) self.assertEqual(res['port']['status'], 'ACTIVE') port_ref = self._show('ports', port_id) self.assertEqual(port_ref['port']['status'], 'ACTIVE') self._delete('ports', port['port']['id']) ctx = mock.ANY port = mock.ANY expected = [ mock.call.exists_ofc_port(ctx, port_id), mock.call.create_ofc_port(ctx, port_id, port), mock.call.exists_ofc_port(ctx, port_id), mock.call.exists_ofc_port(ctx, port_id), mock.call.create_ofc_port(ctx, port_id, port), mock.call.exists_ofc_port(ctx, port_id), mock.call.delete_ofc_port(ctx, port_id, port), ] self.ofc.assert_has_calls(expected) self.assertEqual(self.ofc.create_ofc_port.call_count, 2)
def create_router(self, ofc_tenant_id, router_id, description): ofc_id = "ofc-" + router_id[:-4] if self.autocheck: if ofc_tenant_id not in self.ofc_tenant_dict: raise Exception( _('(create_router) OFC tenant %s not found') % ofc_tenant_id) if ofc_id in self.ofc_router_dict: raise Exception( _('(create_router) OFC router %s ' 'already exists') % ofc_id) if len(self.ofc_router_dict) >= MAX_NUM_OPENFLOW_ROUTER: params = {'reason': _("Operation on OFC is failed"), 'status': 409} raise nexc.OFCException(**params) self.ofc_router_dict[ofc_id] = { 'tenant_id': ofc_tenant_id, 'router_id': router_id, 'description': description } return ofc_id
def test_delete_port_with_error_status(self): self.ofc.set_raise_exc('create_ofc_port', nexc.OFCException(reason='fake')) with self.port() as port: port_id = port['port']['id'] portinfo = {'id': port_id, 'port_no': 123} self.rpcapi_update_ports(added=[portinfo]) port_ref = self._show('ports', port_id) self.assertEqual(port_ref['port']['status'], 'ERROR') self._delete('ports', port['port']['id']) ctx = mock.ANY port = mock.ANY expected = [ mock.call.exists_ofc_port(ctx, port_id), mock.call.create_ofc_port(ctx, port_id, port), mock.call.exists_ofc_port(ctx, port_id), ] self.ofc.assert_has_calls(expected) self.assertFalse(self.ofc.delete_ofc_port.call_count)
def test_deactivate_pf_with_ofc_deletion_failure(self): ctx = mock.ANY with self.packet_filter_on_network() as pf: pf_id = pf['packet_filter']['id'] self.ofc.set_raise_exc('delete_ofc_packet_filter', nexc.OFCException(reason='hoge')) data = {'packet_filter': {'admin_state_up': False}} self._update('packet_filters', pf_id, data, expected_code=webob.exc.HTTPInternalServerError.code) pf_ref = self._show('packet_filters', pf_id) self.assertEqual(pf_ref['packet_filter']['status'], 'ERROR') self.ofc.set_raise_exc('delete_ofc_packet_filter', None) data = {'packet_filter': {'priority': 1000}} self._update('packet_filters', pf_id, data) pf_ref = self._show('packet_filters', pf_id) self.assertEqual(pf_ref['packet_filter']['status'], 'DOWN') ctx = mock.ANY pf_dict = mock.ANY expected = [ mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.create_ofc_packet_filter(ctx, pf_id, pf_dict), mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.delete_ofc_packet_filter(ctx, pf_id), mock.call.exists_ofc_packet_filter(ctx, pf_id), mock.call.delete_ofc_packet_filter(ctx, pf_id), mock.call.exists_ofc_packet_filter(ctx, pf_id), ] self.ofc.assert_has_calls(expected) self.assertEqual(self.ofc.delete_ofc_packet_filter.call_count, 2)
def test_delete_network_with_error_status(self): self.ofc.set_raise_exc('create_ofc_network', nexc.OFCException(reason='fake error')) with self.network() as net: net_id = net['network']['id'] net_ref = self._show('networks', net_id) self.assertEqual(net_ref['network']['status'], 'ERROR') self._delete('networks', net['network']['id']) ctx = mock.ANY tenant_id = self._tenant_id net_name = mock.ANY net = mock.ANY expected = [ mock.call.exists_ofc_tenant(ctx, tenant_id), mock.call.create_ofc_tenant(ctx, tenant_id), mock.call.create_ofc_network(ctx, tenant_id, net_id, net_name), mock.call.exists_ofc_network(ctx, net_id), mock.call.exists_ofc_tenant(ctx, tenant_id), mock.call.delete_ofc_tenant(ctx, tenant_id), ] self.ofc.assert_has_calls(expected) self.assertFalse(self.ofc.delete_ofc_network.call_count)
def test_create_network_fail(self): self.ofc.create_ofc_network.side_effect = nexc.OFCException( reason='hoge') net = None ctx = mock.ANY # NOTE: We don't delete network through api, but db will be cleaned in # tearDown(). When OFCManager has failed to create a network on OFC, # it does not keeps ofc_network entry and will fail to delete this # network from OFC. Deletion of network is not the scope of this test. with self.network() as network: net = network['network'] self.assertEqual(net['status'], 'ERROR') net_ref = self._show('networks', net['id']) self.assertEqual(net_ref['network']['status'], 'ERROR') expected = [ mock.call.exists_ofc_tenant(ctx, self._tenant_id), mock.call.create_ofc_tenant(ctx, self._tenant_id), mock.call.create_ofc_network(ctx, self._tenant_id, net['id'], net['name']) ] self.ofc.assert_has_calls(expected)