Skip to content

Commit

Permalink
Pytest Github Action (#1)
Browse files Browse the repository at this point in the history
Run pytest on PR/push.

Fix main run test.

Add coverage/license/build pass badges to README

Create pytest comment on PR

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
p-rintz and github-actions[bot] authored Feb 27, 2023
1 parent c3c766d commit 4010f15
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python_version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python_version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
with:
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/pytest-pull.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: pytest-coverage-comment
on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python_version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3

- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python_version }}

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
if [ -f requirements_dev.txt ]; then pip3 install -r requirements_dev.txt; fi
- name: Build coverage file
run: |
pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered | tee pytest-coverage.txt
- name: Pytest coverage comment
uses: MishaKav/pytest-coverage-comment@main
with:
pytest-coverage-path: ./pytest-coverage.txt
junitxml-path: ./pytest.xml
43 changes: 43 additions & 0 deletions .github/workflows/pytest-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# From: https://github.com/marketplace/actions/pytest-coverage-comment
name: Update Coverage in Readme
on:
push:
jobs:
update-coverage-on-readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
fetch-depth: 0 # otherwise, you will failed to push refs to dest repo

- uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
if [ -f requirements_dev.txt ]; then pip3 install -r requirements_dev.txt; fi
- name: Build coverage file
run: |
pytest --junitxml=pytest.xml --cov | tee data/pytest-coverage.txt
- name: Pytest coverage comment
id: coverageComment
uses: MishaKav/pytest-coverage-comment@main
with:
hide-comment: true
pytest-coverage-path: ./data/pytest-coverage.txt

- name: Update Readme with Coverage Html
run: |
sed -i '/<!-- Pytest Coverage Comment:Begin -->/,/<!-- Pytest Coverage Comment:End -->/c\![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/p-rintz/netbox-dnsmasq/pytest-push.yml)![GitHub](https://img.shields.io/github/license/p-rintz/netbox-dnsmasq?color=brightgreen&style=flat)<!-- Pytest Coverage Comment:Begin -->\n\${{ steps.coverageComment.outputs.coverageHtml }}\n<!-- Pytest Coverage Comment:End -->' ./README.md
- name: Commit & Push changes to Readme
uses: actions-js/push@master
with:
message: Update coverage on Readme
branch: ${{ github.ref_name }}
github_token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ dhcphosts.conf
dnsmasq.hosts
/Dockerfile-* # Test Dockerfiles
.coverage

