1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import absolute_import
6 from future.utils import listitems
15 import arvados.collection as collection
16 import arvados.commands.get as arv_get
17 from . import run_test_server
19 from . import arvados_testutil as tutil
20 from .arvados_testutil import ArvadosBaseTestCase
22 class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
29 super(ArvadosGetTestCase, self).setUp()
30 self.tempdir = tempfile.mkdtemp()
31 self.col_loc, self.col_pdh, self.col_manifest = self.write_test_collection()
34 super(ArvadosGetTestCase, self).tearDown()
35 shutil.rmtree(self.tempdir)
37 def write_test_collection(self,
42 'subdir/baz.txt' : 'baz',
44 c = collection.Collection()
45 for path, data in listitems(contents):
46 with c.open(path, 'wb') as f:
50 return (c.manifest_locator(),
51 c.portable_data_hash(),
52 c.manifest_text(strip=strip_manifest))
54 def run_get(self, args):
55 self.stdout = tutil.BytesIO()
56 self.stderr = tutil.StringIO()
57 return arv_get.main(args, self.stdout, self.stderr)
59 def test_version_argument(self):
60 with tutil.redirected_streams(
61 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
62 with self.assertRaises(SystemExit):
63 self.run_get(['--version'])
64 self.assertVersionOutput(out, err)
66 def test_get_single_file(self):
67 # Get the file using the collection's locator
68 r = self.run_get(["{}/subdir/baz.txt".format(self.col_loc), '-'])
69 self.assertEqual(0, r)
70 self.assertEqual(b'baz', self.stdout.getvalue())
72 r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
73 self.assertEqual(0, r)
74 self.assertEqual(b'baz', self.stdout.getvalue())
76 def test_get_multiple_files(self):
77 # Download the entire collection to the temp directory
78 r = self.run_get(["{}/".format(self.col_loc), self.tempdir])
79 self.assertEqual(0, r)
80 with open(os.path.join(self.tempdir, "foo.txt"), "r") as f:
81 self.assertEqual("foo", f.read())
82 with open(os.path.join(self.tempdir, "bar.txt"), "r") as f:
83 self.assertEqual("bar", f.read())
84 with open(os.path.join(self.tempdir, "subdir", "baz.txt"), "r") as f:
85 self.assertEqual("baz", f.read())
87 def test_get_collection_unstripped_manifest(self):
88 dummy_token = "+Axxxxxxx"
89 # Get the collection manifest by UUID
90 r = self.run_get([self.col_loc, self.tempdir])
91 self.assertEqual(0, r)
92 m_from_collection = re.sub(r"\+A[0-9a-f@]+", dummy_token, self.col_manifest)
93 with open(os.path.join(self.tempdir, self.col_loc), "r") as f:
94 # Replace manifest tokens before comparison to avoid races
95 m_from_file = re.sub(r"\+A[0-9a-f@]+", dummy_token, f.read())
96 self.assertEqual(m_from_collection, m_from_file)
97 # Get the collection manifest by PDH
98 r = self.run_get([self.col_pdh, self.tempdir])
99 self.assertEqual(0, r)
100 with open(os.path.join(self.tempdir, self.col_pdh), "r") as f:
101 # Replace manifest tokens before comparison to avoid races
102 m_from_file = re.sub(r"\+A[0-9a-f@]+", dummy_token, f.read())
103 self.assertEqual(m_from_collection, m_from_file)
105 def test_get_collection_stripped_manifest(self):
106 col_loc, col_pdh, col_manifest = self.write_test_collection(
108 # Get the collection manifest by UUID
109 r = self.run_get(['--strip-manifest', col_loc, self.tempdir])
110 self.assertEqual(0, r)
111 with open(os.path.join(self.tempdir, col_loc), "r") as f:
112 self.assertEqual(col_manifest, f.read())
113 # Get the collection manifest by PDH
114 r = self.run_get(['--strip-manifest', col_pdh, self.tempdir])
115 self.assertEqual(0, r)
116 with open(os.path.join(self.tempdir, col_pdh), "r") as f:
117 self.assertEqual(col_manifest, f.read())
119 def test_invalid_collection(self):
120 # Asking for an invalid collection should generate an error.
121 r = self.run_get(['this-uuid-seems-to-be-fake', self.tempdir])
122 self.assertNotEqual(0, r)
124 def test_invalid_file_request(self):
125 # Asking for an inexistant file within a collection should generate an error.
126 r = self.run_get(["{}/im-not-here.txt".format(self.col_loc), self.tempdir])
127 self.assertNotEqual(0, r)
129 def test_invalid_destination(self):
130 # Asking to place the collection's files on a non existant directory
131 # should generate an error.
132 r = self.run_get([self.col_loc, "/fake/subdir/"])
133 self.assertNotEqual(0, r)
135 def test_preexistent_destination(self):
136 # Asking to place a file with the same path as a local one should
137 # generate an error and avoid overwrites.
138 with open(os.path.join(self.tempdir, "foo.txt"), "w") as f:
139 f.write("another foo")
140 r = self.run_get(["{}/foo.txt".format(self.col_loc), self.tempdir])
141 self.assertNotEqual(0, r)
142 with open(os.path.join(self.tempdir, "foo.txt"), "r") as f:
143 self.assertEqual("another foo", f.read())
145 def test_no_progress_when_stderr_not_a_tty(self):
146 # Create a collection with a big file (>64MB) to force the progress
148 c = collection.Collection()
149 with c.open('bigfile.txt', 'wb') as f:
151 f.write("x" * 1024 * 1024)
153 tmpdir = self.make_tmpdir()
154 # Simulate a TTY stderr
155 stderr = mock.MagicMock()
156 stdout = tutil.BytesIO()
158 # Confirm that progress is written to stderr when is a tty
159 stderr.isatty.return_value = True
160 r = arv_get.main(['{}/bigfile.txt'.format(c.manifest_locator()),
161 '{}/bigfile.txt'.format(tmpdir)],
163 self.assertEqual(0, r)
164 self.assertEqual(b'', stdout.getvalue())
165 self.assertTrue(stderr.write.called)
167 # Clean up and reset stderr mock
168 os.remove('{}/bigfile.txt'.format(tmpdir))
169 stderr = mock.MagicMock()
170 stdout = tutil.BytesIO()
172 # Confirm that progress is not written to stderr when isn't a tty
173 stderr.isatty.return_value = False
174 r = arv_get.main(['{}/bigfile.txt'.format(c.manifest_locator()),
175 '{}/bigfile.txt'.format(tmpdir)],
177 self.assertEqual(0, r)
178 self.assertEqual(b'', stdout.getvalue())
179 self.assertFalse(stderr.write.called)