Exemplo n.º 1
0
    def test_must_run_translator_plugins(self, resolve_params_mock,
                                         SamTranslatorWrapperMock):
        translator_instance = SamTranslatorWrapperMock.return_value = Mock()

        template = {"Key": "Value"}
        overrides = {'some': 'value'}

        SamBaseProvider.get_template(template, overrides)

        SamTranslatorWrapperMock.assert_called_once_with(template)
        translator_instance.run_plugins.assert_called_once()
        resolve_params_mock.assert_called_once()
Exemplo n.º 2
0
    def test_must_run_translator_plugins(self, resolve_template_mock,
                                         SamTranslatorWrapperMock,
                                         resource_metadata_normalizer_patch):
        resource_metadata_normalizer_patch.normalize.return_value = True
        resolve_template_mock.return_value = {}
        translator_instance = SamTranslatorWrapperMock.return_value = Mock()

        template = {"Key": "Value"}
        overrides = {"some": "value"}

        SamBaseProvider.get_template(template, overrides)

        SamTranslatorWrapperMock.assert_called_once_with(template)
        translator_instance.run_plugins.assert_called_once()
Exemplo n.º 3
0
    def __init__(self, template_dict, parameter_overrides=None, cwd=None):
        """
        Initialize the class with SAM template data. The template_dict (SAM Templated) is assumed
        to be valid, normalized and a dictionary. template_dict should be normalized by running any and all
        pre-processing before passing to this class.
        This class does not perform any syntactic validation of the template.

        After the class is initialized, changes to ``template_dict`` will not be reflected in here.
        You will need to explicitly update the class with new template, if necessary.

        Parameters
        ----------
        template_dict : dict
            SAM Template as a dictionary
        cwd : str
            Optional working directory with respect to which we will resolve relative path to Swagger file
        """

        self.template_dict = SamBaseProvider.get_template(
            template_dict, parameter_overrides)
        self.resources = self.template_dict.get("Resources", {})

        LOG.debug("%d resources found in the template", len(self.resources))

        # Store a set of apis
        self.cwd = cwd
        self.apis = self._extract_apis(self.resources)

        LOG.debug("%d APIs found in the template", len(self.apis))
Exemplo n.º 4
0
    def test_must_skip_empty_template(self):
        template = {}
        override = None
        expected_template = {}

        self.assertEquals(SamBaseProvider._resolve_parameters(template, override),
                          expected_template)
Exemplo n.º 5
0
    def test_must_skip_empty_overrides(self):
        template = {"Key": {"Ref": "Param"}}
        override = None
        expected_template = {"Key": {"Ref": "Param"}}

        self.assertEquals(SamBaseProvider._resolve_parameters(template, override),
                          expected_template)
Exemplo n.º 6
0
    def test_with_pseudo_parameters(self, parameter, expected_value):

        template_dict = {"Key": {"Ref": parameter}}

        expected_template = {"Key": expected_value}

        result = SamBaseProvider._resolve_parameters(template_dict, {})
        self.assertEquals(result, expected_template)
Exemplo n.º 7
0
    def test_override_parameters(self):
        template = {
            "Parameters": {
                "Key1": {
                    "Default": "Value1"
                },
                "Key2": {
                    "Default": "Value2"
                },
                "NoDefaultKey3": {},
                "NoOverrideKey4": {}  # No override Value provided
            },
            "Resources": {
                "R1": {
                    "Ref": "Key1"
                },
                "R2": {
                    "Ref": "Key2"
                },
                "R3": {
                    "Ref": "NoDefaultKey3"
                },
                "R4": {
                    "Ref": "NoOverrideKey4"
                }
            }
        }

        override = {
            "Key1": "OverrideValue1",
            "Key2": "OverrideValue2",
            "NoDefaultKey3": "OverrideValue3"
        }

        expected_template = {
            "Parameters": {
                "Key1": {
                    "Default": "Value1"
                },
                "Key2": {
                    "Default": "Value2"
                },
                "NoDefaultKey3": {},
                "NoOverrideKey4": {}  # No override Value provided
            },
            "Resources": {
                "R1": "OverrideValue1",
                "R2": "OverrideValue2",
                "R3": "OverrideValue3",
                "R4": {
                    "Ref": "NoOverrideKey4"
                }
            }
        }

        self.assertEquals(
            SamBaseProvider._resolve_parameters(template, override),
            expected_template)
Exemplo n.º 8
0
    def test_must_run_translator_plugins(self,
                                         resolve_params_mock,
                                         SamTranslatorWrapperMock,
                                         resource_metadata_normalizer_patch):
        translator_instance = SamTranslatorWrapperMock.return_value = Mock()

        parameter_resolved_template = {"Key": "Value", "Parameter": "Resolved"}
        resolve_params_mock.return_value = parameter_resolved_template

        template = {"Key": "Value"}
        overrides = {'some': 'value'}

        SamBaseProvider.get_template(template, overrides)

        SamTranslatorWrapperMock.assert_called_once_with(template)
        translator_instance.run_plugins.assert_called_once()
        resolve_params_mock.assert_called_once()
        resource_metadata_normalizer_patch.normalize.assert_called_once_with(parameter_resolved_template)
