Skip to content

Commit

Permalink
Merge pull request #117 from adfinis/fix-missing-snapshot
Browse files Browse the repository at this point in the history
fix: Don't fail if a to-be archived publish has missing snapshots
  • Loading branch information
Melkor333 authored Nov 7, 2024
2 parents 24c28fd + f8a3654 commit 99a8be4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyaptly/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def mirror_update(environment, config):

@pytest.fixture()
def snapshot_create(config, mirror_update, freeze):
"""Test if createing snapshots works."""
"""Test if creating snapshots works."""
args = ["-c", config, "snapshot", "create"]
main.main(args)
state = state_reader.SystemStateReader()
Expand Down
2 changes: 1 addition & 1 deletion pyaptly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def prepare(args):
import yaml

cfg = yaml.safe_load(f)
lg.warn(
lg.warning(
"NOTE: yaml has beed deprecated and will be remove on the next major release."
)
else:
Expand Down
23 changes: 15 additions & 8 deletions pyaptly/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ def publish_cmd_update(cfg, publish_name, publish_config, ignore_existing=False)
return cmd

publish_fullname = "%s %s" % (publish_name, publish_config["distribution"])
current_snapshots = state_reader.state_reader().publish_map()[publish_fullname]
# TODO: add flag --create to create publishes when they haven't been created yet
# TODO: Fail gracefully and show an error when there is no existing publish
try:
current_snapshots = state_reader.state_reader().publish_map()[publish_fullname]
except KeyError: # pragma: no cover
util.exit_with_error(f"The publish {publish_fullname} hasn't been created yet.")
if "snapshots" in publish_config:
snapshots_config = publish_config["snapshots"]
new_snapshots = [
Expand Down Expand Up @@ -129,13 +134,15 @@ def publish_cmd_update(cfg, publish_name, publish_config, ignore_existing=False)
continue
prefix_to_search = re.sub("%T$", "", snap["name"])

current_snapshot = [
snap_name
for snap_name in sorted(current_snapshots, key=lambda x: -len(x))
if snap_name.startswith(prefix_to_search)
][0]

snapshot.clone_snapshot(current_snapshot, archive).execute()
current_snapshot = None
for snap_name in sorted(current_snapshots, key=lambda x: -len(x)):
if snap_name.startswith(prefix_to_search):
current_snapshot = snap_name
break
if current_snapshot is None:
lg.warning("Snapshot %s doesn't exist on to-be archived publish %s." % (snap["name"], publish_fullname))
else:
snapshot.clone_snapshot(current_snapshot, archive).execute()

publish_cmd.append("switch")
options.append("-component=%s" % ",".join(components))
Expand Down
43 changes: 43 additions & 0 deletions pyaptly/tests/publish-bad.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[[publish.fakerepo01]]
gpg-key = "6D79A810B9B7ABAE"
skip-contents = true
automatic-update = true
components = "main"
distribution = "main"
snapshots = [
{ name = "fakerepo02-%T", timestamp = "current", archive-on-update = "archived-fakerepo01-%T" },
]

[[publish.fakerepo02]]
gpg-key = "6D79A810B9B7ABAE"
automatic-update = true
components = "main"
distribution = "main"
snapshots = [
{ name = "fakerepo01-%T", timestamp = "current", archive-on-update = "archived-fakerepo02-%T" },
]

[mirror.fakerepo01]
max-tries = 2
archive = "http://localhost:3123/fakerepo01"
gpg-keys = [
"2841988729C7F3FF",
]
components = "main"
distribution = "main"

[mirror.fakerepo02]
archive = "http://localhost:3123/fakerepo02"
gpg-keys = [
"2841988729C7F3FF",
]
components = "main"
distribution = "main"

[snapshot."fakerepo01-%T"]
mirror = "fakerepo01"
timestamp = { time = "00:00" }

[snapshot."fakerepo02-%T"]
mirror = "fakerepo02"
timestamp = { time = "00:00", repeat-weekly = "sat" }
27 changes: 26 additions & 1 deletion pyaptly/tests/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_publish_update_republish(config, publish_create_republish, freeze):


@pytest.mark.parametrize("config", ["publish.toml"], indirect=True)
def test_publish_updating_basic(config, publish_create, freeze):
def test_publish_update_basic(config, publish_create, freeze):
"""Test if updating publishes works."""
freeze.move_to("2012-10-11 10:10:10")
args = ["-c", config, "snapshot", "create"]
Expand Down Expand Up @@ -191,3 +191,28 @@ def test_repo_create_single(config, repo, test_key_03):
main.main(args)
state = state_reader.SystemStateReader()
assert set(["centrify"]) == state.repos()


@pytest.mark.parametrize("config", ["publish.toml"], indirect=True)
def test_publish_update_wrong_snapshots(config, publish_create, freeze):
"""Test if updating publishes works when the snapshot history is wrong."""
freeze.move_to("2012-10-11 10:10:10")
args = ["-c", config, "snapshot", "create"]
main.main(args)
args = ["-c", config.replace('.toml','-bad.toml'), "publish", "update"]
main.main(args)
state = state_reader.SystemStateReader()
expect = set(
[
"fakerepo01-20121011T0000Z",
"fakerepo02-20121006T0000Z",
"fakerepo01-20121010T0000Z",
]
)
assert expect == state.snapshots()
# Reversed!
expect2 = {
"fakerepo01 main": set(["fakerepo02-20121006T0000Z"]),
"fakerepo02 main": set(["fakerepo01-20121011T0000Z"]),
}
assert expect2 == state.publish_map()

0 comments on commit 99a8be4

Please sign in to comment.