From 9bc5b497179a3cd593e799c4b565054e91ee7d22 Mon Sep 17 00:00:00 2001 From: Alba Hita <93577878+ahitacat@users.noreply.github.com> Date: Wed, 15 Jun 2022 17:05:30 +0200 Subject: [PATCH] Append compression type to content-type of MIME. Compare file compression with content_type. (#3435) Resolves: rhbz#2083665 Signed-off-by: ahitacat --- insights/client/phase/v1.py | 10 ++++- .../client/phase/test_collect_and_upload.py | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/insights/client/phase/v1.py b/insights/client/phase/v1.py index a566877c8e..e40278f93e 100644 --- a/insights/client/phase/v1.py +++ b/insights/client/phase/v1.py @@ -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) diff --git a/insights/tests/client/phase/test_collect_and_upload.py b/insights/tests/client/phase/test_collect_and_upload.py index db9f3837a6..8b1ff5dda7 100644 --- a/insights/tests/client/phase/test_collect_and_upload.py +++ b/insights/tests/client/phase/test_collect_and_upload.py @@ -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")