11167: Merge branch 'master' into 11167-wb-remove-arvget
[arvados.git] / sdk / python / tests / test_arv_get.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 from __future__ import absolute_import
6 from future.utils import listitems
7 import io
8 import mock
9 import os
10 import re
11 import shutil
12 import tempfile
13
14 import arvados
15 import arvados.collection as collection
16 import arvados.commands.get as arv_get
17 from . import run_test_server
18
19 from . import arvados_testutil as tutil
20 from .arvados_testutil import ArvadosBaseTestCase
21
22 class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
23                          tutil.VersionChecker,
24                          ArvadosBaseTestCase):
25     MAIN_SERVER = {}
26     KEEP_SERVER = {}
27
28     def setUp(self):
29         super(ArvadosGetTestCase, self).setUp()
30         self.tempdir = tempfile.mkdtemp()
31         self.col_loc, self.col_pdh, self.col_manifest = self.write_test_collection()
32
33     def tearDown(self):
34         super(ArvadosGetTestCase, self).tearDown()
35         shutil.rmtree(self.tempdir)
36
37     def write_test_collection(self,
38                               strip_manifest=False,
39                               contents = {
40                                   'foo.txt' : 'foo',
41                                   'bar.txt' : 'bar',
42                                   'subdir/baz.txt' : 'baz',
43                               }):
44         c = collection.Collection()
45         for path, data in listitems(contents):
46             with c.open(path, 'wb') as f:
47                 f.write(data)
48         c.save_new()
49
50         return (c.manifest_locator(),
51                 c.portable_data_hash(),
52                 c.manifest_text(strip=strip_manifest))
53
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)
58
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)
65
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())
71         # Then, try by PDH
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())
75
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())
86
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)
104
105     def test_get_collection_stripped_manifest(self):
106         col_loc, col_pdh, col_manifest = self.write_test_collection(
107             strip_manifest=True)
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())
118
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)
123
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)
128
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)
134
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())
144
145     def test_no_progress_when_stderr_not_a_tty(self):
146         # Create a collection with a big file (>64MB) to force the progress
147         # to be printed
148         c = collection.Collection()
149         with c.open('bigfile.txt', 'wb') as f:
150             for _ in range(65):
151                 f.write("x" * 1024 * 1024)
152         c.save_new()
153         tmpdir = self.make_tmpdir()
154         # Simulate a TTY stderr
155         stderr = mock.MagicMock()
156         stdout = tutil.BytesIO()
157
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)],
162                          stdout, stderr)
163         self.assertEqual(0, r)
164         self.assertEqual(b'', stdout.getvalue())
165         self.assertTrue(stderr.write.called)
166
167         # Clean up and reset stderr mock
168         os.remove('{}/bigfile.txt'.format(tmpdir))
169         stderr = mock.MagicMock()
170         stdout = tutil.BytesIO()
171
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)],
176                          stdout, stderr)
177         self.assertEqual(0, r)
178         self.assertEqual(b'', stdout.getvalue())
179         self.assertFalse(stderr.write.called)