Merge branch '11502-arvget-flaky-test'
[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=True,
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_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())
85
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)
90
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)
95
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)
101
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())
111