def create_resources(self, _type, ctx, body): if _type == constants.RT_PORT_PAIR: pp_list = self._res_map[self.region_name][constants.RT_PORT_PAIR] for pp in pp_list: if body[_type]['ingress'] == pp['ingress']: raise client_exceptions.BadRequest(constants.STR_USED_BY) elif _type == constants.RT_PORT_PAIR_GROUP: ppg_list = self._res_map[self.region_name][ constants.RT_PORT_PAIR_GROUP] for pp in body[_type]['port_pairs']: for ppg in ppg_list: if pp in ppg['port_pairs']: raise client_exceptions.Conflict(constants.STR_IN_USE) elif _type == constants.RT_FLOW_CLASSIFIER: fc_list = self._res_map[self.region_name][ constants.RT_FLOW_CLASSIFIER] for fc in fc_list: if (body[_type]['logical_source_port'] == fc['logical_source_port']): raise client_exceptions.BadRequest( constants.STR_CONFLICTS_WITH) elif _type == constants.RT_PORT_CHAIN: pc_list = self._res_map[self.region_name][constants.RT_PORT_CHAIN] for fc in body[_type]['flow_classifiers']: for pc in pc_list: if fc in pc['flow_classifiers']: raise client_exceptions.Conflict(constants.STR_IN_USE) return super(FakeClient, self).create_resources(_type, ctx, body)
def test_exception_status(self): e = exceptions.BadRequest() self.assertEqual(e.status_code, 400) e = exceptions.BadRequest(status_code=499) self.assertEqual(e.status_code, 499) # SslCertificateValidationError has no explicit status_code, # but should have a 'safe' defined fallback. e = exceptions.SslCertificateValidationError() self.assertIsNotNone(e.status_code) e = exceptions.SslCertificateValidationError(status_code=599) self.assertEqual(e.status_code, 599)
def test_assign_private_ip_addresses_invalid_parameters(self): self.set_mock_db_items(fakes.DB_NETWORK_INTERFACE_1, fakes.DB_SUBNET_1) self.neutron.show_subnet.return_value = ({'subnet': fakes.OS_SUBNET_1}) self.neutron.show_port.return_value = ({ 'port': copy.deepcopy(fakes.OS_PORT_1) }) def do_check(error_code): self.assert_execution_error( error_code, 'AssignPrivateIpAddresses', { 'NetworkInterfaceId': fakes.ID_EC2_NETWORK_INTERFACE_1, 'PrivateIpAddress.1': '10.10.1.5', 'PrivateIpAddress.2': '10.10.1.6', }) self.neutron.update_port.side_effect = ( neutron_exception.IpAddressGenerationFailureClient()) do_check('NetworkInterfaceLimitExceeded') self.neutron.update_port.side_effect = ( neutron_exception.IpAddressInUseClient()) do_check('InvalidParameterValue') self.neutron.update_port.side_effect = (neutron_exception.BadRequest()) do_check('InvalidParameterValue')
def test_add_to_instance_with_bad_request(self): sg_name = 'web_server' sg_id = '85cc3048-abc3-43cc-89b3-377341426ac5' port_id = 1 port_list = { 'ports': [{ 'id': port_id, 'device_id': uuids.instance, 'fixed_ips': [{ 'ip_address': '10.0.0.1' }], 'port_security_enabled': True, 'security_groups': [] }] } self.mocked_client.list_ports.return_value = port_list sg_api = neutron_driver.SecurityGroupAPI() self.mocked_client.update_port.side_effect = (n_exc.BadRequest( message='error')) with mock.patch.object(neutronv20, 'find_resourceid_by_name_or_id', return_value=sg_id): self.assertRaises(exception.SecurityGroupCannotBeApplied, sg_api.add_to_instance, self.context, objects.Instance(uuid=uuids.instance), sg_name) self.mocked_client.list_ports.assert_called_once_with( device_id=uuids.instance) self.mocked_client.update_port.assert_called_once_with( port_id, {'port': { 'security_groups': [sg_id] }})
def test_get_fixed_network_id_with_client_error(self, mock_neutron_v20_find): ex = n_exception.BadRequest() self.assert_raises_from_get_fixed_network_id( mock_neutron_v20_find, ex, exception.InvalidSubnet, )
def test_create_floatingip_failure(self, mock_log_info): from neutronclient.common import exceptions as neutron_exceptions # case 1: an error which we should not handle self.nc.create_floatingip.side_effect = neutron_exceptions.BadRequest( "oops") self.assertRaises(neutron_exceptions.BadRequest, self.neutron.create_floatingip, floating_network={"id": "net-id"}) self.assertFalse(mock_log_info.called) # case 2: exception that we should handle self.nc.create_floatingip.side_effect = neutron_exceptions.BadRequest( "Unrecognized attribute: 'description'") self.assertRaises(neutron_exceptions.BadRequest, self.neutron.create_floatingip, floating_network={"id": "net-id"}) self.assertTrue(mock_log_info.called)
def create_security_group(self, body=None): s = body.get('security_group') if not isinstance(s.get('name', ''), six.string_types): msg = ('BadRequest: Invalid input for name. Reason: ' 'None is not a valid string.') raise n_exc.BadRequest(message=msg) if not isinstance(s.get('description.', ''), six.string_types): msg = ('BadRequest: Invalid input for description. Reason: ' 'None is not a valid string.') raise n_exc.BadRequest(message=msg) if len(s.get('name')) > 255 or len(s.get('description')) > 255: msg = 'Security Group name great than 255' raise n_exc.NeutronClientException(message=msg, status_code=401) ret = {'name': s.get('name'), 'description': s.get('description'), 'tenant_id': 'fake', 'security_group_rules': [], 'id': str(uuid.uuid4())} self._fake_security_groups[ret['id']] = ret return {'security_group': ret}
def test_create_security_group_with_bad_request(self): name = 'test-security-group' description = None body = {'security_group': {'name': name, 'description': description}} message = "Invalid input. Reason: 'None' is not a valid string." self.moxed_client.create_security_group(body).AndRaise( n_exc.BadRequest(message=message)) self.mox.ReplayAll() sg_api = neutron_driver.SecurityGroupAPI() self.assertRaises(exception.Invalid, sg_api.create_security_group, self.context, name, description)
def test_create_security_group_with_bad_request(self): name = 'test-security-group' description = None body = {'security_group': {'name': name, 'description': description}} message = "Invalid input. Reason: 'None' is not a valid string." self.mocked_client.create_security_group.side_effect = ( n_exc.BadRequest(message=message)) self.assertRaises(exception.Invalid, sg_api.create_security_group, self.context, name, description) self.mocked_client.create_security_group.assert_called_once_with(body)
def test_create_subnet_overlapped(self): self.set_mock_db_items(fakes.DB_VPC_1, fakes.DB_ROUTE_TABLE_1) self.neutron.create_network.side_effect = (tools.get_neutron_create( 'network', fakes.ID_OS_NETWORK_1, {'status': 'available'})) self.neutron.create_subnet.side_effect = (tools.get_neutron_create( 'subnet', fakes.ID_OS_SUBNET_1)) self.neutron.add_interface_router.side_effect = ( neutron_exception.BadRequest()) self.assert_execution_error('InvalidSubnet.Conflict', 'CreateSubnet', { 'VpcId': fakes.ID_EC2_VPC_1, 'CidrBlock': fakes.CIDR_SUBNET_1 })
def args2body(self, parsed_args): body = {'network': parsed_args.network} if parsed_args.min: body['min'] = parsed_args.min else: raise exceptions.BadRequest('min must be set') if parsed_args.max: body['max'] = parsed_args.max return {self.resource: body}
def args2body(self, parsed_args): body = { 'project': parsed_args.project } if parsed_args.min: body['min'] = parsed_args.min else: if not parsed_args.max: raise exceptions.BadRequest('Either min or max must be set') if parsed_args.max: body['max'] = parsed_args.max return {self.resource: body}
def args2body(self, parsed_args): body = {'direction': parsed_args.direction} if parsed_args.min: body['min'] = parsed_args.min else: if not parsed_args.max: raise exceptions.BadRequest('Either min or max must be set') if parsed_args.max: body['max'] = parsed_args.max if parsed_args.parent: body['parent'] = parsed_args.parent return {self.resource: body}
def test_execute_error(self): @tools.screen_all_logs def do_check(ex, status, code, message): self.controller.reset_mock() self.controller.fake_action.side_effect = ex res = self.request.send(self.application) self.assertEqual(status, res.status_code) self.assertEqual('text/xml', res.content_type) expected_xml = fakes.XML_ERROR_TEMPLATE % { 'code': code, 'message': message, 'request_id': self.fake_context.request_id } self.assertThat(res.body.decode("utf-8"), matchers.XMLMatches(expected_xml)) self.controller.fake_action.assert_called_once_with( self.fake_context, param='fake_param') do_check(exception.EC2Exception('fake_msg'), 400, 'EC2Exception', 'fake_msg') do_check(KeyError('fake_msg'), 500, 'KeyError', 'Unknown error occurred.') do_check(exception.InvalidVpcIDNotFound('fake_msg'), 400, 'InvalidVpcID.NotFound', 'fake_msg') do_check(nova_exception.BadRequest(400, message='fake_msg'), 400, 'BadRequest', 'fake_msg') do_check(glance_exception.HTTPBadRequest(), 400, 'HTTPBadRequest', 'HTTP HTTPBadRequest') do_check(cinder_exception.BadRequest(400, message='fake_msg'), 400, 'BadRequest', 'fake_msg') do_check(neutron_exception.BadRequest(message='fake_msg'), 400, 'BadRequest', 'fake_msg') do_check(keystone_exception.BadRequest(message='fake_msg'), 400, 'BadRequest', 'fake_msg (HTTP 400)') do_check( botocore_exceptions.ClientError( { 'Error': { 'Code': '', 'Message': '' }, 'Code': 'FakeCode', 'Message': 'fake_msg' }, 'register_image'), 400, 'FakeCode', 'fake_msg')
def test_add_security_groups_to_ports_bad_update( self, mock_neutron_api_cls): addresses = {'private': [{'port': '1234567'}]} container = Container(self.context, **utils.get_test_container( addresses=addresses)) mock_neutron_api = mock.MagicMock() mock_neutron_api_cls.return_value = mock_neutron_api self.network_api.neutron_api.context = mock.Mock() security_group_ids = ['sg2'] mock_neutron_api.update_port.side_effect = n_exc.BadRequest( message='error') self.assertRaises(exception.SecurityGroupCannotBeApplied, self.network_api.add_security_groups_to_ports, container, security_group_ids) mock_neutron_api.update_port.assert_called_once_with( '1234567', {'port': {'security_groups': ['sg1', 'sg2']}})
def test_add_security_groups_to_ports_bad_update(self, mock_neutron_api_cls): addresses = {'fake-net-id': [{'port': 'fake-port-id'}]} container = Container(self.context, **utils.get_test_container(addresses=addresses)) mock_neutron_api_cls.return_value = self.network_api.neutron_api security_group_ids = ['sg2'] with mock.patch.object(self.network_api.neutron_api, 'update_port') as mock_update_port: mock_update_port.side_effect = n_exc.BadRequest(message='error') self.assertRaises(exception.SecurityGroupCannotBeApplied, self.network_api.add_security_groups_to_ports, container, security_group_ids) mock_update_port.assert_called_once_with( 'fake-port-id', {'port': { 'security_groups': ['sg1', 'sg2'] }})