2960: Merge branch 'main' into 2960-keepstore-streaming
[arvados.git] / sdk / cwl / tests / test_util.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 from builtins import bytes
6
7 import unittest
8 import mock
9 import datetime
10 import httplib2
11
12 from arvados_cwl.util import *
13 from arvados.errors import ApiError
14 from arvados_cwl.util import common_prefix
15
16 class MockDateTime(datetime.datetime):
17     @classmethod
18     def utcnow(cls):
19         return datetime.datetime(2018, 1, 1, 0, 0, 0, 0)
20
21 datetime.datetime = MockDateTime
22
23 class TestUtil(unittest.TestCase):
24     def test_get_intermediate_collection_info(self):
25         name = "one"
26         current_container = {"uuid": "zzzzz-8i9sb-zzzzzzzzzzzzzzz"}
27         intermediate_output_ttl = 120
28
29         info = get_intermediate_collection_info(name, current_container, intermediate_output_ttl)
30
31         self.assertEqual(info["name"], "Intermediate collection for step one")
32         self.assertEqual(info["trash_at"], datetime.datetime(2018, 1, 1, 0, 2, 0, 0))
33         self.assertEqual(info["properties"], {"type" : "intermediate", "container_uuid" : "zzzzz-8i9sb-zzzzzzzzzzzzzzz"})
34
35     def test_get_current_container_success(self):
36         api = mock.MagicMock()
37         api.containers().current().execute.return_value = {"uuid" : "zzzzz-8i9sb-zzzzzzzzzzzzzzz"}
38
39         current_container = get_current_container(api)
40
41         self.assertEqual(current_container, {"uuid" : "zzzzz-8i9sb-zzzzzzzzzzzzzzz"})
42
43     def test_get_current_container_error(self):
44         api = mock.MagicMock()
45         api.containers().current().execute.side_effect = ApiError(httplib2.Response({"status": 300}), bytes(b""))
46         logger = mock.MagicMock()
47
48         with self.assertRaises(ApiError):
49             get_current_container(api, num_retries=0, logger=logger)
50
51     def test_get_current_container_404_error(self):
52         api = mock.MagicMock()
53         api.containers().current().execute.side_effect = ApiError(httplib2.Response({"status": 404}), bytes(b""))
54         logger = mock.MagicMock()
55
56         current_container = get_current_container(api, num_retries=0, logger=logger)
57         self.assertEqual(current_container, None)
58
59     def test_common_prefix(self):
60         self.assertEqual(common_prefix("file:///foo/bar", ["file:///foo/bar/baz"]), "file:///foo/")
61         self.assertEqual(common_prefix("file:///foo", ["file:///foo", "file:///foo/bar", "file:///foo/bar/"]), "file:///")
62         self.assertEqual(common_prefix("file:///foo/", ["file:///foo/", "file:///foo/bar", "file:///foo/bar/"]), "file:///foo/")
63         self.assertEqual(common_prefix("file:///foo/bar", ["file:///foo/bar", "file:///foo/baz", "file:///foo/quux/q2"]), "file:///foo/")
64         self.assertEqual(common_prefix("file:///foo/bar/", ["file:///foo/bar/", "file:///foo/baz", "file:///foo/quux/q2"]), "file:///foo/")
65         self.assertEqual(common_prefix("file:///foo/bar/splat", ["file:///foo/bar/splat", "file:///foo/baz", "file:///foo/quux/q2"]), "file:///foo/")
66         self.assertEqual(common_prefix("file:///foo/bar/splat", ["file:///foo/bar/splat", "file:///nope", "file:///foo/quux/q2"]), "file:///")
67         self.assertEqual(common_prefix("file:///blub/foo", ["file:///blub/foo", "file:///blub/foo/bar", "file:///blub/foo/bar/"]), "file:///blub/")
68
69         # sanity check, the subsequent code strips off the prefix so
70         # just confirm the logic doesn't have a fencepost error
71         prefix = "file:///"
72         self.assertEqual("file:///foo/bar"[len(prefix):], "foo/bar")