diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..4ead663 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,3 @@ +[run] +relative_files = True +omit = tests/* diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..4c13605 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,57 @@ +name: Run Unit Test via Pytest + +on: + pull_request: + branches: [ master ] + +jobs: + lint-and-test: + runs-on: ubuntu-latest + permissions: + pull-requests: write + contents: write + checks: write + strategy: + matrix: + python-version: ["3.11"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Setup PDM + uses: pdm-project/setup-pdm@v3 + - name: Install dependencies + run: pdm install + - name: Lint with black + uses: psf/black@stable + with: + options: "--check --verbose" + - name: Test with pytest + run: | + pdm run pytest --junit-xml=test-results.xml + - name: Publish Test Results + uses: EnricoMi/publish-unit-test-result-action@v2 + if: always() + with: + files: | + test-results.xml + - name: Test with coverage + run: | + pdm run coverage run -m pytest -v -s + - name: Generate Coverage Report + run: | + pdm run coverage report -m + - name: Coverage comment + id: coverage_comment + uses: py-cov-action/python-coverage-comment-action@v3 + with: + GITHUB_TOKEN: ${{ github.token }} + - name: Store Pull Request comment to be posted + uses: actions/upload-artifact@v3 + if: steps.coverage_comment.outputs.COMMENT_FILE_WRITTEN == 'true' + with: + name: python-coverage-comment-action + path: python-coverage-comment-action.txt diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..a63f4e4 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,29 @@ +repos: + - repo: local + hooks: + - id: black + name: black + stages: [commit] + types: [python] + entry: pdm run black . + language: system + pass_filenames: false + always_run: true + - id: ruff + name: ruff + stages: [commit] + types: [python] + entry: pdm run ruff . + language: system + pass_filenames: false + always_run: true + fail_fast: true + - id: pytest + name: pytest + stages: [commit] + types: [python] + entry: pdm run pytest + language: system + pass_filenames: false + always_run: true + fail_fast: true diff --git a/pdm.lock b/pdm.lock index c97ef6b..62b32fe 100644 --- a/pdm.lock +++ b/pdm.lock @@ -2,11 +2,31 @@ # It is not intended for manual editing. [metadata] -groups = ["default"] -cross_platform = true -static_urls = false -lock_version = "4.3" -content_hash = "sha256:ec00e4f386c7e3cac870ab101184e565ee290c82a8b3d759fb7ad2762d0366ba" +groups = ["default", "dev"] +strategy = ["cross_platform"] +lock_version = "4.4" +content_hash = "sha256:474c773ee86217d652b5fc82f921ce2fbb513b244d50fbe82b74ea9e47dad96e" + +[[package]] +name = "black" +version = "23.11.0" +requires_python = ">=3.8" +summary = "The uncompromising code formatter." +dependencies = [ + "click>=8.0.0", + "mypy-extensions>=0.4.3", + "packaging>=22.0", + "pathspec>=0.9.0", + "platformdirs>=2", +] +files = [ + {file = "black-23.11.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:cf57719e581cfd48c4efe28543fea3d139c6b6f1238b3f0102a9c73992cbb479"}, + {file = "black-23.11.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:698c1e0d5c43354ec5d6f4d914d0d553a9ada56c85415700b81dc90125aac244"}, + {file = "black-23.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:760415ccc20f9e8747084169110ef75d545f3b0932ee21368f63ac0fee86b221"}, + {file = "black-23.11.0-cp311-cp311-win_amd64.whl", hash = "sha256:58e5f4d08a205b11800332920e285bd25e1a75c54953e05502052738fe16b3b5"}, + {file = "black-23.11.0-py3-none-any.whl", hash = "sha256:54caaa703227c6e0c87b76326d0862184729a69b73d3b7305b6288e1d830067e"}, + {file = "black-23.11.0.tar.gz", hash = "sha256:4c68855825ff432d197229846f971bc4d6666ce90492e5b02013bcaca4d9ab05"}, +] [[package]] name = "certifi" @@ -43,6 +63,59 @@ files = [ {file = "charset_normalizer-3.2.0-py3-none-any.whl", hash = "sha256:8e098148dd37b4ce3baca71fb394c81dc5d9c7728c95df695d2dca218edf40e6"}, ] +[[package]] +name = "click" +version = "8.1.7" +requires_python = ">=3.7" +summary = "Composable command line interface toolkit" +dependencies = [ + "colorama; platform_system == \"Windows\"", +] +files = [ + {file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"}, + {file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"}, +] + +[[package]] +name = "colorama" +version = "0.4.6" +requires_python = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +summary = "Cross-platform colored terminal text." +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "coverage" +version = "7.3.2" +requires_python = ">=3.8" +summary = "Code coverage measurement for Python" +files = [ + {file = "coverage-7.3.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:ac8c802fa29843a72d32ec56d0ca792ad15a302b28ca6203389afe21f8fa062c"}, + {file = "coverage-7.3.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:89a937174104339e3a3ffcf9f446c00e3a806c28b1841c63edb2b369310fd074"}, + {file = "coverage-7.3.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e267e9e2b574a176ddb983399dec325a80dbe161f1a32715c780b5d14b5f583a"}, + {file = "coverage-7.3.2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:2443cbda35df0d35dcfb9bf8f3c02c57c1d6111169e3c85fc1fcc05e0c9f39a3"}, + {file = "coverage-7.3.2-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4175e10cc8dda0265653e8714b3174430b07c1dca8957f4966cbd6c2b1b8065a"}, + {file = "coverage-7.3.2-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:0cbf38419fb1a347aaf63481c00f0bdc86889d9fbf3f25109cf96c26b403fda1"}, + {file = "coverage-7.3.2-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:5c913b556a116b8d5f6ef834038ba983834d887d82187c8f73dec21049abd65c"}, + {file = "coverage-7.3.2-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:1981f785239e4e39e6444c63a98da3a1db8e971cb9ceb50a945ba6296b43f312"}, + {file = "coverage-7.3.2-cp311-cp311-win32.whl", hash = "sha256:43668cabd5ca8258f5954f27a3aaf78757e6acf13c17604d89648ecc0cc66640"}, + {file = "coverage-7.3.2-cp311-cp311-win_amd64.whl", hash = "sha256:e10c39c0452bf6e694511c901426d6b5ac005acc0f78ff265dbe36bf81f808a2"}, + {file = "coverage-7.3.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:4cbae1051ab791debecc4a5dcc4a1ff45fc27b91b9aee165c8a27514dd160836"}, + {file = "coverage-7.3.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:12d15ab5833a997716d76f2ac1e4b4d536814fc213c85ca72756c19e5a6b3d63"}, + {file = "coverage-7.3.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3c7bba973ebee5e56fe9251300c00f1579652587a9f4a5ed8404b15a0471f216"}, + {file = "coverage-7.3.2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:fe494faa90ce6381770746077243231e0b83ff3f17069d748f645617cefe19d4"}, + {file = "coverage-7.3.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f6e9589bd04d0461a417562649522575d8752904d35c12907d8c9dfeba588faf"}, + {file = "coverage-7.3.2-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:d51ac2a26f71da1b57f2dc81d0e108b6ab177e7d30e774db90675467c847bbdf"}, + {file = "coverage-7.3.2-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:99b89d9f76070237975b315b3d5f4d6956ae354a4c92ac2388a5695516e47c84"}, + {file = "coverage-7.3.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:fa28e909776dc69efb6ed975a63691bc8172b64ff357e663a1bb06ff3c9b589a"}, + {file = "coverage-7.3.2-cp312-cp312-win32.whl", hash = "sha256:289fe43bf45a575e3ab10b26d7b6f2ddb9ee2dba447499f5401cfb5ecb8196bb"}, + {file = "coverage-7.3.2-cp312-cp312-win_amd64.whl", hash = "sha256:7dbc3ed60e8659bc59b6b304b43ff9c3ed858da2839c78b804973f613d3e92ed"}, + {file = "coverage-7.3.2-pp38.pp39.pp310-none-any.whl", hash = "sha256:ae97af89f0fbf373400970c0a21eef5aa941ffeed90aee43650b81f7d7f47637"}, + {file = "coverage-7.3.2.tar.gz", hash = "sha256:be32ad29341b0170e795ca590e1c07e81fc061cb5b10c74ce7203491484404ef"}, +] + [[package]] name = "idna" version = "3.4" @@ -53,6 +126,36 @@ files = [ {file = "idna-3.4.tar.gz", hash = "sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4"}, ] +[[package]] +name = "iniconfig" +version = "2.0.0" +requires_python = ">=3.7" +summary = "brain-dead simple config-ini parsing" +files = [ + {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"}, + {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, +] + +[[package]] +name = "isort" +version = "5.12.0" +requires_python = ">=3.8.0" +summary = "A Python utility / library to sort Python imports." +files = [ + {file = "isort-5.12.0-py3-none-any.whl", hash = "sha256:f84c2818376e66cf843d497486ea8fed8700b340f308f076c6fb1229dff318b6"}, + {file = "isort-5.12.0.tar.gz", hash = "sha256:8bef7dde241278824a6d83f44a544709b065191b95b6e50894bdc722fcba0504"}, +] + +[[package]] +name = "mypy-extensions" +version = "1.0.0" +requires_python = ">=3.5" +summary = "Type system extensions for programs checked with the mypy type checker." +files = [ + {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"}, + {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, +] + [[package]] name = "packaging" version = "23.1" @@ -63,6 +166,36 @@ files = [ {file = "packaging-23.1.tar.gz", hash = "sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f"}, ] +[[package]] +name = "pathspec" +version = "0.11.2" +requires_python = ">=3.7" +summary = "Utility library for gitignore style pattern matching of file paths." +files = [ + {file = "pathspec-0.11.2-py3-none-any.whl", hash = "sha256:1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20"}, + {file = "pathspec-0.11.2.tar.gz", hash = "sha256:e0d8d0ac2f12da61956eb2306b69f9469b42f4deb0f3cb6ed47b9cce9996ced3"}, +] + +[[package]] +name = "platformdirs" +version = "4.0.0" +requires_python = ">=3.7" +summary = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." +files = [ + {file = "platformdirs-4.0.0-py3-none-any.whl", hash = "sha256:118c954d7e949b35437270383a3f2531e99dd93cf7ce4dc8340d3356d30f173b"}, + {file = "platformdirs-4.0.0.tar.gz", hash = "sha256:cb633b2bcf10c51af60beb0ab06d2f1d69064b43abf4c185ca6b28865f3f9731"}, +] + +[[package]] +name = "pluggy" +version = "1.3.0" +requires_python = ">=3.8" +summary = "plugin and hook calling mechanisms for python" +files = [ + {file = "pluggy-1.3.0-py3-none-any.whl", hash = "sha256:d89c696a773f8bd377d18e5ecda92b7a3793cbe66c87060a6fb58c7b6e1061f7"}, + {file = "pluggy-1.3.0.tar.gz", hash = "sha256:cf61ae8f126ac6f7c451172cf30e3e43d3ca77615509771b3a984a0730651e12"}, +] + [[package]] name = "prometheus-client" version = "0.17.1" @@ -73,6 +206,22 @@ files = [ {file = "prometheus_client-0.17.1.tar.gz", hash = "sha256:21e674f39831ae3f8acde238afd9a27a37d0d2fb5a28ea094f0ce25d2cbf2091"}, ] +[[package]] +name = "pytest" +version = "7.4.3" +requires_python = ">=3.7" +summary = "pytest: simple powerful testing with Python" +dependencies = [ + "colorama; sys_platform == \"win32\"", + "iniconfig", + "packaging", + "pluggy<2.0,>=0.12", +] +files = [ + {file = "pytest-7.4.3-py3-none-any.whl", hash = "sha256:0d009c083ea859a71b76adf7c1d502e4bc170b80a8ef002da5806527b9591fac"}, + {file = "pytest-7.4.3.tar.gz", hash = "sha256:d989d136982de4e3b29dabcc838ad581c64e8ed52c11fbe86ddebd9da0818cd5"}, +] + [[package]] name = "python-json-logger" version = "2.0.7" diff --git a/pyproject.toml b/pyproject.toml index eb5d056..8f79963 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,3 +26,11 @@ qbittorrent-exporter = "qbittorrent_exporter.exporter:main" [build-system] requires = ["pdm-backend"] build-backend = "pdm.backend" + +[tool.pdm.dev-dependencies] +dev = [ + "pytest>=7.4.3", + "isort>=5.12.0", + "black>=23.11.0", + "coverage>=7.3.2", +] diff --git a/qbittorrent_exporter/exporter.py b/qbittorrent_exporter/exporter.py index bae7608..2d2ba88 100644 --- a/qbittorrent_exporter/exporter.py +++ b/qbittorrent_exporter/exporter.py @@ -1,16 +1,17 @@ -import time -import os -import sys -import signal import faulthandler -from qbittorrentapi import Client, TorrentStates -from prometheus_client import start_http_server -from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY import logging -from pythonjsonlogger import jsonlogger -from enum import StrEnum, auto -from typing import Iterable, Any +import os +import signal +import sys +import time from dataclasses import dataclass, field +from enum import StrEnum, auto +from typing import Any, Iterable + +from prometheus_client import start_http_server +from prometheus_client.core import REGISTRY, CounterMetricFamily, GaugeMetricFamily +from pythonjsonlogger import jsonlogger +from qbittorrentapi import Client, TorrentStates # Enable dumps on stderr in case of segfault faulthandler.enable() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/exporter_test.py b/tests/exporter_test.py new file mode 100644 index 0000000..4c38593 --- /dev/null +++ b/tests/exporter_test.py @@ -0,0 +1,70 @@ +import unittest +from unittest.mock import MagicMock, patch + +from prometheus_client.metrics_core import CounterMetricFamily, GaugeMetricFamily + +from qbittorrent_exporter.exporter import ( + Metric, + MetricType, + QbittorrentMetricsCollector, +) + + +class TestQbittorrentMetricsCollector(unittest.TestCase): + @patch("qbittorrent_exporter.exporter.Client") + def setUp(self, mock_client): + self.mock_client = mock_client + self.config = { + "host": "localhost", + "port": "8080", + "username": "user", + "password": "pass", + "verify_webui_certificate": False, + } + self.collector = QbittorrentMetricsCollector(self.config) + + def test_init(self): + self.assertEqual(self.collector.config, self.config) + self.mock_client.assert_called_once_with( + host=self.config["host"], + port=self.config["port"], + username=self.config["username"], + password=self.config["password"], + VERIFY_WEBUI_CERTIFICATE=self.config["verify_webui_certificate"], + ) + + def test_collect_gauge(self): + mock_metric = Metric( + name="test_gauge", + metric_type=MetricType.GAUGE, + help_text="Test Gauge", + labels={"label1": "value1"}, + value=10, + ) + self.collector.get_qbittorrent_metrics = MagicMock(return_value=[mock_metric]) + + result = next(self.collector.collect()) + + self.assertIsInstance(result, GaugeMetricFamily) + self.assertEqual(result.name, "test_gauge") + self.assertEqual(result.documentation, "Test Gauge") + self.assertEqual(result.samples[0].labels, {"label1": "value1"}) + self.assertEqual(result.samples[0].value, 10) + + def test_collect_counter(self): + mock_metric = Metric( + name="test_counter", + metric_type=MetricType.COUNTER, + help_text="Test Counter", + labels={"label2": "value2"}, + value=230, + ) + self.collector.get_qbittorrent_metrics = MagicMock(return_value=[mock_metric]) + + result = next(self.collector.collect()) + + self.assertIsInstance(result, CounterMetricFamily) + self.assertEqual(result.name, "test_counter") + self.assertEqual(result.documentation, "Test Counter") + self.assertEqual(result.samples[0].labels, {"label2": "value2"}) + self.assertEqual(result.samples[0].value, 230) diff --git a/tests/metric_test.py b/tests/metric_test.py new file mode 100644 index 0000000..7c597ee --- /dev/null +++ b/tests/metric_test.py @@ -0,0 +1,13 @@ +import unittest + +from qbittorrent_exporter.exporter import Metric, MetricType + + +class TestMetric(unittest.TestCase): + def test_metric_initialization(self): + metric = Metric(name="test_metric", value=10) + self.assertEqual(metric.name, "test_metric") + self.assertEqual(metric.value, 10) + self.assertEqual(metric.labels, {}) + self.assertEqual(metric.help_text, "") + self.assertEqual(metric.metric_type, MetricType.GAUGE)