Skip to content

Commit

Permalink
Merge pull request #47 from ganwell/t/test_mirror_create_5520
Browse files Browse the repository at this point in the history
test: convert test_mirror_create to new test
  • Loading branch information
Jean-Louis Fuchs authored Dec 26, 2023
2 parents 5b792fb + b89df7a commit 90565d0
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
7 changes: 6 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ The following files have just been copied without checking if their content is o
- CHANELOG.rst
- README.rst

The reason for this is to ensure the continuity of the git history.
The reason for this is to ensure the continuity of the git history.

# Tool to convert yaml config to toml

- This is probably just a sub-command of pyaptly
- We probably still want to switch to a modern yaml parser
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyaptly/aptly_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os

import freezegun
import pytest
import testfixtures

from pyaptly import (Command, SystemStateReader, call_output, main,
Expand Down Expand Up @@ -70,6 +71,7 @@ def test_pretend():
assert Command.pretend_mode


@pytest.mark.skip
def test_mirror_create():
"""Test if createing mirrors works."""
with test.clean_and_config(
Expand Down
51 changes: 51 additions & 0 deletions pyaptly/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
import os
import tempfile
from pathlib import Path

import pytest
import toml
import yaml

aptly_conf = Path.home().absolute() / ".aptly.conf"
test_base = Path(__file__).absolute().parent / "tests"


@pytest.fixture()
def environment():
tempdir_obj = tempfile.TemporaryDirectory()
tempdir = Path(tempdir_obj.name).absolute()

aptly = tempdir / "aptly"
aptly.mkdir(parents=True)
config = {"rootDir": str(aptly)}
if aptly_conf.exists():
aptly_conf.unlink()
with aptly_conf.open("w") as f:
json.dump(config, f)

gnupg = tempdir / "gnugp"
gnupg.mkdir(parents=True)
os.chown(gnupg, 0, 0)
gnupg.chmod(0o700)
os.environ["GNUPGHOME"] = str(gnupg)

try:
yield
finally:
tempdir_obj.cleanup()
aptly_conf.unlink()


@pytest.fixture()
def config(request):
config_file = test_base / request.param
with config_file.open("r", encoding="UTF-8") as f:
config = toml.load(f)
# TODO: remove yaml conversion
try:
with tempfile.NamedTemporaryFile(mode="w", encoding="UTF-8", delete=False) as f:
yaml.dump(config, f)
yield f.name, config
finally:
Path(f.name).unlink()
6 changes: 6 additions & 0 deletions pyaptly/tests/publish.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[mirror.fakerepo03]
archive = "http://localhost:3123/fakerepo03"
gpg-keys = ["EC54D33E5B5EBE98"]
gpg-urls = ["http://localhost:3123/keys/test02.key"]
components = "main"
distribution = "main"
28 changes: 28 additions & 0 deletions pyaptly/tests/test_mirror.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import logging

import pytest

import pyaptly


@pytest.mark.parametrize("config", ["publish.toml"], indirect=True)
def test_mirror_create(environment, config, caplog):
"""Test if creating mirrors works."""
config_file, config_dict = config

caplog.set_level(logging.DEBUG)
pyaptly.main(["-c", config_file, "mirror", "create"])

keys_added = []
for rec in caplog.records:
for arg in rec.args:
if isinstance(arg, list):
if arg[0] == "gpg":
keys_added.append(arg[7])
assert len(keys_added) > 0
assert len(keys_added) == len(set(keys_added)), "Key multiple times added"

expect = set(config_dict["mirror"].keys())
state = pyaptly.SystemStateReader()
state.read()
assert state.mirrors == expect
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python = "^3.11"
pretty-dump = {git = "https://github.com/adfinis/freeze"}
pytz = "^2023.3.post1"
pyyaml = "^6.0.1"
toml = "^0.10.2"

[tool.poetry.group.dev.dependencies]
freezegun = "^1.2.2"
Expand Down

0 comments on commit 90565d0

Please sign in to comment.