Merge branch 'master' into 3141-keep-logging-improvements
[arvados.git] / sdk / python / tests / test_keep_client.py
1 # usage example:
2 #
3 # ARVADOS_API_TOKEN=abc ARVADOS_API_HOST=arvados.local python -m unittest discover
4
5 import os
6 import unittest
7
8 import arvados
9 import run_test_server
10
11 class KeepTestCase(unittest.TestCase):
12     @classmethod
13     def setUpClass(cls):
14         super(KeepTestCase, cls).setUpClass()
15         try:
16             del os.environ['KEEP_LOCAL_STORE']
17         except KeyError:
18             pass
19
20         # Make sure these are clear, we want to talk to the Keep servers
21         # directly.
22         os.environ["ARVADOS_KEEP_PROXY"] = ""
23         os.environ["ARVADOS_EXTERNAL_CLIENT"] = ""
24
25         run_test_server.run()
26         run_test_server.run_keep()
27         arvados.keep.global_client_object = None
28         arvados.config._settings = None
29         run_test_server.authorize_with("admin")
30
31     @classmethod
32     def tearDownClass(cls):
33         super(KeepTestCase, cls).tearDownClass()
34         run_test_server.stop()
35         run_test_server.stop_keep()
36
37     def test_KeepBasicRWTest(self):
38         foo_locator = arvados.Keep.put('foo')
39         self.assertEqual(foo_locator,
40                          'acbd18db4cc2f85cedef654fccc4a4d8+3',
41                          'wrong md5 hash from Keep.put("foo"): ' + foo_locator)
42         self.assertEqual(arvados.Keep.get(foo_locator),
43                          'foo',
44                          'wrong content from Keep.get(md5("foo"))')
45
46     def test_KeepBinaryRWTest(self):
47         blob_str = '\xff\xfe\xf7\x00\x01\x02'
48         blob_locator = arvados.Keep.put(blob_str)
49         self.assertEqual(blob_locator,
50                          '7fc7c53b45e53926ba52821140fef396+6',
51                          ('wrong locator from Keep.put(<binarydata>):' +
52                           blob_locator))
53         self.assertEqual(arvados.Keep.get(blob_locator),
54                          blob_str,
55                          'wrong content from Keep.get(md5(<binarydata>))')
56
57     def test_KeepLongBinaryRWTest(self):
58         blob_str = '\xff\xfe\xfd\xfc\x00\x01\x02\x03'
59         for i in range(0,23):
60             blob_str = blob_str + blob_str
61         blob_locator = arvados.Keep.put(blob_str)
62         self.assertEqual(blob_locator,
63                          '84d90fc0d8175dd5dcfab04b999bc956+67108864',
64                          ('wrong locator from Keep.put(<binarydata>): ' +
65                           blob_locator))
66         self.assertEqual(arvados.Keep.get(blob_locator),
67                          blob_str,
68                          'wrong content from Keep.get(md5(<binarydata>))')
69
70     def test_KeepSingleCopyRWTest(self):
71         blob_str = '\xff\xfe\xfd\xfc\x00\x01\x02\x03'
72         blob_locator = arvados.Keep.put(blob_str, copies=1)
73         self.assertEqual(blob_locator,
74                          'c902006bc98a3eb4a3663b65ab4a6fab+8',
75                          ('wrong locator from Keep.put(<binarydata>): ' +
76                           blob_locator))
77         self.assertEqual(arvados.Keep.get(blob_locator),
78                          blob_str,
79                          'wrong content from Keep.get(md5(<binarydata>))')
80
81 class KeepPermissionTestCase(unittest.TestCase):
82     @classmethod
83     def setUpClass(cls):
84         try:
85             del os.environ['KEEP_LOCAL_STORE']
86         except KeyError:
87             pass
88
89         run_test_server.run()
90         run_test_server.run_keep(blob_signing_key='abcdefghijk0123456789',
91                                  enforce_permissions=True)
92
93     @classmethod
94     def tearDownClass(cls):
95         run_test_server.stop()
96         run_test_server.stop_keep()
97
98     def test_KeepBasicRWTest(self):
99         run_test_server.authorize_with('active')
100         foo_locator = arvados.Keep.put('foo')
101         self.assertRegexpMatches(
102             foo_locator,
103             r'^acbd18db4cc2f85cedef654fccc4a4d8\+3\+A[a-f0-9]+@[a-f0-9]+$',
104             'invalid locator from Keep.put("foo"): ' + foo_locator)
105         self.assertEqual(arvados.Keep.get(foo_locator),
106                          'foo',
107                          'wrong content from Keep.get(md5("foo"))')
108
109         # GET with an unsigned locator => NotFound
110         bar_locator = arvados.Keep.put('bar')
111         unsigned_bar_locator = "37b51d194a7513e45b56f6524f2d51f2+3"
112         self.assertRegexpMatches(
113             bar_locator,
114             r'^37b51d194a7513e45b56f6524f2d51f2\+3\+A[a-f0-9]+@[a-f0-9]+$',
115             'invalid locator from Keep.put("bar"): ' + bar_locator)
116         self.assertRaises(arvados.errors.NotFoundError,
117                           arvados.Keep.get,
118                           unsigned_bar_locator)
119
120         # GET from a different user => NotFound
121         run_test_server.authorize_with('spectator')
122         self.assertRaises(arvados.errors.NotFoundError,
123                           arvados.Keep.get,
124                           bar_locator)
125
126         # Unauthenticated GET for a signed locator => NotFound
127         # Unauthenticated GET for an unsigned locator => NotFound
128         del arvados.config.settings()["ARVADOS_API_TOKEN"]
129         self.assertRaises(arvados.errors.NotFoundError,
130                           arvados.Keep.get,
131                           bar_locator)
132         self.assertRaises(arvados.errors.NotFoundError,
133                           arvados.Keep.get,
134                           unsigned_bar_locator)
135
136 # KeepOptionalPermission: starts Keep with --permission-key-file
137 # but not --enforce-permissions (i.e. generate signatures on PUT
138 # requests, but do not require them for GET requests)
139 #
140 # All of these requests should succeed when permissions are optional:
141 # * authenticated request, signed locator
142 # * authenticated request, unsigned locator
143 # * unauthenticated request, signed locator
144 # * unauthenticated request, unsigned locator
145
146 class KeepOptionalPermission(unittest.TestCase):
147     @classmethod
148     def setUpClass(cls):
149         try:
150             del os.environ['KEEP_LOCAL_STORE']
151         except KeyError:
152             pass
153         run_test_server.run()
154         run_test_server.run_keep(blob_signing_key='abcdefghijk0123456789',
155                                  enforce_permissions=False)
156
157     @classmethod
158     def tearDownClass(cls):
159         run_test_server.stop()
160         run_test_server.stop_keep()
161
162     def test_KeepAuthenticatedSignedTest(self):
163         run_test_server.authorize_with('active')
164         signed_locator = arvados.Keep.put('foo')
165         self.assertRegexpMatches(
166             signed_locator,
167             r'^acbd18db4cc2f85cedef654fccc4a4d8\+3\+A[a-f0-9]+@[a-f0-9]+$',
168             'invalid locator from Keep.put("foo"): ' + signed_locator)
169         self.assertEqual(arvados.Keep.get(signed_locator),
170                          'foo',
171                          'wrong content from Keep.get(md5("foo"))')
172
173     def test_KeepAuthenticatedUnsignedTest(self):
174         run_test_server.authorize_with('active')
175         signed_locator = arvados.Keep.put('foo')
176         self.assertRegexpMatches(
177             signed_locator,
178             r'^acbd18db4cc2f85cedef654fccc4a4d8\+3\+A[a-f0-9]+@[a-f0-9]+$',
179             'invalid locator from Keep.put("foo"): ' + signed_locator)
180         self.assertEqual(arvados.Keep.get("acbd18db4cc2f85cedef654fccc4a4d8"),
181                          'foo',
182                          'wrong content from Keep.get(md5("foo"))')
183
184     def test_KeepUnauthenticatedSignedTest(self):
185         # Since --enforce-permissions is not in effect, GET requests
186         # need not be authenticated.
187         run_test_server.authorize_with('active')
188         signed_locator = arvados.Keep.put('foo')
189         self.assertRegexpMatches(
190             signed_locator,
191             r'^acbd18db4cc2f85cedef654fccc4a4d8\+3\+A[a-f0-9]+@[a-f0-9]+$',
192             'invalid locator from Keep.put("foo"): ' + signed_locator)
193
194         del arvados.config.settings()["ARVADOS_API_TOKEN"]
195         self.assertEqual(arvados.Keep.get(signed_locator),
196                          'foo',
197                          'wrong content from Keep.get(md5("foo"))')
198
199     def test_KeepUnauthenticatedUnsignedTest(self):
200         # Since --enforce-permissions is not in effect, GET requests
201         # need not be authenticated.
202         run_test_server.authorize_with('active')
203         signed_locator = arvados.Keep.put('foo')
204         self.assertRegexpMatches(
205             signed_locator,
206             r'^acbd18db4cc2f85cedef654fccc4a4d8\+3\+A[a-f0-9]+@[a-f0-9]+$',
207             'invalid locator from Keep.put("foo"): ' + signed_locator)
208
209         del arvados.config.settings()["ARVADOS_API_TOKEN"]
210         self.assertEqual(arvados.Keep.get("acbd18db4cc2f85cedef654fccc4a4d8"),
211                          'foo',
212                          'wrong content from Keep.get(md5("foo"))')
213
214
215 class KeepProxyTestCase(unittest.TestCase):
216     @classmethod
217     def setUpClass(cls):
218         super(KeepProxyTestCase, cls).setUpClass()
219
220         try:
221             del os.environ['KEEP_LOCAL_STORE']
222         except KeyError:
223             pass
224
225         os.environ["ARVADOS_KEEP_PROXY"] = ""
226         os.environ["ARVADOS_EXTERNAL_CLIENT"] = ""
227
228         run_test_server.run()
229         run_test_server.run_keep()
230         arvados.keep.global_client_object = None
231         arvados.config._settings = None
232         run_test_server.run_keep_proxy("admin")
233         KeepProxyTestCase.arvados_keep_proxy = arvados.config.get("ARVADOS_KEEP_PROXY")
234
235     @classmethod
236     def tearDownClass(cls):
237         super(KeepProxyTestCase, cls).tearDownClass()
238         run_test_server.stop()
239         run_test_server.stop_keep()
240         run_test_server.stop_keep_proxy()
241
242     def test_KeepProxyTest1(self):
243         # Will use ARVADOS_KEEP_PROXY environment variable that is set by
244         # run_keep_proxy() in setUpClass()
245
246         os.environ["ARVADOS_KEEP_PROXY"] = KeepProxyTestCase.arvados_keep_proxy
247         os.environ["ARVADOS_EXTERNAL_CLIENT"] = ""
248         arvados.keep.global_client_object = None
249         arvados.config._settings = None
250
251         baz_locator = arvados.Keep.put('baz')
252         self.assertEqual(baz_locator,
253                          '73feffa4b7f6bb68e44cf984c85f6e88+3',
254                          'wrong md5 hash from Keep.put("baz"): ' + baz_locator)
255         self.assertEqual(arvados.Keep.get(baz_locator),
256                          'baz',
257                          'wrong content from Keep.get(md5("baz"))')
258
259         self.assertEqual(True, arvados.Keep.global_client_object().using_proxy)
260
261     def test_KeepProxyTest2(self):
262         # We don't want to use ARVADOS_KEEP_PROXY from run_keep_proxy() in
263         # setUpClass(), so clear it and set ARVADOS_EXTERNAL_CLIENT which will
264         # contact the API server.
265         os.environ["ARVADOS_KEEP_PROXY"] = ""
266         os.environ["ARVADOS_EXTERNAL_CLIENT"] = "true"
267         arvados.keep.global_client_object = None
268         arvados.config._settings = None
269
270         # Will send X-External-Client to server and get back the proxy from
271         # keep_services/accessible
272
273         baz_locator = arvados.Keep.put('baz2')
274         self.assertEqual(baz_locator,
275                          '91f372a266fe2bf2823cb8ec7fda31ce+4',
276                          'wrong md5 hash from Keep.put("baz2"): ' + baz_locator)
277         self.assertEqual(arvados.Keep.get(baz_locator),
278                          'baz2',
279                          'wrong content from Keep.get(md5("baz2"))')
280
281         self.assertEqual(True, arvados.Keep.global_client_object().using_proxy)