X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/54837bcae94d30f37f19e4ee436c2bb96bd8f21a..c4f252e766aa196d97801d99bb74720e451ca185:/sdk/python/tests/test_arv_keepdocker.py?ds=sidebyside diff --git a/sdk/python/tests/test_arv_keepdocker.py b/sdk/python/tests/test_arv_keepdocker.py index e67aacc87b..36b4954e51 100644 --- a/sdk/python/tests/test_arv_keepdocker.py +++ b/sdk/python/tests/test_arv_keepdocker.py @@ -1,9 +1,10 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 +from __future__ import absolute_import import arvados import hashlib -import io import mock import os import subprocess @@ -13,15 +14,15 @@ import unittest import logging import arvados.commands.keepdocker as arv_keepdocker -import arvados_testutil as tutil -import run_test_server +from . import arvados_testutil as tutil +from . import run_test_server class StopTest(Exception): pass -class ArvKeepdockerTestCase(unittest.TestCase): +class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker): def run_arv_keepdocker(self, args, err): sys.argv = ['arv-keepdocker'] + args log_handler = logging.StreamHandler(err) @@ -36,27 +37,25 @@ class ArvKeepdockerTestCase(unittest.TestCase): self.run_arv_keepdocker(['-x=unknown'], sys.stderr) def test_version_argument(self): - err = io.BytesIO() - out = io.BytesIO() - with tutil.redirected_streams(stdout=out, stderr=err): + with tutil.redirected_streams( + stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err): with self.assertRaises(SystemExit): self.run_arv_keepdocker(['--version'], sys.stderr) - self.assertEqual(out.getvalue(), '') - self.assertRegexpMatches(err.getvalue(), "[0-9]+\.[0-9]+\.[0-9]+") + self.assertVersionOutput(out, err) @mock.patch('arvados.commands.keepdocker.find_image_hashes', return_value=['abc123']) @mock.patch('arvados.commands.keepdocker.find_one_image_hash', return_value='abc123') def test_image_format_compatibility(self, _1, _2): - old_id = hashlib.sha256('old').hexdigest() - new_id = 'sha256:'+hashlib.sha256('new').hexdigest() + old_id = hashlib.sha256(b'old').hexdigest() + new_id = 'sha256:'+hashlib.sha256(b'new').hexdigest() for supported, img_id, expect_ok in [ (['v1'], old_id, True), (['v1'], new_id, False), - (None, old_id, True), - ([], old_id, True), - ([], new_id, True), + (None, old_id, False), + ([], old_id, False), + ([], new_id, False), (['v1', 'v2'], new_id, True), (['v1'], new_id, False), (['v2'], new_id, True)]: @@ -67,8 +66,8 @@ class ArvKeepdockerTestCase(unittest.TestCase): else: fakeDD['dockerImageFormats'] = supported - err = io.BytesIO() - out = io.BytesIO() + err = tutil.StringIO() + out = tutil.StringIO() with tutil.redirected_streams(stdout=out), \ mock.patch('arvados.api') as api, \ @@ -85,23 +84,23 @@ class ArvKeepdockerTestCase(unittest.TestCase): self.assertEqual(out.getvalue(), '') if expect_ok: - self.assertNotRegexpMatches( + self.assertNotRegex( err.getvalue(), "refusing to store", msg=repr((supported, img_id))) else: - self.assertRegexpMatches( + self.assertRegex( err.getvalue(), "refusing to store", msg=repr((supported, img_id))) if not supported: - self.assertRegexpMatches( + self.assertRegex( err.getvalue(), "server does not specify supported image formats", msg=repr((supported, img_id))) fakeDD = arvados.api('v1')._rootDesc fakeDD['dockerImageFormats'] = ['v1'] - err = io.BytesIO() - out = io.BytesIO() + err = tutil.StringIO() + out = tutil.StringIO() with tutil.redirected_streams(stdout=out), \ mock.patch('arvados.api') as api, \ mock.patch('arvados.commands.keepdocker.popen_docker', @@ -114,4 +113,4 @@ class ArvKeepdockerTestCase(unittest.TestCase): api()._rootDesc = fakeDD self.run_arv_keepdocker( ['--force', '--force-image-format', 'testimage'], err) - self.assertRegexpMatches(err.getvalue(), "forcing incompatible image") + self.assertRegex(err.getvalue(), "forcing incompatible image")