Exemplo n.º 1
0
    def test_delete_rule_list(self):
        schema = {
            'far': properties.Schema(
                properties.Schema.LIST,
                schema=properties.Schema(
                    properties.Schema.MAP,
                    schema={
                        'red': properties.Schema(
                            properties.Schema.STRING
                        ),
                        'check': properties.Schema(
                            properties.Schema.STRING
                        )
                    }
                )
            )}

        data = {
            'far': [{'red': 'blue'},
                    {'red': 'roses'}],
        }
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.DELETE,
            ['far', 'red'])

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far.red'))
        self.assertIsNone(tran.translate('far.red'))
        self.assertIsNone(tran.resolved_translations['far.red'])
Exemplo n.º 2
0
    def test_add_rule_invalid(self):
        schema = {
            'far': properties.Schema(
                properties.Schema.MAP,
                schema={
                    'red': properties.Schema(
                        properties.Schema.STRING
                    )
                }
            ),
            'bar': properties.Schema(
                properties.Schema.STRING
            )}

        data = {
            'far': 'tran',
            'bar': 'dak'
        }
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.ADD,
            ['far'],
            [props.get('bar')])

        tran = translation.Translation(props)
        tran.set_rules([rule])

        self.assertTrue(tran.has_translation('far'))
        ex = self.assertRaises(ValueError, tran.translate, 'far', 'tran')
        self.assertEqual('Incorrect translation rule using - cannot '
                         'resolve Add rule for non-list translation '
                         'value "far".', str(ex))
Exemplo n.º 3
0
 def test_resolve_rule_nested_list_populated(self):
     client_plugin, schema = self._test_resolve_rule_nested_list()
     data = {
         'instances': [{
             'networks': [{
                 'port': 'port1',
                 'net': 'net1'
             }]
         }]
     }
     props = properties.Properties(schema, data)
     rule = translation.TranslationRule(props,
                                        translation.TranslationRule.RESOLVE,
                                        ['instances', 'networks', 'port'],
                                        client_plugin=client_plugin,
                                        finder='find_name_id',
                                        entity='port')
     tran = translation.Translation(props)
     tran.set_rules([rule])
     self.assertTrue(tran.has_translation('instances.networks.port'))
     result = tran.translate('instances.0.networks.0.port',
                             data['instances'][0]['networks'][0]['port'])
     self.assertEqual('port1_id', result)
     self.assertEqual(
         'port1_id',
         tran.resolved_translations['instances.0.networks.0.port'])
Exemplo n.º 4
0
    def test_replace_rule_map_with_custom_value_path(self):
        schema = {
            'far': properties.Schema(
                properties.Schema.MAP,
                schema={
                    'red': properties.Schema(
                        properties.Schema.STRING
                    )
                }
            ),
            'bar': properties.Schema(
                properties.Schema.MAP
            )}

        data = {
            'far': {},
            'bar': {'red': 'dak'}
        }
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.REPLACE,
            ['far', 'red'],
            value_path=['bar'],
            custom_value_path=['red']
        )

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far.red'))
        result = tran.translate('far.red')
        self.assertEqual('dak', result)
        self.assertEqual('dak', tran.resolved_translations['far.red'])
Exemplo n.º 5
0
    def test_resolve_rule_other_with_ref(self):
        client_plugin, schema = self._test_resolve_rule()

        class rsrc(object):
            action = INIT = "INIT"

            def FnGetRefId(self):
                return 'resource_id'

        class DummyStack(dict):
            pass

        stack = DummyStack(another_res=rsrc())
        ref = hot_funcs.GetResource(stack, 'get_resource',
                                    'another_res')
        data = {'far': ref}
        props = properties.Properties(schema, data)
        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.RESOLVE,
            ['far'],
            client_plugin=client_plugin,
            finder='find_name_id')

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far'))
        result = tran.translate('far', data['far'])
        self.assertEqual('yellow', result)
Exemplo n.º 6
0
    def test_replace_rule_map_dont_exist(self):
        schema = {
            'far': properties.Schema(
                properties.Schema.MAP,
                schema={
                    'red': properties.Schema(
                        properties.Schema.STRING
                    )
                }
            ),
            'bar': properties.Schema(
                properties.Schema.STRING
            )}

        data = {
            'bar': 'dak'
        }
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.REPLACE,
            ['far', 'red'],
            props.get('bar'))

        tran = translation.Translation(props)
        tran.set_rules([rule])

        self.assertTrue(tran.has_translation('far.red'))
        result = tran.translate('far.red')
        self.assertEqual('dak', result)
        self.assertEqual('dak', tran.resolved_translations['far.red'])
