summaryrefslogtreecommitdiff
path: root/nova/tests/unit/test_json_ref.py
blob: 5a139055f5aa979cfe604ab343e22e08e85b92fb (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
import copy
import mock

from nova import test
from nova.tests import json_ref


class TestJsonRef(test.NoDBTestCase):

    def test_update_dict_recursively(self):
        input = {'foo': 1,
                 'bar': 13,
                 'a list': [],
                 'nesting': {
                     'baz': 42,
                     'foobar': 121
                 }}

        d = copy.deepcopy(input)
        json_ref._update_dict_recursively(d, {})
        self.assertDictEqual(input, d)

        d = copy.deepcopy(input)
        json_ref._update_dict_recursively(d, {'foo': 111,
                                              'new_key': 1,
                                              'nesting': {
                                                  'baz': 142,
                                                  'new_nested': 1
                                              }})
        expected = copy.deepcopy(input)
        expected['foo'] = 111
        expected['new_key'] = 1
        expected['nesting']['baz'] = 142
        expected['nesting']['new_nested'] = 1
        self.assertDictEqual(expected, d)

        d = copy.deepcopy(input)
        json_ref._update_dict_recursively(d, {'nesting': 1})
        expected = copy.deepcopy(input)
        expected['nesting'] = 1
        self.assertDictEqual(expected, d)
        d = copy.deepcopy(input)

    @mock.patch('oslo_serialization.jsonutils.load')
    @mock.patch('builtins.open', new_callable=mock.mock_open())
    def test_resolve_ref(self, mock_open, mock_json_load):
        mock_json_load.return_value = {'baz': 13}

        actual = json_ref.resolve_refs(
            {'foo': 1,
             'bar': {'$ref': 'another.json#'}},
            'some/base/path/')

        self.assertDictEqual({'foo': 1,
                              'bar': {'baz': 13}},
                             actual)
        mock_open.assert_called_once_with('some/base/path/another.json', 'r+b')

    @mock.patch('oslo_serialization.jsonutils.load')
    @mock.patch('builtins.open', new_callable=mock.mock_open())
    def test_resolve_ref_recursively(self, mock_open, mock_json_load):
        mock_json_load.side_effect = [
            # this is the content of direct_ref.json
            {'baz': 13,
             'nesting': {'$ref': 'subdir/nested_ref.json#'}},
            # this is the content of subdir/nested_ref.json
            {'a deep key': 'happiness'}]

        actual = json_ref.resolve_refs(
            {'foo': 1,
             'bar': {'$ref': 'direct_ref.json#'}},
            'some/base/path/')

        self.assertDictEqual({'foo': 1,
                              'bar': {'baz': 13,
                                      'nesting':
                                          {'a deep key': 'happiness'}}},
                             actual)
        mock_open.assert_any_call('some/base/path/direct_ref.json', 'r+b')
        mock_open.assert_any_call('some/base/path/subdir/nested_ref.json',
                                  'r+b')

    @mock.patch('oslo_serialization.jsonutils.load')
    @mock.patch('builtins.open', new_callable=mock.mock_open())
    def test_resolve_ref_with_override(self, mock_open, mock_json_load):
        mock_json_load.return_value = {'baz': 13,
                                       'boo': 42}

        actual = json_ref.resolve_refs(
            {'foo': 1,
             'bar': {'$ref': 'another.json#',
                     'boo': 0}},
            'some/base/path/')

        self.assertDictEqual({'foo': 1,
                              'bar': {'baz': 13,
                                      'boo': 0}},
                             actual)
        mock_open.assert_called_once_with('some/base/path/another.json', 'r+b')

    @mock.patch('oslo_serialization.jsonutils.load')
    @mock.patch('builtins.open', new_callable=mock.mock_open())
    def test_resolve_ref_with_nested_override(self, mock_open, mock_json_load):
        mock_json_load.return_value = {'baz': 13,
                                       'boo': {'a': 1,
                                               'b': 2}}

        actual = json_ref.resolve_refs(
            {'foo': 1,
             'bar': {'$ref': 'another.json#',
                     'boo': {'b': 3,
                             'c': 13}}},
            'some/base/path/')

        self.assertDictEqual({'foo': 1,
                              'bar': {'baz': 13,
                                      'boo': {'a': 1,
                                              'b': 3,
                                              'c': 13}}},
                             actual)
        mock_open.assert_called_once_with('some/base/path/another.json', 'r+b')

    @mock.patch('oslo_serialization.jsonutils.load')
    @mock.patch('builtins.open', new_callable=mock.mock_open())
    def test_resolve_ref_with_override_having_refs(self, mock_open,
                                                   mock_json_load):
        mock_json_load.side_effect = [
            {'baz': 13,
             'boo': 42},
            {'something': 0}]

        actual = json_ref.resolve_refs(
            {'foo': 1,
             'bar': {'$ref': 'another.json#',
                     'boo': {'$ref': 'override_ref.json#'}}},
            'some/base/path/')

        self.assertDictEqual({'foo': 1,
                              'bar': {'baz': 13,
                                      'boo': {'something': 0}}},
                             actual)
        self.assertEqual(2, mock_open.call_count)
        # any_order=True is needed as context manager calls also done on open
        mock_open.assert_has_calls(
            [mock.call('some/base/path/another.json', 'r+b'),
             mock.call('some/base/path/override_ref.json', 'r+b')],
            any_order=True)

    def test_ref_with_json_path_not_supported(self):

        self.assertRaises(
            NotImplementedError, json_ref.resolve_refs,
            {'foo': 1,
             'bar': {'$ref': 'another.json#/key-in-another',
                     'boo': {'b': 3,
                             'c': 13}}},
            'some/base/path/')