test_vars.py 3.51 KB
Newer Older
Toshio Kuratomi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
# (c) 2015, Toshio Kuraotmi <tkuratomi@ansible.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from collections import defaultdict

from ansible.compat.tests import mock, unittest
from ansible.errors import AnsibleError

from ansible.utils.vars import combine_vars, merge_hash

class TestVariableUtils(unittest.TestCase):

    test_merge_data = (
            dict(
                a=dict(a=1),
                b=dict(b=2),
                result=dict(a=1, b=2)
            ),
            dict(
                a=dict(a=1, c=dict(foo='bar')),
                b=dict(b=2, c=dict(baz='bam')),
                result=dict(a=1, b=2, c=dict(foo='bar', baz='bam'))
            ),
            dict(
                a=defaultdict(a=1, c=defaultdict(foo='bar')),
                b=dict(b=2, c=dict(baz='bam')),
                result=defaultdict(a=1, b=2, c=defaultdict(foo='bar', baz='bam'))
            ),
        )
    test_replace_data = (
            dict(
                a=dict(a=1),
                b=dict(b=2),
                result=dict(a=1, b=2)
            ),
            dict(
                a=dict(a=1, c=dict(foo='bar')),
                b=dict(b=2, c=dict(baz='bam')),
                result=dict(a=1, b=2, c=dict(baz='bam'))
            ),
            dict(
                a=defaultdict(a=1, c=dict(foo='bar')),
                b=dict(b=2, c=defaultdict(baz='bam')),
                result=defaultdict(a=1, b=2, c=defaultdict(baz='bam'))
            ),
        )

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_merge_hash(self):
        for test in self.test_merge_data:
            self.assertEqual(merge_hash(test['a'], test['b']), test['result'])

    def test_improper_args(self):
        with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'replace'):
            with self.assertRaises(AnsibleError):
                combine_vars([1, 2, 3], dict(a=1))
            with self.assertRaises(AnsibleError):
                combine_vars(dict(a=1), [1, 2, 3])

        with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'merge'):
            with self.assertRaises(AnsibleError):
                combine_vars([1, 2, 3], dict(a=1))
            with self.assertRaises(AnsibleError):
                combine_vars(dict(a=1), [1, 2, 3])

    def test_combine_vars_replace(self):
        with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'replace'):
            for test in self.test_replace_data:
                self.assertEqual(combine_vars(test['a'], test['b']), test['result'])

    def test_combine_vars_merge(self):
        with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'merge'):
            for test in self.test_merge_data:
                self.assertEqual(combine_vars(test['a'], test['b']), test['result'])