__pycache__
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Netbox-dnsmasq
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/p-rintz/netbox-dnsmasq/pytest-push.yml)![GitHub](https://img.shields.io/github/license/p-rintz/netbox-dnsmasq?color=brightgreen&style=flat)<!-- Pytest Coverage Comment:Begin -->
<a href="https://github.com/p-rintz/netbox-dnsmasq/blob/main/README.md"><img alt="Coverage" src="https://img.shields.io/badge/Coverage-97%25-brightgreen.svg" /></a><details><summary>Coverage Report </summary><table><tr><th>File</th><th>Stmts</th><th>Miss</th><th>Cover</th><th>Missing</th></tr><tbody><tr><td><a href="https://github.com/p-rintz/netbox-dnsmasq/blob/main/netbox_dnsmasq.py">netbox_dnsmasq.py</a></td><td>180</td><td>8</td><td>8</td><td><a href="https://github.com/p-rintz/netbox-dnsmasq/blob/main/netbox_dnsmasq.py#L 96%"> 96%</a></td></tr><tr><td colspan="5"><b>tests</b></td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/p-rintz/netbox-dnsmasq/blob/main/tests/__init__.py">__init__.py</a></td><td>0</td><td>0</td><td>100%</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/p-rintz/netbox-dnsmasq/blob/main/tests/conftest.py">conftest.py</a></td><td>29</td><td>0</td><td>100%</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/p-rintz/netbox-dnsmasq/blob/main/tests/test_netbox-dnsmasq.py">test_netbox-dnsmasq.py</a></td><td>76</td><td>0</td><td>100%</td><td>&nbsp;</td></tr><tr><td><b>TOTAL</b></td><td><b>285</b></td><td><b>8</b></td><td><b>97%</b></td><td>&nbsp;</td></tr></tbody></table></details>
<!-- Pytest Coverage Comment:End -->

Create dnsmasq DHCP/DNS configs using the Netbox API.

## Configuration
Expand All @@ -10,7 +14,7 @@ python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
**Python 3.7 or higher is required.**
**Python 3.8 or higher is required.**

### Config locations

Expand Down
1 change: 1 addition & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

21 changes: 21 additions & 0 deletions data/pytest-coverage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
============================= test session starts ==============================
platform linux -- Python 3.11.2, pytest-7.2.1, pluggy-1.0.0
rootdir: /home/runner/work/netbox-dnsmasq/netbox-dnsmasq
plugins: cov-4.0.0, recording-0.12.2
collected 14 items

tests/test_netbox-dnsmasq.py .............. [100%]

- generated xml file: /home/runner/work/netbox-dnsmasq/netbox-dnsmasq/pytest.xml -

---------- coverage: platform linux, python 3.11.2-final-0 -----------
Name Stmts Miss Cover
--------------------------------------------------
netbox_dnsmasq.py 180 8 96%
tests/__init__.py 0 0 100%
tests/conftest.py 29 0 100%
tests/test_netbox-dnsmasq.py 76 0 100%
--------------------------------------------------
TOTAL 285 8 97%

============================== 14 passed in 1.36s ==============================
1 change: 1 addition & 0 deletions pytest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="14" time="1.379" timestamp="2023-02-27T04:10:58.426150" hostname="fv-az399-857"><testcase classname="tests.test_netbox-dnsmasq" name="test_format_dns" time="0.014" /><testcase classname="tests.test_netbox-dnsmasq" name="test_format_dns_merged" time="0.012" /><testcase classname="tests.test_netbox-dnsmasq" name="test_format_dhcp" time="0.013" /><testcase classname="tests.test_netbox-dnsmasq" name="test_write_config" time="0.014" /><testcase classname="tests.test_netbox-dnsmasq" name="test_write_config_denied" time="0.013" /><testcase classname="tests.test_netbox-dnsmasq" name="test_get_ip_data" time="0.190" /><testcase classname="tests.test_netbox-dnsmasq" name="test_check_duplicates" time="0.013" /><testcase classname="tests.test_netbox-dnsmasq" name="test_check_duplicates_no_remove" time="0.013" /><testcase classname="tests.test_netbox-dnsmasq" name="test_import_config[True]" time="0.012" /><testcase classname="tests.test_netbox-dnsmasq" name="test_import_config[False]" time="0.012" /><testcase classname="tests.test_netbox-dnsmasq" name="test_import_config_missing_env" time="0.012" /><testcase classname="tests.test_netbox-dnsmasq" name="test_main_help[-h]" time="0.015" /><testcase classname="tests.test_netbox-dnsmasq" name="test_main_help[--help]" time="0.013" /><testcase classname="tests.test_netbox-dnsmasq" name="test_main_run" time="0.190" /></testsuite></testsuites>
8 changes: 6 additions & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
pytest~=7.2.1
coverage==7.2.1
pytest-cov~=4.0.0
pynetbox~=7.0.1
vcrpy~=4.2.1
pytest-recording~=0.12.2
loguru~=0.6.0
loguru~=0.6.0
flake8~=6.0.0
black~=23.1.0
isort~=5.12.0
mypy~=1.0.1
2 changes: 2 additions & 0 deletions tests/test_netbox-dnsmasq.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ def test_main_help(capsys, option):
)


@pytest.mark.default_cassette("test_get_ip_data.yaml")
@pytest.mark.vcr
def test_main_run(capsys, set_test_environment):
"""
Test main function for correctly starting the script.
Expand Down

0 comments on commit 4010f15

Please sign in to comment.