Exemplo n.º 9
0
    def test_override_pseudo_parameters(self):
        template = {"Key": {"Ref": "AWS::Region"}}

        override = {"AWS::Region": "someregion"}

        expected_template = {"Key": "someregion"}

        self.assertEquals(
            SamBaseProvider._resolve_parameters(template, override),
            expected_template)
Exemplo n.º 10
0
    def test_override_parameters(self):
        template = {
            "Parameters": {
                "Key1": {
                    "Default": "Value1"
                },
                "Key2": {
                    "Default": "Value2"
                },
                "NoDefaultKey3": {},

                "NoOverrideKey4": {}   # No override Value provided
            },

            "Resources": {
                "R1": {"Ref": "Key1"},
                "R2": {"Ref": "Key2"},
                "R3": {"Ref": "NoDefaultKey3"},
                "R4": {"Ref": "NoOverrideKey4"}
            }
        }

        override = {
            "Key1": "OverrideValue1",
            "Key2": "OverrideValue2",
            "NoDefaultKey3": "OverrideValue3"
        }

        expected_template = {
            "Parameters": {
                "Key1": {
                    "Default": "Value1"
                },
                "Key2": {
                    "Default": "Value2"
                },
                "NoDefaultKey3": {},
                "NoOverrideKey4": {}   # No override Value provided
            },

            "Resources": {
                "R1": "OverrideValue1",
                "R2": "OverrideValue2",
                "R3": "OverrideValue3",
                "R4": {"Ref": "NoOverrideKey4"}
            }
        }

        self.assertEquals(SamBaseProvider._resolve_parameters(template, override),
                          expected_template)
Exemplo n.º 11
0
    def test_with_pseudo_parameters(self, parameter, expected_value):

        template_dict = {
            "Key": {
                "Ref": parameter
            }
        }

        expected_template = {
            "Key": expected_value
        }

        result = SamBaseProvider._resolve_parameters(template_dict, {})
        self.assertEquals(result, expected_template)
Exemplo n.º 12
0
    def test_must_skip_non_ref_intrinsics(self):
        template = {
            "Key1": {"Fn::Sub": ["${AWS::Region}"]},  # Sub is not implemented
            "Key2": {"Ref": "MyParam"}
        }

        override = {"MyParam": "MyValue"}

        expected_template = {
            "Key1": {"Fn::Sub": ["${AWS::Region}"]},
            "Key2": "MyValue"
        }

        self.assertEquals(SamBaseProvider._resolve_parameters(template, override),
                          expected_template)
Exemplo n.º 13
0
    def test_parameter_with_defaults(self):
        override = {}  # No overrides

        template = {
            "Parameters": {
                "Key1": {
                    "Default": "Value1"
                },
                "Key2": {
                    "Default": "Value2"
                },
                "NoDefaultKey3": {}  # No Default
            },
            "Resources": {
                "R1": {
                    "Ref": "Key1"
                },
                "R2": {
                    "Ref": "Key2"
                },
                "R3": {
                    "Ref": "NoDefaultKey3"
                }
            }
        }

        expected_template = {
            "Parameters": {
                "Key1": {
                    "Default": "Value1"
                },
                "Key2": {
                    "Default": "Value2"
                },
                "NoDefaultKey3": {}  # No Default
            },
            "Resources": {
                "R1": "Value1",
                "R2": "Value2",
                "R3": {
                    "Ref": "NoDefaultKey3"
                }  # No default value. so no subsitution
            }
        }

        self.assertEquals(
            SamBaseProvider._resolve_parameters(template, override),
            expected_template)
Exemplo n.º 14
0
    def test_override_pseudo_parameters(self):
        template = {
            "Key": {
                "Ref": "AWS::Region"
            }
        }

        override = {
            "AWS::Region": "someregion"
        }

        expected_template = {
            "Key": "someregion"
        }

        self.assertEquals(SamBaseProvider._resolve_parameters(template, override),
                          expected_template)
Exemplo n.º 15
0
    def test_parameter_with_defaults(self):
        override = {}  # No overrides

        template = {
            "Parameters": {
                "Key1": {
                    "Default": "Value1"
                },
                "Key2": {
                    "Default": "Value2"
                },
                "NoDefaultKey3": {}  # No Default
            },

            "Resources": {
                "R1": {"Ref": "Key1"},
                "R2": {"Ref": "Key2"},
                "R3": {"Ref": "NoDefaultKey3"}
            }
        }

        expected_template = {
            "Parameters": {
                "Key1": {
                    "Default": "Value1"
                },
                "Key2": {
                    "Default": "Value2"
                },
                "NoDefaultKey3": {}  # No Default
            },

            "Resources": {
                "R1": "Value1",
                "R2": "Value2",
                "R3": {"Ref": "NoDefaultKey3"}  # No default value. so no subsitution
            }
        }

        self.assertEquals(SamBaseProvider._resolve_parameters(template, override),
                          expected_template)