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)
def prepare_plugins(plugins, parameters=None): """ Creates & returns a plugins object with the given list of plugins installed. In addition to the given plugins, we will also install a few "required" plugins that are necessary to provide complete support for SAM template spec. :param plugins: list of samtranslator.plugins.BasePlugin plugins: List of plugins to install :param parameters: Dictionary of parameter values :return samtranslator.plugins.SamPlugins: Instance of `SamPlugins` """ if parameters is None: parameters = {} required_plugins = [ DefaultDefinitionBodyPlugin(), make_implicit_rest_api_plugin(), make_implicit_http_api_plugin(), GlobalsPlugin(), make_policy_template_for_function_plugin(), ] plugins = [] if not plugins else plugins # If a ServerlessAppPlugin does not yet exist, create one and add to the beginning of the required plugins list. if not any(isinstance(plugin, ServerlessAppPlugin) for plugin in plugins): required_plugins.insert(0, ServerlessAppPlugin(parameters=parameters)) # Execute customer's plugins first before running SAM plugins. It is very important to retain this order because # other plugins will be dependent on this ordering. return SamPlugins(plugins + required_plugins)
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")
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")
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"})
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)
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")
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")
def test_plugin_accepts_flags(self): self.plugin = ServerlessAppPlugin(wait_for_template_active_status=True) self.assertEqual(self.plugin._wait_for_template_active_status, True)
def test_plugin_accepts_different_sar_client(self): client = boto3.client('serverlessrepo', endpoint_url='https://example.com') self.plugin = ServerlessAppPlugin(sar_client=client) self.assertEqual(self.plugin._sar_client, client) self.assertEqual(self.plugin._sar_client._endpoint, client._endpoint)
def setUp(self): client = boto3.client('serverlessrepo', region_name='us-east-1') self.plugin = ServerlessAppPlugin(sar_client=client)
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'])
def test_plugin_invalid_configuration_raises_exception(self): with self.assertRaises(InvalidPluginException): plugin = ServerlessAppPlugin(wait_for_template_active_status=True, validate_only=True)
def setUp(self): client = boto3.client("serverlessrepo", region_name="us-east-1") self.plugin = ServerlessAppPlugin(sar_client=client)
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)
def test_plugin_accepts_parameters(self): parameters = {"a": "b"} self.plugin = ServerlessAppPlugin(parameters=parameters) self.assertEqual(self.plugin._parameters, parameters)
def setUp(self): client = boto3.client('serverlessrepo', region_name='us-east-1') self.plugin = ServerlessAppPlugin(sar_client=client)
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'])