Merge branch 'master' into 10401-limit-dir-expansion
[arvados.git] / sdk / python / tests / test_arv_get.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import io
5 import shutil
6 import tempfile
7
8 import arvados
9 import arvados.collection as collection
10 import arvados.commands.get as arv_get
11 import run_test_server
12
13 from arvados_testutil import redirected_streams
14
15 class ArvadosGetTestCase(run_test_server.TestCaseWithServers):
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 contents.items():
36             with c.open(path, 'w') 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 = io.BytesIO()
43         self.stderr = io.BytesIO()
44         return arv_get.main(args, self.stdout, self.stderr)
45
46     def test_version_argument(self):
47         err = io.BytesIO()
48         out = io.BytesIO()
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]+")
54
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())
60         # Then, try by PDH
61         r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
62         self.assertEqual(0, r)
63         self.assertEqual('baz', self.stdout.getvalue())        
64
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())
75
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())
82
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)
87
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)
92
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)
98
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())
108