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