Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return code for backy backup #67

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/20240607_123138_jb_fix_backup_rc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. A new scriv changelog fragment.

- Fixed the return code for `backy backup` which could have caused an infinite loop in the scheduler
3 changes: 2 additions & 1 deletion src/backy/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def tags(

@locked(target=".backup", mode="exclusive")
@locked(target=".purge", mode="shared")
def backup(self, tags: set[str], force: bool = False) -> None:
def backup(self, tags: set[str], force: bool = False) -> bool:
if not force:
self.validate_tags(tags)

Expand Down Expand Up @@ -381,6 +381,7 @@ def backup(self, tags: set[str], force: bool = False) -> None:
self.log.warning("inconsistent")
revision.backend.verify()
break
return verified

@locked(target=".backup", mode="exclusive")
def distrust(self, revision: str) -> None:
Expand Down
8 changes: 5 additions & 3 deletions src/backy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,18 @@ def status(self, yaml_: bool, revision: str) -> None:
f"[yellow]{pending_changes} pending change(s)[/] (Push changes with `backy push`)"
)

def backup(self, tags: str, force: bool) -> None:
def backup(self, tags: str, force: bool) -> int:
b = Backup(self.path, self.log)
b._clean()
try:
tags_ = set(t.strip() for t in tags.split(","))
b.backup(tags_, force)
success = b.backup(tags_, force)
return int(not success)
except IOError as e:
if e.errno not in [errno.EDEADLK, errno.EAGAIN]:
raise
self.log.info("backup-already-running")
self.log.warning("backup-already-running")
return 1
finally:
b._clean()

Expand Down
21 changes: 14 additions & 7 deletions src/backy/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import pprint
import sys
from functools import partialmethod

import pytest

Expand Down Expand Up @@ -110,9 +111,10 @@ def test_verbose_logging(capsys, argv):
assert exit.value.code == 0


def print_args(*args, **kw):
def print_args(*args, return_value=None, **kw):
print(args)
pprint.pprint(kw)
return return_value


async def async_print_args(*args, **kw):
Expand Down Expand Up @@ -148,7 +150,8 @@ def test_call_status(capsys, backup, argv, monkeypatch):
)


def test_call_backup(tmp_path, capsys, argv, monkeypatch):
@pytest.mark.parametrize("success", [False, True])
def test_call_backup(success, tmp_path, capsys, argv, monkeypatch):
os.makedirs(tmp_path / "backy")
os.chdir(tmp_path / "backy")

Expand All @@ -170,7 +173,11 @@ def test_call_backup(tmp_path, capsys, argv, monkeypatch):
)
)

monkeypatch.setattr(backy.backup.Backup, "backup", print_args)
monkeypatch.setattr(
backy.backup.Backup,
"backup",
partialmethod(print_args, return_value=success),
)
argv.extend(["-v", "backup", "manual:test"])
utils.log_data = ""
with pytest.raises(SystemExit) as exit:
Expand All @@ -187,16 +194,16 @@ def test_call_backup(tmp_path, capsys, argv, monkeypatch):
)
assert (
Ellipsis(
"""\
f"""\
... D command/invoked args='... -v backup manual:test'
... D command/parsed func='backup' func_args={'force': False, 'tags': 'manual:test'}
... D command/parsed func='backup' func_args={{'force': False, 'tags': 'manual:test'}}
... D quarantine/scan entries=0
... D command/successful \n\
... D command/return-code code={int(not success)}
"""
)
== utils.log_data
)
assert exit.value.code == 0
assert exit.value.code == int(not success)


def test_call_find(capsys, backup, argv, monkeypatch):
Expand Down
Loading