2 # -*- coding: utf-8 -*-
4 from __future__ import absolute_import
15 import arvados.commands.keepdocker as arv_keepdocker
16 from . import arvados_testutil as tutil
17 from . import run_test_server
20 class StopTest(Exception):
24 class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
25 def run_arv_keepdocker(self, args, err):
26 sys.argv = ['arv-keepdocker'] + args
27 log_handler = logging.StreamHandler(err)
28 arv_keepdocker.logger.addHandler(log_handler)
30 return arv_keepdocker.main()
32 arv_keepdocker.logger.removeHandler(log_handler)
34 def test_unsupported_arg(self):
35 with self.assertRaises(SystemExit):
36 self.run_arv_keepdocker(['-x=unknown'], sys.stderr)
38 def test_version_argument(self):
39 with tutil.redirected_streams(
40 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
41 with self.assertRaises(SystemExit):
42 self.run_arv_keepdocker(['--version'], sys.stderr)
43 self.assertVersionOutput(out, err)
45 @mock.patch('arvados.commands.keepdocker.find_image_hashes',
46 return_value=['abc123'])
47 @mock.patch('arvados.commands.keepdocker.find_one_image_hash',
48 return_value='abc123')
49 def test_image_format_compatibility(self, _1, _2):
50 old_id = hashlib.sha256(b'old').hexdigest()
51 new_id = 'sha256:'+hashlib.sha256(b'new').hexdigest()
52 for supported, img_id, expect_ok in [
53 (['v1'], old_id, True),
54 (['v1'], new_id, False),
55 (None, old_id, False),
58 (['v1', 'v2'], new_id, True),
59 (['v1'], new_id, False),
60 (['v2'], new_id, True)]:
62 fakeDD = arvados.api('v1')._rootDesc
64 del fakeDD['dockerImageFormats']
66 fakeDD['dockerImageFormats'] = supported
68 err = tutil.StringIO()
69 out = tutil.StringIO()
71 with tutil.redirected_streams(stdout=out), \
72 mock.patch('arvados.api') as api, \
73 mock.patch('arvados.commands.keepdocker.popen_docker',
74 return_value=subprocess.Popen(
76 stdout=subprocess.PIPE)), \
77 mock.patch('arvados.commands.keepdocker.prep_image_file',
78 side_effect=StopTest), \
79 self.assertRaises(StopTest if expect_ok else SystemExit):
81 api()._rootDesc = fakeDD
82 self.run_arv_keepdocker(['--force', 'testimage'], err)
84 self.assertEqual(out.getvalue(), '')
87 err.getvalue(), "refusing to store",
88 msg=repr((supported, img_id)))
91 err.getvalue(), "refusing to store",
92 msg=repr((supported, img_id)))
96 "server does not specify supported image formats",
97 msg=repr((supported, img_id)))
99 fakeDD = arvados.api('v1')._rootDesc
100 fakeDD['dockerImageFormats'] = ['v1']
101 err = tutil.StringIO()
102 out = tutil.StringIO()
103 with tutil.redirected_streams(stdout=out), \
104 mock.patch('arvados.api') as api, \
105 mock.patch('arvados.commands.keepdocker.popen_docker',
106 return_value=subprocess.Popen(
108 stdout=subprocess.PIPE)), \
109 mock.patch('arvados.commands.keepdocker.prep_image_file',
110 side_effect=StopTest), \
111 self.assertRaises(StopTest):
112 api()._rootDesc = fakeDD
113 self.run_arv_keepdocker(
114 ['--force', '--force-image-format', 'testimage'], err)
115 self.assertRegex(err.getvalue(), "forcing incompatible image")