This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdriver_test.py
More file actions
709 lines (542 loc) · 28.3 KB
/
driver_test.py
File metadata and controls
709 lines (542 loc) · 28.3 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
"""Tests for the SSH wrapper driver"""
from unittest.mock import MagicMock, patch
import pytest
from jumpstarter_driver_network.driver import TcpNetwork
from jumpstarter_driver_ssh.client import SSHCommandRunOptions, SSHCommandRunResult
from jumpstarter_driver_ssh.driver import SSHWrapper
from jumpstarter.common.exceptions import ConfigurationError
from jumpstarter.common.utils import serve
# Test SSH key content used in multiple tests
TEST_SSH_KEY = (
"-----BEGIN OPENSSH PRIVATE KEY-----\n"
"test-key-content\n"
"-----END OPENSSH PRIVATE KEY-----"
)
def test_ssh_wrapper_defaults():
"""Test SSH wrapper with default configuration"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username=""
)
# Test that the instance was created correctly
assert instance.default_username == ""
assert instance.ssh_command.startswith("ssh")
# Test that the client class is correct
assert instance.client() == "jumpstarter_driver_ssh.client.SSHWrapperClient"
def test_ssh_wrapper_configuration_error():
"""Test SSH wrapper raises error when tcp child is missing"""
with pytest.raises(ConfigurationError):
SSHWrapper(
children={}, # Missing tcp child
default_username=""
)
def test_ssh_command_with_default_username():
"""Test SSH command execution with default username provided"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser"
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command with default username
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include -l testuser
assert "-l" in call_args
assert "testuser" in call_args
assert call_args[call_args.index("-l") + 1] == "testuser"
# Should include the actual hostname (127.0.0.1) at the end, and preserve "hostname" as a command
assert "127.0.0.1" in call_args
assert "hostname" in call_args # Should be preserved as command argument
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_without_default_username():
"""Test SSH command execution without default username"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username=""
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command without default username
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should NOT include -l flag
assert "-l" not in call_args
# Should include the actual hostname (127.0.0.1) at the end, and preserve "hostname" as a command
assert "127.0.0.1" in call_args
assert "hostname" in call_args # Should be preserved as command argument
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_with_user_override():
"""Test SSH command execution with -l flag overriding default username"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser"
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command with -l flag overriding default username
result = client.run(SSHCommandRunOptions(direct=False), ["-l", "myuser", "hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include -l myuser (not testuser)
assert "-l" in call_args
assert "myuser" in call_args
assert "testuser" not in call_args
assert call_args[call_args.index("-l") + 1] == "myuser"
# Should include the actual hostname (127.0.0.1) at the end, and preserve "hostname" as a command
assert "127.0.0.1" in call_args
assert "hostname" in call_args # Should be preserved as command argument
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_with_port():
"""Test SSH command execution with custom port"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=2222)},
default_username="testuser"
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Mock the TcpPortforwardAdapter to return the expected port
with patch('jumpstarter_driver_ssh.client.TcpPortforwardAdapter') as mock_adapter:
mock_adapter.return_value.__enter__.return_value = ("127.0.0.1", 2222)
mock_adapter.return_value.__exit__.return_value = None
# Test SSH command with custom port
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include -p 2222
assert "-p" in call_args
assert "2222" in call_args
assert call_args[call_args.index("-p") + 1] == "2222"
# Should include -l testuser
assert "-l" in call_args
assert "testuser" in call_args
# Should include the actual hostname (127.0.0.1) at the end
assert "127.0.0.1" in call_args
assert "hostname" in call_args # Should be preserved as command argument
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_with_direct_flag():
"""Test SSH command execution with --direct flag"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="192.168.1.100", port=22)},
default_username="testuser"
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Mock the tcp.address() method
with patch.object(client.tcp, 'address', return_value="tcp://192.168.1.100:22"):
# Test SSH command with direct flag
result = client.run(SSHCommandRunOptions(direct=True), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include -l testuser
assert "-l" in call_args
assert "testuser" in call_args
# Should include the actual hostname (192.168.1.100) at the end, and preserve "hostname" as a command
assert "192.168.1.100" in call_args
assert "hostname" in call_args # Should be preserved as command argument
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_error_handling():
"""Test SSH command error handling when SSH is not found"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username=""
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.side_effect = FileNotFoundError("SSH not found")
# Test SSH command error handling
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Should return error code 127
assert result.return_code == 127
assert result.stdout == ""
assert "not found" in result.stderr
def test_ssh_command_with_multiple_ssh_options():
"""Test SSH command execution with multiple SSH options"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username=""
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command with multiple SSH options
result = client.run(SSHCommandRunOptions(direct=False), [
"-o", "StrictHostKeyChecking=no", "-i", "/path/to/key", "command", "arg1", "arg2"
])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include SSH options
assert "-o" in call_args
assert "StrictHostKeyChecking=no" in call_args
assert "-i" in call_args
assert "/path/to/key" in call_args
# Should include the actual hostname (127.0.0.1) at the end
assert "127.0.0.1" in call_args
# Should preserve command arguments
assert "command" in call_args
assert "arg1" in call_args
assert "arg2" in call_args
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_with_unknown_option_treated_as_command():
"""Test SSH command execution with unknown option treated as command"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username=""
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command with unknown option
result = client.run(SSHCommandRunOptions(direct=False), ["-l", "user", "-unknown", "command", "arg1"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include known SSH options
assert "-l" in call_args
assert "user" in call_args
# Should include the actual hostname (127.0.0.1) at the end
assert "127.0.0.1" in call_args
# Should treat everything after -l user as command (including -unknown)
assert "-unknown" in call_args
assert "command" in call_args
assert "arg1" in call_args
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_with_no_ssh_options():
"""Test SSH command execution with no SSH options, all arguments are command"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username=""
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command with no SSH options
result = client.run(SSHCommandRunOptions(direct=False), ["command", "arg1", "arg2"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include the actual hostname (127.0.0.1) at the end
assert "127.0.0.1" in call_args
# Should preserve all command arguments
assert "command" in call_args
assert "arg1" in call_args
assert "arg2" in call_args
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_with_command_l_flag_does_not_interfere_with_username_injection():
"""Test that command -l flags don't interfere with SSH username injection"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser"
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command with -l flag in the command (like ls -la -l ajo)
result = client.run(SSHCommandRunOptions(direct=False), ["ls", "-la", "-l", "ajo"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include -l testuser (SSH login flag)
assert "-l" in call_args
assert "testuser" in call_args
assert call_args[call_args.index("-l") + 1] == "testuser"
# Should include the actual hostname (127.0.0.1) at the end
assert "127.0.0.1" in call_args
# Should preserve command arguments including the -l flag for ls
assert "ls" in call_args
assert "-la" in call_args
assert "-l" in call_args # This should be the ls -l flag, not SSH -l
assert "ajo" in call_args
# Verify that the SSH -l flag comes before the hostname, and command -l comes after
ssh_l_index = call_args.index("-l")
hostname_index = call_args.index("127.0.0.1")
command_l_index = call_args.index("-l", ssh_l_index + 1) # Find second -l
assert ssh_l_index < hostname_index < command_l_index
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_identity_string_configuration():
"""Test SSH wrapper with ssh_identity string configuration"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity=TEST_SSH_KEY
)
# Test that the instance was created correctly
assert instance.ssh_identity == TEST_SSH_KEY
assert instance.ssh_identity_file is None
# Test that the client class is correct
assert instance.client() == "jumpstarter_driver_ssh.client.SSHWrapperClient"
def test_ssh_identity_file_configuration():
"""Test SSH wrapper with ssh_identity_file configuration"""
import os
import tempfile
# Create a temporary file with SSH key content
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='_test_key') as temp_file:
temp_file.write(TEST_SSH_KEY)
temp_file_path = temp_file.name
try:
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity_file=temp_file_path
)
# Test that the instance was created correctly
# ssh_identity should be None until first use (lazy loading)
assert instance.ssh_identity is None
assert instance.ssh_identity_file == temp_file_path
# Test that get_ssh_identity() reads the file on first use
identity = instance.get_ssh_identity()
assert identity == TEST_SSH_KEY
# Test that ssh_identity is now cached
assert instance.ssh_identity == TEST_SSH_KEY
# Test that the client class is correct
assert instance.client() == "jumpstarter_driver_ssh.client.SSHWrapperClient"
finally:
# Clean up the temporary file
os.unlink(temp_file_path)
def test_ssh_identity_validation_error():
"""Test SSH wrapper raises error when both ssh_identity and ssh_identity_file are provided"""
with pytest.raises(ConfigurationError, match="Cannot specify both ssh_identity and ssh_identity_file"):
SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity="test-key-content",
ssh_identity_file="/path/to/key"
)
def test_ssh_identity_file_read_error():
"""Test SSH wrapper raises error when ssh_identity_file cannot be read on first use"""
# Instance creation should succeed (lazy loading)
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity_file="/nonexistent/path/to/key"
)
# Error should be raised when get_ssh_identity() is called
with pytest.raises(ConfigurationError, match="Failed to read ssh_identity_file"):
instance.get_ssh_identity()
def test_ssh_command_with_identity_string():
"""Test SSH command execution with ssh_identity string"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity=TEST_SSH_KEY
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command with identity string
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include -i flag with temporary identity file
assert "-i" in call_args
identity_file_index = call_args.index("-i")
identity_file_path = call_args[identity_file_index + 1]
# The identity file should be a temporary file
assert identity_file_path.endswith("_ssh_key")
assert "/tmp" in identity_file_path or "/var/tmp" in identity_file_path
# Should include -l testuser
assert "-l" in call_args
assert "testuser" in call_args
# Should include the actual hostname (127.0.0.1) at the end
assert "127.0.0.1" in call_args
assert "hostname" in call_args
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_command_with_identity_file():
"""Test SSH command execution with ssh_identity_file"""
import os
import tempfile
# Create a temporary file with SSH key content
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='_test_key') as temp_file:
temp_file.write(TEST_SSH_KEY)
temp_file_path = temp_file.name
try:
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity_file=temp_file_path
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command with identity file
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should include -i flag with temporary identity file
assert "-i" in call_args
identity_file_index = call_args.index("-i")
identity_file_path = call_args[identity_file_index + 1]
# The identity file should be a temporary file (not the original file)
assert identity_file_path.endswith("_ssh_key")
assert "/tmp" in identity_file_path or "/var/tmp" in identity_file_path
assert identity_file_path != temp_file_path
# Should include -l testuser
assert "-l" in call_args
assert "testuser" in call_args
# Should include the actual hostname (127.0.0.1) at the end
assert "127.0.0.1" in call_args
assert "hostname" in call_args
assert result.return_code == 0
assert result.stdout == "some stdout"
finally:
# Clean up the temporary file
os.unlink(temp_file_path)
def test_ssh_command_without_identity():
"""Test SSH command execution without identity (should not include -i flag)"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser"
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
# Test SSH command without identity
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify subprocess.run was called
assert mock_run.called
call_args = mock_run.call_args[0][0] # First positional argument
# Should NOT include -i flag
assert "-i" not in call_args
# Should include -l testuser
assert "-l" in call_args
assert "testuser" in call_args
# Should include the actual hostname (127.0.0.1) at the end
assert "127.0.0.1" in call_args
assert "hostname" in call_args
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_identity_temp_file_creation_and_cleanup():
"""Test that temporary identity file is created and cleaned up properly"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity=TEST_SSH_KEY
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
with patch('tempfile.NamedTemporaryFile') as mock_temp_file:
with patch('os.chmod') as mock_chmod:
with patch('os.unlink') as mock_unlink:
# Mock the temporary file
mock_temp_file_instance = MagicMock()
mock_temp_file_instance.name = "/tmp/test_ssh_key_12345"
mock_temp_file_instance.write = MagicMock()
mock_temp_file_instance.close = MagicMock()
mock_temp_file.return_value = mock_temp_file_instance
# Test SSH command with identity
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify temporary file was created
mock_temp_file.assert_called_once_with(mode='w', delete=False, suffix='_ssh_key')
mock_temp_file_instance.write.assert_called_once_with(TEST_SSH_KEY)
mock_temp_file_instance.close.assert_called_once()
# Verify proper permissions were set
mock_chmod.assert_called_once_with("/tmp/test_ssh_key_12345", 0o600)
# Verify temporary file was cleaned up
mock_unlink.assert_called_once_with("/tmp/test_ssh_key_12345")
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_identity_temp_file_creation_error():
"""Test error handling when temporary identity file creation fails"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity=TEST_SSH_KEY
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0)
with patch('tempfile.NamedTemporaryFile') as mock_temp_file:
mock_temp_file.side_effect = OSError("Permission denied")
# Test SSH command with identity should raise an error
# The exception will be wrapped in an ExceptionGroup due to the context manager
with pytest.raises(ExceptionGroup) as exc_info:
client.run(SSHCommandRunOptions(direct=False), ["hostname"])
# Check that the original OSError is in the exception group
assert any(isinstance(e, OSError) and "Permission denied" in str(e) for e in exc_info.value.exceptions)
def test_ssh_identity_temp_file_cleanup_error():
"""Test error handling when temporary identity file cleanup fails"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity=TEST_SSH_KEY
)
with serve(instance) as client:
with patch('subprocess.run') as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="some stdout", stderr="")
with patch('tempfile.NamedTemporaryFile') as mock_temp_file:
with patch('os.chmod') as mock_chmod:
with patch('os.unlink') as mock_unlink:
# Mock the temporary file
mock_temp_file_instance = MagicMock()
mock_temp_file_instance.name = "/tmp/test_ssh_key_12345"
mock_temp_file_instance.write = MagicMock()
mock_temp_file_instance.close = MagicMock()
mock_temp_file.return_value = mock_temp_file_instance
# Mock cleanup failure
mock_unlink.side_effect = OSError("Permission denied")
# Test SSH command with identity - should still succeed but log warning
with patch.object(client, 'logger') as mock_logger:
result = client.run(SSHCommandRunOptions(direct=False), ["hostname"])
assert isinstance(result, SSHCommandRunResult)
# Verify chmod was called
mock_chmod.assert_called_once_with("/tmp/test_ssh_key_12345", 0o600)
# Verify warning was logged
mock_logger.warning.assert_called_once_with(
"Failed to clean up temporary identity file %s: %s",
"/tmp/test_ssh_key_12345",
str(mock_unlink.side_effect)
)
assert result.return_code == 0
assert result.stdout == "some stdout"
def test_ssh_client_properties():
"""Test that the client properties correctly reflect the driver configuration"""
instance = SSHWrapper(
children={"tcp": TcpNetwork(host="127.0.0.1", port=22)},
default_username="testuser",
ssh_identity=TEST_SSH_KEY,
ssh_command="my-ssh-command",
)
with serve(instance) as client:
assert client.username == "testuser"
assert client.identity == TEST_SSH_KEY
assert client.command == "my-ssh-command"