Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control leapp-report format #686

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion leapp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
'dir': '/var/log/leapp/',
'files': ','.join(_REPORTS),
'answerfile': '/var/log/leapp/answerfile',
'userchoices': '/var/log/leapp/answerfile.userchoices'
'userchoices': '/var/log/leapp/answerfile.userchoices',
'schema': '1.1.0'
},
'repositories': {
'repo_path': '.',
Expand Down
15 changes: 13 additions & 2 deletions leapp/utils/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def importance(message):
return SEVERITY_LEVELS.get(message['severity'], 99)


def generate_report_file(messages_to_report, context, path):
def generate_report_file(messages_to_report, context, path, report_schema='1.1.0'):
# NOTE(ivasilev) Int conversion should not break as only specific formats of report_schema versions are allowed
report_schema_tuple = tuple([int(x) for x in report_schema.split('.')])
if path.endswith(".txt"):
with open(path, 'w') as f:
for message in sorted(messages_to_report, key=importance):
Expand All @@ -100,8 +102,17 @@ def generate_report_file(messages_to_report, context, path):
remediation = Remediation.from_dict(message.get('detail', {}))
if remediation:
f.write('Remediation: {}\n'.format(remediation))
f.write('Key: {}\n'.format(message['key']))
if report_schema_tuple > (1, 0, 0):
# report-schema 1.0.0 doesn't have a stable report key
f.write('Key: {}\n'.format(message['key']))
f.write('-' * 40 + '\n')
elif path.endswith(".json"):
with open(path, 'w') as f:
# Here all possible convertions will take place
if report_schema_tuple < (1, 1, 0):
# report-schema 1.0.0 doesn't have a stable report key
# copy list of messages here not to mess the initial structure for possible further invocations
messages_to_report = list(messages_to_report)
for m in messages_to_report:
m.pop('key')
json.dump({'entries': messages_to_report, 'leapp_run_id': context}, f, indent=2)
3 changes: 3 additions & 0 deletions man/leapp.1
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ leapp \- command line interface for upgrades between major OS versions
Enable an experimental actor. Can be used multiple times.
To use this variable, \fBLEAPP_UNSUPPORTED\fR has to be set. See \fBDeveloper variables\fR for more information.

\fB--report-schema\fR <\fIversion\fR>
Force the report format to conform to a specific version of the report schema. Expected values: \fB1.0.0\fR, \fB1.1.0\fR. Defaults to \fB1.1.0\fR.

\fB--reboot\fR
(upgrade only) Automatically perform reboot when requested.

Expand Down
32 changes: 32 additions & 0 deletions tests/scripts/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import json
import os
import tempfile

import jsonschema
import pytest
Expand All @@ -14,6 +15,7 @@
_create_report_object,
Audience, Key, Title, Summary, Severity, RelatedResource
)
from leapp.utils.report import generate_report_file


ReportPrimitive = namedtuple('ReportPrimitive', ['data', 'path', 'value', 'is_leaf_list'])
Expand Down Expand Up @@ -220,3 +222,33 @@ def _load_json(a_file):
# schemas directory contain a symlink to report json-schemas
schema_data = _load_json(os.path.join(reports_dir, 'schemas', schema))
jsonschema.validate(report_data, schema_data)


def test_report_backwards_compatibility():
msg1 = {u'groups': [u'selinux', u'security'],
u'title': u'SElinux relabeling has been scheduled',
'timeStamp': u'2021-02-10T13:13:40.576437Z',
'hostname': u'ivasilev-tagsflagsgroups',
'actor': u'check_se_linux',
u'summary': u'SElinux relabeling has been scheduled as the status was permissive/enforcing.',
u'audience': u'sysadmin',
u'key': u'c12a05a22be0b5bc0af3f1119898ea6d8639d9c4',
'id': '29279fc2e3100cfdf8ac30f48b293185192672d5a5ae9e0cfbc76cbfc919d807',
u'severity': u'info'}
report = [msg1]
# make sure normal mode works
for report_format in ['.json', '.txt']:
reportfile = tempfile.NamedTemporaryFile(suffix=report_format)
generate_report_file(report, 'leapp-run-id', reportfile.name, '1.1.0')
# make sure report output conversion to specific version works as well
for report_format in ['.json', '.txt']:
reportfile = tempfile.NamedTemporaryFile(suffix=report_format)
generate_report_file(report, 'leapp-run-id', reportfile.name, '1.0.0')
with open(reportfile.name) as f:
data = f.read()
if report_format == '.json':
a_report = json.loads(data)
msg = a_report['entries'][0]
assert 'key' not in msg
else:
assert 'Key' not in data