2 # -*- coding: utf-8 -*-
11 import arvados.errors as arv_error
12 import arvados.commands.ls as arv_ls
13 import run_test_server
15 from arvados_testutil import str_keep_locator, redirected_streams
17 class ArvLsTestCase(run_test_server.TestCaseWithServers):
18 FAKE_UUID = 'zzzzz-4zz18-12345abcde12345'
20 def newline_join(self, seq):
21 return '\n'.join(seq) + '\n'
23 def random_blocks(self, *sizes):
24 return ' '.join('{:032x}+{:d}'.format(
25 random.randint(0, (16 ** 32) - 1), size
28 def mock_api_for_manifest(self, manifest_lines, uuid=FAKE_UUID):
29 manifest_text = self.newline_join(manifest_lines)
30 pdh = str_keep_locator(manifest_text)
31 coll_info = {'uuid': uuid,
32 'portable_data_hash': pdh,
33 'manifest_text': manifest_text}
34 api_client = mock.MagicMock(name='mock_api_client')
35 api_client.collections().get().execute.return_value = coll_info
36 return coll_info, api_client
38 def run_ls(self, args, api_client):
39 self.stdout = io.BytesIO()
40 self.stderr = io.BytesIO()
41 return arv_ls.main(args, self.stdout, self.stderr, api_client)
43 def test_plain_listing(self):
44 collection, api_client = self.mock_api_for_manifest(
45 ['. {} 0:3:one.txt 3:4:two.txt'.format(self.random_blocks(5, 2)),
46 './dir {} 1:5:sub.txt'.format(self.random_blocks(8))])
47 self.assertEqual(0, self.run_ls([collection['uuid']], api_client))
49 self.newline_join(['./one.txt', './two.txt', './dir/sub.txt']),
50 self.stdout.getvalue())
51 self.assertEqual('', self.stderr.getvalue())
53 def test_size_listing(self):
54 collection, api_client = self.mock_api_for_manifest(
55 ['. {} 0:0:0.txt 0:1000:1.txt 1000:2000:2.txt'.format(
56 self.random_blocks(3000))])
57 self.assertEqual(0, self.run_ls(['-s', collection['uuid']], api_client))
58 self.stdout.seek(0, 0)
59 for expected in range(3):
60 actual_size, actual_name = self.stdout.readline().split()
61 # But she seems much bigger to me...
62 self.assertEqual(str(expected), actual_size)
63 self.assertEqual('./{}.txt'.format(expected), actual_name)
64 self.assertEqual('', self.stdout.read(-1))
65 self.assertEqual('', self.stderr.getvalue())
67 def test_nonnormalized_manifest(self):
68 collection, api_client = self.mock_api_for_manifest(
69 ['. {} 0:1010:non.txt'.format(self.random_blocks(1010)),
70 '. {} 0:2020:non.txt'.format(self.random_blocks(2020))])
71 self.assertEqual(0, self.run_ls(['-s', collection['uuid']], api_client))
72 self.stdout.seek(0, 0)
73 self.assertEqual(['3', './non.txt'], self.stdout.readline().split())
74 self.assertEqual('', self.stdout.read(-1))
75 self.assertEqual('', self.stderr.getvalue())
77 def test_locator_failure(self):
78 api_client = mock.MagicMock(name='mock_api_client')
79 api_client.collections().get().execute.side_effect = (
80 arv_error.NotFoundError)
81 self.assertNotEqual(0, self.run_ls([self.FAKE_UUID], api_client))
82 self.assertNotEqual('', self.stderr.getvalue())
84 def test_version_argument(self):
87 with redirected_streams(stdout=out, stderr=err):
88 with self.assertRaises(SystemExit):
89 self.run_ls(['--version'], None)
90 self.assertEqual(out.getvalue(), '')
91 self.assertRegexpMatches(err.getvalue(), "[0-9]+\.[0-9]+\.[0-9]+")