forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_durable_functions_emulator_container.py
More file actions
436 lines (372 loc) · 18.1 KB
/
test_durable_functions_emulator_container.py
File metadata and controls
436 lines (372 loc) · 18.1 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""
Unit tests for DurableFunctionsEmulatorContainer
"""
import os
from pathlib import Path
from unittest import TestCase
from unittest.mock import Mock, patch, mock_open
from parameterized import parameterized
import docker
from click import ClickException
from samcli.local.docker.durable_functions_emulator_container import DurableFunctionsEmulatorContainer
class TestDurableFunctionsEmulatorContainer(TestCase):
def setUp(self):
"""Set up test fixtures"""
self.mock_docker_client = Mock()
self.mock_container = Mock()
self.mock_docker_client.containers.create.return_value = self.mock_container
self.env_patcher = patch.dict("os.environ", {}, clear=True)
self.env_patcher.start()
def tearDown(self):
"""Clean up after tests"""
self.env_patcher.stop()
def _create_container(self, existing_container=None):
"""Helper to create container with optional existing container"""
return DurableFunctionsEmulatorContainer(
container_client=self.mock_docker_client,
existing_container=existing_container,
)
@parameterized.expand(
[
# (name, env_vars, expected_port, expected_container_name, is_external)
("managed_default", {}, 9014, "sam-durable-execution-emulator", False),
(
"managed_custom_port",
{"DURABLE_EXECUTIONS_EMULATOR_PORT": "9999"},
9999,
"sam-durable-execution-emulator",
False,
),
("managed_custom_name", {"DURABLE_EXECUTIONS_CONTAINER_NAME": "my-emulator"}, 9014, "my-emulator", False),
("external_mode", {"DURABLE_EXECUTIONS_EXTERNAL_EMULATOR_PORT": "8080"}, 8080, None, True),
(
"pin_image_tag",
{"DURABLE_EXECUTIONS_EMULATOR_IMAGE_TAG": "v1.1.1"},
9014,
"sam-durable-execution-emulator",
False,
),
]
)
def test_initialization(self, name, env_vars, expected_port, expected_name, is_external):
"""Test initialization determines mode, port, and container name"""
with patch.dict("os.environ", env_vars, clear=True):
container = self._create_container()
self.assertEqual(container.port, expected_port)
self.assertEqual(container._container_name, expected_name)
self.assertEqual(container._is_external_emulator(), is_external)
def test_initialization_with_invalid_external_port_raises_error(self):
"""Test that invalid external port raises RuntimeError"""
with patch.dict("os.environ", {"DURABLE_EXECUTIONS_EXTERNAL_EMULATOR_PORT": "invalid"}, clear=True):
with self.assertRaises(RuntimeError) as context:
self._create_container()
self.assertIn("Invalid port number", str(context.exception))
def test_initialization_with_existing_container(self):
"""Test that existing container is preserved during initialization"""
mock_existing = Mock()
container = self._create_container(existing_container=mock_existing)
self.assertEqual(container.container, mock_existing)
@patch("samcli.local.docker.durable_functions_emulator_container.get_validated_container_client")
def test_docker_client_lazy_loading(self, mock_get_validated_client):
"""Test that docker client is lazily loaded and cached"""
mock_validated_client = Mock()
mock_get_validated_client.return_value = mock_validated_client
container = DurableFunctionsEmulatorContainer()
mock_get_validated_client.assert_not_called()
client = container._docker_client
mock_get_validated_client.assert_called_once()
self.assertEqual(client, mock_validated_client)
# Subsequent access uses cached client
client2 = container._docker_client
mock_get_validated_client.assert_called_once()
@parameterized.expand(
[
# (name, env_vars, should_create_container, should_start_container)
("managed_mode_creates_container", {}, True, True),
("external_mode_skips_container", {"DURABLE_EXECUTIONS_EXTERNAL_EMULATOR_PORT": "8080"}, False, False),
]
)
@patch("samcli.local.docker.durable_functions_emulator_container.is_image_current")
def test_start_behavior_by_mode(self, name, env_vars, should_create, should_start, mock_is_current):
"""Test that start() behaves correctly for managed vs external mode"""
mock_is_current.return_value = True
with patch.dict("os.environ", env_vars, clear=True):
container = self._create_container()
container._wait_for_ready = Mock()
container.start()
if should_create:
self.mock_docker_client.containers.create.assert_called_once()
self.assertEqual(container.container, self.mock_container)
else:
self.mock_docker_client.containers.create.assert_not_called()
self.assertIsNone(container.container)
if should_start:
self.mock_container.start.assert_called_once()
else:
self.mock_container.start.assert_not_called()
@parameterized.expand(
[
("stops_successfully", None, True),
("handles_stop_exception", Exception("Stop failed"), False),
]
)
def test_stop_behavior(self, name, stop_exception, should_remove):
"""Test that stop() handles success and failure cases"""
container = self._create_container(existing_container=self.mock_container)
if stop_exception:
self.mock_container.stop.side_effect = stop_exception
container.stop()
self.mock_container.stop.assert_called_once()
if should_remove:
self.mock_container.remove.assert_called_once()
else:
self.mock_container.remove.assert_not_called()
@parameterized.expand(
[
# (name, env_vars, container_exists, container_running, expected_reused, should_create_new)
("reuses_running_container", {}, True, True, True, False),
("creates_new_when_none_exists", {}, False, False, False, True),
(
"external_mode_always_reuses",
{"DURABLE_EXECUTIONS_EXTERNAL_EMULATOR_PORT": "8080"},
False,
False,
True,
False,
),
]
)
@patch("samcli.local.docker.durable_functions_emulator_container.DurableFunctionsClient")
def test_start_or_attach_behavior(
self, name, env_vars, container_exists, container_running, expected_reused, should_create, mock_client_class
):
"""Test that start_or_attach() correctly handles reuse vs create scenarios"""
with patch.dict("os.environ", env_vars, clear=True):
container = self._create_container()
if container_exists:
mock_existing = Mock()
mock_existing.status = "running" if container_running else "exited"
self.mock_docker_client.containers.get.return_value = mock_existing
else:
self.mock_docker_client.containers.get.side_effect = Exception("Not found")
container.start = Mock()
result = container.start_or_attach()
self.assertEqual(result, expected_reused)
if should_create:
container.start.assert_called_once()
else:
container.start.assert_not_called()
@parameterized.expand(
[
("running_container", "running", True),
("stopped_container", "exited", False),
("no_container", None, False),
]
)
def test_is_running_status(self, name, container_status, expected):
"""Test that is_running() correctly reports container status"""
existing = self.mock_container if container_status else None
if existing:
self.mock_container.status = container_status
container = self._create_container(existing_container=existing)
result = container.is_running()
self.assertEqual(result, expected)
if existing:
self.mock_container.reload.assert_called_once()
@parameterized.expand(
[
("with_container", True, "test logs"),
("without_container", False, "Durable Functions Emulator container not started"),
]
)
def test_get_logs(self, name, has_container, expected_logs):
"""Test that get_logs() returns logs or appropriate message"""
existing = self.mock_container if has_container else None
if existing:
self.mock_container.logs.return_value = b"test logs"
container = self._create_container(existing_container=existing)
logs = container.get_logs(tail=100)
self.assertEqual(logs, expected_logs)
if existing:
self.mock_container.logs.assert_called_once_with(tail=100)
@parameterized.expand(
[
("x86_64", "aws-durable-execution-emulator-x86_64"),
("arm64", "aws-durable-execution-emulator-arm64"),
]
)
@patch("samcli.local.docker.durable_functions_emulator_container._get_host_architecture")
def test_binary_selection_by_architecture(self, arch, expected_binary, mock_get_host_arch):
"""Test that correct emulator binary is selected for architecture"""
mock_get_host_arch.return_value = arch
container = self._create_container()
self.assertEqual(container._get_emulator_binary_name(), expected_binary)
@parameterized.expand(
[
# (name, env_vars, expected_port, expected_store, expected_scale)
("default_config", {}, 9014, "sqlite", "1"),
("custom_port", {"DURABLE_EXECUTIONS_EMULATOR_PORT": "9999"}, 9999, "sqlite", "1"),
("filesystem_store", {"DURABLE_EXECUTIONS_STORE_TYPE": "filesystem"}, 9014, "filesystem", "1"),
("custom_time_scale", {"DURABLE_EXECUTIONS_TIME_SCALE": "0.5"}, 9014, "sqlite", "0.5"),
(
"all_custom",
{
"DURABLE_EXECUTIONS_EMULATOR_PORT": "8888",
"DURABLE_EXECUTIONS_STORE_TYPE": "filesystem",
"DURABLE_EXECUTIONS_TIME_SCALE": "2.0",
},
8888,
"filesystem",
"2.0",
),
]
)
@patch("samcli.local.docker.durable_functions_emulator_container.is_image_current")
@patch("samcli.local.docker.durable_functions_emulator_container._get_host_architecture")
@patch("os.makedirs")
@patch("os.getcwd")
@patch("pathlib.Path.exists")
def test_create_container(
self,
name,
env_vars,
expected_port,
expected_store,
expected_scale,
mock_path_exists,
mock_getcwd,
mock_makedirs,
mock_get_host_arch,
mock_is_current,
):
"""Test container creation with all configuration permutations"""
mock_get_host_arch.return_value = "x86_64"
test_dir = "/test/dir"
mock_getcwd.return_value = test_dir
mock_path_exists.return_value = True
mock_is_current.return_value = True
# Mock image already exists
mock_image = Mock()
self.mock_docker_client.images.get.return_value = mock_image
with patch.dict("os.environ", env_vars, clear=True):
container = self._create_container()
container._RAPID_SOURCE_PATH = Path(__file__).parent
container._wait_for_ready = Mock()
container.start()
# Verify container was created
self.mock_docker_client.containers.create.assert_called_once()
call_args = self.mock_docker_client.containers.create.call_args
self.assertEqual(call_args.kwargs["working_dir"], "/tmp/.durable-executions-local")
# Verify port configuration
self.assertEqual(call_args.kwargs["ports"], {f"{expected_port}/tcp": expected_port})
# Verify environment variables
environment = call_args.kwargs["environment"]
self.assertEqual(environment["EXECUTION_STORE_TYPE"], expected_store)
self.assertEqual(environment["EXECUTION_TIME_SCALE"], expected_scale)
self.assertEqual(environment["PORT"], str(expected_port))
# Verify volumes
volumes = call_args.kwargs["volumes"]
expected_data_dir = os.path.join(test_dir, ".durable-executions-local")
self.assertIn(expected_data_dir, volumes)
self.assertEqual(volumes[expected_data_dir]["bind"], "/tmp/.durable-executions-local")
self.assertEqual(volumes[expected_data_dir]["mode"], "rw")
# Verify networking
self.assertEqual(call_args.kwargs["extra_hosts"], {"host.docker.internal": "host-gateway"})
# Verify directory creation
mock_makedirs.assert_called_once_with(expected_data_dir, exist_ok=True)
# Verify container lifecycle
self.assertEqual(container.container, self.mock_container)
self.mock_container.start.assert_called_once()
@parameterized.expand(
[
# (name, image_exists, is_current, should_pull)
("image_current", True, True, False),
("image_outdated", True, False, True),
("image_missing", False, None, True),
]
)
@patch("samcli.local.docker.durable_functions_emulator_container.is_image_current")
def test_image_pull_behavior(self, name, image_exists, is_current, should_pull, mock_is_current):
"""Test that images are pulled only when necessary"""
container = self._create_container()
if image_exists:
mock_image = Mock()
self.mock_docker_client.images.get.return_value = mock_image
mock_is_current.return_value = is_current
else:
self.mock_docker_client.images.get.side_effect = docker.errors.ImageNotFound("Not found")
container._pull_image_if_needed()
if should_pull:
self.mock_docker_client.images.pull.assert_called_once()
else:
self.mock_docker_client.images.pull.assert_not_called()
def test_image_pull_failure_raises_click_exception(self):
"""Test that image pull failures raise ClickException"""
container = self._create_container()
self.mock_docker_client.images.get.side_effect = docker.errors.ImageNotFound("Not found")
self.mock_docker_client.images.pull.side_effect = Exception("Network error")
with self.assertRaises(ClickException) as context:
container._pull_image_if_needed()
self.assertIn("Failed to pull emulator image", str(context.exception))
@patch("samcli.local.docker.durable_functions_emulator_container.requests")
def test_wait_for_ready_succeeds_when_healthy(self, mock_requests):
"""Test that _wait_for_ready() succeeds when health check passes"""
mock_response = Mock()
mock_response.status_code = 200
mock_requests.get.return_value = mock_response
container = self._create_container(existing_container=self.mock_container)
self.mock_container.status = "running"
container._wait_for_ready(timeout=1)
mock_requests.get.assert_called()
@parameterized.expand(
[
# (name, env_value, has_container, should_capture, expected_logs)
("enabled_with_container", "1", True, True, "test logs"),
("enabled_true_with_container", "true", True, True, "test logs"),
("disabled_empty", "", False, False, None),
("disabled_none", None, False, False, None),
("enabled_no_container", "1", False, False, None),
]
)
@patch("builtins.open", new_callable=mock_open)
@patch("os.getcwd")
@patch("time.strftime")
def test_log_capture(
self, name, env_value, has_container, should_capture, expected_logs, mock_strftime, mock_getcwd, mock_file
):
"""Test log capture detection and behavior"""
mock_strftime.return_value = "2025-11-29T12-00-00"
mock_getcwd.return_value = "/test/dir"
env = {"DURABLE_EXECUTIONS_CAPTURE_LOGS": env_value} if env_value is not None else {}
with patch.dict("os.environ", env, clear=True):
existing = self.mock_container if has_container else None
container = self._create_container(existing_container=existing)
if has_container:
self.mock_container.logs.return_value = b"test logs"
container._capture_emulator_logs()
if should_capture:
mock_file.assert_called_once()
expected_path = os.path.join(
"/test/dir", ".durable-executions-local", "durable-execution-emulator-2025-11-29T12-00-00.log"
)
mock_file.assert_called_with(expected_path, "w")
else:
mock_file.assert_not_called()
@patch("builtins.open", new_callable=mock_open)
@patch("os.getcwd")
def test_log_capture_handles_exceptions_gracefully(self, mock_getcwd, mock_file):
"""Test that log capture exceptions don't crash the application"""
with patch.dict("os.environ", {"DURABLE_EXECUTIONS_CAPTURE_LOGS": "1"}, clear=True):
mock_getcwd.return_value = "/test/dir"
mock_file.side_effect = IOError("Write failed")
container = self._create_container(existing_container=self.mock_container)
self.mock_container.logs.return_value = b"test logs"
container._capture_emulator_logs() # Should not raise
def test_stop_captures_logs_before_stopping(self):
"""Test that stop() captures logs before stopping container"""
with patch.dict("os.environ", {"DURABLE_EXECUTIONS_CAPTURE_LOGS": "1"}, clear=True):
container = self._create_container(existing_container=self.mock_container)
container._capture_emulator_logs = Mock()
container.stop()
container._capture_emulator_logs.assert_called_once()
self.mock_container.stop.assert_called_once()