18078: Fix tests
[arvados.git] / services / fuse / tests / test_token_expiry.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 from builtins import range
6 import apiclient
7 import arvados
8 import arvados_fuse
9 import logging
10 import mock
11 import multiprocessing
12 import os
13 import re
14 import sys
15 import time
16 import unittest
17
18 from .integration_test import IntegrationTest
19
20 logger = logging.getLogger('arvados.arv-mount')
21
22 class TokenExpiryTest(IntegrationTest):
23     def setUp(self):
24         super(TokenExpiryTest, self).setUp()
25         self.test_start_time = time.time()
26         self.time_now = int(time.time())+1
27
28     def fake_time(self):
29         self.time_now += 1
30         return self.time_now
31
32     orig_open = arvados_fuse.Operations.open
33     def fake_open(self, operations, *args, **kwargs):
34         self.time_now += 86400*13
35         logger.debug('opening file at time=%f', self.time_now)
36         return TokenExpiryTest.orig_open(operations, *args, **kwargs)
37
38     @mock.patch.object(arvados_fuse.Operations, 'open', autospec=True)
39     @mock.patch.object(time, 'time', return_value=0)
40     @mock.patch('arvados.keep.KeepClient.get')
41     @IntegrationTest.mount(argv=['--mount-by-id', 'zzz'])
42     def test_refresh_old_manifest(self, mocked_get, mocked_time, mocked_open):
43         # This test (and associated behavior) is still not strong
44         # enough. We should ensure old tokens are never used even if
45         # blobSignatureTtl seconds elapse between open() and
46         # read(). See https://dev.arvados.org/issues/10008
47
48         mocked_get.return_value = b'fake data'
49         mocked_time.side_effect = self.fake_time
50         mocked_open.side_effect = self.fake_open
51
52         with mock.patch.object(self.mount.api, 'collections', wraps=self.mount.api.collections) as mocked_collections:
53             mocked_collections.return_value = mocked_collections()
54             with mock.patch.object(self.mount.api.collections(), 'get', wraps=self.mount.api.collections().get) as mocked_get:
55                 self.pool_test(os.path.join(self.mnt, 'zzz'))
56
57         # open() several times here to make sure we don't reach our
58         # quota of mocked_get.call_count dishonestly (e.g., the first
59         # open causes 5 mocked_get, and the rest cause none).
60         self.assertEqual(8, mocked_open.call_count)
61         self.assertGreaterEqual(
62             mocked_get.call_count, 8,
63             'Not enough calls to collections().get(): expected 8, got {!r}'.format(
64                 mocked_get.mock_calls))
65
66     @staticmethod
67     def _test_refresh_old_manifest(self, zzz):
68         uuid = 'zzzzz-4zz18-op4e2lbej01tcvu'
69         fnm = 'zzzzz-8i9sb-0vsrcqi7whchuil.log.txt'
70         os.listdir(os.path.join(zzz, uuid))
71         for _ in range(8):
72             with open(os.path.join(zzz, uuid, fnm)) as f:
73                 f.read()