Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Aug 13, 2023
1 parent 04de0d6 commit 61ae717
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 154 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[report]
omit =
*/tests/*
25 changes: 19 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ jobs:
run: |
make flake8
continue-on-error: true
- name: pylint
run: |
make pylint
- name: pytest
run: |
make pytest
- name: mypy
run: |
make mypy
continue-on-error: true
- name: pylint
run: |
make pylint
e2e:
runs-on: ubuntu-latest
steps:
Expand All @@ -41,6 +39,21 @@ jobs:
- name: e2e
run: |
make e2e
codecov:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: pip install -r requirements-test.txt
- name: Run tests and collect coverage
run: make pytest
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
shellcheck:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.venv/
*.pyc
.coverage
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FILES=*.py
FILES=*.py tests/*.py

.PHONY: all
all: flake8 pylint pytest mypy
Expand All @@ -13,11 +13,11 @@ pylint:

.PHONY: pytest
pytest:
@pytest -v
@pytest --capture=sys -v --cov --cov-report term-missing

.PHONY: mypy
mypy:
@mypy .
@mypy $(FILES)

.PHONY: e2e
e2e:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
![Build Status](https://github.com/ricardobranco777/clean_registry/actions/workflows/ci.yml/badge.svg)

[![codecov](https://codecov.io/gh/ricardobranco777/clean_registry/branch/master/graph/badge.svg)](https://codecov.io/gh/ricardobranco777/clean_registry)

# clean_registry

Docker Registry cleanup and image removal tool.
Expand Down
28 changes: 14 additions & 14 deletions clean_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def run_command(command: list) -> int:
return proc.wait()


def clean_registry(images: list[str], dry_run: bool = False) -> None:
def clean_registrydir(images: list[str], dry_run: bool = False) -> None:
'''Clean registry'''
registry_dir = os.environ.get("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/var/lib/registry")
logging.debug("registry directory: %s", registry_dir)
Expand Down Expand Up @@ -114,7 +114,13 @@ def clean_repo(basedir: str, image: str, dry_run: bool = False) -> None:
clean_tag(basedir, repo, tag, dry_run)


def parse_args():
def get_os_release():
'''Get OS release'''
with open("/etc/os-release", encoding="utf-8") as file:
return {k: v.strip('"') for k, v in [line.split('=') for line in file.read().splitlines()]}


def parse_args(): # pragma: no cover
"""Parse args"""
parser = ArgumentParser()
parser.add_argument(
Expand All @@ -131,24 +137,18 @@ def parse_args():
return parser.parse_args()


def print_versions():
'''Print useful information for debugging'''
print(f'{os.path.basename(sys.argv[0])} {VERSION}')
print(f'Python {sys.version}')
print(subprocess.check_output(shlex.split("/bin/registry --version")).decode("utf-8").strip())
with open("/etc/os-release", encoding="utf-8") as file:
osrel = {k: v.strip('"') for k, v in [line.split('=') for line in file.read().splitlines()]}
print(osrel['NAME'], osrel['VERSION_ID'])


def main():
'''Main function'''
if not is_container() or not os.path.isfile("/bin/registry"):
sys.exit("ERROR: This script should run inside a registry:2 container!")

args = parse_args()
if args.version:
print_versions()
print(f'{os.path.basename(sys.argv[0])} {VERSION}')
print(f'Python {sys.version}')
print(subprocess.check_output(shlex.split("/bin/registry --version")).decode("utf-8").strip())
osrel = get_os_release()
print(osrel['NAME'], osrel['VERSION_ID'])
sys.exit(0)

for image in args.images:
Expand All @@ -158,7 +158,7 @@ def main():
fmt = "%(asctime)s %(levelname)-8s %(message)s"
logging.basicConfig(format=fmt, stream=sys.stderr, level=args.log.upper())

clean_registry(images=args.images, dry_run=args.dry_run)
clean_registrydir(images=args.images, dry_run=args.dry_run)


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
flake8
pylint
pytest
pytest-cov
pytest-mock
mypy
131 changes: 0 additions & 131 deletions tests/clean_registry_test.py

This file was deleted.

Loading

0 comments on commit 61ae717

Please sign in to comment.