11502: Fix regression by writing a collection manifest with its access tokens in...
[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                               strip_manifest=False,
30                               contents = {
31                                   'foo.txt' : 'foo',
32                                   'bar.txt' : 'bar',
33                                   'subdir/baz.txt' : 'baz',
34                               }):
35         c = collection.Collection()
36         for path, data in contents.items():
37             with c.open(path, 'w') as f:
38                 f.write(data)
39         c.save_new()
40         return (c.manifest_locator(),
41                 c.portable_data_hash(),
42                 c.manifest_text(strip=strip_manifest))
43     
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)
48
49     def test_version_argument(self):
50         err = io.BytesIO()
51         out = io.BytesIO()
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]+")
57
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())
63         # Then, try by PDH
64         r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
65         self.assertEqual(0, r)
66         self.assertEqual('baz', self.stdout.getvalue())        
67
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())
78
79     def test_get_collection_unstripped_manifest(self):
80         # Get the collection manifest by UUID
81         r = self.run_get([self.col_loc, self.tempdir])
82         self.assertEqual(0, r)
83         with open("{}/{}".format(self.tempdir, self.col_loc), "r") as f:
84             self.assertEqual(self.col_manifest, f.read())
85         # Get the collection manifest by PDH
86         r = self.run_get([self.col_pdh, self.tempdir])
87         self.assertEqual(0, r)
88         with open("{}/{}".format(self.tempdir, self.col_pdh), "r") as f:
89             self.assertEqual(self.col_manifest, f.read())
90
91     def test_get_collection_stripped_manifest(self):
92         col_loc, col_pdh, col_manifest = self.write_test_collection(strip_manifest=True)
93         # Get the collection manifest by UUID
94         r = self.run_get(['--strip-manifest', col_loc, self.tempdir])
95         self.assertEqual(0, r)
96         with open("{}/{}".format(self.tempdir, col_loc), "r") as f:
97             self.assertEqual(col_manifest, f.read())
98         # Get the collection manifest by PDH
99         r = self.run_get(['--strip-manifest', col_pdh, self.tempdir])
100         self.assertEqual(0, r)
101         with open("{}/{}".format(self.tempdir, col_pdh), "r") as f:
102             self.assertEqual(col_manifest, f.read())
103
104     def test_invalid_collection(self):
105         # Asking for an invalid collection should generate an error.
106         r = self.run_get(['this-uuid-seems-to-be-fake', self.tempdir])
107         self.assertNotEqual(0, r)
108
109     def test_invalid_file_request(self):
110         # Asking for an inexistant file within a collection should generate an error.
111         r = self.run_get(["{}/im-not-here.txt".format(self.col_loc), self.tempdir])
112         self.assertNotEqual(0, r)
113
114     def test_invalid_destination(self):
115         # Asking to place the collection's files on a non existant directory
116         # should generate an error.
117         r = self.run_get([self.col_loc, "/fake/subdir/"])
118         self.assertNotEqual(0, r)
119
120     def test_preexistent_destination(self):
121         # Asking to place a file with the same path as a local one should
122         # generate an error and avoid overwrites.
123         with open("{}/foo.txt".format(self.tempdir), "w") as f:
124             f.write("another foo")
125         r = self.run_get(["{}/foo.txt".format(self.col_loc), self.tempdir])
126         self.assertNotEqual(0, r)
127         with open("{}/foo.txt".format(self.tempdir), "r") as f:
128             self.assertEqual("another foo", f.read())
129