diff options
| author | Zuul <zuul@review.opendev.org> | 2023-12-21 15:45:11 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2023-12-21 15:45:11 +0000 |
| commit | a21a7a89d61339e7154447d465269fff179a8871 (patch) | |
| tree | 714bdfcabc8d4f41abc3e667de0488b3ef70e72c | |
| parent | 316bafaf075ee85f555fce5e5c4df72f88e52450 (diff) | |
| parent | a5351473e08ee18cbc86edfb52da708ad558fe85 (diff) | |
| download | kolla-a21a7a89d61339e7154447d465269fff179a8871.tar.gz kolla-a21a7a89d61339e7154447d465269fff179a8871.zip | |
Merge "Summary JSON file"
| -rw-r--r-- | kolla/common/config.py | 4 | ||||
| -rw-r--r-- | kolla/image/build.py | 5 | ||||
| -rw-r--r-- | kolla/image/kolla_worker.py | 20 | ||||
| -rw-r--r-- | kolla/tests/test_build.py | 47 | ||||
| -rw-r--r-- | releasenotes/notes/summary-json-file-96441e67076fc480.yaml | 5 |
5 files changed, 77 insertions, 4 deletions
diff --git a/kolla/common/config.py b/kolla/common/config.py index 9c2755f16..4b7fa75f6 100644 --- a/kolla/common/config.py +++ b/kolla/common/config.py @@ -195,6 +195,10 @@ _CLI_OPTS = [ cfg.StrOpt('format', short='f', default='json', choices=['json', 'none'], help='Format to write the final results in.'), + cfg.StrOpt('summary-json-file', + help='Name of a file to write the build summary to when format ' + 'is json. If unset, the summary will be written to ' + 'standard output'), cfg.StrOpt('tarballs-base', default=TARBALLS_BASE, help='Base url to OpenStack tarballs'), cfg.IntOpt('threads', short='T', default=8, min=1, diff --git a/kolla/image/build.py b/kolla/image/build.py index 6b2d7b82a..63e3ca81d 100644 --- a/kolla/image/build.py +++ b/kolla/image/build.py @@ -11,7 +11,6 @@ # limitations under the License. import contextlib -import json import logging import queue import shutil @@ -206,8 +205,6 @@ def run_build(): raise if conf.summary: - results = kolla.summary() - if conf.format == 'json': - print(json.dumps(results)) + kolla.summary() kolla.cleanup() return kolla.get_image_statuses() diff --git a/kolla/image/kolla_worker.py b/kolla/image/kolla_worker.py index 65abd2492..3ea290d06 100644 --- a/kolla/image/kolla_worker.py +++ b/kolla/image/kolla_worker.py @@ -564,6 +564,26 @@ class KollaWorker(object): 'name': name, }) + if self.conf.format == 'json': + + def json_summary(f, **kwargs): + json.dump(results, f, **kwargs) + + if self.conf.summary_json_file: + try: + with open(self.conf.summary_json_file, "w") as f: + json_summary(f, indent=4) + except OSError as e: + LOG.error(f'Failed to write JSON build summary to ' + '{self.conf.summary_json_file}') + LOG.error(f'Exception caught: {e}') + sys.exit(1) + + else: + # NOTE(mgoddard): Keep single line output for + # backwards-compatibility. + json_summary(sys.stdout) + return results def get_image_statuses(self): diff --git a/kolla/tests/test_build.py b/kolla/tests/test_build.py index 8fd0a5d9a..1aa0b3a9d 100644 --- a/kolla/tests/test_build.py +++ b/kolla/tests/test_build.py @@ -742,6 +742,53 @@ class KollaWorkerTest(base.TestCase): self.assertEqual('error', results['failed'][0]['status']) # bad self.assertEqual('error', results['failed'][1]['status']) # bad2 + @mock.patch('json.dump') + def test_summary_json_format(self, dump_mock): + self.conf.set_override('format', 'json') + kolla = build.KollaWorker(self.conf) + kolla.images = self.images + kolla.image_statuses_good['good'] = build.Status.BUILT + kolla.image_statuses_bad['bad'] = build.Status.ERROR + kolla.image_statuses_allowed_to_fail['bad2'] = build.Status.ERROR + kolla.image_statuses_unmatched['unmatched'] = build.Status.UNMATCHED + results = kolla.summary() + dump_mock.assert_called_once_with(results, sys.stdout) + + @mock.patch('json.dump') + def test_summary_json_format_file(self, dump_mock): + tmpdir = tempfile.mkdtemp() + file_path = os.path.join(tmpdir, 'summary.json') + try: + self.conf.set_override('format', 'json') + self.conf.set_override('summary_json_file', file_path) + kolla = build.KollaWorker(self.conf) + kolla.images = self.images + kolla.image_statuses_good['good'] = build.Status.BUILT + kolla.image_statuses_bad['bad'] = build.Status.ERROR + kolla.image_statuses_allowed_to_fail['bad2'] = build.Status.ERROR + kolla.image_statuses_unmatched['unmatched'] = ( + build.Status.UNMATCHED) + results = kolla.summary() + dump_mock.assert_called_once_with(results, mock.ANY, indent=4) + self.assertEqual(dump_mock.call_args[0][1].name, file_path) + finally: + os.remove(file_path) + os.rmdir(tmpdir) + + @mock.patch('builtins.open') + def test_summary_json_format_file_error(self, open_mock): + open_mock.side_effect = OSError + self.conf.set_override('format', 'json') + self.conf.set_override('summary_json_file', 'fake-file') + kolla = build.KollaWorker(self.conf) + kolla.images = self.images + kolla.image_statuses_good['good'] = build.Status.BUILT + kolla.image_statuses_bad['bad'] = build.Status.ERROR + kolla.image_statuses_allowed_to_fail['bad2'] = build.Status.ERROR + kolla.image_statuses_unmatched['unmatched'] = ( + build.Status.UNMATCHED) + self.assertRaises(SystemExit, kolla.summary) + @mock.patch('shutil.copytree') def test_work_dir(self, copytree_mock): self.conf.set_override('work_dir', 'tmp/foo') diff --git a/releasenotes/notes/summary-json-file-96441e67076fc480.yaml b/releasenotes/notes/summary-json-file-96441e67076fc480.yaml new file mode 100644 index 000000000..d6ff5928e --- /dev/null +++ b/releasenotes/notes/summary-json-file-96441e67076fc480.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Adds support for writing the build summary to a JSON file specified via the + ``[DEFAULT] summary_json_file`` option. |
