Esempio n. 1
0
 def test_derive_name(self):
     """
     Test the naming of a replicated resource. Snake case is converted to camel case and added
     as a suffix to the base name.
     """
     obj = Substitutor('Base', None)
     assert obj.name('resource_one') == 'BaseResourceOne'
Esempio n. 2
0
    def test_substitute_list_form(self):
        """
        Test substitution of replication variables in a CloudFormation sub function. In this case
        the sub command is given in list form, the first entry the string expression, the second
        entry a dictionary.

        Replication variables are prefixed by repl_ in the string expression. Any replication
        variables already present in the dictionary are overwritten if present in the dictionary.
        """
        obj = Substitutor('Base', None)

        variables = {'variable1': 'value1', 'variable2': 'value2'}

        cloudformation = [
            '${repl_variable1}-${repl_variable2}-${variable3}', {
                'repl_variable2': 'value4',
                'variable3': 'value3'
            }
        ]
        result = obj.substitute(variables, cloudformation)

        assert result == [
            '${repl_variable1}-${repl_variable2}-${variable3}', {
                'repl_variable1': 'value1',
                'repl_variable2': 'value2',
                'variable3': 'value3'
            }
        ]
Esempio n. 3
0
    def test_traverse(self, replications):
        base = {
            'Type': 'AWS::Service::Resource',
            'Properties': {
                'Property1': {
                    'Fn::Sub': '${repl_variable1}-${repl_variable2}'
                }
            }
        }

        obj = Substitutor('Base', base)

        obj.traverse(replications['resource_one'], base)

        assert base == {
            'Type': 'AWS::Service::Resource',
            'Properties': {
                'Property1': {
                    'Fn::Sub': [
                        '${repl_variable1}-${repl_variable2}', {
                            'repl_variable1': 'foo',
                            'repl_variable2': 'bar'
                        }
                    ]
                }
            }
        }
Esempio n. 4
0
    def test_traverse_list(self, replications):
        obj = Substitutor('Base', None)

        result = obj.traverse_list(
            replications['resource_one'],
            [{
                'Fn::Sub': '${repl_variable1}-${repl_variable2}'
            }, {
                'Fn::Sub': '${repl_variable2}-${repl_variable1}'
            }])

        assert result == [{
            'Fn::Sub': [
                '${repl_variable1}-${repl_variable2}', {
                    'repl_variable1': 'foo',
                    'repl_variable2': 'bar'
                }
            ]
        }, {
            'Fn::Sub': [
                '${repl_variable2}-${repl_variable1}', {
                    'repl_variable1': 'foo',
                    'repl_variable2': 'bar'
                }
            ]
        }]
Esempio n. 5
0
    def test_process(self, replications):
        base = {
            'Type': 'AWS::Service::Resource',
            'Properties': {
                'Property1': {
                    'Fn::Sub': [
                        '${repl_variable1}-${repl_variable2}', {
                            'repl_variable2': 'test'
                        }
                    ]
                }
            }
        }

        obj = Substitutor('Base', base)

        resources = obj.process(replications)

        assert resources == {
            'BaseResourceOne': {
                'Type': 'AWS::Service::Resource',
                'Properties': {
                    'Property1': {
                        'Fn::Sub': [
                            '${repl_variable1}-${repl_variable2}', {
                                'repl_variable1': 'foo',
                                'repl_variable2': 'bar'
                            }
                        ]
                    }
                }
            },
            'BaseResourceTwo': {
                'Type': 'AWS::Service::Resource',
                'Properties': {
                    'Property1': {
                        'Fn::Sub': [
                            '${repl_variable1}-${repl_variable2}', {
                                'repl_variable1': 'fuu',
                                'repl_variable2': 'bor'
                            }
                        ]
                    }
                }
            }
        }
Esempio n. 6
0
    def test_substitute_string_form(self):
        """
        Test substitution of replication variables in a CloudFormation sub function. In this case
        the sub command is given in string form. It is converted to list form with any replication
        variable added to the dictionary (second list entry).
        """
        obj = Substitutor('Base', None)

        variables = {'variable1': 'value1', 'variable2': 'value2'}

        cloudformation = '${repl_variable1}-${repl_variable2}-${variable3}'
        result = obj.substitute(variables, cloudformation)

        assert result == [
            '${repl_variable1}-${repl_variable2}-${variable3}', {
                'repl_variable1': 'value1',
                'repl_variable2': 'value2'
            }
        ]
Esempio n. 7
0
    def test_substitute_nested(self):
        """
        Test substitution of replication variables in a CloudFormation sub function. In this case
        the sub command is given in list form. In addition, one of the entries in the dictionary,
        the second list entry, is itself a CloudFormation expression that needs to be dealt with
        recursively.
        """
        obj = Substitutor('Base', None)

        variables = {'variable1': 'value1', 'variable2': 'value2'}

        cloudformation = [
            '${repl_variable1}-${repl_variable2}-${variable3}', {
                'repl_variable2': 'value4',
                'variable3': {
                    'Fn::ImportValue': {
                        'Fn::Sub': 'again-${repl_variable1}'
                    }
                }
            }
        ]
        result = obj.substitute(variables, cloudformation)

        assert result == [
            '${repl_variable1}-${repl_variable2}-${variable3}', {
                'repl_variable1': 'value1',
                'repl_variable2': 'value2',
                'variable3': {
                    'Fn::ImportValue': {
                        'Fn::Sub': [
                            'again-${repl_variable1}', {
                                'repl_variable1': 'value1'
                            }
                        ]
                    }
                }
            }
        ]
Esempio n. 8
0
 def test_initialise_with_defaults(self):
     Substitutor('Base', None, {'variable': 'value'})
Esempio n. 9
0
 def test_initialise(self):
     Substitutor('Base', None)