From 04082047fed202498a91357ba7b45ce1933a7ce1 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Thu, 7 Mar 2019 13:11:00 +0100 Subject: [PATCH] Fix the way how KojiContentGenerator computes filesize. The current code reads the data, converts them to unicode string and then uses the `len()` of that string as filesize. This is wrong, because Koji expects filesize to be really number of bytes, not number of characters. Therefore, in this commit, the filesize is computed from raw data (bytes). --- module_build_service/builder/KojiContentGenerator.py | 7 ++++--- tests/test_content_generator.py | 8 ++++---- tests/test_get_generator_json_expected_output.json | 2 +- .../test_get_generator_json_expected_output_with_log.json | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/module_build_service/builder/KojiContentGenerator.py b/module_build_service/builder/KojiContentGenerator.py index 28f45034..6e9c6c84 100644 --- a/module_build_service/builder/KojiContentGenerator.py +++ b/module_build_service/builder/KojiContentGenerator.py @@ -407,11 +407,12 @@ class KojiContentGenerator(object): mmd_path = os.path.join(output_path, mmd_filename) try: with open(mmd_path, 'rb') as mmd_f: - data = to_text_type(mmd_f.read()) + raw_data = mmd_f.read() + data = to_text_type(raw_data) mmd = Modulemd.Module().new_from_string(data) ret['filename'] = mmd_filename - ret['filesize'] = len(data) - ret['checksum'] = hashlib.md5(data.encode('utf-8')).hexdigest() + ret['filesize'] = len(raw_data) + ret['checksum'] = hashlib.md5(raw_data).hexdigest() except IOError: if arch == "src": # This might happen in case the Module is submitted directly diff --git a/tests/test_content_generator.py b/tests/test_content_generator.py index 63d1e10e..2d5d2bd9 100644 --- a/tests/test_content_generator.py +++ b/tests/test_content_generator.py @@ -276,7 +276,7 @@ class TestBuild: @patch("module_build_service.builder.KojiContentGenerator.open", create=True) def test_get_arch_mmd_output(self, patched_open): patched_open.return_value = mock_open( - read_data=self.cg.mmd).return_value + read_data=self.cg.mmd.encode("utf-8")).return_value ret = self.cg._get_arch_mmd_output("./fake-dir", "x86_64") assert ret == { 'arch': 'x86_64', @@ -286,7 +286,7 @@ class TestBuild: 'components': [], 'extra': {'typeinfo': {'module': {}}}, 'filename': 'modulemd.x86_64.txt', - 'filesize': 1136, + 'filesize': 1138, 'type': 'file' } @@ -296,7 +296,7 @@ class TestBuild: rpm_artifacts = mmd.get_rpm_artifacts() rpm_artifacts.add("dhcp-libs-12:4.3.5-5.module_2118aef6.x86_64") mmd.set_rpm_artifacts(rpm_artifacts) - mmd_data = to_text_type(mmd.dumps()) + mmd_data = bytes(mmd.dumps()) patched_open.return_value = mock_open( read_data=mmd_data).return_value @@ -336,7 +336,7 @@ class TestBuild: u'version': '4.3.5'}], 'extra': {'typeinfo': {'module': {}}}, 'filename': 'modulemd.x86_64.txt', - 'filesize': 317, + 'filesize': 319, 'type': 'file' } diff --git a/tests/test_get_generator_json_expected_output.json b/tests/test_get_generator_json_expected_output.json index 07825958..37308aad 100644 --- a/tests/test_get_generator_json_expected_output.json +++ b/tests/test_get_generator_json_expected_output.json @@ -625,7 +625,7 @@ } ], "arch": "noarch", - "filesize": 1136, + "filesize": 1138, "checksum": "96b7739ffa3918e6ac3e3bd422b064ea", "checksum_type": "md5", "type": "file", diff --git a/tests/test_get_generator_json_expected_output_with_log.json b/tests/test_get_generator_json_expected_output_with_log.json index 01a3727d..1aa3a508 100644 --- a/tests/test_get_generator_json_expected_output_with_log.json +++ b/tests/test_get_generator_json_expected_output_with_log.json @@ -625,7 +625,7 @@ } ], "arch": "noarch", - "filesize": 1136, + "filesize": 1138, "checksum": "96b7739ffa3918e6ac3e3bd422b064ea", "checksum_type": "md5", "type": "file",