Skip to content

Commit

Permalink
Append compression type to content-type of MIME. Compare file compres…
Browse files Browse the repository at this point in the history
…sion with content_type. (#3435)

Resolves: rhbz#2083665
Signed-off-by: ahitacat <[email protected]>
  • Loading branch information
ahitacat authored Jun 15, 2022
1 parent daf32aa commit 9bc5b49
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
10 changes: 9 additions & 1 deletion insights/client/phase/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,16 @@ def collect_and_output(client, config):
# no archive to upload, something went wrong
sys.exit(constants.sig_kill_bad)
resp = None
content_type = None
if config.content_type in ['gz', 'bz2', 'xz']:
content_type = 'application/vnd.redhat.advisor.collection+' + config.content_type
extension = os.path.splitext(insights_archive)[1][1:]
compression_type = content_type.split('+')[1]
if extension not in compression_type:
logger.error("Content type different from compression")
sys.exit(constants.sig_kill_bad)
try:
resp = client.upload(payload=insights_archive, content_type=config.content_type)
resp = client.upload(payload=insights_archive, content_type=(content_type if content_type else config.content_type))
except (IOError, ValueError, RuntimeError) as e:
logger.error(str(e))
sys.exit(constants.sig_kill_bad)
Expand Down
40 changes: 40 additions & 0 deletions insights/tests/client/phase/test_collect_and_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,46 @@ def test_collect_and_output_payload_off(insights_config, insights_client):
insights_client.return_value.delete_cached_branch_info.assert_called_once()


@patch("insights.client.phase.v1.InsightsClient")
@patch_insights_config
def test_collect_and_output_payload_on_default(insights_config, insights_client):
insights_config.return_value.load_all.return_value.content_type = 'bz2'
insights_config.return_value.load_all.return_value.payload = 'testct.bz2'
try:
collect_and_output()
except SystemExit:
pass
insights_client.return_value.collect.assert_not_called()
insights_client.return_value.upload.assert_called_with(payload='testct.bz2', content_type='application/vnd.redhat.advisor.collection+bz2')
insights_client.return_value.delete_cached_branch_info.assert_called_once()


@patch("insights.client.phase.v1.InsightsClient")
@patch_insights_config
def test_collect_and_output_different_payload_type(insights_config, insights_client):
insights_config.return_value.load_all.return_value.content_type = 'bz2'
insights_config.return_value.load_all.return_value.payload = 'testct.tgz'
try:
collect_and_output()
except SystemExit:
pass
insights_client.return_value.collect.assert_not_called()
insights_client.return_value.upload.assert_not_called()


@patch("insights.client.phase.v1.InsightsClient")
@patch_insights_config
def test_collect_and_output_none_payload_type(insights_config, insights_client):
insights_config.return_value.load_all.return_value.payload = 'testct'
try:
collect_and_output()
except SystemExit:
pass
insights_client.return_value.collect.assert_not_called()
insights_client.return_value.upload.assert_called_with(payload='testct', content_type=None)
insights_client.return_value.delete_cached_branch_info.assert_called_once()


@patch("insights.client.phase.v1.InsightsClient")
@patch_insights_config
# @patch("insights.client.phase.v1.InsightsClient")
Expand Down

0 comments on commit 9bc5b49

Please sign in to comment.