Skip to content

Commit e9c7c2b

Browse files
committed
added basic tests
1 parent 933b734 commit e9c7c2b

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

.circleci/test-server.sh

+32
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,36 @@ if [ "$RUN_WEBHOOK_TESTS" == "true" ] ; then
356356

357357
fi
358358

359+
# horizontal scale test
360+
361+
echo -e "\n<########## TEST GRAPHQL-ENGINE WITH HORIZONTAL SCALING ########>\n"
362+
363+
HASURA_HS_TEST_DB='postgres://gql_test:@localhost:5432/hs_hge_test'
364+
echo "Installing psql"
365+
apt-get update && apt-get install -y postgresql-client
366+
psql "$HASURA_GRAPHQL_DATABASE_URL" -c "create database hs_hge_test;"
367+
368+
# start 1st server
369+
"$GRAPHQL_ENGINE" --database-url "$HASURA_HS_TEST_DB" serve >> "$OUTPUT_FOLDER/graphql-engine.log" 2>&1 & PID=$!
370+
wait_for_port 8080
371+
372+
# start 2nd server
373+
"$GRAPHQL_ENGINE" --database-url "$HASURA_HS_TEST_DB" serve \
374+
--server-port 8081 --server-host 0.0.0.0 \
375+
>> "$OUTPUT_FOLDER/hs-graphql-engine.log" 2>&1 & HS_PID=$!
376+
wait_for_port 8081
377+
378+
# run test
379+
pytest -vv --hge-url="$HGE_URL" --pg-url="$HASURA_GRAPHQL_DATABASE_URL" --test-hge-scale-url="http://localhost:8081" test_horizontal_scale.py
380+
381+
kill -INT $PID
382+
kill -INT $HS_PID
383+
psql "$HASURA_GRAPHQL_DATABASE_URL" -c "drop database hs_hge_test;"
384+
sleep 4
385+
combine_hpc_reports
386+
unset HASURA_HS_TEST_DB
387+
388+
389+
# end horizontal scale test
390+
359391
mv graphql-engine-combined.tix "$OUTPUT_FOLDER/graphql-engine.tix" || true

server/tests-py/conftest.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ def pytest_addoption(parser):
5050
help="Run Test cases with GraphQL queries being disabled"
5151
)
5252

53+
parser.addoption(
54+
"--test-hge-scale-url",
55+
metavar="<url>",
56+
required=False,
57+
help="Run testcases for horizontal scaling"
58+
)
59+
5360

5461
@pytest.fixture(scope='session')
5562
def hge_ctx(request):
@@ -63,6 +70,7 @@ def hge_ctx(request):
6370
hge_jwt_conf = request.config.getoption('--hge-jwt-conf')
6471
ws_read_cookie = request.config.getoption('--test-ws-init-cookie')
6572
metadata_disabled = request.config.getoption('--test-metadata-disabled')
73+
hge_scale_url = request.config.getoption('--test-hge-scale-url')
6674
try:
6775
hge_ctx = HGECtx(
6876
hge_url=hge_url,
@@ -73,7 +81,8 @@ def hge_ctx(request):
7381
hge_jwt_key_file=hge_jwt_key_file,
7482
hge_jwt_conf=hge_jwt_conf,
7583
ws_read_cookie=ws_read_cookie,
76-
metadata_disabled=metadata_disabled
84+
metadata_disabled=metadata_disabled,
85+
hge_scale_url=hge_scale_url
7786
)
7887
except HGECtxError as e:
7988
pytest.exit(str(e))

server/tests-py/context.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def server_bind(self):
7575

7676
class HGECtx:
7777
def __init__(self, hge_url, pg_url, hge_key, hge_webhook, webhook_insecure,
78-
hge_jwt_key_file, hge_jwt_conf, metadata_disabled, ws_read_cookie):
78+
hge_jwt_key_file, hge_jwt_conf, metadata_disabled, ws_read_cookie, hge_scale_url):
7979
server_address = ('0.0.0.0', 5592)
8080

8181
self.resp_queue = queue.Queue(maxsize=1)
@@ -118,6 +118,8 @@ def __init__(self, hge_url, pg_url, hge_key, hge_webhook, webhook_insecure,
118118

119119
self.ws_read_cookie = ws_read_cookie
120120

121+
self.hge_scale_url = hge_scale_url
122+
121123
result = subprocess.run(['../../scripts/get-version.sh'], shell=False, stdout=subprocess.PIPE, check=True)
122124
self.version = result.stdout.decode('utf-8').strip()
123125
if not self.metadata_disabled:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-
2+
operation:
3+
type: bulk
4+
args:
5+
- type: run_sql
6+
args:
7+
sql: |
8+
create table test_t1(
9+
c1 int,
10+
c2 text,
11+
PRIMARY KEY (c1)
12+
);
13+
- type: track_table
14+
args:
15+
schema: public
16+
name: test_t1
17+
- type: run_sql
18+
args:
19+
sql: |
20+
insert into test_t1(c1, c2) VALUES(1, 'test');
21+
validate:
22+
response:
23+
data:
24+
test_t1:
25+
- c1: 1
26+
c2: test
27+
query:
28+
query: |
29+
query {
30+
test_t1 {
31+
c1
32+
c2
33+
}
34+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pytest
2+
import yaml
3+
import time
4+
import jsondiff
5+
6+
from validate import json_ordered
7+
8+
9+
if not pytest.config.getoption("--test-hge-scale-url"):
10+
pytest.skip("--test-hge-scale-url flag is missing, skipping tests", allow_module_level=True)
11+
12+
13+
class TestHorizantalScaleBasic():
14+
15+
@pytest.fixture(autouse=True, scope='class')
16+
def transact(self, hge_ctx):
17+
self.teardown = {"type": "clear_metadata", "args": {}}
18+
yield
19+
# teardown
20+
st_code, resp = hge_ctx.v1q(self.teardown)
21+
assert st_code == 200, resp
22+
23+
def test_horizontal_scale_basic(self, hge_ctx):
24+
with open(self.dir() + "/steps.yaml") as c:
25+
conf = yaml.load(c)
26+
27+
assert isinstance(conf, list) == True, 'Not an list'
28+
for _, step in enumerate(conf):
29+
# execute operation on 1st server
30+
st_code, resp = hge_ctx.v1q(step['operation'])
31+
assert st_code == 200, resp
32+
33+
# wait for x sec
34+
time.sleep(0.3)
35+
# validate data on 2nd server
36+
response = hge_ctx.http.post(
37+
hge_ctx.hge_scale_url + "/v1alpha1/graphql",
38+
json=step['validate']['query']
39+
)
40+
st_code = response.status_code
41+
resp = response.json()
42+
assert st_code == 200, resp
43+
44+
if 'response' in step['validate']:
45+
assert json_ordered(resp) == json_ordered(step['validate']['response']), yaml.dump({
46+
'response': resp,
47+
'expected': step['validate']['response'],
48+
'diff': jsondiff.diff(step['validate']['response'], resp)
49+
})
50+
51+
@classmethod
52+
def dir(cls):
53+
return 'queries/horizontal_scale/basic'

0 commit comments

Comments
 (0)