aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Harder <radhermit@gmail.com>2019-02-11 16:05:01 -0600
committerTim Harder <radhermit@gmail.com>2019-02-11 16:05:01 -0600
commita7f959356970a56c32881dd8724be57f579ffee5 (patch)
treea48da5b4e8d4dac971a2cb966cf5ba0bdeadf5f4 /tests/sync
parentsync/rsync: use OSError instead of socket.error (diff)
downloadpkgcore-a7f959356970a56c32881dd8724be57f579ffee5.tar.gz
pkgcore-a7f959356970a56c32881dd8724be57f579ffee5.tar.bz2
pkgcore-a7f959356970a56c32881dd8724be57f579ffee5.zip
tests/sync/test_rsync: fake DNS resolution to avoid hitting the network
And add failed DNS check.
Diffstat (limited to 'tests/sync')
-rw-r--r--tests/sync/test_rsync.py45
1 files changed, 40 insertions, 5 deletions
diff --git a/tests/sync/test_rsync.py b/tests/sync/test_rsync.py
index 22ab6da1..5a6e7b77 100644
--- a/tests/sync/test_rsync.py
+++ b/tests/sync/test_rsync.py
@@ -30,31 +30,66 @@ class TestRsyncSyncer(object):
assert o.rsh == "/bin/sh"
@mock.patch('snakeoil.process.find_binary', return_value='rsync')
+ @mock.patch('socket.getaddrinfo')
@mock.patch('snakeoil.process.spawn.spawn')
- def test_sync(self, spawn, find_binary, tmp_path):
+ def test_sync(self, spawn, getaddrinfo, find_binary, tmp_path):
path = tmp_path / 'repo'
syncer = rsync.rsync_syncer(
str(path), "rsync://rsync.gentoo.org/gentoo-portage")
+ # faked IPv4 addresses for rsync.gentoo.org
+ fake_ips = ['.'.join(str(x) * 4) for x in range(3)]
+ getaddrinfo.return_value = fake_ips
+
# successful sync
spawn.return_value = 0
assert syncer.sync()
spawn.assert_called_once()
spawn.reset_mock()
- # failed sync
+ # failed command sync
spawn.return_value = 1
- with pytest.raises(base.SyncError):
+ with pytest.raises(base.SyncError) as excinfo:
assert syncer.sync()
+ assert str(excinfo.value).startswith('rsync command syntax error:')
+ spawn.assert_called_once()
+ spawn.reset_mock()
+
+ # failed disk space sync
+ spawn.return_value = 11
+ with pytest.raises(base.SyncError) as excinfo:
+ assert syncer.sync()
+ assert str(excinfo.value) == 'rsync ran out of disk space'
spawn.assert_called_once()
spawn.reset_mock()
# retried sync
spawn.return_value = 99
- with pytest.raises(base.SyncError):
+ with pytest.raises(base.SyncError) as excinfo:
assert syncer.sync()
+ assert str(excinfo.value) == 'all attempts failed'
# rsync should retry every resolved IP related to the sync URI
- assert len(spawn.mock_calls) > 1
+ assert len(spawn.mock_calls) == len(fake_ips)
+ spawn.reset_mock()
+
+ # retried sync, more IPs than retries
+ fake_ips = ['.'.join(str(x) * 4) for x in range(10)]
+ getaddrinfo.return_value = fake_ips
+ with pytest.raises(base.SyncError) as excinfo:
+ assert syncer.sync()
+ assert str(excinfo.value) == 'all attempts failed'
+ assert len(fake_ips) > syncer.retries
+ assert len(spawn.mock_calls) == syncer.retries
+ spawn.reset_mock()
+
+ # failed DNS resolution
+ getaddrinfo.side_effect = OSError()
+ with pytest.raises(base.SyncError) as excinfo:
+ assert syncer.sync()
+ assert str(excinfo.value).startswith('DNS resolution failed')
+ spawn.assert_not_called()
+ getaddrinfo.reset_mock()
+ spawn.reset_mock()
@pytest.mark_network