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,
32 'subdir/baz.txt' : 'baz',
34 c = collection.Collection()
35 for path, data in contents.items():
36 with c.open(path, 'w') as f:
39 return (c.manifest_locator(), c.portable_data_hash(), c.manifest_text())
41 def run_get(self, args):
42 self.stdout = io.BytesIO()
43 self.stderr = io.BytesIO()
44 return arv_get.main(args, self.stdout, self.stderr)
46 def test_version_argument(self):
49 with redirected_streams(stdout=out, stderr=err):
50 with self.assertRaises(SystemExit):
51 self.run_get(['--version'])
52 self.assertEqual(out.getvalue(), '')
53 self.assertRegexpMatches(err.getvalue(), "[0-9]+\.[0-9]+\.[0-9]+")
55 def test_get_single_file(self):
56 # Get the file using the collection's locator
57 r = self.run_get(["{}/subdir/baz.txt".format(self.col_loc), '-'])
58 self.assertEqual(0, r)
59 self.assertEqual('baz', self.stdout.getvalue())
61 r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
62 self.assertEqual(0, r)
63 self.assertEqual('baz', self.stdout.getvalue())
65 def test_get_multiple_files(self):
66 # Download the entire collection to the temp directory
67 r = self.run_get(["{}/".format(self.col_loc), self.tempdir])
68 self.assertEqual(0, r)
69 with open("{}/foo.txt".format(self.tempdir), "r") as f:
70 self.assertEqual("foo", f.read())
71 with open("{}/bar.txt".format(self.tempdir), "r") as f:
72 self.assertEqual("bar", f.read())
73 with open("{}/subdir/baz.txt".format(self.tempdir), "r") as f:
74 self.assertEqual("baz", f.read())
76 def test_get_collection_manifest(self):
77 # Get the collection manifest
78 r = self.run_get([self.col_loc, self.tempdir])
79 self.assertEqual(0, r)
80 with open("{}/{}".format(self.tempdir, self.col_loc), "r") as f:
81 self.assertEqual(self.col_manifest, f.read())
83 def test_invalid_collection(self):
84 # Asking for an invalid collection should generate an error.
85 r = self.run_get(['this-uuid-seems-to-be-fake', self.tempdir])
86 self.assertNotEqual(0, r)
88 def test_invalid_file_request(self):
89 # Asking for an inexistant file within a collection should generate an error.
90 r = self.run_get(["{}/im-not-here.txt".format(self.col_loc), self.tempdir])
91 self.assertNotEqual(0, r)
93 def test_invalid_destination(self):
94 # Asking to place the collection's files on a non existant directory
95 # should generate an error.
96 r = self.run_get([self.col_loc, "/fake/subdir/"])
97 self.assertNotEqual(0, r)
99 def test_preexistent_destination(self):
100 # Asking to place a file with the same path as a local one should
101 # generate an error and avoid overwrites.
102 with open("{}/foo.txt".format(self.tempdir), "w") as f:
103 f.write("another foo")
104 r = self.run_get(["{}/foo.txt".format(self.col_loc), self.tempdir])
105 self.assertNotEqual(0, r)
106 with open("{}/foo.txt".format(self.tempdir), "r") as f:
107 self.assertEqual("another foo", f.read())