1 from __future__ import absolute_import
12 import arvados.commands.keepdocker as arv_keepdocker
13 from . import arvados_testutil as tutil
14 from . import run_test_server
17 class StopTest(Exception):
21 class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
22 def run_arv_keepdocker(self, args, err):
23 sys.argv = ['arv-keepdocker'] + args
24 log_handler = logging.StreamHandler(err)
25 arv_keepdocker.logger.addHandler(log_handler)
27 return arv_keepdocker.main()
29 arv_keepdocker.logger.removeHandler(log_handler)
31 def test_unsupported_arg(self):
32 with self.assertRaises(SystemExit):
33 self.run_arv_keepdocker(['-x=unknown'], sys.stderr)
35 def test_version_argument(self):
36 with tutil.redirected_streams(
37 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
38 with self.assertRaises(SystemExit):
39 self.run_arv_keepdocker(['--version'], sys.stderr)
40 self.assertVersionOutput(out, err)
42 @mock.patch('arvados.commands.keepdocker.find_image_hashes',
43 return_value=['abc123'])
44 @mock.patch('arvados.commands.keepdocker.find_one_image_hash',
45 return_value='abc123')
46 def test_image_format_compatibility(self, _1, _2):
47 old_id = hashlib.sha256(b'old').hexdigest()
48 new_id = 'sha256:'+hashlib.sha256(b'new').hexdigest()
49 for supported, img_id, expect_ok in [
50 (['v1'], old_id, True),
51 (['v1'], new_id, False),
52 (None, old_id, False),
55 (['v1', 'v2'], new_id, True),
56 (['v1'], new_id, False),
57 (['v2'], new_id, True)]:
59 fakeDD = arvados.api('v1')._rootDesc
61 del fakeDD['dockerImageFormats']
63 fakeDD['dockerImageFormats'] = supported
65 err = tutil.StringIO()
66 out = tutil.StringIO()
68 with tutil.redirected_streams(stdout=out), \
69 mock.patch('arvados.api') as api, \
70 mock.patch('arvados.commands.keepdocker.popen_docker',
71 return_value=subprocess.Popen(
73 stdout=subprocess.PIPE)), \
74 mock.patch('arvados.commands.keepdocker.prep_image_file',
75 side_effect=StopTest), \
76 self.assertRaises(StopTest if expect_ok else SystemExit):
78 api()._rootDesc = fakeDD
79 self.run_arv_keepdocker(['--force', 'testimage'], err)
81 self.assertEqual(out.getvalue(), '')
84 err.getvalue(), "refusing to store",
85 msg=repr((supported, img_id)))
88 err.getvalue(), "refusing to store",
89 msg=repr((supported, img_id)))
93 "server does not specify supported image formats",
94 msg=repr((supported, img_id)))
96 fakeDD = arvados.api('v1')._rootDesc
97 fakeDD['dockerImageFormats'] = ['v1']
98 err = tutil.StringIO()
99 out = tutil.StringIO()
100 with tutil.redirected_streams(stdout=out), \
101 mock.patch('arvados.api') as api, \
102 mock.patch('arvados.commands.keepdocker.popen_docker',
103 return_value=subprocess.Popen(
105 stdout=subprocess.PIPE)), \
106 mock.patch('arvados.commands.keepdocker.prep_image_file',
107 side_effect=StopTest), \
108 self.assertRaises(StopTest):
109 api()._rootDesc = fakeDD
110 self.run_arv_keepdocker(
111 ['--force', '--force-image-format', 'testimage'], err)
112 self.assertRegex(err.getvalue(), "forcing incompatible image")