Merge branch '8784-dir-listings'
[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 os
9 import re
10 import shutil
11 import tempfile
12
13 import arvados
14 import arvados.collection as collection
15 import arvados.commands.get as arv_get
16 from . import run_test_server
17
18 from . import arvados_testutil as tutil
19
20 class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
21                          tutil.VersionChecker):
22     MAIN_SERVER = {}
23     KEEP_SERVER = {}
24
25     def setUp(self):
26         super(ArvadosGetTestCase, self).setUp()
27         self.tempdir = tempfile.mkdtemp()
28         self.col_loc, self.col_pdh, self.col_manifest = self.write_test_collection()
29
30     def tearDown(self):
31         super(ArvadosGetTestCase, self).tearDown()
32         shutil.rmtree(self.tempdir)
33
34     def write_test_collection(self,
35                               strip_manifest=False,
36                               contents = {
37                                   'foo.txt' : 'foo',
38                                   'bar.txt' : 'bar',
39                                   'subdir/baz.txt' : 'baz',
40                               }):
41         c = collection.Collection()
42         for path, data in listitems(contents):
43             with c.open(path, 'wb') as f:
44                 f.write(data)
45         c.save_new()
46
47         return (c.manifest_locator(),
48                 c.portable_data_hash(),
49                 c.manifest_text(strip=strip_manifest))
50
51     def run_get(self, args):
52         self.stdout = tutil.BytesIO()
53         self.stderr = tutil.StringIO()
54         return arv_get.main(args, self.stdout, self.stderr)
55
56     def test_version_argument(self):
57         with tutil.redirected_streams(
58                 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
59             with self.assertRaises(SystemExit):
60                 self.run_get(['--version'])
61         self.assertVersionOutput(out, err)
62
63     def test_get_single_file(self):
64         # Get the file using the collection's locator
65         r = self.run_get(["{}/subdir/baz.txt".format(self.col_loc), '-'])
66         self.assertEqual(0, r)
67         self.assertEqual(b'baz', self.stdout.getvalue())
68         # Then, try by PDH
69         r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
70         self.assertEqual(0, r)
71         self.assertEqual(b'baz', self.stdout.getvalue())
72
73     def test_get_multiple_files(self):
74         # Download the entire collection to the temp directory
75         r = self.run_get(["{}/".format(self.col_loc), self.tempdir])
76         self.assertEqual(0, r)
77         with open(os.path.join(self.tempdir, "foo.txt"), "r") as f:
78             self.assertEqual("foo", f.read())
79         with open(os.path.join(self.tempdir, "bar.txt"), "r") as f:
80             self.assertEqual("bar", f.read())
81         with open(os.path.join(self.tempdir, "subdir", "baz.txt"), "r") as f:
82             self.assertEqual("baz", f.read())
83
84     def test_get_collection_unstripped_manifest(self):
85         dummy_token = "+Axxxxxxx"
86         # Get the collection manifest by UUID
87         r = self.run_get([self.col_loc, self.tempdir])
88         self.assertEqual(0, r)
89         m_from_collection = re.sub(r"\+A[0-9a-f@]+", dummy_token, self.col_manifest)
90         with open(os.path.join(self.tempdir, self.col_loc), "r") as f:
91             # Replace manifest tokens before comparison to avoid races
92             m_from_file = re.sub(r"\+A[0-9a-f@]+", dummy_token, f.read())
93             self.assertEqual(m_from_collection, m_from_file)
94         # Get the collection manifest by PDH
95         r = self.run_get([self.col_pdh, self.tempdir])
96         self.assertEqual(0, r)
97         with open(os.path.join(self.tempdir, self.col_pdh), "r") as f:
98             # Replace manifest tokens before comparison to avoid races
99             m_from_file = re.sub(r"\+A[0-9a-f@]+", dummy_token, f.read())
100             self.assertEqual(m_from_collection, m_from_file)
101
102     def test_get_collection_stripped_manifest(self):
103         col_loc, col_pdh, col_manifest = self.write_test_collection(strip_manifest=True)
104         # Get the collection manifest by UUID
105         r = self.run_get(['--strip-manifest', col_loc, self.tempdir])
106         self.assertEqual(0, r)
107         with open(os.path.join(self.tempdir, col_loc), "r") as f:
108             self.assertEqual(col_manifest, f.read())
109         # Get the collection manifest by PDH
110         r = self.run_get(['--strip-manifest', col_pdh, self.tempdir])
111         self.assertEqual(0, r)
112         with open(os.path.join(self.tempdir, col_pdh), "r") as f:
113             self.assertEqual(col_manifest, f.read())
114
115     def test_invalid_collection(self):
116         # Asking for an invalid collection should generate an error.
117         r = self.run_get(['this-uuid-seems-to-be-fake', self.tempdir])
118         self.assertNotEqual(0, r)
119
120     def test_invalid_file_request(self):
121         # Asking for an inexistant file within a collection should generate an error.
122         r = self.run_get(["{}/im-not-here.txt".format(self.col_loc), self.tempdir])
123         self.assertNotEqual(0, r)
124
125     def test_invalid_destination(self):
126         # Asking to place the collection's files on a non existant directory
127         # should generate an error.
128         r = self.run_get([self.col_loc, "/fake/subdir/"])
129         self.assertNotEqual(0, r)
130
131     def test_preexistent_destination(self):
132         # Asking to place a file with the same path as a local one should
133         # generate an error and avoid overwrites.
134         with open(os.path.join(self.tempdir, "foo.txt"), "w") as f:
135             f.write("another foo")
136         r = self.run_get(["{}/foo.txt".format(self.col_loc), self.tempdir])
137         self.assertNotEqual(0, r)
138         with open(os.path.join(self.tempdir, "foo.txt"), "r") as f:
139             self.assertEqual("another foo", f.read())
140