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