11308: Fix bytes vs. strings in arv-get command and tests.
[arvados.git] / sdk / python / tests / test_arv_get.py
1 from __future__ import absolute_import
2 from future.utils import listitems
3 import io
4 import shutil
5 import tempfile
6
7 import arvados
8 import arvados.collection as collection
9 import arvados.commands.get as arv_get
10 from . import run_test_server
11
12 from . import arvados_testutil as tutil
13
14 class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
15                          tutil.VersionChecker):
16     MAIN_SERVER = {}
17     KEEP_SERVER = {}
18
19     def setUp(self):
20         super(ArvadosGetTestCase, self).setUp()
21         self.tempdir = tempfile.mkdtemp()
22         self.col_loc, self.col_pdh, self.col_manifest = self.write_test_collection()
23
24     def tearDown(self):
25         super(ArvadosGetTestCase, self).tearDown()
26         shutil.rmtree(self.tempdir)
27
28     def write_test_collection(self,
29                               contents = {
30                                   'foo.txt' : 'foo',
31                                   'bar.txt' : 'bar',
32                                   'subdir/baz.txt' : 'baz',
33                               }):
34         c = collection.Collection()
35         for path, data in listitems(contents):
36             with c.open(path, 'wb') as f:
37                 f.write(data)
38         c.save_new()
39         return (c.manifest_locator(), c.portable_data_hash(), c.manifest_text())
40
41     def run_get(self, args):
42         self.stdout = tutil.BytesIO()
43         self.stderr = tutil.StringIO()
44         return arv_get.main(args, self.stdout, self.stderr)
45
46     def test_version_argument(self):
47         with tutil.redirected_streams(
48                 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
49             with self.assertRaises(SystemExit):
50                 self.run_get(['--version'])
51         self.assertVersionOutput(out, err)
52
53     def test_get_single_file(self):
54         # Get the file using the collection's locator
55         r = self.run_get(["{}/subdir/baz.txt".format(self.col_loc), '-'])
56         self.assertEqual(0, r)
57         self.assertEqual(b'baz', self.stdout.getvalue())
58         # Then, try by PDH
59         r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
60         self.assertEqual(0, r)
61         self.assertEqual(b'baz', self.stdout.getvalue())
62
63     def test_get_multiple_files(self):
64         # Download the entire collection to the temp directory
65         r = self.run_get(["{}/".format(self.col_loc), self.tempdir])
66         self.assertEqual(0, r)
67         with open("{}/foo.txt".format(self.tempdir), "r") as f:
68             self.assertEqual("foo", f.read())
69         with open("{}/bar.txt".format(self.tempdir), "r") as f:
70             self.assertEqual("bar", f.read())
71         with open("{}/subdir/baz.txt".format(self.tempdir), "r") as f:
72             self.assertEqual("baz", f.read())
73
74     def test_get_collection_manifest(self):
75         # Get the collection manifest
76         r = self.run_get([self.col_loc, self.tempdir])
77         self.assertEqual(0, r)
78         with open("{}/{}".format(self.tempdir, self.col_loc), "r") as f:
79             self.assertEqual(self.col_manifest, f.read())
80
81     def test_invalid_collection(self):
82         # Asking for an invalid collection should generate an error.
83         r = self.run_get(['this-uuid-seems-to-be-fake', self.tempdir])
84         self.assertNotEqual(0, r)
85
86     def test_invalid_file_request(self):
87         # Asking for an inexistant file within a collection should generate an error.
88         r = self.run_get(["{}/im-not-here.txt".format(self.col_loc), self.tempdir])
89         self.assertNotEqual(0, r)
90
91     def test_invalid_destination(self):
92         # Asking to place the collection's files on a non existant directory
93         # should generate an error.
94         r = self.run_get([self.col_loc, "/fake/subdir/"])
95         self.assertNotEqual(0, r)
96
97     def test_preexistent_destination(self):
98         # Asking to place a file with the same path as a local one should
99         # generate an error and avoid overwrites.
100         with open("{}/foo.txt".format(self.tempdir), "w") as f:
101             f.write("another foo")
102         r = self.run_get(["{}/foo.txt".format(self.col_loc), self.tempdir])
103         self.assertNotEqual(0, r)
104         with open("{}/foo.txt".format(self.tempdir), "r") as f:
105             self.assertEqual("another foo", f.read())
106