This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotecache_test.py
156 lines (107 loc) · 4.8 KB
/
remotecache_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import unittest
from unittest import mock
from uuid import uuid4
# noinspection PyProtectedMember
from remotecache import RemoteCacheFlagsResolver, remote_cache_flags_for, remote_cache_bucket_name, _macos_fingerprint
_RANDOM_BUCKET_NAME = str(uuid4())
def fixed_env_fingerprint():
return "fingerprint"
def random_mac_ver():
return str(uuid4()), None, None
def fixed_mac_ver():
return "1.1.1", None, None
# noinspection PyUnusedLocal
def fixed_clang_fingerprint(cmd, stdout, stderr, encoding):
assert cmd == ["clang", "--version"]
d = Dummy()
d.stdout = "clang_is_clang_is_clang"
return d
# noinspection PyUnusedLocal
def random_clang_fingerprint(cmd, stdout, stderr, encoding):
assert cmd == ["clang", "--version"]
d = Dummy()
d.stdout = str(uuid4())
return d
class RemoteCacheTest(unittest.TestCase):
# noinspection PyUnusedLocal
@mock.patch("platform.mac_ver", side_effect=fixed_mac_ver)
@mock.patch("subprocess.run", side_effect=fixed_clang_fingerprint)
def test_macos_fingerprint_reproducibility(self, m1, m2):
self.assertEqual(_macos_fingerprint(), _macos_fingerprint())
# noinspection PyUnusedLocal
@mock.patch("platform.mac_ver", side_effect=random_mac_ver)
@mock.patch("subprocess.run", side_effect=fixed_clang_fingerprint)
def test_macos_version_fingerprint(self, m1, m2):
os1_fingerprint = _macos_fingerprint()
os2_fingerprint = _macos_fingerprint()
self.assertNotEqual(os1_fingerprint, os2_fingerprint)
# noinspection PyUnusedLocal
@mock.patch("platform.mac_ver", side_effect=fixed_mac_ver)
@mock.patch("subprocess.run", side_effect=random_clang_fingerprint)
def test_macos_version_fingerprint(self, m1, m2):
clang1_fingerprint = _macos_fingerprint()
clang2_fingerprint = _macos_fingerprint()
self.assertNotEqual(clang1_fingerprint, clang2_fingerprint)
@mock.patch.dict(os.environ, {'BAZEL_DISABLE_REMOTE_CACHE': '1'})
def test_enable_remote_cache_env_var_disable(self):
self.assertEqual([], remote_cache_flags_for("Darwin"))
@mock.patch.dict(os.environ, {'BAZEL_DISABLE_REMOTE_CACHE': '0'})
def test_enable_remote_cache_os_disable(self):
self.assertFalse([], remote_cache_flags_for(str(uuid4())))
@mock.patch.dict(os.environ, {'BAZEL_REMOTE_CACHE_BUCKET_NAME': _RANDOM_BUCKET_NAME})
def test_remote_cache_bucket_name_env_var(self):
self.assertEqual(_RANDOM_BUCKET_NAME, remote_cache_bucket_name())
def test_remote_cache_bucket_name_default(self):
self.assertEqual("bazel-dev-remote-cache", remote_cache_bucket_name())
class RemoteCacheFlagsResolverTest(unittest.TestCase):
@staticmethod
def new_remote_cache_flags_resolver(os_name="tests", env_fingerprint=fixed_env_fingerprint):
return RemoteCacheFlagsResolver(
os_name=os_name,
env_fingerprint=env_fingerprint
)
def test_equal_envs_write_to_the_same_location(self):
argmap1 = resolved_args_map_for(
self.new_remote_cache_flags_resolver(os_name="MyOS", env_fingerprint=fixed_env_fingerprint)
)
argmap2 = resolved_args_map_for(
self.new_remote_cache_flags_resolver(os_name="MyOS", env_fingerprint=fixed_env_fingerprint)
)
self.assertEqual(remote_cache_url_for(argmap1), remote_cache_url_for(argmap2))
def test_os_name_variant(self):
argmap1 = resolved_args_map_for(
self.new_remote_cache_flags_resolver(os_name="OS1", env_fingerprint=fixed_env_fingerprint)
)
argmap2 = resolved_args_map_for(
self.new_remote_cache_flags_resolver(os_name="OS2", env_fingerprint=fixed_env_fingerprint)
)
self.assertNotEqual(remote_cache_url_for(argmap1), remote_cache_url_for(argmap2))
def test_env_fingerprint_variant(self):
argmap1 = resolved_args_map_for(
self.new_remote_cache_flags_resolver(os_name="MyOS", env_fingerprint=fixed_env_fingerprint)
)
argmap2 = resolved_args_map_for(
self.new_remote_cache_flags_resolver(os_name="MyOS", env_fingerprint=random_env_fingerprint)
)
self.assertNotEqual(remote_cache_url_for(argmap1), remote_cache_url_for(argmap2))
def parse_bazel_args(args):
argmap = {}
for arg in args:
segments = arg.split("=")
if len(segments) == 1:
argmap[segments[0]] = True
else:
assert len(segments) == 2
argmap[segments[0]] = segments[1]
return argmap
def remote_cache_url_for(argmap):
return argmap["--remote_http_cache"]
def resolved_args_map_for(resolver):
return parse_bazel_args(resolver.resolve_remote_cache_flags())
def random_env_fingerprint():
return str(uuid4())
class Dummy:
pass
if __name__ == '__main__':
unittest.main()