diff --git a/.github/workflows/test-prometrix.yml b/.github/workflows/test-prometrix.yml new file mode 100644 index 0000000..a3d7d0d --- /dev/null +++ b/.github/workflows/test-prometrix.yml @@ -0,0 +1,40 @@ +name: Test Prometrix with pytest + +on: + push: + workflow_dispatch: + pull_request: + types: [opened, reopened] + +jobs: + run_tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + # setup a KIND cluster for Prometheus etc + - name: Create k8s Kind Cluster + uses: helm/kind-action@v1.2.0 + - name: Output KIND info + run: | + kubectl config get-contexts + + # TODO install Kubernetes, Prometheus etc? + + # install Prometrix so that we can run tests on it + - name: Install Prometrix + run: | + curl -sSL https://install.python-poetry.org | python3 - --version 1.4.0 + poetry config virtualenvs.create false + poetry install --with test + + # run the actual tests + - name: Test Prometrix + env: + TEST_CONFIG: ${{ secrets.TEST_CONFIG }} + run: | + python tests/main.py --from-env diff --git a/poetry.lock b/poetry.lock index c84464c..f2b52c5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "annotated-types" @@ -175,6 +175,17 @@ files = [ {file = "charset_normalizer-3.2.0-py3-none-any.whl", hash = "sha256:8e098148dd37b4ce3baca71fb394c81dc5d9c7728c95df695d2dca218edf40e6"}, ] +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +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 = "contourpy" version = "1.1.0" @@ -270,6 +281,20 @@ calendars = ["convertdate", "hijri-converter"] fasttext = ["fasttext"] langdetect = ["langdetect"] +[[package]] +name = "exceptiongroup" +version = "1.2.1" +description = "Backport of PEP 654 (exception groups)" +optional = false +python-versions = ">=3.7" +files = [ + {file = "exceptiongroup-1.2.1-py3-none-any.whl", hash = "sha256:5258b9ed329c5bbdd31a309f53cbfb0b155341807f6ff7606a1e801a891b29ad"}, + {file = "exceptiongroup-1.2.1.tar.gz", hash = "sha256:a4785e48b045528f5bfe627b6ad554ff32def154f42372786903b7abcfe1aa16"}, +] + +[package.extras] +test = ["pytest (>=6)"] + [[package]] name = "fonttools" version = "4.51.0" @@ -378,6 +403,17 @@ zipp = {version = ">=3.1.0", markers = "python_version < \"3.10\""} docs = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-lint"] testing = ["pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-mypy (>=0.9.1)", "pytest-ruff"] +[[package]] +name = "iniconfig" +version = "2.0.0" +description = "brain-dead simple config-ini parsing" +optional = false +python-versions = ">=3.7" +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 = "jmespath" version = "1.0.1" @@ -729,6 +765,21 @@ tests = ["check-manifest", "coverage", "defusedxml", "markdown2", "olefile", "pa typing = ["typing-extensions"] xmp = ["defusedxml"] +[[package]] +name = "pluggy" +version = "1.5.0" +description = "plugin and hook calling mechanisms for python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"}, + {file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"}, +] + +[package.extras] +dev = ["pre-commit", "tox"] +testing = ["pytest", "pytest-benchmark"] + [[package]] name = "prometheus-api-client" version = "0.5.3" @@ -872,6 +923,28 @@ files = [ [package.extras] diagrams = ["jinja2", "railroad-diagrams"] +[[package]] +name = "pytest" +version = "8.2.2" +description = "pytest: simple powerful testing with Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pytest-8.2.2-py3-none-any.whl", hash = "sha256:c434598117762e2bd304e526244f67bf66bbd7b5d6cf22138be51ff661980343"}, + {file = "pytest-8.2.2.tar.gz", hash = "sha256:de4bb8104e201939ccdc688b27a89a7be2079b22e2bd2b07f806b6ba71117977"}, +] + +[package.dependencies] +colorama = {version = "*", markers = "sys_platform == \"win32\""} +exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""} +iniconfig = "*" +packaging = "*" +pluggy = ">=1.5,<2.0" +tomli = {version = ">=1", markers = "python_version < \"3.11\""} + +[package.extras] +dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] + [[package]] name = "python-dateutil" version = "2.8.2" @@ -1043,6 +1116,17 @@ files = [ {file = "six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"}, ] +[[package]] +name = "tomli" +version = "2.0.1" +description = "A lil' TOML parser" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"}, + {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"}, +] + [[package]] name = "typing-extensions" version = "4.7.1" @@ -1117,4 +1201,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "fe90704d29fbf577e435e39a05d4b380971c05c1a0ded8147af0b09064b935a7" +content-hash = "eb0d368fca3c0e2f403479a6edd49302b1a334ed853bf92400b1d6f0f62a985c" diff --git a/pyproject.toml b/pyproject.toml index e55d47e..48ca7d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,11 @@ urllib3 = "^1.26.18" # added to Pin transitive dependency, not needed directly idna = "^3.7" requests = "^2.32.0" +[tool.poetry.group.test] +optional = true + +[tool.poetry.group.test.dependencies] +pytest = "^8.0.0" [build-system] requires = ["poetry-core"] diff --git a/tests/main.py b/tests/main.py index acad89d..4513939 100644 --- a/tests/main.py +++ b/tests/main.py @@ -1,5 +1,5 @@ -import json import os.path +import sys from datetime import datetime, timedelta from typing import Dict @@ -71,24 +71,37 @@ def run_test(test_type: str, config: PrometheusConfig): print(f"Test {test_type} failed, results of wrong format") -def main(): - test_config_file_name = "config.yaml" - if not os.path.isfile(test_config_file_name): - print( - f"To run tests you must create a test config file called '{test_config_file_name}'.\n See 'test_config_example.yaml' for the format and examples" - ) - return +def main(from_env=False): + if not from_env: + test_config_file_name = "config.yaml" + if not os.path.isfile(test_config_file_name): + print( + f"To run tests you must create a test config file called '{test_config_file_name}'.\n See 'test_config_example.yaml' for the format and examples" + ) + return + with open(test_config_file_name, "r") as tests_yaml_file: + yaml_obj = yaml.safe_load( + tests_yaml_file + ) # yaml_object will be a list or a dict + else: + print(f"Getting config YAML from env variable TEST_CONFIG") + yaml_text = os.environ.get("TEST_CONFIG") + if not yaml_text: + print("To run tests inside Github workflow, you must define the TEST_CONFIG secret") + sys.exit(1) + yaml_obj = yaml.safe_load(yaml_text) - with open(test_config_file_name, "r") as tests_yaml_file: - yaml_obj = yaml.safe_load( - tests_yaml_file - ) # yaml_object will be a list or a dict - for test_config in yaml_obj["testConfig"]: - config_type = test_config["type"] - config_params = test_config["params"] - config = generate_prometheus_config(config_type, config_params) - run_test(config_type, config) + for test_config in yaml_obj["testConfig"]: + config_type = test_config["type"] + config_params = test_config["params"] + config = generate_prometheus_config(config_type, config_params) + run_test(config_type, config) if __name__ == "__main__": - main() + if len(sys.argv) == 2: + # Get configuration from Github secrets + main(from_env=True) + else: + # Get configuration from a local file + main()