-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_platform_config.py
More file actions
556 lines (446 loc) · 23.1 KB
/
test_platform_config.py
File metadata and controls
556 lines (446 loc) · 23.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
"""
Unit tests for platform_config module
"""
import plistlib
import unittest
from unittest.mock import patch, mock_open, Mock
from parameterized import parameterized
from samcli.local.docker.platform_config import (
MacOSHandler,
LinuxHandler,
WindowsHandler,
get_platform_handler,
get_finch_socket_path,
)
from samcli.local.docker.container_engine import ContainerEngine
class TestPlatformHandlerBase(unittest.TestCase):
"""Unit tests for PlatformHandler base class"""
def test_read_config_with_none_from_subclass(self):
"""Test read_config when _read_config returns None"""
handler = MacOSHandler()
with patch.object(handler, "_read_config", return_value=None):
result = handler.read_config()
self.assertIsNone(result)
def test_read_config_with_whitespace_value(self):
"""Test read_config strips whitespace and converts to lowercase"""
handler = MacOSHandler()
with patch.object(handler, "_read_config", return_value=" DOCKER "):
result = handler.read_config()
self.assertEqual(result, "docker")
class TestMacOSHandler(unittest.TestCase):
"""Unit tests for MacOSHandler"""
def setUp(self):
self.handler = MacOSHandler()
def test_read_config_success_with_finch(self):
"""Test successful plist reading with finch preference"""
mock_plist_data = {"DefaultContainerRuntime": "finch"}
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", return_value=mock_plist_data
):
result = self.handler.read_config()
self.assertEqual(result, "finch")
def test_read_config_success_with_docker(self):
"""Test successful plist reading with docker preference"""
mock_plist_data = {"DefaultContainerRuntime": "docker"}
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", return_value=mock_plist_data
):
result = self.handler.read_config()
self.assertEqual(result, "docker")
def test_read_config_file_not_exists(self):
"""Test when plist file doesn't exist"""
with patch("os.path.exists", return_value=False):
result = self.handler.read_config()
self.assertIsNone(result)
def test_read_config_no_default_container_runtime_key(self):
"""Test when plist exists but has no DefaultContainerRuntime key"""
mock_plist_data = {"SomeOtherKey": "value"}
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", return_value=mock_plist_data
):
result = self.handler.read_config()
self.assertIsNone(result)
@parameterized.expand(
[
(FileNotFoundError("File not found"),),
(OSError("Permission denied"),),
(plistlib.InvalidFileException("Invalid plist"),),
]
)
def test_read_config_exception_handling(self, exception):
"""Test exception handling during plist reading"""
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", side_effect=exception
):
result = self.handler.read_config()
self.assertIsNone(result)
def test_get_finch_socket_path(self):
"""Test macOS Finch socket path"""
result = self.handler.get_finch_socket_path()
self.assertEqual(result, "unix:////Applications/Finch/lima/data/finch/sock/finch.sock")
def test_supports_finch(self):
"""Test that macOS supports Finch"""
self.assertTrue(self.handler.supports_finch())
def test_get_container_not_reachable_message_with_finch_preference(self):
"""Test macOS error message when admin preference is finch"""
with patch.object(self.handler, "read_config", return_value="finch"):
result = self.handler.get_container_not_reachable_message()
self.assertEqual(
result, "Running AWS SAM projects locally requires Finch. Do you have Finch installed and running?"
)
def test_get_container_not_reachable_message_with_docker_preference(self):
"""Test macOS error message when admin preference is docker"""
with patch.object(self.handler, "read_config", return_value="docker"):
result = self.handler.get_container_not_reachable_message()
self.assertEqual(
result, "Running AWS SAM projects locally requires Docker. Do you have Docker installed and running?"
)
def test_get_container_not_reachable_message_no_preference(self):
"""Test macOS error message when no admin preference is set"""
with patch.object(self.handler, "read_config", return_value=None):
result = self.handler.get_container_not_reachable_message()
expected = (
"Running AWS SAM projects locally requires a container runtime. "
"Do you have Docker or Finch installed and running?"
)
self.assertEqual(result, expected)
def test_read_config_with_none_container_runtime(self):
"""Test read_config when container_runtime is None"""
mock_plist_data = {"DefaultContainerRuntime": None}
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", return_value=mock_plist_data
):
result = self.handler.read_config()
self.assertIsNone(result)
def test_get_container_not_reachable_message_with_unknown_preference(self):
"""Test macOS error message when admin preference is unknown value"""
with patch.object(self.handler, "read_config", return_value="unknown"):
result = self.handler.get_container_not_reachable_message()
expected = (
"Running AWS SAM projects locally requires a container runtime. "
"Do you have Docker or Finch installed and running?"
)
self.assertEqual(result, expected)
class TestLinuxHandler(unittest.TestCase):
"""Unit tests for LinuxHandler"""
def setUp(self):
self.handler = LinuxHandler()
def test_read_config_not_implemented(self):
"""Test that Linux config reading returns None (not implemented)"""
result = self.handler.read_config()
self.assertIsNone(result)
@patch("os.path.exists")
@patch.dict("os.environ", {"XDG_RUNTIME_DIR": "/run/user/1001"})
def test_get_finch_socket_path_xdg_containerd(self, mock_exists):
"""Test Linux Finch socket path with XDG_RUNTIME_DIR containerd socket"""
# Mock that containerd socket exists
def exists_side_effect(path):
return path == "/run/user/1001/containerd/containerd.sock"
mock_exists.side_effect = exists_side_effect
result = self.handler.get_finch_socket_path()
self.assertEqual(result, "unix:///run/user/1001/containerd/containerd.sock")
@patch("os.path.exists")
@patch.dict("os.environ", {"XDG_RUNTIME_DIR": "/run/user/1001"})
def test_get_finch_socket_path_xdg_finch(self, mock_exists):
"""Test Linux Finch socket path with XDG_RUNTIME_DIR finch socket"""
# Mock that finch socket exists (but not containerd)
def exists_side_effect(path):
return path == "/run/user/1001/finch.sock"
mock_exists.side_effect = exists_side_effect
result = self.handler.get_finch_socket_path()
self.assertEqual(result, "unix:///run/user/1001/finch.sock")
@patch("os.path.exists")
@patch("os.path.expanduser")
@patch.dict("os.environ", {}, clear=True)
def test_get_finch_socket_path_home_directory(self, mock_expanduser, mock_exists):
"""Test Linux Finch socket path in home directory"""
mock_expanduser.return_value = "/home/testuser"
# Mock that home finch socket exists
def exists_side_effect(path):
return path == "/home/testuser/.finch/finch.sock"
mock_exists.side_effect = exists_side_effect
result = self.handler.get_finch_socket_path()
self.assertEqual(result, "unix:///home/testuser/.finch/finch.sock")
@patch("os.path.exists")
@patch.dict("os.environ", {}, clear=True)
def test_get_finch_socket_path_system_socket(self, mock_exists):
"""Test Linux Finch socket path at system location"""
# Mock that system socket exists
def exists_side_effect(path):
return path == "/var/run/finch.sock"
mock_exists.side_effect = exists_side_effect
result = self.handler.get_finch_socket_path()
self.assertEqual(result, "unix:///var/run/finch.sock")
@patch("os.path.exists", return_value=False)
@patch.dict("os.environ", {}, clear=True)
def test_get_finch_socket_path_not_found(self, mock_exists):
"""Test Linux Finch socket path when no socket exists"""
result = self.handler.get_finch_socket_path()
self.assertIsNone(result)
def test_supports_finch(self):
"""Test that Linux supports Finch"""
self.assertTrue(self.handler.supports_finch())
def test_get_container_not_reachable_message_with_finch_preference(self):
"""Test Linux error message when admin preference is finch"""
with patch.object(self.handler, "read_config", return_value="finch"):
result = self.handler.get_container_not_reachable_message()
self.assertEqual(
result, "Running AWS SAM projects locally requires Finch. Do you have Finch installed and running?"
)
def test_get_container_not_reachable_message_with_docker_preference(self):
"""Test Linux error message when admin preference is docker"""
with patch.object(self.handler, "read_config", return_value="docker"):
result = self.handler.get_container_not_reachable_message()
self.assertEqual(
result, "Running AWS SAM projects locally requires Docker. Do you have Docker installed and running?"
)
def test_get_container_not_reachable_message_no_preference(self):
"""Test Linux error message when no admin preference is set"""
with patch.object(self.handler, "read_config", return_value=None):
result = self.handler.get_container_not_reachable_message()
expected = (
"Running AWS SAM projects locally requires a container runtime. "
"Do you have Docker or Finch installed and running?"
)
self.assertEqual(result, expected)
def test_get_container_not_reachable_message_with_unknown_preference(self):
"""Test Linux error message when admin preference is unknown value"""
with patch.object(self.handler, "read_config", return_value="unknown"):
result = self.handler.get_container_not_reachable_message()
expected = (
"Running AWS SAM projects locally requires a container runtime. "
"Do you have Docker or Finch installed and running?"
)
self.assertEqual(result, expected)
class TestWindowsHandler(unittest.TestCase):
"""Unit tests for WindowsHandler"""
def setUp(self):
self.handler = WindowsHandler()
def test_read_config_not_implemented(self):
"""Test that Windows config reading returns None (not implemented)"""
result = self.handler.read_config()
self.assertIsNone(result)
def test_get_finch_socket_path_not_supported(self):
"""Test that Windows Finch socket path returns None (not supported)"""
result = self.handler.get_finch_socket_path()
self.assertIsNone(result)
def test_supports_finch(self):
"""Test that Windows does not support Finch"""
self.assertFalse(self.handler.supports_finch())
def test_get_container_not_reachable_message(self):
"""Test Windows error message"""
result = self.handler.get_container_not_reachable_message()
expected = (
"Running AWS SAM projects locally requires a container runtime. Do you have Docker installed and running?"
)
self.assertEqual(result, expected)
class TestGetPlatformHandler(unittest.TestCase):
"""Tests for get_platform_handler function"""
@parameterized.expand(
[
("Darwin", MacOSHandler),
("Linux", LinuxHandler),
("Windows", WindowsHandler),
]
)
@patch("samcli.local.docker.platform_config.platform.system")
def test_returns_correct_handler(self, platform_name, expected_handler_class, mock_system):
"""Test that get_platform_handler returns the correct handler for each platform"""
mock_system.return_value = platform_name
handler = get_platform_handler()
self.assertIsInstance(handler, expected_handler_class)
mock_system.assert_called_once()
@patch("samcli.local.docker.platform_config.platform.system")
def test_returns_none_for_unsupported_platform(self, mock_system):
"""Test that get_platform_handler returns None for unsupported platforms"""
mock_system.return_value = "FreeBSD"
handler = get_platform_handler()
self.assertIsNone(handler)
mock_system.assert_called_once()
class TestGetFinchSocketPath(unittest.TestCase):
"""Tests for get_finch_socket_path utility function"""
@patch("os.path.exists")
@patch("samcli.local.docker.platform_config.platform.system")
def test_get_finch_socket_path_linux_with_system_socket(self, mock_system, mock_exists):
"""Test that get_finch_socket_path returns system socket path on Linux when it exists"""
mock_system.return_value = "Linux"
# Mock that system socket exists
def exists_side_effect(path):
return path == "/var/run/finch.sock"
mock_exists.side_effect = exists_side_effect
result = get_finch_socket_path()
self.assertEqual(result, "unix:///var/run/finch.sock")
@patch("os.path.exists", return_value=False)
@patch("samcli.local.docker.platform_config.platform.system")
def test_get_finch_socket_path_linux_no_socket(self, mock_system, mock_exists):
"""Test that get_finch_socket_path returns None on Linux when no socket exists"""
mock_system.return_value = "Linux"
result = get_finch_socket_path()
self.assertIsNone(result)
@patch("samcli.local.docker.platform_config.platform.system")
def test_get_finch_socket_path_macos(self, mock_system):
"""Test that get_finch_socket_path returns correct path on macOS"""
mock_system.return_value = "Darwin"
result = get_finch_socket_path()
self.assertEqual(result, "unix:////Applications/Finch/lima/data/finch/sock/finch.sock")
@patch("samcli.local.docker.platform_config.platform.system")
def test_get_finch_socket_path_windows(self, mock_system):
"""Test that get_finch_socket_path returns None on Windows"""
mock_system.return_value = "Windows"
result = get_finch_socket_path()
self.assertIsNone(result)
@patch("samcli.local.docker.platform_config.get_platform_handler")
def test_get_finch_socket_path_returns_none_when_no_handler(self, mock_get_handler):
"""Test that get_finch_socket_path returns None when no platform handler available"""
mock_get_handler.return_value = None
result = get_finch_socket_path()
self.assertEqual(result, None)
mock_get_handler.assert_called_once()
@patch("samcli.local.docker.platform_config.get_platform_handler")
def test_get_finch_socket_path_handler_not_supports_finch(self, mock_get_handler):
"""Test that get_finch_socket_path returns None when handler doesn't support Finch"""
mock_handler = Mock()
mock_handler.supports_finch.return_value = False
mock_get_handler.return_value = mock_handler
result = get_finch_socket_path()
self.assertEqual(result, None)
@patch("samcli.local.docker.platform_config.get_platform_handler")
def test_get_finch_socket_path_handler_supports_finch(self, mock_get_handler):
"""Test that get_finch_socket_path returns path when handler supports Finch"""
mock_handler = Mock()
mock_handler.supports_finch.return_value = True
mock_handler.get_finch_socket_path.return_value = "unix:///custom/finch.sock"
mock_get_handler.return_value = mock_handler
result = get_finch_socket_path()
self.assertEqual(result, "unix:///custom/finch.sock")
class TestMacOSHandlerIntegration(unittest.TestCase):
"""Integration tests for macOS platform handler"""
@patch("samcli.local.docker.platform_config.platform.system")
def test_happy_path(self, mock_system):
"""Test MacOS handler integration happy path"""
mock_system.return_value = "Darwin"
handler = get_platform_handler()
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", return_value={"DefaultContainerRuntime": "finch"}
):
config = handler.read_config()
self.assertEqual(config, "finch")
@parameterized.expand(
[
("finch", "finch"),
("docker", "docker"),
("FINCH", "finch"),
("DOCKER", "docker"),
("Finch", "finch"),
("Docker", "docker"),
]
)
@patch("samcli.local.docker.platform_config.platform.system")
def test_valid_values(self, container_runtime, expected, mock_system):
"""Test MacOS handler integration with valid container runtime values"""
mock_system.return_value = "Darwin"
handler = get_platform_handler()
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", return_value={"DefaultContainerRuntime": container_runtime}
):
config = handler.read_config()
self.assertEqual(config, expected)
@patch("samcli.local.docker.platform_config.platform.system")
def test_sad_path_file_not_found(self, mock_system):
"""Test MacOS handler when config file doesn't exist"""
mock_system.return_value = "Darwin"
handler = get_platform_handler()
with patch("os.path.exists", return_value=False), self.assertLogs(
"samcli.local.docker.platform_config", level="DEBUG"
) as log_context:
result = handler.read_config()
self.assertIsNone(result)
self.assertIn("Administrator config file not found on macOS", log_context.output[0])
@parameterized.expand(
[
("missing_key", {"SomeOtherKey": "value"}),
("empty_plist", {}),
]
)
@patch("samcli.local.docker.platform_config.platform.system")
def test_sad_path_no_debug(self, scenario, plist_data, mock_system):
"""Test MacOS handler sad path scenarios that don't log debug messages"""
mock_system.return_value = "Darwin"
handler = get_platform_handler()
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", return_value=plist_data
):
result = handler.read_config()
self.assertIsNone(result)
@parameterized.expand(
[
(FileNotFoundError, "Test file error"),
(OSError, "Test OS error"),
(plistlib.InvalidFileException, "Test plist error"),
]
)
@patch("samcli.local.docker.platform_config.platform.system")
def test_exception_handling(self, exception_type, error_message, mock_system):
"""Test MacOS handler integration exception handling"""
mock_system.return_value = "Darwin"
handler = get_platform_handler()
with patch("os.path.exists", return_value=True), patch("builtins.open", mock_open()), patch(
"plistlib.load", side_effect=exception_type(error_message)
), self.assertLogs("samcli.local.docker.platform_config", level="DEBUG") as log_context:
result = handler.read_config()
self.assertIsNone(result)
self.assertIn(f"Error reading macOS administrator config: {error_message}", log_context.output[0])
class TestLinuxHandlerIntegration(unittest.TestCase):
"""Integration tests for Linux platform handler"""
@patch("samcli.local.docker.platform_config.platform.system")
def test_integration(self, mock_system):
"""Test Linux handler integration through get_platform_handler"""
mock_system.return_value = "Linux"
handler = get_platform_handler()
self.assertIsInstance(handler, LinuxHandler)
# Linux handler returns None (not implemented yet)
config = handler.read_config()
self.assertIsNone(config)
class TestWindowsHandlerIntegration(unittest.TestCase):
"""Integration tests for Windows platform handler"""
@patch("samcli.local.docker.platform_config.platform.system")
def test_integration(self, mock_system):
"""Test Windows handler integration through get_platform_handler"""
mock_system.return_value = "Windows"
handler = get_platform_handler()
self.assertIsInstance(handler, WindowsHandler)
# Windows handler returns None (not implemented yet)
config = handler.read_config()
self.assertIsNone(config)
class TestAbstractPlatformHandlerMethods(unittest.TestCase):
"""Test abstract methods in PlatformHandler to achieve 100% coverage"""
def test_abstract_method_pass_statements_coverage(self):
"""Test that abstract method pass statements are covered"""
from samcli.local.docker.platform_config import PlatformHandler
# Create a concrete implementation that calls the abstract methods directly
# This will cover the pass statements in the abstract methods
class TestPlatformHandler(PlatformHandler):
def _read_config(self):
# Call the parent abstract method to cover the pass statement
super()._read_config()
return "test"
def get_finch_socket_path(self):
# Call the parent abstract method to cover the pass statement
super().get_finch_socket_path()
return "test"
def supports_finch(self):
# Call the parent abstract method to cover the pass statement
super().supports_finch()
return True
def get_container_not_reachable_message(self):
# Call the parent abstract method to cover the pass statement
super().get_container_not_reachable_message()
return "test"
# Instantiate and call methods to cover the pass statements
handler = TestPlatformHandler()
self.assertEqual(handler._read_config(), "test")
self.assertEqual(handler.get_finch_socket_path(), "test")
self.assertTrue(handler.supports_finch())
self.assertEqual(handler.get_container_not_reachable_message(), "test")
if __name__ == "__main__":
unittest.main()