1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import absolute_import
6 from builtins import str
7 from builtins import range
14 import arvados.errors as arv_error
15 import arvados.commands.ls as arv_ls
16 from . import run_test_server
18 from . import arvados_testutil as tutil
19 from .arvados_testutil import str_keep_locator, redirected_streams, StringIO
21 class ArvLsTestCase(run_test_server.TestCaseWithServers, tutil.VersionChecker):
22 FAKE_UUID = 'zzzzz-4zz18-12345abcde12345'
24 def newline_join(self, seq):
25 return '\n'.join(seq) + '\n'
27 def random_blocks(self, *sizes):
28 return ' '.join('{:032x}+{:d}'.format(
29 random.randint(0, (16 ** 32) - 1), size
32 def mock_api_for_manifest(self, manifest_lines, uuid=FAKE_UUID):
33 manifest_text = self.newline_join(manifest_lines)
34 pdh = str_keep_locator(manifest_text)
35 coll_info = {'uuid': uuid,
36 'portable_data_hash': pdh,
37 'manifest_text': manifest_text}
38 api_client = mock.MagicMock(name='mock_api_client')
39 api_client.collections().get().execute.return_value = coll_info
40 return coll_info, api_client
42 def run_ls(self, args, api_client, logger=None):
43 self.stdout = StringIO()
44 self.stderr = StringIO()
45 return arv_ls.main(args, self.stdout, self.stderr, api_client, logger)
47 def test_plain_listing(self):
48 collection, api_client = self.mock_api_for_manifest(
49 ['. {} 0:3:one.txt 3:4:two.txt'.format(self.random_blocks(5, 2)),
50 './dir {} 1:5:sub.txt'.format(self.random_blocks(8))])
51 self.assertEqual(0, self.run_ls([collection['uuid']], api_client))
53 self.newline_join(['./one.txt', './two.txt', './dir/sub.txt']),
54 self.stdout.getvalue())
55 self.assertEqual('', self.stderr.getvalue())
57 def test_size_listing(self):
58 collection, api_client = self.mock_api_for_manifest(
59 ['. {} 0:0:0.txt 0:1000:1.txt 1000:2000:2.txt'.format(
60 self.random_blocks(3000))])
61 self.assertEqual(0, self.run_ls(['-s', collection['uuid']], api_client))
62 self.stdout.seek(0, 0)
63 for expected in range(3):
64 actual_size, actual_name = self.stdout.readline().split()
65 # But she seems much bigger to me...
66 self.assertEqual(str(expected), actual_size)
67 self.assertEqual('./{}.txt'.format(expected), actual_name)
68 self.assertEqual('', self.stdout.read(-1))
69 self.assertEqual('', self.stderr.getvalue())
71 def test_nonnormalized_manifest(self):
72 collection, api_client = self.mock_api_for_manifest(
73 ['. {} 0:1010:non.txt'.format(self.random_blocks(1010)),
74 '. {} 0:2020:non.txt'.format(self.random_blocks(2020))])
75 self.assertEqual(0, self.run_ls(['-s', collection['uuid']], api_client))
76 self.stdout.seek(0, 0)
77 self.assertEqual(['3', './non.txt'], self.stdout.readline().split())
78 self.assertEqual('', self.stdout.read(-1))
79 self.assertEqual('', self.stderr.getvalue())
81 def test_locator_failure(self):
82 api_client = mock.MagicMock(name='mock_api_client')
83 error_mock = mock.MagicMock()
84 logger = mock.MagicMock()
85 logger.error = error_mock
86 api_client.collections().get().execute.side_effect = (
87 arv_error.NotFoundError)
88 self.assertNotEqual(0, self.run_ls([self.FAKE_UUID], api_client, logger))
89 self.assertEqual(1, error_mock.call_count)
91 def test_version_argument(self):
92 if sys.version_info >= (3, 0):
94 warnings.simplefilter("ignore")
95 with redirected_streams(stdout=StringIO, stderr=StringIO) as (out, err):
96 with self.assertRaises(SystemExit):
97 self.run_ls(['--version'], None)
98 self.assertVersionOutput(out, err)