def test_get_available_partitions(self): bc_session = mock.Mock() bc_session.get_available_partitions.return_value = ['foo'] session = Session(botocore_session=bc_session) partitions = session.get_available_partitions() self.assertEqual(partitions, ['foo'])
def test_get_available_services(self): bc_session = self.bc_session_cls.return_value session = Session() session.get_available_services() self.assertTrue(bc_session.get_available_services.called, 'Botocore session get_available_services not called')
def setup_client_and_resource(self): self._write_models() self.loader = Loader(extra_search_paths=[self.root_dir]) self.botocore_session = botocore.session.get_session() self.botocore_session.register_component('data_loader', self.loader) self.session = Session(botocore_session=self.botocore_session, region_name='us-east-1') self.client = self.session.client('myservice', 'us-east-1') self.resource = self.session.resource('myservice', 'us-east-1')
def test_get_available_resources(self): mock_bc_session = mock.Mock() loader = mock.Mock(spec=loaders.Loader) loader.list_available_services.return_value = ['foo', 'bar'] mock_bc_session.get_component.return_value = loader session = Session(botocore_session=mock_bc_session) names = session.get_available_resources() self.assertEqual(names, ['foo', 'bar'])
def test_get_available_regions(self): bc_session = mock.Mock() bc_session.get_available_regions.return_value = ['foo'] session = Session(botocore_session=bc_session) partitions = session.get_available_regions('myservice') bc_session.get_available_regions.assert_called_with( service_name='myservice', partition_name='aws', allow_non_regional=False ) self.assertEqual(partitions, ['foo'])
def test_create_client_with_args(self): bc_session = self.bc_session_cls.return_value session = Session(region_name='us-east-1') session.client('sqs', region_name='us-west-2') bc_session.create_client.assert_called_with( 'sqs', aws_secret_access_key=None, aws_access_key_id=None, endpoint_url=None, use_ssl=True, aws_session_token=None, verify=None, region_name='us-west-2', api_version=None, config=None)
def setUp(self): self.http_response = requests.models.Response() self.http_response.status_code = 200 self.parsed_response = {} self.make_request_patch = mock.patch( 'botocore.endpoint.Endpoint.make_request') self.make_request_mock = self.make_request_patch.start() self.make_request_mock.return_value = ( self.http_response, self.parsed_response) self.session = Session( aws_access_key_id='dummy', aws_secret_access_key='dummy', region_name='us-east-1')
class TestDynamoDB(unittest.TestCase): def setUp(self): self.http_response = requests.models.Response() self.http_response.status_code = 200 self.parsed_response = {} self.make_request_patch = mock.patch( 'botocore.endpoint.Endpoint.make_request') self.make_request_mock = self.make_request_patch.start() self.make_request_mock.return_value = ( self.http_response, self.parsed_response) self.session = Session( aws_access_key_id='dummy', aws_secret_access_key='dummy', region_name='us-east-1') def tearDown(self): self.make_request_patch.stop() def test_resource(self): dynamodb = self.session.resource('dynamodb') table = dynamodb.Table('MyTable') # Make sure it uses the high level interface table.scan(FilterExpression=Attr('mykey').eq('myvalue')) request = self.make_request_mock.call_args_list[0][0][1] request_params = json.loads(request['body'].decode('utf-8')) self.assertEqual( request_params, {'TableName': 'MyTable', 'FilterExpression': '#n0 = :v0', 'ExpressionAttributeNames': {'#n0': 'mykey'}, 'ExpressionAttributeValues': {':v0': {'S': 'myvalue'}}} ) def test_client(self): dynamodb = self.session.client('dynamodb') # Make sure the client still uses the botocore level interface dynamodb.scan( TableName='MyTable', FilterExpression='#n0 = :v0', ExpressionAttributeNames={'#n0': 'mykey'}, ExpressionAttributeValues={':v0': {'S': 'myvalue'}} ) request = self.make_request_mock.call_args_list[0][0][1] request_params = json.loads(request['body'].decode('utf-8')) self.assertEqual( request_params, {'TableName': 'MyTable', 'FilterExpression': '#n0 = :v0', 'ExpressionAttributeNames': {'#n0': 'mykey'}, 'ExpressionAttributeValues': {':v0': {'S': 'myvalue'}}} )
def test_create_resource_latest_version(self): mock_bc_session = mock.Mock() loader = mock.Mock(spec=loaders.Loader) loader.determine_latest_version.return_value = '2014-11-02' loader.load_service_model.return_value = { 'resources': [], 'service': []} mock_bc_session.get_component.return_value = loader session = Session(botocore_session=mock_bc_session) session.resource_factory.load_from_definition = mock.Mock() session.resource('sqs') loader.load_service_model.assert_called_with( 'sqs', 'resources-1', None)
def test_all_collections_have_paginators_if_needed(): # If a collection relies on an operation that is paginated, it # will require a paginator to iterate through all of the resources # with the all() method. If there is no paginator, it will only # make it through the first page of results. So we need to make sure # if a collection looks like it uses a paginated operation then there # should be a paginator applied to it. botocore_session = botocore.session.get_session() session = Session(botocore_session=botocore_session) loader = botocore_session.get_component('data_loader') for service_name in session.get_available_resources(): client = session.client(service_name, region_name='us-east-1') json_resource_model = loader.load_service_model( service_name, 'resources-1') resource_defs = json_resource_model['resources'] resource_models = [] # Get the service resource model service_resource_model = ResourceModel( service_name, json_resource_model['service'], resource_defs) resource_models.append(service_resource_model) # Generate all of the resource models for a service for resource_name, resource_defintion in resource_defs.items(): resource_models.append(ResourceModel( resource_name, resource_defintion, resource_defs)) for resource_model in resource_models: # Iterate over all of the collections for each resource model # and ensure that the collection has a paginator if it needs one. for collection_model in resource_model.collections: yield ( _assert_collection_has_paginator_if_needed, client, service_name, resource_name, collection_model)
def test_can_access_region_name(self): bc_session = self.bc_session_cls.return_value bc_session.get_config_variable.return_value = 'us-west-2' session = Session('abc123', region_name='us-west-2') bc_session.set_config_variable.assert_called_with('region', 'us-west-2') self.assertEqual(session.region_name, 'us-west-2')
def test_repr(self): bc_session = self.bc_session_cls.return_value bc_session.get_credentials.return_value.access_key = 'abc123' bc_session.get_config_variable.return_value = 'us-west-2' session = Session('abc123', region_name='us-west-2') self.assertEqual(repr(session), 'Session(region_name=\'us-west-2\')')
def setup_default_session(**kwargs): """ Set up a default session, passing through any parameters to the session constructor. There is no need to call this unless you wish to pass custom parameters, because a default session will be created for you. """ global DEFAULT_SESSION DEFAULT_SESSION = Session(**kwargs)
def test_custom_session(self): bc_session = self.bc_session_cls() self.bc_session_cls.reset_mock() Session(botocore_session=bc_session) # No new session was created self.assertFalse(self.bc_session_cls.called)
def test_bad_resource_name_with_no_client_has_simple_err_msg(self): mock_bc_session = mock.Mock() loader = mock.Mock(spec=loaders.Loader) loader.load_service_model.side_effect = UnknownServiceError( service_name='foo', known_service_names='asdf' ) mock_bc_session.get_component.return_value = loader loader.list_available_services.return_value = ['good-resource'] mock_bc_session.get_available_services.return_value = ['good-client'] session = Session(botocore_session=mock_bc_session) with self.assertRaises(ResourceNotExistsError) as e: session.resource('bad-client') err_msg = str(e.exception) # Shouldn't mention anything about clients because # 'bad-client' it not a valid boto3_wasabi.client(...) self.assertNotIn('boto3_wasabi.client', err_msg)
def test_profile_can_be_set(self): bc_session = self.bc_session_cls.return_value session = Session(profile_name='foo') bc_session.set_config_variable.assert_called_with( 'profile', 'foo') bc_session.profile = 'foo' # We should also be able to read the value self.assertEqual(session.profile_name, 'foo')
def test_user_agent_extra(self): # This test is the same as above, but includes custom extra content # which must still be in the final modified user-agent. bc_session = self.bc_session_cls.return_value bc_session.user_agent_name = 'Botocore' bc_session.user_agent_version = '0.68.0' bc_session.user_agent_extra = 'foo' Session(botocore_session=bc_session) self.assertEqual(bc_session.user_agent_extra, 'foo Botocore/0.68.0')
def test_bad_resource_name(self): mock_bc_session = mock.Mock() loader = mock.Mock(spec=loaders.Loader) loader.load_service_model.side_effect = UnknownServiceError( service_name='foo', known_service_names='asdf' ) mock_bc_session.get_component.return_value = loader loader.list_available_services.return_value = ['good-resource'] mock_bc_session.get_available_services.return_value = ['sqs'] session = Session(botocore_session=mock_bc_session) with self.assertRaises(ResourceNotExistsError) as e: session.resource('sqs') err_msg = str(e.exception) # 1. should say the resource doesn't exist. self.assertIn('resource does not exist', err_msg) self.assertIn('sqs', err_msg) # 2. Should list available resources you can choose. self.assertIn('good-resource', err_msg) # 3. Should list client if available. self.assertIn('client', err_msg)
def test_create_resource_with_args(self): mock_bc_session = mock.Mock() loader = mock.Mock(spec=loaders.Loader) loader.determine_latest_version.return_value = '2014-11-02' loader.load_service_model.return_value = { 'resources': [], 'service': []} mock_bc_session.get_component.return_value = loader session = Session(botocore_session=mock_bc_session) session.resource_factory.load_from_definition = mock.Mock() session.client = mock.Mock() session.resource('sqs', verify=False) session.client.assert_called_with( 'sqs', aws_secret_access_key=None, aws_access_key_id=None, endpoint_url=None, use_ssl=True, aws_session_token=None, verify=False, region_name=None, api_version='2014-11-02', config=mock.ANY) client_config = session.client.call_args[1]['config'] self.assertEqual(client_config.user_agent_extra, 'Resource') self.assertEqual(client_config.signature_version, None)
def test_custom_user_agent(self): # This test ensures that a customized user-agent is left untouched. bc_session = self.bc_session_cls.return_value bc_session.user_agent_name = 'Custom' bc_session.user_agent_version = '1.0' bc_session.user_agent_extra = '' Session(botocore_session=bc_session) self.assertEqual(bc_session.user_agent_name, 'Custom') self.assertEqual(bc_session.user_agent_version, '1.0') self.assertEqual(bc_session.user_agent_extra, '')
def test_can_get_credentials(self): access_key = 'foo' secret_key = 'bar' token = 'baz' creds = mock.Mock() creds.access_key = access_key creds.secret_key = secret_key creds.token = token bc_session = self.bc_session_cls.return_value bc_session.get_credentials.return_value = creds session = Session( aws_access_key_id=access_key, aws_secret_access_key=secret_key, aws_session_token=token) credentials = session.get_credentials() self.assertEqual(credentials.access_key, access_key) self.assertEqual(credentials.secret_key, secret_key) self.assertEqual(credentials.token, token)
def test_user_agent(self): # Here we get the underlying Botocore session, create a Boto 3 # session, and ensure that the user-agent is modified as expected bc_session = self.bc_session_cls.return_value bc_session.user_agent_name = 'Botocore' bc_session.user_agent_version = '0.68.0' bc_session.user_agent_extra = '' Session(botocore_session=bc_session) self.assertEqual(bc_session.user_agent_name, 'boto3_wasabi') self.assertEqual(bc_session.user_agent_version, __version__) self.assertEqual(bc_session.user_agent_extra, 'Botocore/0.68.0')
def test_credentials_can_be_set(self): bc_session = self.bc_session_cls.return_value # Set values in constructor Session(aws_access_key_id='key', aws_secret_access_key='secret', aws_session_token='token') self.assertTrue(self.bc_session_cls.called, 'Botocore session was not created') self.assertTrue(bc_session.set_credentials.called, 'Botocore session set_credentials not called from constructor') bc_session.set_credentials.assert_called_with( 'key', 'secret', 'token')
class TestCollection(unittest.TestCase): def setUp(self): self.session = Session(aws_access_key_id='dummy', aws_secret_access_key='dummy', region_name='us-east-1') # Pick an arbitrary resource. self.ec2_resource = self.session.resource('ec2') def test_can_use_collection_methods(self): self.assertIsInstance(self.ec2_resource.instances.all(), ResourceCollection) def test_can_chain_methods(self): self.assertIsInstance(self.ec2_resource.instances.all().page_size(5), ResourceCollection)
def setUp(self): self.session = Session(aws_access_key_id='dummy', aws_secret_access_key='dummy', region_name='us-east-1') # Pick an arbitrary resource. self.ec2_resource = self.session.resource('ec2')
def test_can_reach_events(self): mock_bc_session = self.bc_session_cls() session = Session(botocore_session=mock_bc_session) session.events mock_bc_session.get_component.assert_called_with('event_emitter')
def test_available_profiles(self): bc_session = mock.Mock() bc_session.available_profiles.return_value = ['foo','bar'] session = Session(botocore_session=bc_session) profiles = session.available_profiles self.assertEqual(len(profiles.return_value), 2)
def setUp(self): self.documenter = ServiceDocumenter( 'ec2', session=Session(region_name='us-east-1')) self.generated_contents = self.documenter.document_service() self.generated_contents = self.generated_contents.decode('utf-8')
def test_arguments_not_required(self): Session() self.assertTrue(self.bc_session_cls.called, 'Botocore session was not created')
def test_create_client(self): session = Session(region_name='us-east-1') client = session.client('sqs', region_name='us-west-2') self.assertTrue(client, 'No low-level client was returned')