Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
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)
Beispiel #4
0
    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)
Beispiel #5
0
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'}}}
        )
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)
Beispiel #7
0
class BaseDocsTest(unittest.TestCase):
    def setUp(self):
        self.root_dir = tempfile.mkdtemp()
        self.version_dirs = os.path.join(self.root_dir, 'myservice',
                                         '2014-01-01')
        os.makedirs(self.version_dirs)

        self.model_file = os.path.join(self.version_dirs, 'service-2.json')
        self.waiter_model_file = os.path.join(self.version_dirs,
                                              'waiters-2.json')
        self.paginator_model_file = os.path.join(self.version_dirs,
                                                 'paginators-1.json')
        self.resource_model_file = os.path.join(self.version_dirs,
                                                'resources-1.json')
        self.example_model_file = os.path.join(self.version_dirs,
                                               'examples-1.json')

        self.json_model = {}
        self.waiter_json_model = {}
        self.paginator_json_model = {}
        self.resource_json_model = {}
        self._setup_models()

        self.doc_name = 'MyDoc'
        self.doc_structure = DocumentStructure(self.doc_name)

        self.setup_client_and_resource()

    def tearDown(self):
        shutil.rmtree(self.root_dir)

    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 _setup_models(self):
        self.json_model = {
            'metadata': {
                'apiVersion': '2014-01-01',
                'endpointPrefix': 'myservice',
                'signatureVersion': 'v4',
                'serviceFullName': 'AWS MyService',
                'protocol': 'query'
            },
            'operations': {
                'SampleOperation': {
                    'name': 'SampleOperation',
                    'input': {
                        'shape': 'SampleOperationInputOutput'
                    },
                    'output': {
                        'shape': 'SampleOperationInputOutput'
                    }
                }
            },
            'shapes': {
                'SampleOperationInputOutput': {
                    'type':
                    'structure',
                    'members':
                    OrderedDict([
                        ('Foo', {
                            'shape': 'String',
                            'documentation': 'Documents Foo'
                        }),
                        ('Bar', {
                            'shape': 'String',
                            'documentation': 'Documents Bar'
                        }),
                    ])
                },
                'String': {
                    'type': 'string'
                }
            }
        }

        self.example_json_model = {
            "version": 1,
            "examples": {
                "SampleOperation": [{
                    "id": "sample-id",
                    "title": "sample-title",
                    "description": "Sample Description.",
                    "input": OrderedDict([
                        ("Foo", "bar"),
                    ]),
                    "comments": {
                        "input": {
                            "Foo": "biz"
                        },
                    }
                }]
            }
        }

        self.waiter_json_model = {
            "version": 2,
            "waiters": {
                "SampleOperationComplete": {
                    "delay":
                    15,
                    "operation":
                    "SampleOperation",
                    "maxAttempts":
                    40,
                    "acceptors": [{
                        "expected": "complete",
                        "matcher": "pathAll",
                        "state": "success",
                        "argument": "Biz"
                    }, {
                        "expected": "failed",
                        "matcher": "pathAny",
                        "state": "failure",
                        "argument": "Biz"
                    }]
                }
            }
        }

        self.paginator_json_model = {
            "pagination": {
                "SampleOperation": {
                    "input_token": "NextResult",
                    "output_token": "NextResult",
                    "limit_key": "MaxResults",
                    "result_key": "Biz"
                }
            }
        }

        self.resource_json_model = {
            "service": {
                "actions":
                OrderedDict([("SampleOperation", {
                    "request": {
                        "operation": "SampleOperation"
                    }
                }),
                             ("SampleListReturnOperation", {
                                 "request": {
                                     "operation": "SampleOperation"
                                 },
                                 "resource": {
                                     "type":
                                     "Sample",
                                     "identifiers": [{
                                         "target": "Name",
                                         "source": "response",
                                         "path": "Samples[].Name"
                                     }],
                                     "path":
                                     "Samples[]"
                                 }
                             })]),
                "has": {
                    "Sample": {
                        "resource": {
                            "type": "Sample",
                            "identifiers": [{
                                "target": "Name",
                                "source": "input"
                            }]
                        }
                    }
                },
                "hasMany": {
                    "Samples": {
                        "request": {
                            "operation": "SampleOperation"
                        },
                        "resource": {
                            "type":
                            "Sample",
                            "identifiers": [{
                                "target": "Name",
                                "source": "response",
                                "path": "Samples[].Foo"
                            }]
                        }
                    }
                }
            },
            "resources": {
                "Sample": {
                    "identifiers": [{
                        "name": "Name",
                        "memberName": "Foo"
                    }],
                    "shape": "SampleOperationInputOutput",
                    "load": {
                        "request": {
                            "operation":
                            "SampleOperation",
                            "params": [{
                                "target": "Foo",
                                "source": "identifier",
                                "name": "Name"
                            }]
                        }
                    },
                    "actions": {
                        "Operate": {
                            "request": {
                                "operation":
                                "SampleOperation",
                                "params": [{
                                    "target": "Foo",
                                    "source": "identifier",
                                    "name": "Name"
                                }]
                            }
                        }
                    },
                    "batchActions": {
                        "Operate": {
                            "request": {
                                "operation":
                                "SampleOperation",
                                "params": [{
                                    "target": "Samples[].Foo",
                                    "source": "identifier",
                                    "name": "Name"
                                }]
                            }
                        }
                    },
                    "has": {
                        "RelatedSample": {
                            "resource": {
                                "type":
                                "Sample",
                                "identifiers": [{
                                    "target": "Name",
                                    "source": "data",
                                    "path": "Foo"
                                }]
                            }
                        }
                    },
                    "waiters": {
                        "Complete": {
                            "waiterName":
                            "SampleOperationComplete",
                            "params": [{
                                "target": "Foo",
                                "source": "identifier",
                                "name": "Name"
                            }]
                        }
                    }
                }
            }
        }

    def _write_models(self):
        with open(self.resource_model_file, 'w') as f:
            json.dump(self.resource_json_model, f)

        with open(self.waiter_model_file, 'w') as f:
            json.dump(self.waiter_json_model, f)

        with open(self.paginator_model_file, 'w') as f:
            json.dump(self.paginator_json_model, f)

        with open(self.model_file, 'w') as f:
            json.dump(self.json_model, f)

        with open(self.example_model_file, 'w') as f:
            json.dump(self.example_json_model, f)

    def add_shape(self, shape):
        shape_name = list(shape.keys())[0]
        self.json_model['shapes'][shape_name] = shape[shape_name]

    def add_shape_to_params(self,
                            param_name,
                            shape_name,
                            documentation=None,
                            is_required=False):
        params_shape = self.json_model['shapes']['SampleOperationInputOutput']
        member = {'shape': shape_name}
        if documentation is not None:
            member['documentation'] = documentation
        params_shape['members'][param_name] = member

        if is_required:
            required_list = params_shape.get('required', [])
            required_list.append(param_name)
            params_shape['required'] = required_list

    def assert_contains_lines_in_order(self, lines, contents=None):
        if contents is None:
            contents = self.doc_structure.flush_structure().decode('utf-8')
        for line in lines:
            self.assertIn(line, contents)
            beginning = contents.find(line)
            contents = contents[(beginning + len(line)):]

    def assert_not_contains_lines(self, lines):
        contents = self.doc_structure.flush_structure().decode('utf-8')
        for line in lines:
            self.assertNotIn(line, contents)