| 1 |
# SPDX-FileCopyrightText: 2023-2024 Greenbone AG |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
# |
| 5 |
|
| 6 |
import asyncio |
| 7 |
import unittest |
| 8 |
from asyncio.subprocess import Process |
| 9 |
from pathlib import Path |
| 10 |
from unittest.mock import AsyncMock, patch |
| 11 |
|
| 12 |
from greenbone.feed.sync.errors import RsyncError |
| 13 |
from greenbone.feed.sync.rsync import Rsync, exec_rsync |
| 14 |
|
| 15 |
|
| 16 |
class RsyncTestCase(unittest.IsolatedAsyncioTestCase): |
| 17 |
@patch("greenbone.feed.sync.rsync.exec_rsync", autospec=True) |
| 18 |
async def test_rsync_with_defaults(self, exec_mock: AsyncMock): |
| 19 |
rsync = Rsync() |
| 20 |
await rsync.sync("rsync://foo.bar/baz", "/tmp/baz") |
| 21 |
|
| 22 |
exec_mock.assert_awaited_once_with( |
| 23 |
"--links", |
| 24 |
"--times", |
| 25 |
"--omit-dir-times", |
| 26 |
"--recursive", |
| 27 |
"--partial", |
| 28 |
"--progress", |
| 29 |
"-q", |
| 30 |
"--compress-level=9", |
| 31 |
"--delete", |
| 32 |
"--perms", |
| 33 |
"--chmod=Fugo+r,Fug+w,Dugo-s,Dugo+rx,Dug+w", |
| 34 |
"--copy-unsafe-links", |
| 35 |
"--hard-links", |
| 36 |
"rsync://foo.bar/baz", |
| 37 |
"/tmp/baz", |
| 38 |
) |
| 39 |
|
| 40 |
@patch("greenbone.feed.sync.rsync.exec_rsync", autospec=True) |
| 41 |
async def test_rsync_with_private_subdir(self, exec_mock: AsyncMock): |
| 42 |
rsync = Rsync(private_subdir="private") |
| 43 |
await rsync.sync("rsync://foo.bar/baz", "/tmp/baz") |
| 44 |
|
| 45 |
exec_mock.assert_awaited_once_with( |
| 46 |
"--links", |
| 47 |
"--times", |
| 48 |
"--omit-dir-times", |
| 49 |
"--recursive", |
| 50 |
"--partial", |
| 51 |
"--progress", |
| 52 |
"-q", |
| 53 |
"--compress-level=9", |
| 54 |
"--delete", |
| 55 |
"--exclude", |
| 56 |
"private", |
| 57 |
"--perms", |
| 58 |
"--chmod=Fugo+r,Fug+w,Dugo-s,Dugo+rx,Dug+w", |
| 59 |
"--copy-unsafe-links", |
| 60 |
"--hard-links", |
| 61 |
"rsync://foo.bar/baz", |
| 62 |
"/tmp/baz", |
| 63 |
) |
| 64 |
|
| 65 |
@patch("greenbone.feed.sync.rsync.exec_rsync", autospec=True) |
| 66 |
async def test_rsync_with_verbose(self, exec_mock: AsyncMock): |
| 67 |
rsync = Rsync(verbose=True) |
| 68 |
await rsync.sync("rsync://foo.bar/baz", "/tmp/baz") |
| 69 |
|
| 70 |
exec_mock.assert_awaited_once_with( |
| 71 |
"--links", |
| 72 |
"--times", |
| 73 |
"--omit-dir-times", |
| 74 |
"--recursive", |
| 75 |
"--partial", |
| 76 |
"--progress", |
| 77 |
"-v", |
| 78 |
"--compress-level=9", |
| 79 |
"--delete", |
| 80 |
"--perms", |
| 81 |
"--chmod=Fugo+r,Fug+w,Dugo-s,Dugo+rx,Dug+w", |
| 82 |
"--copy-unsafe-links", |
| 83 |
"--hard-links", |
| 84 |
"rsync://foo.bar/baz", |
| 85 |
"/tmp/baz", |
| 86 |
) |
| 87 |
|
| 88 |
@patch("greenbone.feed.sync.rsync.exec_rsync", autospec=True) |
| 89 |
async def test_rsync_with_compression_level(self, exec_mock: AsyncMock): |
| 90 |
rsync = Rsync(compression_level=1) |
| 91 |
await rsync.sync("rsync://foo.bar/baz", "/tmp/baz") |
| 92 |
|
| 93 |
exec_mock.assert_awaited_once_with( |
| 94 |
"--links", |
| 95 |
"--times", |
| 96 |
"--omit-dir-times", |
| 97 |
"--recursive", |
| 98 |
"--partial", |
| 99 |
"--progress", |
| 100 |
"-q", |
| 101 |
"--compress-level=1", |
| 102 |
"--delete", |
| 103 |
"--perms", |
| 104 |
"--chmod=Fugo+r,Fug+w,Dugo-s,Dugo+rx,Dug+w", |
| 105 |
"--copy-unsafe-links", |
| 106 |
"--hard-links", |
| 107 |
"rsync://foo.bar/baz", |
| 108 |
"/tmp/baz", |
| 109 |
) |
| 110 |
|
| 111 |
@patch("greenbone.feed.sync.rsync.exec_rsync", autospec=True) |
| 112 |
async def test_rsync_with_exclude(self, exec_mock: AsyncMock): |
| 113 |
rsync = Rsync(exclude=["foo", Path("exclude/this")]) |
| 114 |
await rsync.sync("rsync://foo.bar/baz", "/tmp/baz") |
| 115 |
|
| 116 |
exec_mock.assert_awaited_once_with( |
| 117 |
"--links", |
| 118 |
"--times", |
| 119 |
"--omit-dir-times", |
| 120 |
"--recursive", |
| 121 |
"--partial", |
| 122 |
"--progress", |
| 123 |
"-q", |
| 124 |
"--compress-level=9", |
| 125 |
"--delete", |
| 126 |
"--exclude", |
| 127 |
"foo", |
| 128 |
"--exclude", |
| 129 |
"exclude/this", |
| 130 |
"--perms", |
| 131 |
"--chmod=Fugo+r,Fug+w,Dugo-s,Dugo+rx,Dug+w", |
| 132 |
"--copy-unsafe-links", |
| 133 |
"--hard-links", |
| 134 |
"rsync://foo.bar/baz", |
| 135 |
"/tmp/baz", |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
class ExecRsyncTestCase(unittest.IsolatedAsyncioTestCase): |
| 140 |
@patch( |
| 141 |
"greenbone.feed.sync.rsync.asyncio.create_subprocess_exec", |
| 142 |
autospec=True, |
| 143 |
) |
| 144 |
async def test_failure(self, exec_mock: AsyncMock): |
| 145 |
process_mock = AsyncMock(spec=Process) |
| 146 |
process_mock.communicate.return_value = (None, b"An error occurred") |
| 147 |
process_mock.wait.return_value = 1 |
| 148 |
exec_mock.return_value = process_mock |
| 149 |
|
| 150 |
with self.assertRaises(RsyncError) as cm: |
| 151 |
await exec_rsync("foo", "bar") |
| 152 |
|
| 153 |
exec_mock.assert_awaited_once_with( |
| 154 |
"rsync", "foo", "bar", stderr=asyncio.subprocess.PIPE |
| 155 |
) |
| 156 |
|
| 157 |
self.assertEqual(cm.exception.returncode, 1) |
| 158 |
self.assertEqual(cm.exception.stderr, "An error occurred") |
| 159 |
self.assertEqual(cm.exception.cmd, ["rsync", "foo", "bar"]) |
| 160 |
self.assertIsNone(cm.exception.stout) |
| 161 |
self.assertEqual( |
| 162 |
str(cm.exception), |
| 163 |
"'rsync foo bar' returned non-zero exit status 1.", |
| 164 |
) |
| 165 |
|
| 166 |
@patch( |
| 167 |
"greenbone.feed.sync.rsync.asyncio.create_subprocess_exec", |
| 168 |
autospec=True, |
| 169 |
) |
| 170 |
async def test_success(self, exec_mock: AsyncMock): |
| 171 |
process_mock = AsyncMock(spec=Process) |
| 172 |
process_mock.communicate.return_value = (None, None) |
| 173 |
process_mock.wait.return_value = 0 |
| 174 |
exec_mock.return_value = process_mock |
| 175 |
|
| 176 |
await exec_rsync("foo", "bar") |
| 177 |
|
| 178 |
exec_mock.assert_awaited_once_with( |
| 179 |
"rsync", "foo", "bar", stderr=asyncio.subprocess.PIPE |
| 180 |
) |
| 181 |
|
| 182 |
@patch("greenbone.feed.sync.rsync.exec_rsync", autospec=True) |
| 183 |
async def test_rsync_with_timeout(self, exec_mock: AsyncMock): |
| 184 |
rsync = Rsync(timeout=120) |
| 185 |
await rsync.sync("rsync://foo.bar/baz", "/tmp/baz") |
| 186 |
|
| 187 |
exec_mock.assert_awaited_once_with( |
| 188 |
"--links", |
| 189 |
"--times", |
| 190 |
"--omit-dir-times", |
| 191 |
"--recursive", |
| 192 |
"--partial", |
| 193 |
"--progress", |
| 194 |
"--timeout=120", |
| 195 |
"-q", |
| 196 |
"--compress-level=9", |
| 197 |
"--delete", |
| 198 |
"--perms", |
| 199 |
"--chmod=Fugo+r,Fug+w,Dugo-s,Dugo+rx,Dug+w", |
| 200 |
"--copy-unsafe-links", |
| 201 |
"--hard-links", |
| 202 |
"rsync://foo.bar/baz", |
| 203 |
"/tmp/baz", |
| 204 |
) |
| 205 |
|
| 206 |
@patch("greenbone.feed.sync.rsync.exec_rsync", autospec=True) |
| 207 |
async def test_rsync_with_ssh(self, exec_mock: AsyncMock): |
| 208 |
ssh_key = Path("/tmp/ssh.key") |
| 209 |
rsync = Rsync(ssh_key=ssh_key) |
| 210 |
await rsync.sync("ssh://[email protected]/baz", "/tmp/baz") |
| 211 |
|
| 212 |
exec_mock.assert_awaited_once_with( |
| 213 |
"--links", |
| 214 |
"--times", |
| 215 |
"--omit-dir-times", |
| 216 |
"--recursive", |
| 217 |
"--partial", |
| 218 |
"--progress", |
| 219 |
"-e", |
| 220 |
"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 24 -i '/tmp/ssh.key'", # pylint: disable=line-too-long # noqa: E501 |
| 221 |
"-q", |
| 222 |
"--compress-level=9", |
| 223 |
"--delete", |
| 224 |
"--perms", |
| 225 |
"--chmod=Fugo+r,Fug+w,Dugo-s,Dugo+rx,Dug+w", |
| 226 |
"--copy-unsafe-links", |
| 227 |
"--hard-links", |
| 228 |
"[email protected]:/baz", |
| 229 |
"/tmp/baz", |
| 230 |
) |