Exemplo n.º 7
0
    def test_resolve_rule_other_with_get_attr(self):
        client_plugin, schema = self._test_resolve_rule()

        class DummyStack(dict):
            pass

        class rsrc(object):
            pass

        stack = DummyStack(another_res=rsrc())
        attr_func = cfn_funcs.GetAtt(stack, 'Fn::GetAtt',
                                     ['another_res', 'name'])
        data = {'far': attr_func}
        props = properties.Properties(schema, data)
        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.RESOLVE,
            ['far'],
            client_plugin=client_plugin,
            finder='find_name_id')

        tran = translation.Translation(props)
        tran.set_rules([rule], client_resolve=False)
        self.assertFalse(tran.store_translated_values)
        self.assertFalse(tran.has_translation('far'))
        result = tran.translate('far', 'no_check', data['far'])
        self.assertEqual('no_check', result)
        self.assertIsNone(tran.resolved_translations.get('far'))
Exemplo n.º 8
0
    def test_replace_rule_list_same(self):
        schema = {
            'far':
            properties.Schema(
                properties.Schema.LIST,
                schema=properties.Schema(
                    properties.Schema.MAP,
                    schema={
                        'red': properties.Schema(properties.Schema.STRING),
                        'blue': properties.Schema(properties.Schema.STRING)
                    }))
        }

        data = {'far': [{'blue': 'white'}, {'red': 'roses'}]}
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(props,
                                           translation.TranslationRule.REPLACE,
                                           ['far', 'red'], None, 'blue')

        tran = translation.Translation(props)
        tran.set_rules([rule])

        self.assertTrue(tran.has_translation('far.0.red'))
        result = tran.translate('far.0.red', data['far'][0].get('red'),
                                data['far'][0])
        self.assertEqual('white', result)
        self.assertEqual('white', tran.resolved_translations['far.0.red'])
        self.assertIsNone(tran.resolved_translations['far.0.blue'])
        self.assertTrue(tran.has_translation('far.1.red'))
        result = tran.translate('far.1.red', data['far'][1]['red'],
                                data['far'][1])
        self.assertEqual('roses', result)
        self.assertEqual('roses', tran.resolved_translations['far.1.red'])
        self.assertIsNone(tran.resolved_translations['far.1.blue'])
Exemplo n.º 9
0
    def test_replace_rule_list_different(self):
        schema = {
            'far':
            properties.Schema(properties.Schema.LIST,
                              schema=properties.Schema(
                                  properties.Schema.MAP,
                                  schema={
                                      'red':
                                      properties.Schema(
                                          properties.Schema.STRING)
                                  })),
            'bar':
            properties.Schema(properties.Schema.STRING)
        }

        data = {'far': [{'red': 'blue'}, {'red': 'roses'}], 'bar': 'dak'}
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(props,
                                           translation.TranslationRule.REPLACE,
                                           ['far', 'red'], props.get('bar'))

        tran = translation.Translation(props)
        tran.set_rules([rule])

        self.assertTrue(tran.has_translation('far.red'))
        result = tran.translate('far.0.red', data['far'][0]['red'])
        self.assertEqual('dak', result)
        self.assertEqual('dak', tran.resolved_translations['far.0.red'])
Exemplo n.º 10
0
    def test_translate_add(self):
        rules = [
            translation.TranslationRule(
                self.props,
                translation.TranslationRule.ADD,
                ['a', 'b'],
                value=['check']
            )
        ]

        tran = translation.Translation()
        tran.set_rules(rules)

        result = tran.translate('a.b', ['test'])
        self.assertEqual(['test', 'check'], result)
        self.assertEqual(['test', 'check'], tran.resolved_translations['a.b'])

        # Test without storing
        tran.resolved_translations = {}
        tran.store_translated_values = False
        result = tran.translate('a.b', ['test'])
        self.assertEqual(['test', 'check'], result)
        self.assertEqual({}, tran.resolved_translations)
        tran.store_translated_values = True

        # Test no prop_value
        self.assertEqual(['check'], tran.translate('a.b', None))

        # Check digits in path skipped for rule
        self.assertEqual(['test', 'check'], tran.translate('a.0.b', ['test']))
