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