2 # -*- coding: utf-8 -*-
10 import arvados.collection as collection
11 import arvados.commands.get as arv_get
12 import run_test_server
14 from arvados_testutil import redirected_streams
16 class ArvadosGetTestCase(run_test_server.TestCaseWithServers):
21 super(ArvadosGetTestCase, self).setUp()
22 self.tempdir = tempfile.mkdtemp()
23 self.col_loc, self.col_pdh, self.col_manifest = self.write_test_collection()
26 super(ArvadosGetTestCase, self).tearDown()
27 shutil.rmtree(self.tempdir)
29 def write_test_collection(self,
34 'subdir/baz.txt' : 'baz',
36 c = collection.Collection()
37 for path, data in contents.items():
38 with c.open(path, 'w') as f:
41 return (c.manifest_locator(),
42 c.portable_data_hash(),
43 c.manifest_text(strip=strip_manifest))
45 def run_get(self, args):
46 self.stdout = io.BytesIO()
47 self.stderr = io.BytesIO()
48 return arv_get.main(args, self.stdout, self.stderr)
50 def test_version_argument(self):
53 with redirected_streams(stdout=out, stderr=err):
54 with self.assertRaises(SystemExit):
55 self.run_get(['--version'])
56 self.assertEqual(out.getvalue(), '')
57 self.assertRegexpMatches(err.getvalue(), "[0-9]+\.[0-9]+\.[0-9]+")
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('baz', self.stdout.getvalue())
65 r = self.run_get(["{}/subdir/baz.txt".format(self.col_pdh), '-'])
66 self.assertEqual(0, r)
67 self.assertEqual('baz', self.stdout.getvalue())
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("{}/foo.txt".format(self.tempdir), "r") as f:
74 self.assertEqual("foo", f.read())
75 with open("{}/bar.txt".format(self.tempdir), "r") as f:
76 self.assertEqual("bar", f.read())
77 with open("{}/subdir/baz.txt".format(self.tempdir), "r") as f:
78 self.assertEqual("baz", f.read())
80 def test_get_collection_unstripped_manifest(self):
81 # Get the collection manifest by UUID
82 r = self.run_get([self.col_loc, self.tempdir])
83 self.assertEqual(0, r)
84 m_from_collection = collection.Collection(self.col_manifest).manifest_text(strip=True)
85 with open(os.path.join(self.tempdir, self.col_loc), "r") as f:
86 # Strip manifest before comparison to avoid races
87 m_from_file = collection.Collection(f.read()).manifest_text(strip=True)
88 self.assertEqual(m_from_collection, m_from_file)
89 # Get the collection manifest by PDH
90 r = self.run_get([self.col_pdh, self.tempdir])
91 self.assertEqual(0, r)
92 with open(os.path.join(self.tempdir, self.col_pdh), "r") as f:
93 # Strip manifest before comparison to avoid races
94 m_from_file = collection.Collection(f.read()).manifest_text(strip=True)
95 self.assertEqual(m_from_collection, m_from_file)
97 def test_get_collection_stripped_manifest(self):
98 col_loc, col_pdh, col_manifest = self.write_test_collection(strip_manifest=True)
99 # Get the collection manifest by UUID
100 r = self.run_get(['--strip-manifest', col_loc, self.tempdir])
101 self.assertEqual(0, r)
102 with open("{}/{}".format(self.tempdir, col_loc), "r") as f:
103 self.assertEqual(col_manifest, f.read())
104 # Get the collection manifest by PDH
105 r = self.run_get(['--strip-manifest', col_pdh, self.tempdir])
106 self.assertEqual(0, r)
107 with open("{}/{}".format(self.tempdir, col_pdh), "r") as f:
108 self.assertEqual(col_manifest, f.read())
110 def test_invalid_collection(self):
111 # Asking for an invalid collection should generate an error.
112 r = self.run_get(['this-uuid-seems-to-be-fake', self.tempdir])
113 self.assertNotEqual(0, r)
115 def test_invalid_file_request(self):
116 # Asking for an inexistant file within a collection should generate an error.
117 r = self.run_get(["{}/im-not-here.txt".format(self.col_loc), self.tempdir])
118 self.assertNotEqual(0, r)
120 def test_invalid_destination(self):
121 # Asking to place the collection's files on a non existant directory
122 # should generate an error.
123 r = self.run_get([self.col_loc, "/fake/subdir/"])
124 self.assertNotEqual(0, r)
126 def test_preexistent_destination(self):
127 # Asking to place a file with the same path as a local one should
128 # generate an error and avoid overwrites.
129 with open("{}/foo.txt".format(self.tempdir), "w") as f:
130 f.write("another foo")
131 r = self.run_get(["{}/foo.txt".format(self.col_loc), self.tempdir])
132 self.assertNotEqual(0, r)
133 with open("{}/foo.txt".format(self.tempdir), "r") as f:
134 self.assertEqual("another foo", f.read())