-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremote_runner_test.py
More file actions
232 lines (185 loc) · 6.39 KB
/
remote_runner_test.py
File metadata and controls
232 lines (185 loc) · 6.39 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""Tests for keras_remote.runner.remote_runner — GCS helpers and execution."""
import os
import pathlib
import shutil
import sys
import tempfile
import zipfile
from unittest import mock
from unittest.mock import MagicMock
import cloudpickle
from absl.testing import absltest
from keras_remote.runner.remote_runner import (
_download_from_gcs,
_upload_to_gcs,
main,
run_gcs_mode,
)
def _make_temp_path(test_case):
"""Create a temp directory that is cleaned up after the test."""
td = tempfile.TemporaryDirectory()
test_case.addCleanup(td.cleanup)
return pathlib.Path(td.name)
class TestDownloadFromGcs(absltest.TestCase):
def test_parses_gcs_path(self):
mock_client = MagicMock()
mock_bucket = MagicMock()
mock_blob = MagicMock()
mock_client.bucket.return_value = mock_bucket
mock_bucket.blob.return_value = mock_blob
_download_from_gcs(
mock_client, "gs://my-bucket/path/to/file.pkl", "/tmp/local.pkl"
)
mock_client.bucket.assert_called_once_with("my-bucket")
mock_bucket.blob.assert_called_once_with("path/to/file.pkl")
mock_blob.download_to_filename.assert_called_once_with("/tmp/local.pkl")
def test_handles_nested_path(self):
mock_client = MagicMock()
mock_bucket = MagicMock()
mock_blob = MagicMock()
mock_client.bucket.return_value = mock_bucket
mock_bucket.blob.return_value = mock_blob
_download_from_gcs(
mock_client,
"gs://bucket/a/b/c/deep/file.zip",
"/tmp/out.zip",
)
mock_client.bucket.assert_called_once_with("bucket")
mock_bucket.blob.assert_called_once_with("a/b/c/deep/file.zip")
class TestUploadToGcs(absltest.TestCase):
def test_parses_gcs_path(self):
mock_client = MagicMock()
mock_bucket = MagicMock()
mock_blob = MagicMock()
mock_client.bucket.return_value = mock_bucket
mock_bucket.blob.return_value = mock_blob
_upload_to_gcs(
mock_client, "/tmp/result.pkl", "gs://my-bucket/results/result.pkl"
)
mock_client.bucket.assert_called_once_with("my-bucket")
mock_bucket.blob.assert_called_once_with("results/result.pkl")
mock_blob.upload_from_filename.assert_called_once_with("/tmp/result.pkl")
class TestRunGcsMode(absltest.TestCase):
def setUp(self):
super().setUp()
# Prevent run_gcs_mode's sys.path.insert from leaking across tests.
original_path = sys.path[:]
self.addCleanup(setattr, sys, "path", original_path)
def _setup_gcs_test(self, tmp_path, func, args=(), env_vars=None):
"""Set up common GCS test fixtures: context zip, payload pkl, patches."""
if env_vars is None:
env_vars = {}
src_dir = tmp_path / "src"
src_dir.mkdir()
context_zip = src_dir / "context.zip"
with zipfile.ZipFile(context_zip, "w") as zf:
zf.writestr("dummy.py", "x = 1")
payload_pkl = src_dir / "payload.pkl"
with open(payload_pkl, "wb") as f:
cloudpickle.dump(
{
"func": func,
"args": args,
"kwargs": {},
"env_vars": env_vars,
},
f,
)
mock_client = MagicMock()
def fake_download(client, gcs_path, local_path):
if "context.zip" in gcs_path:
shutil.copy(str(context_zip), local_path)
elif "payload.pkl" in gcs_path:
shutil.copy(str(payload_pkl), local_path)
return mock_client, fake_download
def _run_gcs_mode(self, func, args=(), env_vars=None):
"""Set up fixtures, run run_gcs_mode(), return (exit_code, result_payload)."""
tmp_path = _make_temp_path(self)
mock_client, fake_download = self._setup_gcs_test(
tmp_path, func, args=args, env_vars=env_vars
)
with (
mock.patch(
"sys.argv",
[
"remote_runner.py",
"gs://bucket/context.zip",
"gs://bucket/payload.pkl",
"gs://bucket/result.pkl",
],
),
mock.patch(
"keras_remote.runner.remote_runner._download_from_gcs",
side_effect=fake_download,
),
mock.patch(
"keras_remote.runner.remote_runner._upload_to_gcs",
) as mock_upload,
mock.patch(
"keras_remote.runner.remote_runner.storage.Client",
return_value=mock_client,
),
):
with self.assertRaises(SystemExit) as cm:
run_gcs_mode()
result_path = mock_upload.call_args[0][1]
with open(result_path, "rb") as f:
result_payload = cloudpickle.load(f)
return cm.exception.code, result_payload
def test_success_flow(self):
"""Verify successful function execution: download, execute, upload result."""
def add(a, b):
return a + b
exit_code, result = self._run_gcs_mode(add, args=(2, 3))
self.assertEqual(exit_code, 0)
self.assertTrue(result["success"])
self.assertEqual(result["result"], 5)
def test_function_exception(self):
"""When the user function raises, result has success=False."""
def bad_func():
raise ValueError("test error")
exit_code, result = self._run_gcs_mode(bad_func)
self.assertEqual(exit_code, 1)
self.assertFalse(result["success"])
self.assertIsInstance(result["exception"], ValueError)
self.assertIn("test error", str(result["exception"]))
def test_env_vars_applied(self):
"""Verify env_vars from payload are set in os.environ."""
def read_env():
return os.environ.get("TEST_REMOTE_VAR")
exit_code, result = self._run_gcs_mode(
read_env, env_vars={"TEST_REMOTE_VAR": "hello"}
)
self.assertEqual(exit_code, 0)
self.assertTrue(result["success"])
self.assertEqual(result["result"], "hello")
class TestMain(absltest.TestCase):
def test_too_few_args(self):
with mock.patch("sys.argv", ["remote_runner.py"]):
with self.assertRaises(SystemExit) as cm:
main()
self.assertEqual(cm.exception.code, 1)
def test_too_few_args_two(self):
with mock.patch(
"sys.argv", ["remote_runner.py", "gs://bucket/context.zip"]
):
with self.assertRaises(SystemExit) as cm:
main()
self.assertEqual(cm.exception.code, 1)
def test_correct_args_calls_run_gcs_mode(self):
with (
mock.patch(
"sys.argv",
[
"remote_runner.py",
"gs://bucket/context.zip",
"gs://bucket/payload.pkl",
"gs://bucket/result.pkl",
],
),
mock.patch("keras_remote.runner.remote_runner.run_gcs_mode") as mock_run,
):
main()
mock_run.assert_called_once()
if __name__ == "__main__":
absltest.main()