def test_from_dict_with_non_dict_hooks_raises_invalid_resource_exception(
         self):
     with self.assertRaises(InvalidResourceException):
         DeploymentPreference.from_dict("logical_id", {
             "Type": "Canary",
             "Hooks": "badhook"
         })
    def test_sam_function_without_alias_allows_disabled_deployment_preference(
            self):
        enabled = False
        deploy_preference_dict = {"Enabled": enabled}
        func = {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "CodeUri": self.code_uri,
                "Runtime": "nodejs4.3",
                "Handler": "index.handler",
                "DeploymentPreference": deploy_preference_dict
            }
        }

        sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func)

        kwargs = dict()
        kwargs["managed_policy_map"] = {"a": "b"}
        kwargs["event_resources"] = []
        kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock
        kwargs["mappings_resolver"] = self.mappings_resolver_mock

        preference_collection = self._make_deployment_preference_collection()
        preference_collection.get.return_value = DeploymentPreference.from_dict(
            sam_func.logical_id, deploy_preference_dict)

        kwargs['deployment_preference_collection'] = preference_collection
        self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = enabled
        resources = sam_func.to_cloudformation(**kwargs)

        self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(
            enabled)
        # Function, IAM Role
        self.assertEqual(len(resources), 2)
    def test_from_dict_with_intrinsic_function_type(self):

        type = {"Ref": "SomeType"}
        expected_deployment_preference = DeploymentPreference(
            type,
            self.pre_traffic_hook,
            self.post_traffic_hook,
            self.alarms,
            True,
            self.role,
            self.trigger_configurations,
        )

        deployment_preference_yaml_dict = dict()
        deployment_preference_yaml_dict["Type"] = type
        deployment_preference_yaml_dict["Hooks"] = {
            "PreTraffic": self.pre_traffic_hook,
            "PostTraffic": self.post_traffic_hook,
        }
        deployment_preference_yaml_dict["Alarms"] = self.alarms
        deployment_preference_yaml_dict["Role"] = self.role
        deployment_preference_yaml_dict["TriggerConfigurations"] = self.trigger_configurations
        deployment_preference_from_yaml_dict = DeploymentPreference.from_dict(
            "logical_id", deployment_preference_yaml_dict
        )

        self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict)
    def test_sam_function_without_alias_allows_disabled_deployment_preference(self):
        enabled = False
        deploy_preference_dict = {"Enabled": enabled}
        func = {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "CodeUri": self.code_uri,
                "Runtime": "nodejs4.3",
                "Handler": "index.handler",
                "DeploymentPreference": deploy_preference_dict
            }
        }

        sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func)

        kwargs = dict()
        kwargs["managed_policy_map"] = {"a": "b"}
        kwargs["event_resources"] = []
        kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock
        preference_collection = self._make_deployment_preference_collection()
        preference_collection.get.return_value = DeploymentPreference.from_dict(sam_func.logical_id,
                                                                                deploy_preference_dict)

        kwargs['deployment_preference_collection'] = preference_collection
        self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = enabled
        resources = sam_func.to_cloudformation(**kwargs)

        self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled)
        # Function, IAM Role
        self.assertEquals(len(resources), 2)
Exemple #5
0
    def test_from_dict_with_disabled_preference_does_not_require_other_parameters(self):
        expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None)

        deployment_preference_yaml_dict = dict()
        deployment_preference_yaml_dict['Enabled'] = False
        deployment_preference_from_yaml_dict = DeploymentPreference.from_dict('logical_id',
                                                                              deployment_preference_yaml_dict)

        self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict)
    def test_from_dict_with_disabled_preference_does_not_require_other_parameters(self):
        expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None)

        deployment_preference_yaml_dict = dict()
        deployment_preference_yaml_dict['Enabled'] = False
        deployment_preference_from_yaml_dict = DeploymentPreference.from_dict('logical_id',
                                                                              deployment_preference_yaml_dict)

        self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict)
Exemple #7
0
    def test_from_dict(self):
        deployment_preference_yaml_dict = dict()
        deployment_preference_yaml_dict['Type'] = self.deployment_type
        deployment_preference_yaml_dict['Hooks'] = {'PreTraffic': self.pre_traffic_hook, 'PostTraffic': self.post_traffic_hook}
        deployment_preference_yaml_dict['Alarms'] = self.alarms
        deployment_preference_yaml_dict['Role'] = self.role
        deployment_preference_from_yaml_dict = DeploymentPreference.from_dict('logical_id', deployment_preference_yaml_dict)

        self.assertEqual(self.expected_deployment_preference, deployment_preference_from_yaml_dict)
    def test_from_dict_with_lowercase_string_disabled_preference_does_not_require_other_parameters(
            self):
        expected_deployment_preference = DeploymentPreference(
            None, None, None, None, False, None, None)

        deployment_preference_yaml_dict = dict()
        deployment_preference_yaml_dict["Enabled"] = "false"
        deployment_preference_from_yaml_dict = DeploymentPreference.from_dict(
            "logical_id", deployment_preference_yaml_dict)

        self.assertEqual(expected_deployment_preference,
                         deployment_preference_from_yaml_dict)
