Exemple #1
0
    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)
Exemple #2
0
    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')
Exemple #3
0
    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')
    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')
Exemple #5
0
    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')
Exemple #6
0
    def _verify_notification(self,
                             sample_file_name,
                             replacements=None,
                             actual=None):
        """Assert if the generated notification matches with the stored sample

        :param sample_file_name: The name of the sample file to match relative
                                 to doc/notification_samples
        :param replacements: A dict of key value pairs that is used to update
                             the payload field of the sample data before it is
                             matched against the generated notification.
                             The 'x.y':'new-value' key-value pair selects the
                             ["payload"]["nova_object.data"]["x"]
                             ["nova_object.data"]["y"] value from the sample
                             data and overrides it with 'new-value'. There is
                             a special value ANY that can be used to indicate
                             that the actual field value shall be ignored
                             during matching.
        :param actual: Defines the actual notification to compare with. If
                       None then it defaults to the first versioned
                       notification emitted during the test.
        """
        if not actual:
            self.assertEqual(1, len(fake_notifier.VERSIONED_NOTIFICATIONS),
                             fake_notifier.VERSIONED_NOTIFICATIONS)
            notification = fake_notifier.VERSIONED_NOTIFICATIONS[0]
        else:
            notification = actual
        sample_file = self._get_notification_sample(sample_file_name)
        with open(sample_file) as sample:
            sample_data = sample.read()

        sample_obj = jsonutils.loads(sample_data)
        sample_base_dir = os.path.dirname(sample_file)
        sample_obj = json_ref.resolve_refs(sample_obj,
                                           base_path=sample_base_dir)
        self._apply_replacements(replacements, sample_obj, notification)

        self.assertJsonEqual(sample_obj, notification)
Exemple #7
0
    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 _verify_notification(self, sample_file_name, replacements=None,
                             actual=None):
        """Assert if the generated notification matches with the stored sample

        :param sample_file_name: The name of the sample file to match relative
                                 to doc/notification_samples
        :param replacements: A dict of key value pairs that is used to update
                             the payload field of the sample data before it is
                             matched against the generated notification.
                             The 'x.y':'new-value' key-value pair selects the
                             ["payload"]["nova_object.data"]["x"]
                             ["nova_object.data"]["y"] value from the sample
                             data and overrides it with 'new-value'. There is
                             a special value ANY that can be used to indicate
                             that the actual field value shall be ignored
                             during matching.
        :param actual: Defines the actual notification to compare with. If
                       None then it defaults to the first versioned
                       notification emitted during the test.
        """
        if not actual:
            self.assertEqual(1, len(fake_notifier.VERSIONED_NOTIFICATIONS))
            notification = fake_notifier.VERSIONED_NOTIFICATIONS[0]
        else:
            notification = actual
        sample_file = self._get_notification_sample(sample_file_name)
        with open(sample_file) as sample:
            sample_data = sample.read()

        sample_obj = jsonutils.loads(sample_data)
        sample_base_dir = os.path.dirname(sample_file)
        sample_obj = json_ref.resolve_refs(
            sample_obj, base_path=sample_base_dir)
        self._apply_replacements(replacements, sample_obj, notification)

        self.assertJsonEqual(sample_obj, notification)
    def _build_markup(self, notifications):
        content = []
        cols = ['Event type', 'Notification class', 'Payload class', 'Sample']
        table = nodes.table()
        content.append(table)
        group = nodes.tgroup(cols=len(cols))
        table.append(group)

        head = nodes.thead()
        group.append(head)

        for _ in cols:
            group.append(nodes.colspec(colwidth=1))

        body = nodes.tbody()
        group.append(body)

        # fill the table header
        row = nodes.row()
        body.append(row)
        for col_name in cols:
            col = nodes.entry()
            row.append(col)
            text = nodes.strong(text=col_name)
            col.append(text)

        # fill the table content, one notification per row
        for name, payload, sample_file in notifications:
            event_type = sample_file[0:-5].replace('-', '.')

            row = nodes.row()
            body.append(row)
            col = nodes.entry()
            row.append(col)
            text = nodes.literal(text=event_type)
            col.append(text)

            col = nodes.entry()
            row.append(col)
            text = nodes.literal(text=name)
            col.append(text)

            col = nodes.entry()
            row.append(col)
            text = nodes.literal(text=payload)
            col.append(text)

            col = nodes.entry()
            row.append(col)

            with open(os.path.join(self.SAMPLE_ROOT, sample_file), 'r') as f:
                sample_content = f.read()

            sample_obj = jsonutils.loads(sample_content)
            sample_obj = json_ref.resolve_refs(sample_obj,
                                               base_path=os.path.abspath(
                                                   self.SAMPLE_ROOT))
            sample_content = jsonutils.dumps(sample_obj,
                                             sort_keys=True,
                                             indent=4,
                                             separators=(',', ': '))

            event_type = sample_file[0:-5]
            html_str = self.TOGGLE_SCRIPT % ((event_type, ) * 3)
            html_str += ("<input type='button' id='%s-hideshow' "
                         "value='hide/show sample'>" % event_type)
            html_str += ("<div id='%s-div'><pre>%s</pre></div>" %
                         (event_type, sample_content))

            raw = nodes.raw('', html_str, format="html")
            col.append(raw)

        return content
    def _build_markup(self, notifications):
        content = []
        cols = ['Event type', 'Notification class', 'Payload class', 'Sample']
        table = nodes.table()
        content.append(table)
        group = nodes.tgroup(cols=len(cols))
        table.append(group)

        head = nodes.thead()
        group.append(head)

        for _ in cols:
            group.append(nodes.colspec(colwidth=1))

        body = nodes.tbody()
        group.append(body)

        # fill the table header
        row = nodes.row()
        body.append(row)
        for col_name in cols:
            col = nodes.entry()
            row.append(col)
            text = nodes.strong(text=col_name)
            col.append(text)

        # fill the table content, one notification per row
        for name, payload, sample_file in notifications:
            event_type = sample_file[0: -5].replace('-', '.')

            row = nodes.row()
            body.append(row)
            col = nodes.entry()
            row.append(col)
            text = nodes.literal(text=event_type)
            col.append(text)

            col = nodes.entry()
            row.append(col)
            text = nodes.literal(text=name)
            col.append(text)

            col = nodes.entry()
            row.append(col)
            text = nodes.literal(text=payload)
            col.append(text)

            col = nodes.entry()
            row.append(col)

            with open(os.path.join(self.SAMPLE_ROOT, sample_file), 'r') as f:
                sample_content = f.read()

            sample_obj = jsonutils.loads(sample_content)
            sample_obj = json_ref.resolve_refs(
                sample_obj,
                base_path=os.path.abspath(self.SAMPLE_ROOT))
            sample_content = jsonutils.dumps(sample_obj,
                                             sort_keys=True, indent=4,
                                             separators=(',', ': '))

            event_type = sample_file[0: -5]
            html_str = self.TOGGLE_SCRIPT % ((event_type, ) * 3)
            html_str += ("<input type='button' id='%s-hideshow' "
                         "value='hide/show sample'>" % event_type)
            html_str += ("<div id='%s-div'><pre>%s</pre></div>"
                         % (event_type, sample_content))

            raw = nodes.raw('', html_str, format="html")
            col.append(raw)

        return content