Skip to content

Commit

Permalink
Add Python tests and CI wofklow
Browse files Browse the repository at this point in the history
Add some basic tests with pytest. The functions that require Docker Hub
access aren't currently tested.

A GitHub workflow is included to run the tests on push.
  • Loading branch information
joshbeard committed Oct 7, 2022
1 parent acb2cb7 commit eed790b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Validation

on: [push]

jobs:
build:
name: Python tests
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]

steps:
- uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: pip install -r requirements-dev.txt -r requirements.txt

- name: Run pytest
run: pytest -v tests/test.py

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Docker Hub Image Tag Deleter

[![Validation](https://github.com/joshbeard/docker-hub-tag-delete/actions/workflows/validation.yml/badge.svg)](https://github.com/joshbeard/docker-hub-tag-delete/actions/workflows/validation.yml)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/25f78736c6fb48db96bde0f04bda7029)](https://www.codacy.com/gh/joshbeard/docker-hub-tag-delete/dashboard?utm_source=github.com&utm_medium=referral&utm_content=joshbeard/docker-hub-tag-delete&utm_campaign=Badge_Grade)
[![CodeFactor](https://www.codefactor.io/repository/github/joshbeard/docker-hub-tag-delete/badge)](https://www.codefactor.io/repository/github/joshbeard/docker-hub-tag-delete)
[![DeepSource](https://deepsource.io/gh/joshbeard/docker-hub-tag-delete.svg/?label=active+issues&show_trend=true&token=JBOrbjcsB0m6ImmQ5Sl2MMve)](https://deepsource.io/gh/joshbeard/docker-hub-tag-delete/?ref=repository-badge)
Expand Down Expand Up @@ -233,6 +234,7 @@ To activate the Python virtual environment in the container:
* Improve Markdown parsing and customization (v1)
* Improve output (v1)
* List on Marketplace once (v1) items are completed
* Improve [tests](tests/)
* CLI arguments in addition to existing env vars?
* GitLab registry support
* Build and publish image to Docker Hub (of this tool)
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest>=7.0.0
6 changes: 6 additions & 0 deletions tests/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{ "tags": ["4", "4.*"], "date": "April 1, 2022" },
{ "tags": ["5", "5.*"], "date": "June 10, 2023" },
{ "tags": ["json-foobar"], "date": "October 31, 2023" }
]

9 changes: 9 additions & 0 deletions tests/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- BEGIN deletion_table -->
| Tag | Deletion Date
| ---------- | ----------------------
| `1*` | October 5, 2022
| `2.*` | October 5, 2022
| `3.*` | October 5, 2040
| `foobar` | December 25, 2021
<!-- END deletion_table -->

74 changes: 74 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from datetime import datetime
import os,sys,inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)

os.environ['MARKDOWN_FILE'] = "tests/test.md"
os.environ['JSON_FILE'] = "tests/test.json"
os.environ['DOCKERHUB_REPOSITORY'] = "foo/bar"

target = __import__("hub-tag-delete")

def test_line_is_ignored():
assert target.line_is_ignored('| Tag') is True
assert target.line_is_ignored('| xTag') is False
assert target.line_is_ignored('| 1.0') is False
assert target.line_is_ignored('| `1.0`') is False

def test_get_readme_table():
taglist = [
{'date': 'October 5, 2022', 'tags': ['1*']},
{'date': 'October 5, 2022', 'tags': ['2.*']},
{'date': 'October 5, 2040', 'tags': ['3.*']},
{'date': 'December 25, 2021', 'tags': ['foobar']}
]
assert target.get_readme_table() == taglist

def test_parse_date():
date = "October 6, 2022"
assert target.parse_date(date) == datetime(2022, 10, 6, 0, 0)

def test_parse_md_line():
mdline = '| `1.*`, `2.0` | October 6, 2022'
result = {'tags': ['1.*',' 2.0'], 'date': 'October 6, 2022'}
assert target.parse_md_line(mdline) == result

def test_json_tags():
result = [
{'date': 'April 1, 2022', 'tags': ['4', '4.*']},
{'date': 'June 10, 2023', 'tags': ['5', '5.*']},
{'date': 'October 31, 2023', 'tags': ['json-foobar']}
]
assert target.json_tags() == result

def test_get_tag_list():
taglist = [
{'date': 'April 1, 2022', 'tags': ['4', '4.*']},
{'date': 'June 10, 2023', 'tags': ['5', '5.*']},
{'date': 'October 31, 2023', 'tags': ['json-foobar']},
{'date': 'October 5, 2022', 'tags': ['1*']},
{'date': 'October 5, 2022', 'tags': ['2.*']},
{'date': 'October 5, 2040', 'tags': ['3.*']},
{'date': 'December 25, 2021', 'tags': ['foobar']}
]
assert target.get_tag_list() == taglist

# TODO: The tests that require Docker Hub connectivity are pending
def test_tags_to_delete():
"""Docker Hub required"""
pass

def test_delete_expired_tags():
"""Docker Hub required"""
pass

def test_docker_hub_token():
"""Docker Hub required"""
pass

def test_tags_matching_pattern():
"""Docker Hub required"""
pass

0 comments on commit eed790b

Please sign in to comment.