From 2ec0f65b99db37d1bd815f1300dcbea00303ebbf Mon Sep 17 00:00:00 2001 From: Jiacai Liu Date: Thu, 27 Apr 2023 17:46:01 +0800 Subject: [PATCH] test: add prometheus integration tests (#864) --- .github/workflows/ci.yml | 4 ++ integration_tests/Makefile | 3 ++ integration_tests/prom/prometheus.yml | 3 ++ integration_tests/prom/remote-query.py | 73 ++++++++++++++++++++++++++ integration_tests/prom/run-tests.sh | 10 ++++ 5 files changed, 93 insertions(+) create mode 100644 integration_tests/prom/prometheus.yml create mode 100755 integration_tests/prom/remote-query.py create mode 100755 integration_tests/prom/run-tests.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ad8b08c1b..ccd5660ccf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/integration_tests/Makefile b/integration_tests/Makefile index 27b4650aca..0ce460ecfa 100644 --- a/integration_tests/Makefile +++ b/integration_tests/Makefile @@ -66,3 +66,6 @@ run-rust: run-mysql: cd mysql && ./basic.sh + +run-prom: + cd prom && ./run-tests.sh diff --git a/integration_tests/prom/prometheus.yml b/integration_tests/prom/prometheus.yml new file mode 100644 index 0000000000..7c8eb6e571 --- /dev/null +++ b/integration_tests/prom/prometheus.yml @@ -0,0 +1,3 @@ +remote_read: + - url: "http://127.0.0.1:5440/prom/v1/read" + read_recent: true diff --git a/integration_tests/prom/remote-query.py b/integration_tests/prom/remote-query.py new file mode 100755 index 0000000000..046d370600 --- /dev/null +++ b/integration_tests/prom/remote-query.py @@ -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() diff --git a/integration_tests/prom/run-tests.sh b/integration_tests/prom/run-tests.sh new file mode 100755 index 0000000000..c3cf6a2830 --- /dev/null +++ b/integration_tests/prom/run-tests.sh @@ -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