8784: Fix test for latest firefox.
[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 os
5 import re
6 import shutil
7 import tempfile
8
9 import arvados
10 import arvados.collection as collection
11 import arvados.commands.get as arv_get
12 from . import run_test_server
13
14 from . import arvados_testutil as tutil
15
16 class ArvadosGetTestCase(run_test_server.TestCaseWithServers,
17                          tutil.VersionChecker):
18     MAIN_SERVER = {}
19     KEEP_SERVER = {}
20
21     def setUp(self):
22         super(ArvadosGetTestCase, self).setUp()
23         self.tempdir = tempfile.mkdtemp()
24         self.col_loc, self.col_pdh, self.col_manifest = self.write_test_collection()
25
26     def tearDown(self):
27         super(ArvadosGetTestCase, self).tearDown()
28         shutil.rmtree(self.tempdir)
29
30     def write_test_collection(self,
31                               strip_manifest=False,
32                               contents = {
33                                   'foo.txt' : 'foo',
34                                   'bar.txt' : 'bar',
35                                   'subdir/baz.txt' : 'baz',
36                               }):
37         c = collection.Collection()
38         for path, data in listitems(contents):
39             with c.open(path, 'wb') as f:
40                 f.write(data)
41         c.save_new()
42
43         return (c.manifest_locator(),
44                 c.portable_data_hash(),
45                 c.manifest_text(strip=strip_manifest))
46
47     def run_get(self, args):
48         self.stdout = tutil.BytesIO()
49         self.stderr = tutil.StringIO()
50         return arv_get.main(args, self.stdout, self.stderr)
51
52     def test_version_argument(self):
53         with tutil.redirected_streams(
54                 stdout=tutil.StringIO, stderr=tutil.StringIO) as (out, err):
55             with self.assertRaises(SystemExit):
56                 self.run_get(['--version'])
57         self.assertVersionOutput(out, err)
58
59     def test_get_single_file(self):
60         # Get the file using the collection's locator
61         r = self.run_get(["{}/subdir/baz.txt".format(self.col_loc), '-'])
62         self.assertEqual(0, r)
63         self.assertEqual(b'baz', self.stdout.getvalue())
64         # Then, try by PDH
65         r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
66         self.assertEqual(0, r)
67         self.assertEqual(b'baz', self.stdout.getvalue())
68
69     def test_get_multiple_files(self):
70         # Download the entire collection to the temp directory
71         r = self.run_get(["{}/".format(self.col_loc), self.tempdir])
72         self.assertEqual(0, r)
73         with open(os.path.join(self.tempdir, "foo.txt"), "r") as f:
74             self.assertEqual("foo", f.read())
75         with open(os.path.join(self.tempdir, "bar.txt"), "r") as f:
76             self.assertEqual("bar", f.read())
77         with open(os.path.join(self.tempdir, "subdir", "baz.txt"), "r") as f:
78             self.assertEqual("baz", f.read())
79
80     def test_get_collection_unstripped_manifest(self):
81         dummy_token = "+Axxxxxxx"
82         # Get the collection manifest by UUID
83         r = self.run_get([self.col_loc, self.tempdir])
84         self.assertEqual(0, r)
85         m_from_collection = re.sub(r"\+A[0-9a-f@]+", dummy_token, self.col_manifest)
86         with open(os.path.join(self.tempdir, self.col_loc), "r") as f:
87             # Replace manifest tokens before comparison to avoid races
88             m_from_file = re.sub(r"\+A[0-9a-f@]+", dummy_token, f.read())
89             self.assertEqual(m_from_collection, m_from_file)
90         # Get the collection manifest by PDH
91         r = self.run_get([self.col_pdh, self.tempdir])
92         self.assertEqual(0, r)
93         with open(os.path.join(self.tempdir, self.col_pdh), "r") as f:
94             # Replace manifest tokens before comparison to avoid races
95             m_from_file = re.sub(r"\+A[0-9a-f@]+", dummy_token, f.read())
96             self.assertEqual(m_from_collection, m_from_file)
97
98     def test_get_collection_stripped_manifest(self):
99         col_loc, col_pdh, col_manifest = self.write_test_collection(strip_manifest=True)
100         # Get the collection manifest by UUID
101         r = self.run_get(['--strip-manifest', col_loc, self.tempdir])
102         self.assertEqual(0, r)
103         with open(os.path.join(self.tempdir, col_loc), "r") as f:
104             self.assertEqual(col_manifest, f.read())
105         # Get the collection manifest by PDH
106         r = self.run_get(['--strip-manifest', col_pdh, self.tempdir])
107         self.assertEqual(0, r)
108         with open(os.path.join(self.tempdir, col_pdh), "r") as f:
109             self.assertEqual(col_manifest, f.read())
110
111     def test_invalid_collection(self):
112         # Asking for an invalid collection should generate an error.
113         r = self.run_get(['this-uuid-seems-to-be-fake', self.tempdir])
114         self.assertNotEqual(0, r)
115
116     def test_invalid_file_request(self):
117         # Asking for an inexistant file within a collection should generate an error.
118         r = self.run_get(["{}/im-not-here.txt".format(self.col_loc), self.tempdir])
119         self.assertNotEqual(0, r)
120
121     def test_invalid_destination(self):
122         # Asking to place the collection's files on a non existant directory
123         # should generate an error.
124         r = self.run_get([self.col_loc, "/fake/subdir/"])
125         self.assertNotEqual(0, r)
126
127     def test_preexistent_destination(self):
128         # Asking to place a file with the same path as a local one should
129         # generate an error and avoid overwrites.
130         with open(os.path.join(self.tempdir, "foo.txt"), "w") as f:
131             f.write("another foo")
132         r = self.run_get(["{}/foo.txt".format(self.col_loc), self.tempdir])
133         self.assertNotEqual(0, r)
134         with open(os.path.join(self.tempdir, "foo.txt"), "r") as f:
135             self.assertEqual("another foo", f.read())
136