X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/0f11eb78e075445d1769b445ee12328bdb397781..32b86ab3e24cbc47344a23be2587af1bc73bf165:/tools/crunchstat-summary/tests/test_examples.py diff --git a/tools/crunchstat-summary/tests/test_examples.py b/tools/crunchstat-summary/tests/test_examples.py index af92becd80..0e3ccd35a4 100644 --- a/tools/crunchstat-summary/tests/test_examples.py +++ b/tools/crunchstat-summary/tests/test_examples.py @@ -8,20 +8,24 @@ import crunchstat_summary.command import difflib import glob import gzip +from io import open import mock import os import unittest +from crunchstat_summary.command import UTF8Decode + TESTS_DIR = os.path.dirname(os.path.abspath(__file__)) class ReportDiff(unittest.TestCase): def diff_known_report(self, logfile, cmd): expectfile = logfile+'.report' - expect = open(expectfile).readlines() + with open(expectfile, encoding='utf-8') as f: + expect = f.readlines() self.diff_report(cmd, expect, expectfile=expectfile) - def diff_report(self, cmd, expect, expectfile=None): + def diff_report(self, cmd, expect, expectfile='(expected)'): got = [x+"\n" for x in cmd.report().strip("\n").split("\n")] self.assertEqual(got, expect, "\n"+"".join(difflib.context_diff( expect, got, fromfile=expectfile, tofile="(generated)"))) @@ -51,10 +55,9 @@ class HTMLFromFile(ReportDiff): cmd.run() self.assertRegexpMatches(cmd.report(), r'(?is).*\s*$') - class SummarizeEdgeCases(unittest.TestCase): def test_error_messages(self): - logfile = open(os.path.join(TESTS_DIR, 'crunchstat_error_messages.txt')) + logfile = open(os.path.join(TESTS_DIR, 'crunchstat_error_messages.txt'), encoding='utf-8') s = crunchstat_summary.summarizer.Summarizer(logfile) s.run() @@ -89,9 +92,9 @@ class SummarizeContainer(ReportDiff): 'container.json', 'crunchstat.txt', 'arv-mount.txt'] def _open(n): if n == "crunchstat.txt": - return gzip.open(self.logfile) + return UTF8Decode(gzip.open(self.logfile)) elif n == "arv-mount.txt": - return gzip.open(self.arvmountlog) + return UTF8Decode(gzip.open(self.arvmountlog)) mock_cr().open.side_effect = _open args = crunchstat_summary.command.ArgumentParser().parse_args( ['--job', self.fake_request['uuid']]) @@ -114,7 +117,7 @@ class SummarizeJob(ReportDiff): def test_job_report(self, mock_api, mock_cr): mock_api().jobs().get().execute.return_value = self.fake_job mock_cr().__iter__.return_value = ['fake-logfile.txt'] - mock_cr().open.return_value = gzip.open(self.logfile) + mock_cr().open.return_value = UTF8Decode(gzip.open(self.logfile)) args = crunchstat_summary.command.ArgumentParser().parse_args( ['--job', self.fake_job_uuid]) cmd = crunchstat_summary.command.Command(args) @@ -175,15 +178,14 @@ class SummarizePipeline(ReportDiff): mock_api().pipeline_instances().get().execute. \ return_value = self.fake_instance mock_cr().__iter__.return_value = ['fake-logfile.txt'] - mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)] + mock_cr().open.side_effect = [UTF8Decode(gzip.open(logfile)) for _ in range(3)] args = crunchstat_summary.command.ArgumentParser().parse_args( ['--pipeline-instance', self.fake_instance['uuid']]) cmd = crunchstat_summary.command.Command(args) cmd.run() - job_report = [ - line for line in open(logfile+'.report').readlines() - if not line.startswith('#!! ')] + with open(logfile+'.report', encoding='utf-8') as f: + job_report = [line for line in f if not line.startswith('#!! ')] expect = ( ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] + job_report + ['\n'] + @@ -251,15 +253,14 @@ class SummarizeACRJob(ReportDiff): mock_api().jobs().index().execute.return_value = self.fake_jobs_index mock_api().jobs().get().execute.return_value = self.fake_job mock_cr().__iter__.return_value = ['fake-logfile.txt'] - mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)] + mock_cr().open.side_effect = [UTF8Decode(gzip.open(logfile)) for _ in range(3)] args = crunchstat_summary.command.ArgumentParser().parse_args( ['--job', self.fake_job['uuid']]) cmd = crunchstat_summary.command.Command(args) cmd.run() - job_report = [ - line for line in open(logfile+'.report').readlines() - if not line.startswith('#!! ')] + with open(logfile+'.report', encoding='utf-8') as f: + job_report = [line for line in f if not line.startswith('#!! ')] expect = ( ['### Summary for zzzzz-8i9sb-i3e77t9z5y8j9cc (partial) (zzzzz-8i9sb-i3e77t9z5y8j9cc)\n', '(no report generated)\n',