Skip to content

Commit

Permalink
test: add prometheus integration tests (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 authored Apr 27, 2023
1 parent ac5733c commit 2ec0f65
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ jobs:
working-directory: integration_tests
run: |
make run-mysql
- name: Run Prometheus query tests
working-directory: integration_tests
run: |
make run-prom
- name: Upload Logs
if: always()
uses: actions/upload-artifact@v3
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ run-rust:

run-mysql:
cd mysql && ./basic.sh

run-prom:
cd prom && ./run-tests.sh
3 changes: 3 additions & 0 deletions integration_tests/prom/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
remote_read:
- url: "http://127.0.0.1:5440/prom/v1/read"
read_recent: true
73 changes: 73 additions & 0 deletions integration_tests/prom/remote-query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python
# coding: utf-8

import requests
import time

api_root = 'http://localhost:5440'
prom_api_root = 'http://localhost:9090'
headers = {
'Content-Type': 'application/json'
}

def now():
return int(time.time()) * 1000

table = 'prom_remote_query_test' + str(now())

def execute_sql(sql):
r = requests.post('{}/sql'.format(api_root), json={'query': sql}, headers=headers)
assert r.status_code == 200, r.text

def execute_pql(pql):
r = requests.get('{}/api/v1/query?query={}'.format(prom_api_root, pql))
assert r.status_code == 200, r.text
return r.json()

def prepare_data(ts):
execute_sql("""
CREATE TABLE if not exists `{}` (
`t` timestamp NOT NULL,
`tag1` string TAG,
`tag2` string TAG,
`value` double NOT NULL,
`value2` double NOT NULL,
timestamp KEY (t)
);
""".format(table))

execute_sql("""
insert into {}(t, tag1, tag2, value, value2)
values
({}, "v1", "v2", 1, 2),
({}, "v1", "v2", 11, 22)
;
""".format(table, ts-5000, ts))


def remote_query(ts):
ts = ts/1000 # prom return seconds

r = execute_pql(table + '{tag1="v1"}[5m]')
result = r['data']['result']
assert result == [{'metric': {'__name__': table, 'tag1': 'v1', 'tag2': 'v2'}, 'values': [[ts-5, '1'], [ts, '11']]}]

r = execute_pql(table + '{tag1=~"v1"}[5m]')
result = r['data']['result']
assert result == [{'metric': {'__name__': table, 'tag1': 'v1', 'tag2': 'v2'}, 'values': [[ts-5, '1'], [ts, '11']]}]

r = execute_pql(table + '{tag1!="v1"}[5m]')
result = r['data']['result']
assert result == []

r = execute_pql(table + '{tag1!~"v1"}[5m]')
result = r['data']['result']
assert result == []

def main():
ts = now()
prepare_data(ts)
remote_query(ts)

if __name__ == '__main__':
main()
10 changes: 10 additions & 0 deletions integration_tests/prom/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

VERSION=prometheus-2.43.0.linux-amd64
wget "https://github.com/prometheus/prometheus/releases/download/v2.43.0/${VERSION}.tar.gz"

tar xvf prometheus*.tar.gz
nohup ./${VERSION}/prometheus --config.file ./prometheus.yml &
sleep 5

python ./remote-query.py

0 comments on commit 2ec0f65

Please sign in to comment.