Merge branch '3198-writable-fuse' closes #3198
[arvados.git] / sdk / python / tests / test_arv_ls.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import hashlib
5 import io
6 import random
7
8 import mock
9
10 import arvados.errors as arv_error
11 import arvados.commands.ls as arv_ls
12 import run_test_server
13
14 class ArvLsTestCase(run_test_server.TestCaseWithServers):
15     FAKE_UUID = 'zzzzz-4zz18-12345abcde12345'
16
17     def newline_join(self, seq):
18         return '\n'.join(seq) + '\n'
19
20     def random_blocks(self, *sizes):
21         return ' '.join('{:032x}+{:d}'.format(
22                   random.randint(0, (16 ** 32) - 1), size
23                 ) for size in sizes)
24
25     def mock_api_for_manifest(self, manifest_lines, uuid=FAKE_UUID):
26         manifest_text = self.newline_join(manifest_lines)
27         pdh = '{}+{}'.format(hashlib.md5(manifest_text).hexdigest(),
28                              len(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())