1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import absolute_import
18 import arvados.commands.keepdocker as arv_keepdocker
19 from . import arvados_testutil as tutil
20 from . import run_test_server
23 class StopTest(Exception):
27 class ArvKeepdockerTestCase(unittest.TestCase, tutil.VersionChecker):
28 def run_arv_keepdocker(self, args, err):
29 sys.argv = ['arv-keepdocker'] + args
30 log_handler = logging.StreamHandler(err)
31 arv_keepdocker.logger.addHandler(log_handler)
33 return arv_keepdocker.main()
35 arv_keepdocker.logger.removeHandler(log_handler)
37 def test_unsupported_arg(self):
38 out = tutil.StringIO()
39 with tutil.redirected_streams(stdout=out, stderr=out), \
40 self.assertRaises(SystemExit):
41 self.run_arv_keepdocker(['-x=unknown'], sys.stderr)
42 self.assertRegex(out.getvalue(), 'unrecognized arguments')
44 def test_version_argument(self):
45 with tutil.redirected_streams(
46 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
47 with self.assertRaises(SystemExit):
48 self.run_arv_keepdocker(['--version'], sys.stderr)
49 self.assertVersionOutput(out, err)
51 @mock.patch('arvados.commands.keepdocker.list_images_in_arv',
53 @mock.patch('arvados.commands.keepdocker.find_image_hashes',
54 return_value=['abc123'])
55 @mock.patch('arvados.commands.keepdocker.find_one_image_hash',
56 return_value='abc123')
57 def test_image_format_compatibility(self, _1, _2, _3):
58 old_id = hashlib.sha256(b'old').hexdigest()
59 new_id = 'sha256:'+hashlib.sha256(b'new').hexdigest()
60 for supported, img_id, expect_ok in [
61 (['v1'], old_id, True),
62 (['v1'], new_id, False),
63 (None, old_id, False),
66 (['v1', 'v2'], new_id, True),
67 (['v1'], new_id, False),
68 (['v2'], new_id, True)]:
70 fakeDD = arvados.api('v1')._rootDesc
72 del fakeDD['dockerImageFormats']
74 fakeDD['dockerImageFormats'] = supported
76 err = tutil.StringIO()
77 out = tutil.StringIO()
79 with tutil.redirected_streams(stdout=out), \
80 mock.patch('arvados.api') as api, \
81 mock.patch('arvados.commands.keepdocker.popen_docker',
82 return_value=subprocess.Popen(
84 stdout=subprocess.PIPE)), \
85 mock.patch('arvados.commands.keepdocker.prep_image_file',
86 side_effect=StopTest), \
87 self.assertRaises(StopTest if expect_ok else SystemExit):
89 api()._rootDesc = fakeDD
90 self.run_arv_keepdocker(['--force', 'testimage'], err)
92 self.assertEqual(out.getvalue(), '')
95 err.getvalue(), "refusing to store",
96 msg=repr((supported, img_id)))
99 err.getvalue(), "refusing to store",
100 msg=repr((supported, img_id)))
104 "server does not specify supported image formats",
105 msg=repr((supported, img_id)))
107 fakeDD = arvados.api('v1')._rootDesc
108 fakeDD['dockerImageFormats'] = ['v1']
109 err = tutil.StringIO()
110 out = tutil.StringIO()
111 with tutil.redirected_streams(stdout=out), \
112 mock.patch('arvados.api') as api, \
113 mock.patch('arvados.commands.keepdocker.popen_docker',
114 return_value=subprocess.Popen(
116 stdout=subprocess.PIPE)), \
117 mock.patch('arvados.commands.keepdocker.prep_image_file',
118 side_effect=StopTest), \
119 self.assertRaises(StopTest):
120 api()._rootDesc = fakeDD
121 self.run_arv_keepdocker(
122 ['--force', '--force-image-format', 'testimage'], err)
123 self.assertRegex(err.getvalue(), "forcing incompatible image")
125 def test_tag_given_twice(self):
126 with tutil.redirected_streams(stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
127 with self.assertRaises(SystemExit):
128 self.run_arv_keepdocker(['myrepo:mytag', 'extratag'], sys.stderr)
129 self.assertRegex(err.getvalue(), "cannot add tag argument 'extratag'")
131 def test_image_given_as_repo_colon_tag(self):
132 with self.assertRaises(StopTest), \
133 mock.patch('arvados.commands.keepdocker.find_one_image_hash',
134 side_effect=StopTest) as find_image_mock:
135 self.run_arv_keepdocker(['repo:tag'], sys.stderr)
136 find_image_mock.assert_called_with('repo', 'tag')
138 with self.assertRaises(StopTest), \
139 mock.patch('arvados.commands.keepdocker.find_one_image_hash',
140 side_effect=StopTest) as find_image_mock:
141 self.run_arv_keepdocker(['myreg.example:8888/repo/img:tag'], sys.stderr)
142 find_image_mock.assert_called_with('myreg.example:8888/repo/img', 'tag')
144 def test_image_has_colons(self):
145 with self.assertRaises(StopTest), \
146 mock.patch('arvados.commands.keepdocker.find_one_image_hash',
147 side_effect=StopTest) as find_image_mock:
148 self.run_arv_keepdocker(['[::1]:8888/repo/img'], sys.stderr)
149 find_image_mock.assert_called_with('[::1]:8888/repo/img', 'latest')
151 with self.assertRaises(StopTest), \
152 mock.patch('arvados.commands.keepdocker.find_one_image_hash',
153 side_effect=StopTest) as find_image_mock:
154 self.run_arv_keepdocker(['[::1]/repo/img'], sys.stderr)
155 find_image_mock.assert_called_with('[::1]/repo/img', 'latest')
157 @mock.patch('arvados.commands.keepdocker.list_images_in_arv',
159 @mock.patch('arvados.commands.keepdocker.find_image_hashes',
160 return_value=['abc123'])
161 @mock.patch('arvados.commands.keepdocker.find_one_image_hash',
162 return_value='abc123')
163 def test_collection_property_update(self, _1, _2, _3):
164 image_id = 'sha256:'+hashlib.sha256(b'image').hexdigest()
165 fakeDD = arvados.api('v1')._rootDesc
166 fakeDD['dockerImageFormats'] = ['v2']
168 err = tutil.StringIO()
169 out = tutil.StringIO()
170 File = collections.namedtuple('File', ['name'])
171 mocked_file = File(name='docker_image')
172 mocked_collection = {
173 'uuid': 'new-collection-uuid',
175 'responsible_person_uuid': 'person_uuid',
179 with tutil.redirected_streams(stdout=out), \
180 mock.patch('arvados.api') as api, \
181 mock.patch('arvados.commands.keepdocker.popen_docker',
182 return_value=subprocess.Popen(
184 stdout=subprocess.PIPE)), \
185 mock.patch('arvados.commands.keepdocker.prep_image_file',
186 return_value=(mocked_file, False)), \
187 mock.patch('arvados.commands.put.main',
188 return_value='new-collection-uuid'), \
189 self.assertRaises(StopTest):
191 api()._rootDesc = fakeDD
192 api().collections().get().execute.return_value = copy.deepcopy(mocked_collection)
193 api().collections().update().execute.side_effect = StopTest
194 self.run_arv_keepdocker(['--force', 'testimage'], err)
196 updated_properties = mocked_collection['properties']
197 updated_properties.update({'docker-image-repo-tag': 'testimage:latest'})
198 api().collections().update.assert_called_with(
199 uuid=mocked_collection['uuid'],
200 body={'properties': updated_properties})