21126: Merge branch 'main' into 21126-trash-when-ro
[arvados.git] / sdk / python / tests / test_safeapi.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import os
6 import unittest
7
8 import googleapiclient
9
10 from arvados import safeapi
11
12 from . import run_test_server
13
14 class SafeApiTest(run_test_server.TestCaseWithServers):
15     MAIN_SERVER = {}
16
17     def test_constructor(self):
18         env_mapping = {
19             key: value
20             for key, value in os.environ.items()
21             if key.startswith('ARVADOS_API_')
22         }
23         extra_params = {
24             'timeout': 299,
25         }
26         base_params = {
27             key[12:].lower(): value
28             for key, value in env_mapping.items()
29         }
30         try:
31             base_params['insecure'] = base_params.pop('host_insecure')
32         except KeyError:
33             pass
34         expected_keep_params = {}
35         for config, params, subtest in [
36                 (None, {}, "default arguments"),
37                 (None, extra_params, "extra params"),
38                 (env_mapping, {}, "explicit config"),
39                 (env_mapping, extra_params, "explicit config and params"),
40                 ({}, base_params, "params only"),
41         ]:
42             with self.subTest(f"test constructor with {subtest}"):
43                 expected_timeout = params.get('timeout', 300)
44                 expected_params = dict(params)
45                 keep_params = dict(expected_keep_params)
46                 client = safeapi.ThreadSafeApiCache(config, keep_params, params, 'v1')
47                 self.assertTrue(hasattr(client, 'localapi'), "client missing localapi method")
48                 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'])
49                 self.assertEqual(client._http.timeout, expected_timeout)
50                 self.assertEqual(params, expected_params,
51                                  "api_params was modified in-place")
52                 self.assertEqual(keep_params, expected_keep_params,
53                                  "keep_params was modified in-place")
54
55     def test_constructor_no_args(self):
56         client = safeapi.ThreadSafeApiCache()
57         self.assertTrue(hasattr(client, 'localapi'), "client missing localapi method")
58         self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'])
59         self.assertTrue(client.insecure)
60
61     def test_constructor_bad_version(self):
62         with self.assertRaises(googleapiclient.errors.UnknownApiNameOrVersion):
63             safeapi.ThreadSafeApiCache(version='BadTestVersion')