From 5a5155ea4e4e3ade01e89a8c221ccd73fe1827b9 Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Mon, 3 Apr 2017 18:05:07 +0200 Subject: [PATCH] Factor out duplicated notification sample data Versioned notifications are functionally tested against stored sample data. Also most of the instance notitication shares payload structure. Today we store a separate sample for every instance notification. These samples store similar, mostly redundant data. When a new field is added to the InstancePayload then every instance notification related sample file needs to be modified. This leads to big and redundant changes like I18af99479562e2fe5e74e6e1252b804b074fee58. To remove the redundancy this patch proposes to use json references in the sample files instead of copy pasting the same notification sample fragment to every sample. As a first step this patch introduces a small json ref resolver. Then subsequent patches will replace the duplicated sample data with references to common sample fragments. This proposed resolver supports resolving the refs recursively so a referenced json fragment can reference further fragments. However the current implementation does not handle reference loops. The resolver also supports overriding parts of the referenced json fragment to support content customization needed in the next patch. Change-Id: Ic3ab7d60e4ac12b767fe70bef97b327545a86e74 --- doc/ext/versioned_notifications.py | 15 +- .../notification_sample_base.py | 8 +- nova/tests/json_ref.py | 63 ++++++++ nova/tests/unit/test_json_ref.py | 134 ++++++++++++++++++ 4 files changed, 216 insertions(+), 4 deletions(-) create mode 100644 nova/tests/json_ref.py create mode 100644 nova/tests/unit/test_json_ref.py diff --git a/doc/ext/versioned_notifications.py b/doc/ext/versioned_notifications.py index ae457544a3b9..dee53445a1ce 100644 --- a/doc/ext/versioned_notifications.py +++ b/doc/ext/versioned_notifications.py @@ -19,15 +19,18 @@ It is used via a single directive in the .rst file .. versioned_notifications:: """ +import os from docutils import nodes from docutils.parsers import rst import importlib +from oslo_serialization import jsonutils import pkgutil -import nova.notifications.objects from nova.notifications.objects import base as notification from nova.objects import base +from nova.tests import json_ref +import nova.utils class VersionedNotificationDirective(rst.Directive): @@ -125,9 +128,17 @@ jQuery(document).ready(function(){ col = nodes.entry() row.append(col) - with open(self.SAMPLE_ROOT + sample_file, 'r') as f: + 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 += ("