def test_compare_hashes_with_bad_files(self, mocker): """Ensure that the formatting of errors is consistent if improperly formatted files are provided to hashsum. """ hash_file = "metadata/checksum.sha1" job = Job("stub", "stub", ["", ""]) hashsum = self.setup_hashsum(hash_file, job) toolname = "sha1sum" objects_dir = "objects" no_proper_output = ( b"sha1sum: metadata/checksum.sha1: no properly formatted SHA1 " b"checksum lines found") except_string_no_proper_out = ( "sha1: comparison exited with status: 1. Please check the formatting of the checksums or integrity of the files.\n" "sha1: sha1sum: metadata/checksum.sha1: no properly formatted " "SHA1 checksum lines found") improper_formatting = b"sha1sum: WARNING: 1 line is improperly formatted" except_string_improper_format = ( "sha1: comparison exited with status: 1. Please check the formatting of the checksums or integrity of the files.\n" "sha1: sha1sum: WARNING: 1 line is improperly formatted") mock = mocker.patch.object(hashsum, "_call", return_value=no_proper_output) mocker.patch.object(hashsum, "count_and_compare_lines", return_value=True) mock.side_effect = subprocess.CalledProcessError( returncode=1, cmd=toolname, output=no_proper_output) ret = hashsum.compare_hashes("") mock.assert_called_once_with("-c", "--strict", hash_file, transfer_dir=objects_dir) assert (job.get_stderr().strip() == except_string_no_proper_out ), self.assert_exception_string assert ret == 1, self.assert_return_value.format(ret) # Flush job.error as it isn't flushed automatically. job.error = "" mock = mocker.patch.object(hashsum, "_call", return_value=improper_formatting) mock.side_effect = subprocess.CalledProcessError( returncode=1, cmd="sha1sum", output=improper_formatting) ret = hashsum.compare_hashes("") assert (job.get_stderr().strip() == except_string_improper_format ), self.assert_exception_string mock.assert_called_once_with("-c", "--strict", hash_file, transfer_dir=objects_dir) assert ret == 1, self.assert_return_value.format(ret)
def test_compare_hashes_failed(self, mocker): """Ensure we get consistent output when the checksum comparison fails.""" hash_file = "metadata/checksum.sha256" job = Job("stub", "stub", ["", ""]) hashsum = self.setup_hashsum(hash_file, job) toolname = "sha256sum" objects_dir = "objects" output_string = ( b"objects/file1.bin: OK\n" b"objects/file2.bin: FAILED\n" b"objects/nested/\xe3\x83\x95\xe3\x82\xa1\xe3\x82\xa4\xe3\x83\xab" b"3.bin: FAILED\n" b"objects/readonly.file: FAILED open or read") exception_string = ( "sha256: comparison exited with status: 1. Please check the formatting of the checksums or integrity of the files.\n" "sha256: objects/file2.bin: FAILED\n" "sha256: objects/nested/ファイル3.bin: FAILED\n" "sha256: objects/readonly.file: FAILED open or read") mock = mocker.patch.object(hashsum, "_call", return_value=output_string) mocker.patch.object(hashsum, "count_and_compare_lines", return_value=True) mock.side_effect = subprocess.CalledProcessError(returncode=1, cmd=toolname, output=output_string) ret = hashsum.compare_hashes("") mock.assert_called_once_with("-c", "--strict", hash_file, transfer_dir=objects_dir) assert ret == 1, self.assert_return_value.format(ret) assert (job.get_stderr().strip() == exception_string ), self.assert_exception_string
def test_job_encoding(): job = Job(name="somejob", uuid=str(uuid4()), args=["a", "b"]) job.pyprint(UNICODE) stdout = job.get_stdout() expected_stdout = f"{UNICODE}\n" expected_output = f"{UNICODE}\n" assert job.output == expected_output assert stdout == expected_stdout assert isinstance(job.output, str) assert isinstance(stdout, str) job.print_error(NON_ASCII) stderr = job.get_stderr() expected_stderr = f"{NON_ASCII}\n" expected_error = f"{NON_ASCII}\n" assert job.error == expected_error assert stderr == expected_stderr assert isinstance(job.error, str) assert isinstance(stderr, str) job_dump = job.dump() assert job.UUID in job_dump assert stderr in job_dump assert stdout in job_dump