Skip to content

Commit

Permalink
Better handle errors for individual snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
d0ugal committed Oct 5, 2018
1 parent f486e8f commit b50f58a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions dropbox-upload/dropbox_upload/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
def main(config_file, sleeper=time.sleep, DropboxAPI=dropbox.Dropbox):

cfg = config.load_config(config_file)
copy = cfg.copy()
copy["access_token"] = "HIDDEN"
LOG.debug(copy)
config.setup_logging(cfg)

try:
Expand Down
11 changes: 10 additions & 1 deletion dropbox-upload/dropbox_upload/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ def backup(dbx, config, snapshots):

for i, snapshot in enumerate(snapshots, start=1):
LOG.info(f"Snapshot: {snapshot['name']} ({i}/{len(snapshots)})")
process_snapshot(config, dbx, snapshot)
try:
process_snapshot(config, dbx, snapshot)
except Exception:
LOG.exception(
"Snapshot backup failed. If this happens after the addon is "
"restarted, please open a bug."
)


def process_snapshot(config, dbx, snapshot):
path = local_path(snapshot)
created = arrow.get(snapshot["date"])
if not os.path.isfile(path):
LOG.warning("The snapshot no longer exists")
return
size = util.bytes_to_human(os.path.getsize(path))
target = str(dropbox_path(config, snapshot))
LOG.info(f"Slug: {snapshot['slug']}")
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def cfg():
}


@pytest.fixture
def snapshot(requests_mock):
return {
"slug": "dbaa2add",
"name": "Automated Backup 2018-09-14",
"date": "2018-09-14T01:00:00.873481+00:00",
"type": "full",
}


@pytest.fixture
def snapshots(requests_mock):

Expand Down Expand Up @@ -46,4 +56,7 @@ def users_get_current_account(self):
def files_delete(self, path):
pass

def files_upload(self, f, path):
pass

return DropboxAPI
37 changes: 37 additions & 0 deletions tests/test_backup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import logging
import pathlib
from unittest import mock

from dropbox_upload import backup

Expand Down Expand Up @@ -39,3 +40,39 @@ def test_dropbox_path_date_filenames(cfg):
}
result = backup.dropbox_path(cfg, snapshot)
assert result == expected


def test_snapshot_deleted(cfg, snapshot, caplog):
backup.process_snapshot(cfg, None, snapshot)
assert (
"dropbox_upload.backup",
logging.WARNING,
"The snapshot no longer exists",
) in caplog.record_tuples


def test_backup_keep_limit(cfg, dropbox_fake, snapshots, caplog):
caplog.set_level(logging.DEBUG)
cfg["keep"] = 2
with mock.patch("dropbox_upload.backup.local_path") as local_path:
local_path.return_value = __file__
backup.backup(dropbox_fake(), cfg, snapshots)
assert (
"dropbox_upload.backup",
logging.INFO,
"Only backing up the first 2 snapshots",
) in caplog.record_tuples


def test_backup_file_exists(cfg, dropbox_fake, snapshot, caplog):
caplog.set_level(logging.DEBUG)
with mock.patch("dropbox_upload.dropbox.file_exists") as file_exists:
with mock.patch("dropbox_upload.backup.local_path") as local_path:
local_path.return_value = __file__
file_exists.return_value = True
backup.backup(dropbox_fake(), cfg, [snapshot])
assert (
"dropbox_upload.backup",
logging.INFO,
"Already found in Dropbox with the same hash",
) in caplog.record_tuples
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ deps =
-rtest-requirements.txt
commands=
pytest -v --basetemp={envtmpdir} \
--cov=dropbox_upload --cov-fail-under=70 {posargs}
--cov=dropbox_upload --cov-fail-under=85 --cov-report=term-missing {posargs}

[testenv:format]
basepython=python3
Expand Down

0 comments on commit b50f58a

Please sign in to comment.