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
14 import arvados.collection as collection
15 import arvados.commands.get as arv_get
16 from . import run_test_server
18 from . import arvados_testutil as tutil
20 class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
21 tutil.VersionChecker):
26 super(ArvadosGetTestCase, self).setUp()
27 self.tempdir = tempfile.mkdtemp()
28 self.col_loc, self.col_pdh, self.col_manifest = self.write_test_collection()
31 super(ArvadosGetTestCase, self).tearDown()
32 shutil.rmtree(self.tempdir)
34 def write_test_collection(self,
39 'subdir/baz.txt' : 'baz',
41 c = collection.Collection()
42 for path, data in listitems(contents):
43 with c.open(path, 'wb') as f:
47 return (c.manifest_locator(),
48 c.portable_data_hash(),
49 c.manifest_text(strip=strip_manifest))
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)
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)
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())
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())
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())
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)
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())
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)
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)
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)
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())