Skip to content

Commit

Permalink
added basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arvi3411301 committed Mar 7, 2019
1 parent 933b734 commit e9c7c2b
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .circleci/test-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,36 @@ if [ "$RUN_WEBHOOK_TESTS" == "true" ] ; then

fi

# horizontal scale test

echo -e "\n<########## TEST GRAPHQL-ENGINE WITH HORIZONTAL SCALING ########>\n"

HASURA_HS_TEST_DB='postgres://gql_test:@localhost:5432/hs_hge_test'
echo "Installing psql"
apt-get update && apt-get install -y postgresql-client
psql "$HASURA_GRAPHQL_DATABASE_URL" -c "create database hs_hge_test;"

# start 1st server
"$GRAPHQL_ENGINE" --database-url "$HASURA_HS_TEST_DB" serve >> "$OUTPUT_FOLDER/graphql-engine.log" 2>&1 & PID=$!
wait_for_port 8080

# start 2nd server
"$GRAPHQL_ENGINE" --database-url "$HASURA_HS_TEST_DB" serve \
--server-port 8081 --server-host 0.0.0.0 \
>> "$OUTPUT_FOLDER/hs-graphql-engine.log" 2>&1 & HS_PID=$!
wait_for_port 8081

# run test
pytest -vv --hge-url="$HGE_URL" --pg-url="$HASURA_GRAPHQL_DATABASE_URL" --test-hge-scale-url="http://localhost:8081" test_horizontal_scale.py

kill -INT $PID
kill -INT $HS_PID
psql "$HASURA_GRAPHQL_DATABASE_URL" -c "drop database hs_hge_test;"
sleep 4
combine_hpc_reports
unset HASURA_HS_TEST_DB


# end horizontal scale test

mv graphql-engine-combined.tix "$OUTPUT_FOLDER/graphql-engine.tix" || true
11 changes: 10 additions & 1 deletion server/tests-py/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def pytest_addoption(parser):
help="Run Test cases with GraphQL queries being disabled"
)

parser.addoption(
"--test-hge-scale-url",
metavar="<url>",
required=False,
help="Run testcases for horizontal scaling"
)


@pytest.fixture(scope='session')
def hge_ctx(request):
Expand All @@ -63,6 +70,7 @@ def hge_ctx(request):
hge_jwt_conf = request.config.getoption('--hge-jwt-conf')
ws_read_cookie = request.config.getoption('--test-ws-init-cookie')
metadata_disabled = request.config.getoption('--test-metadata-disabled')
hge_scale_url = request.config.getoption('--test-hge-scale-url')
try:
hge_ctx = HGECtx(
hge_url=hge_url,
Expand All @@ -73,7 +81,8 @@ def hge_ctx(request):
hge_jwt_key_file=hge_jwt_key_file,
hge_jwt_conf=hge_jwt_conf,
ws_read_cookie=ws_read_cookie,
metadata_disabled=metadata_disabled
metadata_disabled=metadata_disabled,
hge_scale_url=hge_scale_url
)
except HGECtxError as e:
pytest.exit(str(e))
Expand Down
4 changes: 3 additions & 1 deletion server/tests-py/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def server_bind(self):

class HGECtx:
def __init__(self, hge_url, pg_url, hge_key, hge_webhook, webhook_insecure,
hge_jwt_key_file, hge_jwt_conf, metadata_disabled, ws_read_cookie):
hge_jwt_key_file, hge_jwt_conf, metadata_disabled, ws_read_cookie, hge_scale_url):
server_address = ('0.0.0.0', 5592)

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

self.ws_read_cookie = ws_read_cookie

self.hge_scale_url = hge_scale_url

result = subprocess.run(['../../scripts/get-version.sh'], shell=False, stdout=subprocess.PIPE, check=True)
self.version = result.stdout.decode('utf-8').strip()
if not self.metadata_disabled:
Expand Down
34 changes: 34 additions & 0 deletions server/tests-py/queries/horizontal_scale/basic/steps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-
operation:
type: bulk
args:
- type: run_sql
args:
sql: |
create table test_t1(
c1 int,
c2 text,
PRIMARY KEY (c1)
);
- type: track_table
args:
schema: public
name: test_t1
- type: run_sql
args:
sql: |
insert into test_t1(c1, c2) VALUES(1, 'test');
validate:
response:
data:
test_t1:
- c1: 1
c2: test
query:
query: |
query {
test_t1 {
c1
c2
}
}
53 changes: 53 additions & 0 deletions server/tests-py/test_horizontal_scale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest
import yaml
import time
import jsondiff

from validate import json_ordered


if not pytest.config.getoption("--test-hge-scale-url"):
pytest.skip("--test-hge-scale-url flag is missing, skipping tests", allow_module_level=True)


class TestHorizantalScaleBasic():

@pytest.fixture(autouse=True, scope='class')
def transact(self, hge_ctx):
self.teardown = {"type": "clear_metadata", "args": {}}
yield
# teardown
st_code, resp = hge_ctx.v1q(self.teardown)
assert st_code == 200, resp

def test_horizontal_scale_basic(self, hge_ctx):
with open(self.dir() + "/steps.yaml") as c:
conf = yaml.load(c)

assert isinstance(conf, list) == True, 'Not an list'
for _, step in enumerate(conf):
# execute operation on 1st server
st_code, resp = hge_ctx.v1q(step['operation'])
assert st_code == 200, resp

# wait for x sec
time.sleep(0.3)
# validate data on 2nd server
response = hge_ctx.http.post(
hge_ctx.hge_scale_url + "/v1alpha1/graphql",
json=step['validate']['query']
)
st_code = response.status_code
resp = response.json()
assert st_code == 200, resp

if 'response' in step['validate']:
assert json_ordered(resp) == json_ordered(step['validate']['response']), yaml.dump({
'response': resp,
'expected': step['validate']['response'],
'diff': jsondiff.diff(step['validate']['response'], resp)
})

@classmethod
def dir(cls):
return 'queries/horizontal_scale/basic'

0 comments on commit e9c7c2b

Please sign in to comment.