2 # -*- coding: utf-8 -*-
9 import arvados.collection as collection
10 import arvados.commands.get as arv_get
11 import run_test_server
13 from arvados_testutil import redirected_streams
15 class ArvadosGetTestCase(run_test_server.TestCaseWithServers):
20 super(ArvadosGetTestCase, self).setUp()
21 self.tempdir = tempfile.mkdtemp()
22 self.col_loc, self.col_pdh, self.col_manifest = self.write_test_collection()
25 super(ArvadosGetTestCase, self).tearDown()
26 shutil.rmtree(self.tempdir)
28 def write_test_collection(self,
33 'subdir/baz.txt' : 'baz',
35 c = collection.Collection()
36 for path, data in contents.items():
37 with c.open(path, 'w') as f:
40 return (c.manifest_locator(),
41 c.portable_data_hash(),
42 c.manifest_text(strip=strip_manifest))
44 def run_get(self, args):
45 self.stdout = io.BytesIO()
46 self.stderr = io.BytesIO()
47 return arv_get.main(args, self.stdout, self.stderr)
49 def test_version_argument(self):
52 with redirected_streams(stdout=out, stderr=err):
53 with self.assertRaises(SystemExit):
54 self.run_get(['--version'])
55 self.assertEqual(out.getvalue(), '')
56 self.assertRegexpMatches(err.getvalue(), "[0-9]+\.[0-9]+\.[0-9]+")
58 def test_get_single_file(self):
59 # Get the file using the collection's locator
60 r = self.run_get(["{}/subdir/baz.txt".format(self.col_loc), '-'])
61 self.assertEqual(0, r)
62 self.assertEqual('baz', self.stdout.getvalue())
64 r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
65 self.assertEqual(0, r)
66 self.assertEqual('baz', self.stdout.getvalue())
68 def test_get_multiple_files(self):
69 # Download the entire collection to the temp directory
70 r = self.run_get(["{}/".format(self.col_loc), self.tempdir])
71 self.assertEqual(0, r)
72 with open("{}/foo.txt".format(self.tempdir), "r") as f:
73 self.assertEqual("foo", f.read())
74 with open("{}/bar.txt".format(self.tempdir), "r") as f:
75 self.assertEqual("bar", f.read())
76 with open("{}/subdir/baz.txt".format(self.tempdir), "r") as f:
77 self.assertEqual("baz", f.read())
79 def test_get_collection_manifest(self):
80 # Get the collection manifest
81 r = self.run_get([self.col_pdh, self.tempdir])
82 self.assertEqual(0, r)
83 with open("{}/{}".format(self.tempdir, self.col_pdh), "r") as f:
84 self.assertEqual(self.col_manifest, f.read())
86 def test_invalid_collection(self):
87 # Asking for an invalid collection should generate an error.
88 r = self.run_get(['this-uuid-seems-to-be-fake', self.tempdir])
89 self.assertNotEqual(0, r)
91 def test_invalid_file_request(self):
92 # Asking for an inexistant file within a collection should generate an error.
93 r = self.run_get(["{}/im-not-here.txt".format(self.col_loc), self.tempdir])
94 self.assertNotEqual(0, r)
96 def test_invalid_destination(self):
97 # Asking to place the collection's files on a non existant directory
98 # should generate an error.
99 r = self.run_get([self.col_loc, "/fake/subdir/"])
100 self.assertNotEqual(0, r)
102 def test_preexistent_destination(self):
103 # Asking to place a file with the same path as a local one should
104 # generate an error and avoid overwrites.
105 with open("{}/foo.txt".format(self.tempdir), "w") as f:
106 f.write("another foo")
107 r = self.run_get(["{}/foo.txt".format(self.col_loc), self.tempdir])
108 self.assertNotEqual(0, r)
109 with open("{}/foo.txt".format(self.tempdir), "r") as f:
110 self.assertEqual("another foo", f.read())