Exemplo n.º 11
0
    def test_add_rule_dont_exist(self):
        schema = {
            'far': properties.Schema(
                properties.Schema.LIST,
                schema=properties.Schema(
                    properties.Schema.MAP,
                    schema={
                        'red': properties.Schema(
                            properties.Schema.STRING
                        )
                    }
                )
            ),
            'bar': properties.Schema(
                properties.Schema.STRING
            )}

        data = {
            'bar': 'dak'
        }
        props = properties.Properties(schema, copy.copy(data))

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.ADD,
            ['far'],
            [{'red': props.get('bar')}])

        tran = translation.Translation(props)
        tran.set_rules([rule])

        self.assertTrue(tran.has_translation('far'))
        result = tran.translate('far')
        self.assertEqual([{'red': 'dak'}], result)
        self.assertEqual([{'red': 'dak'}], tran.resolved_translations['far'])
Exemplo n.º 12
0
    def test_set_no_resolve_rules(self):
        rules = [
            translation.TranslationRule(self.props,
                                        translation.TranslationRule.RESOLVE,
                                        ['a'],
                                        client_plugin=mock.ANY,
                                        finder='finder')
        ]

        tran = translation.Translation()
        tran.set_rules(rules, client_resolve=False)
        self.assertEqual({}, tran._rules)
Exemplo n.º 13
0
 def __init__(self, schema, data, resolver=lambda d: d, parent_name=None,
              context=None, section=None, translation=None,
              rsrc_description=None):
     self.props = dict((k, Property(s, k, context, path=parent_name))
                       for k, s in schema.items())
     self.resolve = resolver
     self.data = data
     self.error_prefix = [section] if section is not None else []
     self.parent_name = parent_name
     self.context = context
     self.translation = (trans.Translation(properties=self)
                         if translation is None else translation)
     self.rsrc_description = rsrc_description or None
Exemplo n.º 14
0
    def test_property_json_param_to_list_correct_translation(self):
        """Test case when list property with sub-schema takes json param."""
        schema = {
            'far': properties.Schema(properties.Schema.LIST,
                                     schema=properties.Schema(
                                         properties.Schema.MAP,
                                         schema={
                                             'bar': properties.Schema(
                                                 properties.Schema.STRING,
                                             ),
                                             'dar': properties.Schema(
                                                 properties.Schema.STRING
                                             )
                                         }
                                     ))
        }

        class DummyStack(dict):
            @property
            def parameters(self):
                return mock.Mock()

        param = hot_funcs.GetParam(DummyStack(json_far='json_far'),
                                   'get_param',
                                   'json_far')
        param.parameters = {
            'json_far': parameters.JsonParam(
                'json_far',
                {'Type': 'Json'},
                '{"dar": "rad"}').value()}
        data = {'far': [param]}

        props = properties.Properties(schema, data, resolver=function.resolve)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.REPLACE,
            ['far', 'bar'],
            value_name='dar')

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far.0.bar'))
        prop_data = props['far']
        result = tran.translate('far.0.bar', prop_data[0]['bar'],
                                prop_data[0])
        self.assertEqual('rad', result)
        self.assertEqual('rad', tran.resolved_translations['far.0.bar'])
Exemplo n.º 15
0
    def test_resolve_rule_other(self):
        client_plugin, schema = self._test_resolve_rule()
        data = {'far': 'one'}
        props = properties.Properties(schema, data)
        rule = translation.TranslationRule(props,
                                           translation.TranslationRule.RESOLVE,
                                           ['far'],
                                           client_plugin=client_plugin,
                                           finder='find_name_id')

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far'))
        result = tran.translate('far', data['far'])
        self.assertEqual('yellow', result)
        self.assertEqual('yellow', tran.resolved_translations['far'])
Exemplo n.º 16
0
    def test_delete_rule_other(self):
        schema = {'far': properties.Schema(properties.Schema.STRING)}

        data = {'far': 'one'}

        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(props,
                                           translation.TranslationRule.DELETE,
                                           ['far'])

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far'))
        self.assertIsNone(tran.translate('far'))
        self.assertIsNone(tran.resolved_translations['far'])
Exemplo n.º 17
0
    def test_add_rule_list_with_custom_value_path(self):
        schema = {
            'far':
            properties.Schema(properties.Schema.LIST,
                              schema=properties.Schema(
                                  properties.Schema.MAP,
                                  schema={
                                      'red':
                                      properties.Schema(
                                          properties.Schema.LIST,
                                          schema=properties.Schema(
                                              properties.Schema.STRING)),
                                      'blue':
                                      properties.Schema(properties.Schema.MAP)
                                  }))
        }

        data = {
            'far': [{
                'blue': {
                    'black': {
                        'white': 'daisy',
                        'check': ['one']
                    }
                }
            }, {
                'red': ['roses']
            }]
        }
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.ADD, ['far', 'red'],
            value_name='blue',
            custom_value_path=['black', 'check'])

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far.0.red'))
        result = tran.translate('far.0.red', data['far'][0].get('red'),
                                data['far'][0])
        self.assertEqual(['one'], result)
        self.assertEqual(['one'], tran.resolved_translations['far.0.red'])
        self.assertEqual(['roses'],
                         tran.translate('far.1.red', data['far'][1]['red'],
                                        data['far'][1]))
