Skip to content

Commit

Permalink
Test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Szefler committed Jul 5, 2024
1 parent 430410b commit 7e26fd5
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 20 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/test-prometrix.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
- 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
88 changes: 86 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
49 changes: 31 additions & 18 deletions tests/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
import os.path
import sys
from datetime import datetime, timedelta
from typing import Dict

Expand Down Expand Up @@ -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()

0 comments on commit 7e26fd5

Please sign in to comment.