Example #1
0
class TestServerlessAppPlugin_on_before_transform_template_translate(TestCase):
    def setUp(self):
        client = boto3.client("serverlessrepo", region_name="us-east-1")
        self.plugin = ServerlessAppPlugin(sar_client=client)

    @patch(
        "samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch("botocore.client.BaseClient._make_api_call",
           mock_create_cloud_formation_template)
    @patch("botocore.client.ClientEndpointBridge._check_default_region",
           mock_get_region)
    def test_must_process_applications(self, SamTemplateMock):

        self.plugin = ServerlessAppPlugin(
            sar_client=boto3.client("serverlessrepo"))
        template_dict = {"a": "b"}
        app_resources = [
            ("id1", ApplicationResource(app_id="id1")),
            ("id2", ApplicationResource(app_id="id2")),
            ("id3", ApplicationResource()),
        ]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with(
            {"AWS::Serverless::Application"})

    @patch(
        "samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch("botocore.client.BaseClient._make_api_call", mock_get_application)
    @patch("botocore.client.ClientEndpointBridge._check_default_region",
           mock_get_region)
    def test_must_process_applications_validate(self, SamTemplateMock):

        self.plugin = ServerlessAppPlugin(validate_only=True)
        template_dict = {"a": "b"}
        app_resources = [
            ("id1", ApplicationResource(app_id="id1")),
            ("id2", ApplicationResource(app_id="id2")),
            ("id3", ApplicationResource()),
        ]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with(
            {"AWS::Serverless::Application"})

    @patch(
        "samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch("botocore.client.BaseClient._make_api_call",
           mock_create_cloud_formation_template)
    @patch("botocore.client.ClientEndpointBridge._check_default_region",
           mock_get_region)
    def test_process_invalid_applications(self, SamTemplateMock):
        self.plugin = ServerlessAppPlugin(
            sar_client=boto3.client("serverlessrepo", region_name="us-east-1"))
        template_dict = {"a": "b"}
        app_resources = [
            ("id1", ApplicationResource(app_id="")),
            ("id2", ApplicationResource(app_id=None)),
            ("id3", ApplicationResource(app_id="id3", semver=None)),
        ]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with(
            {"AWS::Serverless::Application"})

    @patch(
        "samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch("botocore.client.BaseClient._make_api_call", mock_get_application)
    @patch("botocore.client.ClientEndpointBridge._check_default_region",
           mock_get_region)
    def test_process_invalid_applications_validate(self, SamTemplateMock):
        self.plugin = ServerlessAppPlugin(validate_only=True)
        template_dict = {"a": "b"}
        app_resources = [
            ("id1", ApplicationResource(app_id="")),
            ("id2", ApplicationResource(app_id=None)),
            ("id3", ApplicationResource(app_id="id3", semver=None)),
        ]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with(
            {"AWS::Serverless::Application"})

    @patch("botocore.client.ClientEndpointBridge._check_default_region",
           mock_get_region)
    def test_sar_service_calls(self):
        service_call_lambda = mock_get_application
        logical_id = "logical_id"
        app_id = "app_id"
        semver = "1.0.0"
        response = self.plugin._sar_service_call(service_call_lambda,
                                                 logical_id, app_id, semver)
        self.assertEqual(app_id, response["ApplicationId"])

    def test_resolve_intrinsics(self):
        self.plugin = ServerlessAppPlugin(
            parameters={"AWS::Region": "us-east-1"})
        mappings = {"MapA": {"us-east-1": {"SecondLevelKey1": "value1"}}}
        input = {
            "Fn::FindInMap":
            ["MapA", {
                "Ref": "AWS::Region"
            }, "SecondLevelKey1"]
        }
        intrinsic_resolvers = self.plugin._get_intrinsic_resolvers(mappings)
        output = self.plugin._resolve_location_value(input,
                                                     intrinsic_resolvers)

        self.assertEqual("value1", output)
Example #2
0
class TestServerlessAppPlugin_on_before_transform_template_translate(TestCase):
    def setUp(self):
        client = boto3.client('serverlessrepo', region_name='us-east-1')
        self.plugin = ServerlessAppPlugin(sar_client=client)

    @patch(
        "samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch('botocore.client.BaseClient._make_api_call',
           mock_create_cloud_formation_template)
    @patch('botocore.client.ClientEndpointBridge._check_default_region',
           mock_get_region)
    def test_must_process_applications(self, SamTemplateMock):

        self.plugin = ServerlessAppPlugin(
            sar_client=boto3.client('serverlessrepo'))
        template_dict = {"a": "b"}
        app_resources = [("id1", ApplicationResource(app_id='id1')),
                         ("id2", ApplicationResource(app_id='id2')),
                         ("id3", ApplicationResource())]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Application")

    @patch(
        "samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch('botocore.client.BaseClient._make_api_call', mock_get_application)
    @patch('botocore.client.ClientEndpointBridge._check_default_region',
           mock_get_region)
    def test_must_process_applications_validate(self, SamTemplateMock):

        self.plugin = ServerlessAppPlugin(validate_only=True)
        template_dict = {"a": "b"}
        app_resources = [("id1", ApplicationResource(app_id='id1')),
                         ("id2", ApplicationResource(app_id='id2')),
                         ("id3", ApplicationResource())]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Application")

    @patch(
        "samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch('botocore.client.BaseClient._make_api_call',
           mock_create_cloud_formation_template)
    @patch('botocore.client.ClientEndpointBridge._check_default_region',
           mock_get_region)
    def test_process_invalid_applications(self, SamTemplateMock):
        self.plugin = ServerlessAppPlugin(
            sar_client=boto3.client('serverlessrepo', region_name='us-east-1'))
        template_dict = {"a": "b"}
        app_resources = [("id1", ApplicationResource(app_id='')),
                         ("id2", ApplicationResource(app_id=None))]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Application")

    @patch(
        "samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch('botocore.client.BaseClient._make_api_call', mock_get_application)
    @patch('botocore.client.ClientEndpointBridge._check_default_region',
           mock_get_region)
    def test_process_invalid_applications_validate(self, SamTemplateMock):
        self.plugin = ServerlessAppPlugin(validate_only=True)
        template_dict = {"a": "b"}
        app_resources = [("id1", ApplicationResource(app_id='')),
                         ("id2", ApplicationResource(app_id=None))]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Application")

    @patch('botocore.client.ClientEndpointBridge._check_default_region',
           mock_get_region)
    def test_sar_service_calls(self):
        service_call_lambda = mock_get_application
        logical_id = 'logical_id'
        app_id = 'app_id'
        semver = '1.0.0'
        response = self.plugin._sar_service_call(service_call_lambda,
                                                 logical_id, app_id, semver)
        self.assertEqual(app_id, response['ApplicationId'])
class TestServerlessAppPlugin_on_before_transform_template_translate(TestCase):


    def setUp(self):
        client = boto3.client('serverlessrepo', region_name='us-east-1')
        self.plugin = ServerlessAppPlugin(sar_client=client)

    @patch("samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch('botocore.client.BaseClient._make_api_call', mock_create_cloud_formation_template)
    @patch('botocore.client.ClientEndpointBridge._check_default_region', mock_get_region)
    def test_must_process_applications(self, SamTemplateMock):

        self.plugin = ServerlessAppPlugin(sar_client=boto3.client('serverlessrepo'))
        template_dict = {"a": "b"}
        app_resources = [("id1", ApplicationResource(app_id = 'id1')), ("id2", ApplicationResource(app_id='id2')), ("id3", ApplicationResource())]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Application")


    @patch("samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch('botocore.client.BaseClient._make_api_call', mock_get_application)
    @patch('botocore.client.ClientEndpointBridge._check_default_region', mock_get_region)
    def test_must_process_applications_validate(self, SamTemplateMock):

        self.plugin = ServerlessAppPlugin(validate_only=True)
        template_dict = {"a": "b"}
        app_resources = [("id1", ApplicationResource(app_id = 'id1')), ("id2", ApplicationResource(app_id='id2')), ("id3", ApplicationResource())]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Application")
        

    @patch("samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch('botocore.client.BaseClient._make_api_call', mock_create_cloud_formation_template)
    @patch('botocore.client.ClientEndpointBridge._check_default_region', mock_get_region)
    def test_process_invalid_applications(self, SamTemplateMock):
        self.plugin = ServerlessAppPlugin(sar_client=boto3.client('serverlessrepo', region_name='us-east-1'))
        template_dict = {"a": "b"}
        app_resources = [("id1", ApplicationResource(app_id = '')), ("id2", ApplicationResource(app_id=None))]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Application")


    @patch("samtranslator.plugins.application.serverless_app_plugin.SamTemplate")
    @patch('botocore.client.BaseClient._make_api_call', mock_get_application)
    @patch('botocore.client.ClientEndpointBridge._check_default_region', mock_get_region)
    def test_process_invalid_applications_validate(self, SamTemplateMock):
        self.plugin = ServerlessAppPlugin(validate_only=True)
        template_dict = {"a": "b"}
        app_resources = [("id1", ApplicationResource(app_id = '')), ("id2", ApplicationResource(app_id=None))]

        sam_template = Mock()
        SamTemplateMock.return_value = sam_template
        sam_template.iterate = Mock()
        sam_template.iterate.return_value = app_resources

        self.plugin.on_before_transform_template(template_dict)

        SamTemplateMock.assert_called_with(template_dict)

        # Make sure this is called only for Apis
        sam_template.iterate.assert_called_with("AWS::Serverless::Application")

    @patch('botocore.client.ClientEndpointBridge._check_default_region', mock_get_region)
    def test_sar_service_calls(self):
        service_call_lambda = mock_get_application
        logical_id = 'logical_id'
        app_id = 'app_id'
        semver = '1.0.0'
        response = self.plugin._sar_service_call(service_call_lambda, logical_id, app_id, semver)
        self.assertEquals(app_id, response['ApplicationId'])