Skip to content

Commit a21a7a8

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Summary JSON file"
2 parents 316bafa + a535147 commit a21a7a8

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

kolla/common/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@
195195
cfg.StrOpt('format', short='f', default='json',
196196
choices=['json', 'none'],
197197
help='Format to write the final results in.'),
198+
cfg.StrOpt('summary-json-file',
199+
help='Name of a file to write the build summary to when format '
200+
'is json. If unset, the summary will be written to '
201+
'standard output'),
198202
cfg.StrOpt('tarballs-base', default=TARBALLS_BASE,
199203
help='Base url to OpenStack tarballs'),
200204
cfg.IntOpt('threads', short='T', default=8, min=1,

kolla/image/build.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# limitations under the License.
1212

1313
import contextlib
14-
import json
1514
import logging
1615
import queue
1716
import shutil
@@ -206,8 +205,6 @@ def run_build():
206205
raise
207206

208207
if conf.summary:
209-
results = kolla.summary()
210-
if conf.format == 'json':
211-
print(json.dumps(results))
208+
kolla.summary()
212209
kolla.cleanup()
213210
return kolla.get_image_statuses()

kolla/image/kolla_worker.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,26 @@ def summary(self):
564564
'name': name,
565565
})
566566

567+
if self.conf.format == 'json':
568+
569+
def json_summary(f, **kwargs):
570+
json.dump(results, f, **kwargs)
571+
572+
if self.conf.summary_json_file:
573+
try:
574+
with open(self.conf.summary_json_file, "w") as f:
575+
json_summary(f, indent=4)
576+
except OSError as e:
577+
LOG.error(f'Failed to write JSON build summary to '
578+
'{self.conf.summary_json_file}')
579+
LOG.error(f'Exception caught: {e}')
580+
sys.exit(1)
581+
582+
else:
583+
# NOTE(mgoddard): Keep single line output for
584+
# backwards-compatibility.
585+
json_summary(sys.stdout)
586+
567587
return results
568588

569589
def get_image_statuses(self):

kolla/tests/test_build.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,53 @@ def test_summary(self):
742742
self.assertEqual('error', results['failed'][0]['status']) # bad
743743
self.assertEqual('error', results['failed'][1]['status']) # bad2
744744

745+
@mock.patch('json.dump')
746+
def test_summary_json_format(self, dump_mock):
747+
self.conf.set_override('format', 'json')
748+
kolla = build.KollaWorker(self.conf)
749+
kolla.images = self.images
750+
kolla.image_statuses_good['good'] = build.Status.BUILT
751+
kolla.image_statuses_bad['bad'] = build.Status.ERROR
752+
kolla.image_statuses_allowed_to_fail['bad2'] = build.Status.ERROR
753+
kolla.image_statuses_unmatched['unmatched'] = build.Status.UNMATCHED
754+
results = kolla.summary()
755+
dump_mock.assert_called_once_with(results, sys.stdout)
756+
757+
@mock.patch('json.dump')
758+
def test_summary_json_format_file(self, dump_mock):
759+
tmpdir = tempfile.mkdtemp()
760+
file_path = os.path.join(tmpdir, 'summary.json')
761+
try:
762+
self.conf.set_override('format', 'json')
763+
self.conf.set_override('summary_json_file', file_path)
764+
kolla = build.KollaWorker(self.conf)
765+
kolla.images = self.images
766+
kolla.image_statuses_good['good'] = build.Status.BUILT
767+
kolla.image_statuses_bad['bad'] = build.Status.ERROR
768+
kolla.image_statuses_allowed_to_fail['bad2'] = build.Status.ERROR
769+
kolla.image_statuses_unmatched['unmatched'] = (
770+
build.Status.UNMATCHED)
771+
results = kolla.summary()
772+
dump_mock.assert_called_once_with(results, mock.ANY, indent=4)
773+
self.assertEqual(dump_mock.call_args[0][1].name, file_path)
774+
finally:
775+
os.remove(file_path)
776+
os.rmdir(tmpdir)
777+
778+
@mock.patch('builtins.open')
779+
def test_summary_json_format_file_error(self, open_mock):
780+
open_mock.side_effect = OSError
781+
self.conf.set_override('format', 'json')
782+
self.conf.set_override('summary_json_file', 'fake-file')
783+
kolla = build.KollaWorker(self.conf)
784+
kolla.images = self.images
785+
kolla.image_statuses_good['good'] = build.Status.BUILT
786+
kolla.image_statuses_bad['bad'] = build.Status.ERROR
787+
kolla.image_statuses_allowed_to_fail['bad2'] = build.Status.ERROR
788+
kolla.image_statuses_unmatched['unmatched'] = (
789+
build.Status.UNMATCHED)
790+
self.assertRaises(SystemExit, kolla.summary)
791+
745792
@mock.patch('shutil.copytree')
746793
def test_work_dir(self, copytree_mock):
747794
self.conf.set_override('work_dir', 'tmp/foo')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Adds support for writing the build summary to a JSON file specified via the
5+
``[DEFAULT] summary_json_file`` option.

0 commit comments

Comments
 (0)