11308: Merge branch 'master' into 11308-python3
[arvados.git] / sdk / python / tests / test_arv_ls.py
1 from __future__ import absolute_import
2 from builtins import str
3 from builtins import range
4 import os
5 import random
6 import sys
7 import mock
8 import tempfile
9
10 import arvados.errors as arv_error
11 import arvados.commands.ls as arv_ls
12 from . import run_test_server
13
14 from . import arvados_testutil as tutil
15 from .arvados_testutil import str_keep_locator, redirected_streams, StringIO
16
17 class ArvLsTestCase(run_test_server.TestCaseWithServers, tutil.VersionChecker):
18     FAKE_UUID = 'zzzzz-4zz18-12345abcde12345'
19
20     def newline_join(self, seq):
21         return '\n'.join(seq) + '\n'
22
23     def random_blocks(self, *sizes):
24         return ' '.join('{:032x}+{:d}'.format(
25                   random.randint(0, (16 ** 32) - 1), size
26                 ) for size in sizes)
27
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
37
38     def run_ls(self, args, api_client, logger=None):
39         self.stdout = StringIO()
40         self.stderr = StringIO()
41         return arv_ls.main(args, self.stdout, self.stderr, api_client, logger)
42
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))
48         self.assertEqual(
49             self.newline_join(['./one.txt', './two.txt', './dir/sub.txt']),
50             self.stdout.getvalue())
51         self.assertEqual('', self.stderr.getvalue())
52
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())
66
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())
76
77     def test_locator_failure(self):
78         api_client = mock.MagicMock(name='mock_api_client')
79         error_mock = mock.MagicMock()
80         logger = mock.MagicMock()
81         logger.error = error_mock
82         api_client.collections().get().execute.side_effect = (
83             arv_error.NotFoundError)
84         self.assertNotEqual(0, self.run_ls([self.FAKE_UUID], api_client, logger))
85         self.assertEqual(1, error_mock.call_count)
86
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)