Exemplo n.º 18
0
    def test_resolve_rule_other_with_function(self):
        client_plugin, schema = self._test_resolve_rule()
        join_func = cfn_funcs.Join(None, 'Fn::Join', ['.', ['bar', 'baz']])
        data = {'far': join_func}
        props = properties.Properties(schema, data)
        rule = translation.TranslationRule(props,
                                           translation.TranslationRule.RESOLVE,
                                           ['far'],
                                           client_plugin=client_plugin,
                                           finder='find_name_id')

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far'))
        result = tran.translate('far', data['far'])
        self.assertEqual('yellow', result)
        self.assertEqual('yellow', tran.resolved_translations['far'])
Exemplo n.º 19
0
    def test_translate_delete(self):
        rules = [
            translation.TranslationRule(self.props,
                                        translation.TranslationRule.DELETE,
                                        ['a'])
        ]

        tran = translation.Translation()
        tran.set_rules(rules)

        self.assertIsNone(tran.translate('a'))
        self.assertIsNone(tran.resolved_translations['a'])

        # Test without storing
        tran.resolved_translations = {}
        tran.store_translated_values = False
        self.assertIsNone(tran.translate('a'))
        self.assertEqual({}, tran.resolved_translations)
        tran.store_translated_values = True
Exemplo n.º 20
0
    def test_property_commadelimitedlist_param_correct_translation(self):
        """Test when property with sub-schema takes comma_delimited_list."""
        schema = {
            'far': properties.Schema(
                properties.Schema.LIST,
                schema=properties.Schema(
                    properties.Schema.STRING,
                )
            ),
            'boo': properties.Schema(
                properties.Schema.STRING
            )}

        class DummyStack(dict):
            @property
            def parameters(self):
                return mock.Mock()

        param = hot_funcs.GetParam(DummyStack(list_far='list_far'),
                                   'get_param',
                                   'list_far')
        param.parameters = {
            'list_far': parameters.CommaDelimitedListParam(
                'list_far',
                {'Type': 'CommaDelimitedList'},
                "white,roses").value()}
        data = {'far': param, 'boo': 'chrysanthemums'}

        props = properties.Properties(schema, data, resolver=function.resolve)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.ADD,
            ['far'],
            [props.get('boo')])

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far'))
        result = tran.translate('far', props['far'])
        self.assertEqual(['white', 'roses', 'chrysanthemums'], result)
        self.assertEqual(['white', 'roses', 'chrysanthemums'],
                         tran.resolved_translations['far'])
Exemplo n.º 21
0
    def test_replace_rule_str(self):
        schema = {
            'far': properties.Schema(properties.Schema.STRING),
            'bar': properties.Schema(properties.Schema.STRING)
        }

        data = {'far': 'one', 'bar': 'two'}

        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(props,
                                           translation.TranslationRule.REPLACE,
                                           ['bar'], props.get('far'))

        tran = translation.Translation(props)
        tran.set_rules([rule])

        self.assertTrue(tran.has_translation('bar'))
        result = tran.translate('bar', data['bar'])
        self.assertEqual('one', result)
        self.assertEqual('one', tran.resolved_translations['bar'])
Exemplo n.º 22
0
    def test_replace_rule_list_with_custom_value_path(self):
        schema = {
            'far':
            properties.Schema(properties.Schema.LIST,
                              schema=properties.Schema(
                                  properties.Schema.MAP,
                                  schema={
                                      'red':
                                      properties.Schema(
                                          properties.Schema.STRING),
                                      'blue':
                                      properties.Schema(properties.Schema.MAP)
                                  }))
        }

        data = {
            'far': [{
                'blue': {
                    'black': {
                        'white': 'daisy'
                    }
                }
            }, {
                'red': 'roses'
            }]
        }
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.REPLACE, ['far', 'red'],
            value_name='blue',
            custom_value_path=['black', 'white'])

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far.0.red'))
        result = tran.translate('far.0.red', prop_data=data['far'][0])
        self.assertEqual('daisy', result)
        self.assertEqual('daisy', tran.resolved_translations['far.0.red'])
