|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | + |
| 7 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 8 | +__package__ = "tests" |
| 9 | + |
| 10 | +from dpdispatcher.utils.utils import rsync |
| 11 | + |
| 12 | + |
| 13 | +@unittest.skipIf( |
| 14 | + os.environ.get("DPDISPATCHER_TEST") != "ssh", "outside the ssh testing environment" |
| 15 | +) |
| 16 | +class TestRsyncProxyCommand(unittest.TestCase): |
| 17 | + """Test rsync function with proxy command support.""" |
| 18 | + |
| 19 | + def setUp(self): |
| 20 | + """Set up test files for rsync operations.""" |
| 21 | + # Check if rsync is available before running tests |
| 22 | + if shutil.which("rsync") is None: |
| 23 | + self.skipTest("rsync not available") |
| 24 | + |
| 25 | + # Create temporary test files |
| 26 | + self.test_content = "test content for rsync" |
| 27 | + |
| 28 | + # Local test file |
| 29 | + self.local_file = tempfile.NamedTemporaryFile(mode="w", delete=False) |
| 30 | + self.local_file.write(self.test_content) |
| 31 | + self.local_file.close() |
| 32 | + |
| 33 | + # Remote paths for testing |
| 34 | + self.remote_test_dir = "/tmp/rsync_test" |
| 35 | + self.remote_file_direct = f"root@server:{self.remote_test_dir}/test_direct.txt" |
| 36 | + self.remote_file_proxy = f"root@server:{self.remote_test_dir}/test_proxy.txt" |
| 37 | + |
| 38 | + def tearDown(self): |
| 39 | + """Clean up test files.""" |
| 40 | + # Remove local test file |
| 41 | + os.unlink(self.local_file.name) |
| 42 | + |
| 43 | + def test_rsync_with_proxy_command(self): |
| 44 | + """Test rsync with proxy command via jump host.""" |
| 45 | + # Test rsync through jump host: test -> jumphost -> server |
| 46 | + rsync( |
| 47 | + self.local_file.name, |
| 48 | + self.remote_file_proxy, |
| 49 | + key_filename="/root/.ssh/id_rsa", |
| 50 | + proxy_command="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa -W server:22 root@jumphost", |
| 51 | + ) |
| 52 | + |
| 53 | + # Verify the file was transferred by reading it back |
| 54 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as download_file: |
| 55 | + download_path = download_file.name |
| 56 | + |
| 57 | + rsync( |
| 58 | + self.remote_file_proxy, |
| 59 | + download_path, |
| 60 | + key_filename="/root/.ssh/id_rsa", |
| 61 | + proxy_command="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa -W server:22 root@jumphost", |
| 62 | + ) |
| 63 | + |
| 64 | + # Verify content matches |
| 65 | + with open(download_path) as f: |
| 66 | + content = f.read() |
| 67 | + self.assertEqual(content, self.test_content) |
| 68 | + |
| 69 | + # Clean up |
| 70 | + os.unlink(download_path) |
| 71 | + |
| 72 | + def test_rsync_direct_connection(self): |
| 73 | + """Test rsync without proxy command (direct connection).""" |
| 74 | + # Test direct rsync: test -> server |
| 75 | + rsync( |
| 76 | + self.local_file.name, |
| 77 | + self.remote_file_direct, |
| 78 | + key_filename="/root/.ssh/id_rsa", |
| 79 | + ) |
| 80 | + |
| 81 | + # Verify the file was transferred by reading it back |
| 82 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as download_file: |
| 83 | + download_path = download_file.name |
| 84 | + |
| 85 | + rsync(self.remote_file_direct, download_path, key_filename="/root/.ssh/id_rsa") |
| 86 | + |
| 87 | + # Verify content matches |
| 88 | + with open(download_path) as f: |
| 89 | + content = f.read() |
| 90 | + self.assertEqual(content, self.test_content) |
| 91 | + |
| 92 | + # Clean up |
| 93 | + os.unlink(download_path) |
| 94 | + |
| 95 | + def test_rsync_with_additional_options(self): |
| 96 | + """Test rsync with proxy command and additional SSH options.""" |
| 97 | + # Test rsync with custom port, timeout, and proxy |
| 98 | + rsync( |
| 99 | + self.local_file.name, |
| 100 | + self.remote_file_proxy, |
| 101 | + port=22, |
| 102 | + key_filename="/root/.ssh/id_rsa", |
| 103 | + timeout=30, |
| 104 | + proxy_command="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa -W server:22 root@jumphost", |
| 105 | + ) |
| 106 | + |
| 107 | + # Verify the file exists on remote by attempting to download |
| 108 | + with tempfile.NamedTemporaryFile(mode="w", delete=False) as download_file: |
| 109 | + download_path = download_file.name |
| 110 | + |
| 111 | + rsync( |
| 112 | + self.remote_file_proxy, |
| 113 | + download_path, |
| 114 | + port=22, |
| 115 | + key_filename="/root/.ssh/id_rsa", |
| 116 | + timeout=30, |
| 117 | + proxy_command="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa -W server:22 root@jumphost", |
| 118 | + ) |
| 119 | + |
| 120 | + # Verify content |
| 121 | + with open(download_path) as f: |
| 122 | + content = f.read() |
| 123 | + self.assertEqual(content, self.test_content) |
| 124 | + |
| 125 | + # Clean up |
| 126 | + os.unlink(download_path) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + unittest.main() |
0 commit comments