def setUp(self): self.model = { 'metadata': {'protocol': 'json', 'apiVersion': '2014-01-01', 'jsonVersion': '1.1', 'targetPrefix': 'foo'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Timestamp': {'shape': 'TimestampType'}, } }, 'TimestampType': { 'type': 'timestamp', } } } self.service_model = ServiceModel(self.model)
def setUp(self): self.model = { 'metadata': {'protocol': 'query', 'apiVersion': '2014-01-01'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Timestamp': {'shape': 'StringTestType'}, } }, 'StringTestType': { 'type': 'string', 'min': 15 } } } self.service_model = ServiceModel(self.model)
def setUp(self): self.model = { 'metadata': {'protocol': 'rest-xml', 'apiVersion': '2014-01-01'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Foo': { 'shape': 'FooShape', 'locationName': 'Foo' }, }, 'payload': 'Foo' }, 'FooShape': { 'type': 'list', 'member': {'shape': 'StringShape'} }, 'StringShape': { 'type': 'string', } } } self.service_model = ServiceModel(self.model)
def setUp(self): self.model = { 'metadata': {'protocol': 'rest-xml', 'apiVersion': '2014-01-01'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'TimestampHeader': { 'shape': 'TimestampType', 'location': 'header', 'locationName': 'x-timestamp' }, } }, 'TimestampType': { 'type': 'timestamp', } } } self.service_model = ServiceModel(self.model)
def test_validate_ignores_response_metadata(self): service_response = {'ResponseMetadata': {'foo': 'bar'}} service_model = ServiceModel({ 'documentation': '', 'operations': { 'foo': { 'name': 'foo', 'input': { 'shape': 'StringShape' }, 'output': { 'shape': 'StringShape' } } }, 'shapes': { 'StringShape': { 'type': 'string' } } }) op_name = service_model.operation_names[0] output_shape = service_model.operation_model(op_name).output_shape self.client.meta.service_model = service_model self.stubber.add_response('TestOperation', service_response) self.validate_parameters_mock.assert_called_with({}, output_shape) # Make sure service response hasn't been mutated self.assertEqual(service_response, {'ResponseMetadata': { 'foo': 'bar' }})
def _load_service_model(self, service_name, api_version=None): json_model = self._loader.load_service_model(service_name, 'service-2', api_version=api_version) service_model = ServiceModel(json_model, service_name=service_name) self._register_retries(service_model) return service_model
def get_service_model(self, service, api_version=None): """Get the service model for the service.""" with mock.patch('ibm_botocore.loaders.Loader.list_available_services', return_value=[service]): return ServiceModel(self.loader.load_service_model( service, type_name='service-2', api_version=api_version), service_name=service)
def test_route53_resource_id(self): event = 'before-parameter-build.route53.GetHostedZone' params = { 'Id': '/hostedzone/ABC123', 'HostedZoneId': '/hostedzone/ABC123', 'ResourceId': '/hostedzone/DEF456', 'DelegationSetId': '/hostedzone/GHI789', 'Other': '/hostedzone/foo' } operation_def = { 'name': 'GetHostedZone', 'input': { 'shape': 'GetHostedZoneInput' } } service_def = { 'metadata': {}, 'shapes': { 'GetHostedZoneInput': { 'type': 'structure', 'members': { 'Id': { 'shape': 'ResourceId' }, 'HostedZoneId': { 'shape': 'ResourceId' }, 'ResourceId': { 'shape': 'ResourceId' }, 'DelegationSetId': { 'shape': 'DelegationSetId' }, 'Other': { 'shape': 'String' } } }, 'ResourceId': { 'type': 'string' }, 'DelegationSetId': { 'type': 'string' }, 'String': { 'type': 'string' } } } model = OperationModel(operation_def, ServiceModel(service_def)) self.session.emit(event, params=params, model=model) self.assertEqual(params['Id'], '/hostedzone/ABC123') self.assertEqual(params['HostedZoneId'], '/hostedzone/ABC123') self.assertEqual(params['ResourceId'], '/hostedzone/DEF456') self.assertEqual(params['DelegationSetId'], '/hostedzone/GHI789') # This one should have been left alone self.assertEqual(params['Other'], '/hostedzone/foo')
def test_route53_resource_id_missing_input_shape(self): event = 'before-parameter-build.route53.GetHostedZone' params = {'HostedZoneId': '/hostedzone/ABC123'} operation_def = {'name': 'GetHostedZone'} service_def = {'metadata': {}, 'shapes': {}} model = OperationModel(operation_def, ServiceModel(service_def)) self.session.emit(event, params=params, model=model) self.assertEqual(params['HostedZoneId'], '/hostedzone/ABC123')
class TestRestXMLUnicodeSerialization(unittest.TestCase): def setUp(self): self.model = { 'metadata': { 'protocol': 'rest-xml', 'apiVersion': '2014-01-01' }, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': { 'shape': 'InputShape' }, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Foo': { 'shape': 'FooShape', 'locationName': 'Foo' }, }, 'payload': 'Foo' }, 'FooShape': { 'type': 'list', 'member': { 'shape': 'StringShape' } }, 'StringShape': { 'type': 'string', } } } self.service_model = ServiceModel(self.model) def serialize_to_request(self, input_params): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol']) return request_serializer.serialize_to_request( input_params, self.service_model.operation_model('TestOperation')) def test_restxml_serializes_unicode(self): params = {'Foo': [u'\u65e5\u672c\u8a9e\u3067\u304a\uff4b']} try: self.serialize_to_request(params) except UnicodeEncodeError: self.fail("RestXML serializer failed to serialize unicode text.")
class TestJSONTimestampSerialization(unittest.TestCase): def setUp(self): self.model = { 'metadata': {'protocol': 'json', 'apiVersion': '2014-01-01', 'jsonVersion': '1.1', 'targetPrefix': 'foo'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Timestamp': {'shape': 'TimestampType'}, } }, 'TimestampType': { 'type': 'timestamp', } } } self.service_model = ServiceModel(self.model) def serialize_to_request(self, input_params): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol']) return request_serializer.serialize_to_request( input_params, self.service_model.operation_model('TestOperation')) def test_accepts_iso_8601_format(self): body = json.loads(self.serialize_to_request( {'Timestamp': '1970-01-01T00:00:00'})['body'].decode('utf-8')) self.assertEqual(body['Timestamp'], 0) def test_accepts_epoch(self): body = json.loads(self.serialize_to_request( {'Timestamp': '0'})['body'].decode('utf-8')) self.assertEqual(body['Timestamp'], 0) # Can also be an integer 0. body = json.loads(self.serialize_to_request( {'Timestamp': 0})['body'].decode('utf-8')) self.assertEqual(body['Timestamp'], 0) def test_accepts_partial_iso_format(self): body = json.loads(self.serialize_to_request( {'Timestamp': '1970-01-01'})['body'].decode('utf-8')) self.assertEqual(body['Timestamp'], 0)
def setUp(self): self.model = { "metadata": { 'endpointPrefix': 'myservice', 'serviceFullName': 'MyService', }, 'operations': { 'OperationName': { 'name': 'OperationName', 'errors': [ { 'shape': 'ExceptionMissingCode' }, { 'shape': 'ExceptionWithModeledCode' }, ], }, 'AnotherOperationName': { 'name': 'AnotherOperationName', 'errors': [ { 'shape': 'ExceptionForAnotherOperation' }, { 'shape': 'ExceptionWithModeledCode' }, ], } }, 'shapes': { 'ExceptionWithModeledCode': { 'type': 'structure', 'members': {}, 'error': { 'code': 'ModeledCode' }, 'exception': True, }, 'ExceptionMissingCode': { 'type': 'structure', 'members': {}, 'exception': True, }, 'ExceptionForAnotherOperation': { 'type': 'structure', 'members': {}, 'exception': True, } } } self.service_model = ServiceModel(self.model) self.exceptions_factory = ClientExceptionsFactory()
def test_decode_json_policy(self): parsed = { 'Document': '{"foo": "foobarbaz"}', 'Other': 'bar', } service_def = { 'operations': { 'Foo': { 'output': { 'shape': 'PolicyOutput' }, } }, 'shapes': { 'PolicyOutput': { 'type': 'structure', 'members': { 'Document': { 'shape': 'policyDocumentType' }, 'Other': { 'shape': 'stringType' } } }, 'policyDocumentType': { 'type': 'string' }, 'stringType': { 'type': 'string' }, } } model = ServiceModel(service_def) op_model = model.operation_model('Foo') handlers.json_decode_policies(parsed, op_model) self.assertEqual(parsed['Document'], {'foo': 'foobarbaz'}) no_document = {'Other': 'bar'} handlers.json_decode_policies(no_document, op_model) self.assertEqual(no_document, {'Other': 'bar'})
def test_validates_on_empty_output_shape(self): service_model = ServiceModel({ 'documentation': '', 'operations': { 'foo': { 'name': 'foo' } } }) self.client.meta.service_model = service_model with self.assertRaises(ParamValidationError): self.stubber.add_response('TestOperation', {'foo': 'bar'})
def setUp(self): super(TestCollectionFactory, self).setUp() self.client = mock.Mock() self.client.can_paginate.return_value = False self.parent = mock.Mock() self.parent.meta = ResourceMeta('test', client=self.client) self.resource_factory = ResourceFactory(mock.Mock()) self.service_model = ServiceModel({}) self.event_emitter = HierarchicalEmitter() self.factory = CollectionFactory() self.load = self.factory.load_from_definition
def setUp(self): super().setUp() # Minimal definition so things like repr work self.collection_def = { 'request': {'operation': 'TestOperation'}, 'resource': {'type': 'Frob'}, } self.client = mock.Mock() self.client.can_paginate.return_value = False self.parent = mock.Mock() self.parent.meta = ResourceMeta('test', client=self.client) self.factory = ResourceFactory(mock.Mock()) self.service_model = ServiceModel({})
def get_service_model(self, service_name, api_version=None): """Get the service model object. :type service_name: string :param service_name: The service name :type api_version: string :param api_version: The API version of the service. If none is provided, then the latest API version will be used. :rtype: L{ibm_botocore.model.ServiceModel} :return: The ibm_botocore service model for the service. """ service_description = self.get_service_data(service_name, api_version) return ServiceModel(service_description, service_name=service_name)
def setUp(self): self.waiter_config = { 'version': 2, 'waiters': { 'WaiterName': { 'operation': 'Foo', 'delay': 1, 'maxAttempts': 1, 'acceptors': [], }, }, } self.waiter_model = WaiterModel(self.waiter_config) self.service_json_model = { 'metadata': { 'serviceFullName': 'Amazon MyService' }, 'operations': { 'Foo': { 'name': 'Foo', 'input': { 'shape': 'FooInputOutput' }, 'output': { 'shape': 'FooInputOutput' } } }, 'shapes': { 'FooInputOutput': { 'type': 'structure', 'members': { 'bar': { 'shape': 'String', 'documentation': 'Documents bar' } } }, 'String': { 'type': 'string' } } } self.service_model = ServiceModel(self.service_json_model, 'myservice') self.client = mock.Mock() self.client.meta.service_model = self.service_model
def test_resource_loads_waiters(self): model = { "waiters": { "Exists": { "waiterName": "BucketExists", "params": [{ "target": "Bucket", "source": "identifier", "name": "Name", }], } } } defs = {'Bucket': {}} service_model = ServiceModel({}) resource = self.load('test', model, defs, service_model)() assert hasattr(resource, 'wait_until_exists')
def test_resource_loads_collections(self, mock_model): model = { 'hasMany': { 'Queues': { 'request': { 'operation': 'ListQueues' }, 'resource': { 'type': 'Queue' }, } } } defs = {'Queue': {}} service_model = ServiceModel({}) mock_model.return_value.name = 'queues' resource = self.load('test', model, defs, service_model)() # Resource must expose queues collection assert hasattr(resource, 'queues') assert isinstance(resource.queues, CollectionManager)
def test_resource_waiter_calls_waiter_method(self, waiter_action_cls): model = { "waiters": { "Exists": { "waiterName": "BucketExists", "params": [{ "target": "Bucket", "source": "identifier", "name": "Name" }] } } } defs = {'Bucket': {}} service_model = ServiceModel({}) waiter_action = waiter_action_cls.return_value resource = self.load('test', model, defs, service_model)() resource.wait_until_exists('arg1', arg2=2) waiter_action.assert_called_with(resource, 'arg1', arg2=2)
def test_resource_loads_collections(self, mock_model): model = { 'hasMany': { u'Queues': { 'request': { 'operation': 'ListQueues' }, 'resource': { 'type': 'Queue' } } } } defs = {'Queue': {}} service_model = ServiceModel({}) mock_model.return_value.name = 'queues' resource = self.load('test', model, defs, service_model)() self.assertTrue(hasattr(resource, 'queues'), 'Resource should expose queues collection') self.assertIsInstance( resource.queues, CollectionManager, 'Queues collection should be a collection manager')
def serialize_to_request(self, input_params): service_model = ServiceModel(self.model) request_serializer = serialize.create_serializer( service_model.metadata['protocol']) return request_serializer.serialize_to_request( input_params, service_model.operation_model('TestOperation'))
class TestTimestampHeadersWithRestXML(unittest.TestCase): def setUp(self): self.model = { 'metadata': {'protocol': 'rest-xml', 'apiVersion': '2014-01-01'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'TimestampHeader': { 'shape': 'TimestampType', 'location': 'header', 'locationName': 'x-timestamp' }, } }, 'TimestampType': { 'type': 'timestamp', } } } self.service_model = ServiceModel(self.model) def serialize_to_request(self, input_params): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol']) return request_serializer.serialize_to_request( input_params, self.service_model.operation_model('TestOperation')) def test_accepts_datetime_object(self): request = self.serialize_to_request( {'TimestampHeader': datetime.datetime(2014, 1, 1, 12, 12, 12, tzinfo=dateutil.tz.tzutc())}) self.assertEqual(request['headers']['x-timestamp'], 'Wed, 01 Jan 2014 12:12:12 GMT') def test_accepts_iso_8601_format(self): request = self.serialize_to_request( {'TimestampHeader': '2014-01-01T12:12:12+00:00'}) self.assertEqual(request['headers']['x-timestamp'], 'Wed, 01 Jan 2014 12:12:12 GMT') def test_accepts_iso_8601_format_non_utc(self): request = self.serialize_to_request( {'TimestampHeader': '2014-01-01T07:12:12-05:00'}) self.assertEqual(request['headers']['x-timestamp'], 'Wed, 01 Jan 2014 12:12:12 GMT') def test_accepts_rfc_822_format(self): request = self.serialize_to_request( {'TimestampHeader': 'Wed, 01 Jan 2014 12:12:12 GMT'}) self.assertEqual(request['headers']['x-timestamp'], 'Wed, 01 Jan 2014 12:12:12 GMT') def test_accepts_unix_timestamp_integer(self): request = self.serialize_to_request( {'TimestampHeader': 1388578332}) self.assertEqual(request['headers']['x-timestamp'], 'Wed, 01 Jan 2014 12:12:12 GMT')
if 'params' in case and inp: yield model, case, basename elif 'response' in case and out: yield model, case, basename @pytest.mark.parametrize( "json_description, case, basename", _compliance_tests(TestType.INPUT) ) def test_input_compliance(json_description, case, basename): service_description = copy.deepcopy(json_description) service_description['operations'] = { case.get('name', 'OperationName'): case, } model = ServiceModel(service_description) protocol_type = model.metadata['protocol'] try: protocol_serializer = PROTOCOL_SERIALIZERS[protocol_type] except KeyError: raise RuntimeError("Unknown protocol: %s" % protocol_type) serializer = protocol_serializer() serializer.MAP_TYPE = OrderedDict operation_model = OperationModel(case['given'], model) request = serializer.serialize_to_request(case['params'], operation_model) _serialize_request_description(request) client_endpoint = service_description.get('clientEndpoint') try: _assert_request_body_is_bytes(request['body']) _assert_requests_equal(request, case['serialized']) _assert_endpoints_equal(request, case['serialized'], client_endpoint)
def test_glacier_version_header_added(self): request_dict = {'headers': {}} model = ServiceModel({'metadata': {'apiVersion': '2012-01-01'}}) handlers.add_glacier_version(model, request_dict) self.assertEqual(request_dict['headers']['x-amz-glacier-version'], '2012-01-01')
class TestTimestamps(unittest.TestCase): def setUp(self): self.model = { 'metadata': {'protocol': 'query', 'apiVersion': '2014-01-01'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Timestamp': {'shape': 'TimestampType'}, } }, 'TimestampType': { 'type': 'timestamp', } } } self.service_model = ServiceModel(self.model) def serialize_to_request(self, input_params): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol']) return request_serializer.serialize_to_request( input_params, self.service_model.operation_model('TestOperation')) def test_accepts_datetime_object(self): request = self.serialize_to_request( {'Timestamp': datetime.datetime(2014, 1, 1, 12, 12, 12, tzinfo=dateutil.tz.tzutc())}) self.assertEqual(request['body']['Timestamp'], '2014-01-01T12:12:12Z') def test_accepts_naive_datetime_object(self): request = self.serialize_to_request( {'Timestamp': datetime.datetime(2014, 1, 1, 12, 12, 12)}) self.assertEqual(request['body']['Timestamp'], '2014-01-01T12:12:12Z') def test_accepts_iso_8601_format(self): request = self.serialize_to_request( {'Timestamp': '2014-01-01T12:12:12Z'}) self.assertEqual(request['body']['Timestamp'], '2014-01-01T12:12:12Z') def test_accepts_timestamp_without_tz_info(self): # If a timezone/utc is not specified, assume they meant # UTC. This is also the previous behavior from older versions # of ibm_botocore so we want to make sure we preserve this behavior. request = self.serialize_to_request( {'Timestamp': '2014-01-01T12:12:12'}) self.assertEqual(request['body']['Timestamp'], '2014-01-01T12:12:12Z') def test_microsecond_timestamp_without_tz_info(self): request = self.serialize_to_request( {'Timestamp': '2014-01-01T12:12:12.123456'}) self.assertEqual(request['body']['Timestamp'], '2014-01-01T12:12:12.123456Z')
class TestInstanceCreation(unittest.TestCase): def setUp(self): self.model = { 'metadata': {'protocol': 'query', 'apiVersion': '2014-01-01'}, 'documentation': '', 'operations': { 'TestOperation': { 'name': 'TestOperation', 'http': { 'method': 'POST', 'requestUri': '/', }, 'input': {'shape': 'InputShape'}, } }, 'shapes': { 'InputShape': { 'type': 'structure', 'members': { 'Timestamp': {'shape': 'StringTestType'}, } }, 'StringTestType': { 'type': 'string', 'min': 15 } } } self.service_model = ServiceModel(self.model) def assert_serialize_valid_parameter(self, request_serializer): valid_string = 'valid_string_with_min_15_chars' request = request_serializer.serialize_to_request( {'Timestamp': valid_string}, self.service_model.operation_model('TestOperation')) self.assertEqual(request['body']['Timestamp'], valid_string) def assert_serialize_invalid_parameter(self, request_serializer): invalid_string = 'short string' request = request_serializer.serialize_to_request( {'Timestamp': invalid_string}, self.service_model.operation_model('TestOperation')) self.assertEqual(request['body']['Timestamp'], invalid_string) def test_instantiate_without_validation(self): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol'], False) try: self.assert_serialize_valid_parameter(request_serializer) except ParamValidationError as e: self.fail( "Shouldn't fail serializing valid parameter without " "validation: {}".format(e) ) try: self.assert_serialize_invalid_parameter(request_serializer) except ParamValidationError as e: self.fail( "Shouldn't fail serializing invalid parameter without " "validation: {}".format(e) ) def test_instantiate_with_validation(self): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol'], True) try: self.assert_serialize_valid_parameter(request_serializer) except ParamValidationError as e: self.fail( "Shouldn't fail serializing invalid parameter without " "validation: {}".format(e) ) with self.assertRaises(ParamValidationError): self.assert_serialize_invalid_parameter(request_serializer)
if full_path.endswith('.json'): for model, case, basename in _load_cases(full_path): if model.get('description') in PROTOCOL_TEST_BLACKLIST: continue if 'params' in case: yield _test_input, model, case, basename elif 'response' in case: yield _test_output, model, case, basename def _test_input(json_description, case, basename): service_description = copy.deepcopy(json_description) service_description['operations'] = { case.get('name', 'OperationName'): case, } model = ServiceModel(service_description) protocol_type = model.metadata['protocol'] try: protocol_serializer = PROTOCOL_SERIALIZERS[protocol_type] except KeyError: raise RuntimeError("Unknown protocol: %s" % protocol_type) serializer = protocol_serializer() serializer.MAP_TYPE = OrderedDict operation_model = OperationModel(case['given'], model) request = serializer.serialize_to_request(case['params'], operation_model) _serialize_request_description(request) client_endpoint = service_description.get('clientEndpoint') try: _assert_request_body_is_bytes(request['body']) _assert_requests_equal(request, case['serialized']) _assert_endpoints_equal(request, case['serialized'], client_endpoint)
def build_models(self): self.service_model = ServiceModel(self.json_model) self.operation_model = OperationModel( self.json_model['operations']['SampleOperation'], self.service_model)