Exemplo n.º 23
0
    def test_list_list_add_translation_rule(self):
        schema = {
            'far': properties.Schema(
                properties.Schema.LIST,
                schema=properties.Schema(
                    properties.Schema.MAP,
                    schema={
                        'bar': properties.Schema(
                            properties.Schema.LIST,
                            schema=properties.Schema(properties.Schema.STRING)
                        ),
                        'car': properties.Schema(properties.Schema.STRING)
                    }
                )
            )
        }

        data = {'far': [{'bar': ['shar'], 'car': 'man'}, {'car': 'first'}]}

        props = properties.Properties(schema, data, resolver=function.resolve)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.ADD,
            ['far', 'bar'],
            value_name='car'
        )

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far.0.bar'))
        result = tran.translate('far.0.bar', props['far'][0]['bar'],
                                props['far'][0])
        self.assertEqual(['shar', 'man'], result)
        self.assertEqual(['shar', 'man'],
                         tran.resolved_translations['far.0.bar'])
        result = tran.translate('far.1.bar', prop_data=props['far'][1])
        self.assertEqual(['first'], result)
        self.assertEqual(['first'], tran.resolved_translations['far.1.bar'])
Exemplo n.º 24
0
    def test_replace_rule_str_value_path(self):
        schema = {
            'far': properties.Schema(properties.Schema.STRING),
            'bar': properties.Schema(properties.Schema.STRING)
        }

        props = properties.Properties(schema, {'far': 'one'})

        rule = translation.TranslationRule(props,
                                           translation.TranslationRule.REPLACE,
                                           ['bar'],
                                           value_path=['far'])

        props = properties.Properties(schema, {'far': 'one'})
        tran = translation.Translation(props)
        tran.set_rules([rule])

        self.assertTrue(tran.has_translation('bar'))
        result = tran.translate('bar')
        self.assertEqual('one', result)
        self.assertEqual('one', tran.resolved_translations['bar'])
        self.assertIsNone(tran.resolved_translations['far'])
Exemplo n.º 25
0
    def test_resolve_rule_list_strings(self):
        client_plugin, schema = self._test_resolve_rule()
        data = {'far': ['one', 'rose']}
        schema = {
            'far':
            properties.Schema(properties.Schema.LIST,
                              schema=properties.Schema(
                                  properties.Schema.STRING))
        }
        props = properties.Properties(schema, data)
        rule = translation.TranslationRule(props,
                                           translation.TranslationRule.RESOLVE,
                                           ['far'],
                                           client_plugin=client_plugin,
                                           finder='find_name_id')

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far'))
        result = tran.translate('far', data['far'])
        self.assertEqual(['yellow', 'pink'], result)
        self.assertEqual(['yellow', 'pink'], tran.resolved_translations['far'])
Exemplo n.º 26
0
    def test_resolve_rule_list_populated(self):
        client_plugin, schema = self._test_resolve_rule(is_list=True)
        data = {
            'far': [{'red': 'blue'},
                    {'red': 'roses'}],
        }
        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.RESOLVE,
            ['far', 'red'],
            client_plugin=client_plugin,
            finder='find_name_id'
            )

        tran = translation.Translation(props)
        tran.set_rules([rule])
        self.assertTrue(tran.has_translation('far.red'))
        result = tran.translate('far.0.red', data['far'][0]['red'])
        self.assertEqual('yellow', result)
        self.assertEqual('yellow', tran.resolved_translations['far.0.red'])
Exemplo n.º 27
0
    def test_replace_rule_str_value_path_error(self):
        schema = {
            'far': properties.Schema(properties.Schema.STRING),
            'bar': properties.Schema(properties.Schema.STRING)
        }

        data = {'far': 'one', 'bar': 'two'}

        props = properties.Properties(schema, data)

        rule = translation.TranslationRule(
            props,
            translation.TranslationRule.REPLACE,
            ['bar'],
            value_path=['far'])

        tran = translation.Translation(props)
        tran.set_rules([rule])

        self.assertTrue(tran.has_translation('bar'))
        ex = self.assertRaises(exception.StackValidationFailed,
                               tran.translate, 'bar', data['bar'])
        self.assertEqual('Cannot define the following properties at '
                         'the same time: bar, far', str(ex))
Exemplo n.º 28
0
 def test_set_rules_none(self):
     tran = translation.Translation()
     self.assertEqual({}, tran._rules)