Skip to content

Commit db0f0d7

Browse files
committed
basic tests and workflow test
1 parent e5363c4 commit db0f0d7

File tree

12 files changed

+234
-2
lines changed

12 files changed

+234
-2
lines changed

.github/workflows/tests.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: test-all
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
name: run all tests
13+
runs-on: ubuntu-20.04
14+
strategy:
15+
matrix:
16+
python-version: [3.6, 3.7, 3.8, 3.9, 'pypy3']
17+
services:
18+
victoria:
19+
image: victoriametrics/victoria-metrics:v1.59.0
20+
#options: >-
21+
# --entrypoint "/victoria-metrics-prod", "--envflag.enable"]
22+
env:
23+
httpListenAddr: 8429
24+
influxListenAddr: 8491
25+
graphiteListenAddr: 8492
26+
ports:
27+
- 8428:8428
28+
- 8491:8491
29+
- 8492:8492
30+
steps:
31+
- name: checkout
32+
uses: actions/checkout@v2
33+
- name: python${{ matrix.python-version }}
34+
uses: actions/setup-python@v2
35+
with:
36+
python-version: ${{ matrix.python-version }}
37+
- name: pip-requirements
38+
run: pip install --upgrade -e .[test]
39+
- name: run-test
40+
run: pytest -svvv --continue-on-collection-errors --cov=./prometheus_push_client --cov-report=xml:coverage.xml
41+
env:
42+
VM_HOST: localhost
43+
VM_API_PORT: 8428
44+
VM_INFLUX_PORT: 8491
45+
VM_GRAPHITE_PORT: 8492
46+
- name: upload-cov
47+
uses: codecov/codecov-action@v1
48+
with:
49+
files: coverage.xml
50+
verbose: true

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# prometheus-push-client
2+
3+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/gistart/prometheus-push-client/test-all)
4+
5+
## Default labelvalues
6+
7+
! host AND victoria account id
8+
9+
! side-effect: does not register labelled metrics at creation time, so you report only metrics that you used!

prometheus_push_client/metrics.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def __init__(self, name, documentation="", default_labelvalues=None, **kwargs):
99
kwargs["registry"] = kwargs.get("registry", PUSH_REGISTRY)
1010
self._default_labelvalues = default_labelvalues or {}
1111
super().__init__(name, documentation, **kwargs)
12-
1312
# TODO: hooks for sync mode
1413

1514
def labels(self, *labelvalues, **labelkwargs):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"test": [
3939
"pytest",
4040
"pytest-asyncio",
41-
"aiohttp",
41+
"pytest-cov",
4242
"requests",
4343
],
4444
}

test/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
import os
3+
sys.path.append(os.path.join(os.path.dirname(__file__)))

test/test_offline/__init__.py

Whitespace-only changes.

test/test_offline/test_defaults.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
import prometheus_push_client as ppc
3+
4+
from testutils import make_metric_fixture, collect_metrics
5+
6+
NS = "test_offline"
7+
SUB = "defaults"
8+
9+
10+
_cnt1 = ppc.Counter(
11+
name="c1",
12+
namespace=NS,
13+
subsystem=SUB,
14+
labelnames=["host", "l1", "l2"],
15+
default_labelvalues={
16+
"host": "localhost",
17+
"l1": 0,
18+
}
19+
)
20+
21+
@pytest.fixture
22+
def counter1(request):
23+
return make_metric_fixture(request, _cnt1)
24+
25+
26+
@pytest.fixture
27+
def test_defaults_expected():
28+
return \
29+
"""
30+
test_offline_defaults_c1_total{host="localhost",l1="0",l2="2"} 2.0
31+
test_offline_defaults_c1_total{host="localhost",l1="1",l2="2"} 2.0
32+
test_offline_defaults_c1_total{host="H",l1="1",l2="2"} 2.0
33+
""".strip()
34+
35+
36+
def test_default_labelvalues_usage(counter1, test_defaults_expected):
37+
# pairs of equivalent actions:
38+
39+
counter1.labels(l2=2).inc()
40+
counter1.labels(2).inc()
41+
42+
counter1.labels(l1=1, l2=2).inc()
43+
counter1.labels(1, 2).inc()
44+
45+
counter1.labels(host="H", l1=1, l2=2).inc()
46+
counter1.labels("H", 1, 2).inc()
47+
48+
res = collect_metrics(counter1._name)
49+
assert res == test_defaults_expected

test/test_offline/test_influx.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
3+
import prometheus_push_client as ppc
4+
5+
from testutils import make_metric_fixture, collect_metrics
6+
7+
8+
NS = "test_offline"
9+
SUB = "influx"
10+
11+
12+
@pytest.fixture
13+
def sum1(request):
14+
return make_metric_fixture(
15+
request,
16+
ppc.Summary(name="s1", namespace=NS, subsystem=SUB)
17+
)
18+
19+
20+
@pytest.fixture
21+
def counter1(request):
22+
return make_metric_fixture(
23+
request,
24+
ppc.Counter(name="c1", namespace=NS, subsystem=SUB)
25+
)
26+
27+
28+
@pytest.fixture
29+
def histogram1(request):
30+
return make_metric_fixture(
31+
request,
32+
ppc.Histogram(name="h1", namespace=NS, subsystem=SUB, buckets=[1,2,float("inf")])
33+
)
34+
35+
36+
37+
def test_influx_formatter(sum1, counter1, histogram1):
38+
pass
39+
40+
41+
@pytest.fixture
42+
def invalid_gauge(request):
43+
return make_metric_fixture(request, ppc.Gauge("justgauge"))
44+
45+
46+
@pytest.fixture
47+
def valid_gauge(request):
48+
return make_metric_fixture(request, ppc.Gauge("just_gauge"))
49+
50+
51+
def test_influx_metric_name_invalid(invalid_gauge):
52+
with pytest.raises(ValueError):
53+
fmt = ppc.InfluxFormat()
54+
55+
56+
def test_influx_metric_name_valid(valid_gauge):
57+
fmt = ppc.InfluxFormat()
58+
59+
valid_gauge.inc(10)
60+
valid_gauge.inc(-5)
61+
62+
data = fmt.generate_latest().decode()
63+
assert data == "just gauge=5.0"

test/test_online/__init__.py

Whitespace-only changes.

test/test_online/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
import pytest
3+
4+
5+
@pytest.fixture
6+
def cfg():
7+
8+
class Cfg:
9+
vm_host = os.getenv("VM_HOST", "localhost")
10+
vm_api_port = os.getenv("VM_API_PORT", "8428")
11+
vm_influx_port = os.getenv("VM_INFLUX_PORT", "8491")
12+
vm_graphite_port = os.getenv("VM_GRAPHITE_PORT", "8492")
13+
14+
vm_ping_url = f"http://{vm_host}:{vm_api_port}/health"
15+
vm_write_url = f"http://{vm_host}:{vm_api_port}/write"
16+
vm_export_url = f"http://{vm_host}:{vm_api_port}/api/v1/export"
17+
18+
return Cfg()

0 commit comments

Comments
 (0)