Merge branch 'master' into 7454-azure-custom-data
[arvados.git] / sdk / python / tests / test_arv_ls.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import io
5 import random
6
7 import mock
8
9 import arvados.errors as arv_error
10 import arvados.commands.ls as arv_ls
11 import run_test_server
12
13 from arvados_testutil import str_keep_locator
14
15 class ArvLsTestCase(run_test_server.TestCaseWithServers):
16     FAKE_UUID = 'zzzzz-4zz18-12345abcde12345'
17
18     def newline_join(self, seq):
19         return '\n'.join(seq) + '\n'
20
21     def random_blocks(self, *sizes):
22         return ' '.join('{:032x}+{:d}'.format(
23                   random.randint(0, (16 ** 32) - 1), size
24                 ) for size in sizes)
25
26     def mock_api_for_manifest(self, manifest_lines, uuid=FAKE_UUID):
27         manifest_text = self.newline_join(manifest_lines)
28         pdh = str_keep_locator(manifest_text)
29         coll_info = {'uuid': uuid,
30                      'portable_data_hash': pdh,
31                      'manifest_text': manifest_text}
32         api_client = mock.MagicMock(name='mock_api_client')
33         api_client.collections().get().execute.return_value = coll_info
34         return coll_info, api_client
35
36     def run_ls(self, args, api_client):
37         self.stdout = io.BytesIO()
38         self.stderr = io.BytesIO()
39         return arv_ls.main(args, self.stdout, self.stderr, api_client)
40
41     def test_plain_listing(self):
42         collection, api_client = self.mock_api_for_manifest(
43             ['. {} 0:3:one.txt 3:4:two.txt'.format(self.random_blocks(5, 2)),
44              './dir {} 1:5:sub.txt'.format(self.random_blocks(8))])
45         self.assertEqual(0, self.run_ls([collection['uuid']], api_client))
46         self.assertEqual(
47             self.newline_join(['./one.txt', './two.txt', './dir/sub.txt']),
48             self.stdout.getvalue())
49         self.assertEqual('', self.stderr.getvalue())
50
51     def test_size_listing(self):
52         collection, api_client = self.mock_api_for_manifest(
53             ['. {} 0:0:0.txt 0:1000:1.txt 1000:2000:2.txt'.format(
54                     self.random_blocks(3000))])
55         self.assertEqual(0, self.run_ls(['-s', collection['uuid']], api_client))
56         self.stdout.seek(0, 0)
57         for expected in range(3):
58             actual_size, actual_name = self.stdout.readline().split()
59             # But she seems much bigger to me...
60             self.assertEqual(str(expected), actual_size)
61             self.assertEqual('./{}.txt'.format(expected), actual_name)
62         self.assertEqual('', self.stdout.read(-1))
63         self.assertEqual('', self.stderr.getvalue())
64
65     def test_nonnormalized_manifest(self):
66         collection, api_client = self.mock_api_for_manifest(
67             ['. {} 0:1010:non.txt'.format(self.random_blocks(1010)),
68              '. {} 0:2020:non.txt'.format(self.random_blocks(2020))])
69         self.assertEqual(0, self.run_ls(['-s', collection['uuid']], api_client))
70         self.stdout.seek(0, 0)
71         self.assertEqual(['3', './non.txt'], self.stdout.readline().split())
72         self.assertEqual('', self.stdout.read(-1))
73         self.assertEqual('', self.stderr.getvalue())
74
75     def test_locator_failure(self):
76         api_client = mock.MagicMock(name='mock_api_client')
77         api_client.collections().get().execute.side_effect = (
78             arv_error.NotFoundError)
79         self.assertNotEqual(0, self.run_ls([self.FAKE_UUID], api_client))
80         self.assertNotEqual('', self.stderr.getvalue())