Exemple #9
0
    def test_from_dict_with_intrinsic_function_type(self):

        type = {"Ref": "SomeType"}
        expected_deployment_preference = DeploymentPreference(type, self.pre_traffic_hook,
                                                              self.post_traffic_hook, self.alarms, True, self.role)

        deployment_preference_yaml_dict = dict()
        deployment_preference_yaml_dict['Type'] = type
        deployment_preference_yaml_dict['Hooks'] = {'PreTraffic': self.pre_traffic_hook, 'PostTraffic': self.post_traffic_hook}
        deployment_preference_yaml_dict['Alarms'] = self.alarms
        deployment_preference_yaml_dict['Role'] = self.role
        deployment_preference_from_yaml_dict = DeploymentPreference.from_dict('logical_id', deployment_preference_yaml_dict)

        self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict)
    def test_from_dict(self):
        deployment_preference_yaml_dict = dict()
        deployment_preference_yaml_dict["Type"] = self.deployment_type
        deployment_preference_yaml_dict["Hooks"] = {
            "PreTraffic": self.pre_traffic_hook,
            "PostTraffic": self.post_traffic_hook,
        }
        deployment_preference_yaml_dict["Alarms"] = self.alarms
        deployment_preference_yaml_dict["Role"] = self.role
        deployment_preference_yaml_dict["TriggerConfigurations"] = self.trigger_configurations
        deployment_preference_from_yaml_dict = DeploymentPreference.from_dict(
            "logical_id", deployment_preference_yaml_dict
        )

        self.assertEqual(self.expected_deployment_preference, deployment_preference_from_yaml_dict)
    def test_sam_function_with_disabled_deployment_preference_does_not_add_update_policy(
            self, get_resolved_alias_name_mock):
        alias_name = "AliasName"
        enabled = False
        deploy_preference_dict = {"Enabled": enabled}
        func = {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "CodeUri": self.code_uri,
                "Runtime": "nodejs4.3",
                "Handler": "index.handler",
                "AutoPublishAlias": alias_name,
                "DeploymentPreference": deploy_preference_dict
            }
        }

        sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func)

        kwargs = dict()
        kwargs["managed_policy_map"] = {"a": "b"}
        kwargs["event_resources"] = []
        kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock
        kwargs["mappings_resolver"] = self.mappings_resolver_mock
        preference_collection = self._make_deployment_preference_collection()
        preference_collection.get.return_value = DeploymentPreference.from_dict(
            sam_func.logical_id, deploy_preference_dict)

        kwargs['deployment_preference_collection'] = preference_collection
        self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = enabled
        get_resolved_alias_name_mock.return_value = alias_name

        resources = sam_func.to_cloudformation(**kwargs)

        preference_collection.add.assert_called_once_with(
            sam_func.logical_id, deploy_preference_dict)
        preference_collection.get.assert_called_once_with(sam_func.logical_id)
        self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(
            enabled)
        aliases = [
            r.to_dict() for r in resources
            if r.resource_type == LambdaAlias.resource_type
        ]

        self.assertTrue("UpdatePolicy" not in list(aliases[0].values())[0])
    def test_sam_function_with_disabled_deployment_preference_does_not_add_update_policy(self, get_resolved_alias_name_mock):
        alias_name = "AliasName"
        enabled = False
        deploy_preference_dict = {"Enabled": enabled}
        func = {
            "Type": "AWS::Serverless::Function",
            "Properties": {
                "CodeUri": self.code_uri,
                "Runtime": "nodejs4.3",
                "Handler": "index.handler",
                "AutoPublishAlias": alias_name,
                "DeploymentPreference": deploy_preference_dict
            }
        }

        sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func)

        kwargs = dict()
        kwargs["managed_policy_map"] = {"a": "b"}
        kwargs["event_resources"] = []
        kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock
        preference_collection = self._make_deployment_preference_collection()
        preference_collection.get.return_value = DeploymentPreference.from_dict(sam_func.logical_id,
                                                                                deploy_preference_dict)

        kwargs['deployment_preference_collection'] = preference_collection
        self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = enabled
        get_resolved_alias_name_mock.return_value = alias_name

        resources = sam_func.to_cloudformation(**kwargs)

        preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict)
        preference_collection.get.assert_called_once_with(sam_func.logical_id)
        self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled)
        aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type]

        self.assertTrue("UpdatePolicy" not in list(aliases[0].values())[0])
 def test_from_dict_with_missing_type_raises_invalid_resource_exception(self):
     with self.assertRaises(InvalidResourceException):
         DeploymentPreference.from_dict("logical_id", dict())
Exemple #14
0
 def test_from_dict_with_non_dict_hooks_raises_invalid_resource_exception(self):
     with self.assertRaises(InvalidResourceException):
         DeploymentPreference.from_dict('logical_id', {'Type': 'Canary', 'Hooks': 'badhook'})
 def test_from_dict_with_missing_type_raises_invalid_resource_exception(self):
     with self.assertRaises(InvalidResourceException):
         DeploymentPreference.from_dict('logical_id', dict())
 def test_from_dict_with_non_dict_hooks_raises_invalid_resource_exception(self):
     with self.assertRaises(InvalidResourceException):
         DeploymentPreference.from_dict('logical_id', {'Type': 'Canary', 'Hooks': 'badhook'})