2018-09-18 09:21:57 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-11-23 16:02:46 +03:00
|
|
|
from http import HTTPStatus
|
2020-03-11 09:27:31 +03:00
|
|
|
from socketserver import ThreadingMixIn
|
2018-11-23 16:02:46 +03:00
|
|
|
from urllib.parse import urlparse
|
2020-02-13 20:38:23 +03:00
|
|
|
from ruamel.yaml.comments import CommentedMap as OrderedDict # to avoid '!!omap' in yaml
|
2018-09-18 09:21:57 +03:00
|
|
|
import threading
|
|
|
|
import http.server
|
|
|
|
import json
|
|
|
|
import queue
|
|
|
|
import socket
|
|
|
|
import subprocess
|
2019-02-14 10:37:59 +03:00
|
|
|
import time
|
2019-04-08 10:22:38 +03:00
|
|
|
import string
|
|
|
|
import random
|
2019-11-26 15:14:21 +03:00
|
|
|
import os
|
2020-02-13 20:38:23 +03:00
|
|
|
import re
|
2018-09-18 09:21:57 +03:00
|
|
|
|
2019-09-04 18:02:35 +03:00
|
|
|
import ruamel.yaml as yaml
|
2018-11-23 16:02:46 +03:00
|
|
|
import requests
|
|
|
|
import websocket
|
2018-09-18 09:21:57 +03:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.schema import MetaData
|
2018-11-26 16:08:16 +03:00
|
|
|
import graphql_server
|
2019-04-08 10:22:38 +03:00
|
|
|
import graphql
|
2018-09-18 09:21:57 +03:00
|
|
|
|
2019-11-29 08:14:26 +03:00
|
|
|
# pytest has removed the global pytest.config
|
|
|
|
# As a solution to this we are going to store it in PyTestConf.config
|
|
|
|
class PytestConf():
|
|
|
|
pass
|
2018-10-30 12:21:58 +03:00
|
|
|
|
2018-09-18 09:21:57 +03:00
|
|
|
class HGECtxError(Exception):
|
|
|
|
pass
|
|
|
|
|
2019-05-10 09:05:11 +03:00
|
|
|
class GQLWsClient():
|
2018-10-30 12:21:58 +03:00
|
|
|
|
2019-05-10 09:05:11 +03:00
|
|
|
def __init__(self, hge_ctx, endpoint):
|
2019-04-08 10:22:38 +03:00
|
|
|
self.hge_ctx = hge_ctx
|
|
|
|
self.ws_queue = queue.Queue(maxsize=-1)
|
2019-05-10 09:05:11 +03:00
|
|
|
self.ws_url = urlparse(hge_ctx.hge_url)._replace(scheme='ws',
|
|
|
|
path=endpoint)
|
2019-04-08 10:22:38 +03:00
|
|
|
self.create_conn()
|
|
|
|
|
|
|
|
def create_conn(self):
|
|
|
|
self.ws_queue.queue.clear()
|
|
|
|
self.ws_id_query_queues = dict()
|
|
|
|
self.ws_active_query_ids = set()
|
2019-08-12 12:44:29 +03:00
|
|
|
|
|
|
|
self.connected_event = threading.Event()
|
|
|
|
self.init_done = False
|
|
|
|
self.is_closing = False
|
|
|
|
self.remote_closed = False
|
|
|
|
|
|
|
|
self._ws = websocket.WebSocketApp(self.ws_url.geturl(),
|
|
|
|
on_open=self._on_open, on_message=self._on_message, on_close=self._on_close)
|
2019-04-08 10:22:38 +03:00
|
|
|
self.wst = threading.Thread(target=self._ws.run_forever)
|
|
|
|
self.wst.daemon = True
|
|
|
|
self.wst.start()
|
|
|
|
|
|
|
|
def recreate_conn(self):
|
|
|
|
self.teardown()
|
|
|
|
self.create_conn()
|
|
|
|
|
2019-08-12 12:44:29 +03:00
|
|
|
def wait_for_connection(self, timeout=10):
|
|
|
|
assert not self.is_closing
|
|
|
|
assert self.connected_event.wait(timeout=timeout)
|
|
|
|
|
2019-04-08 10:22:38 +03:00
|
|
|
def get_ws_event(self, timeout):
|
|
|
|
return self.ws_queue.get(timeout=timeout)
|
|
|
|
|
|
|
|
def has_ws_query_events(self, query_id):
|
|
|
|
return not self.ws_id_query_queues[query_id].empty()
|
|
|
|
|
|
|
|
def get_ws_query_event(self, query_id, timeout):
|
|
|
|
return self.ws_id_query_queues[query_id].get(timeout=timeout)
|
|
|
|
|
|
|
|
def send(self, frame):
|
2019-08-12 12:44:29 +03:00
|
|
|
self.wait_for_connection()
|
2019-04-08 10:22:38 +03:00
|
|
|
if frame.get('type') == 'stop':
|
|
|
|
self.ws_active_query_ids.discard( frame.get('id') )
|
|
|
|
elif frame.get('type') == 'start' and 'id' in frame:
|
|
|
|
self.ws_id_query_queues[frame['id']] = queue.Queue(maxsize=-1)
|
|
|
|
self._ws.send(json.dumps(frame))
|
|
|
|
|
|
|
|
def init_as_admin(self):
|
|
|
|
headers={}
|
|
|
|
if self.hge_ctx.hge_key:
|
|
|
|
headers = {'x-hasura-admin-secret': self.hge_ctx.hge_key}
|
|
|
|
self.init(headers)
|
|
|
|
|
|
|
|
def init(self, headers={}):
|
|
|
|
payload = {'type': 'connection_init', 'payload': {}}
|
|
|
|
|
|
|
|
if headers and len(headers) > 0:
|
|
|
|
payload['payload']['headers'] = headers
|
|
|
|
|
|
|
|
self.send(payload)
|
|
|
|
ev = self.get_ws_event(3)
|
|
|
|
assert ev['type'] == 'connection_ack', ev
|
|
|
|
self.init_done = True
|
|
|
|
|
|
|
|
def stop(self, query_id):
|
|
|
|
data = {'id': query_id, 'type': 'stop'}
|
|
|
|
self.send(data)
|
|
|
|
self.ws_active_query_ids.discard(query_id)
|
|
|
|
|
|
|
|
def gen_id(self, size=6, chars=string.ascii_letters + string.digits):
|
2019-05-10 09:05:11 +03:00
|
|
|
new_id = ''.join(random.choice(chars) for _ in range(size))
|
|
|
|
if new_id in self.ws_active_query_ids:
|
|
|
|
return self.gen_id(size, chars)
|
|
|
|
return new_id
|
2019-04-08 10:22:38 +03:00
|
|
|
|
|
|
|
def send_query(self, query, query_id=None, headers={}, timeout=60):
|
|
|
|
graphql.parse(query['query'])
|
|
|
|
if headers and len(headers) > 0:
|
|
|
|
#Do init If headers are provided
|
|
|
|
self.init(headers)
|
|
|
|
elif not self.init_done:
|
|
|
|
self.init()
|
|
|
|
if query_id == None:
|
|
|
|
query_id = self.gen_id()
|
|
|
|
frame = {
|
|
|
|
'id': query_id,
|
|
|
|
'type': 'start',
|
|
|
|
'payload': query,
|
|
|
|
}
|
|
|
|
self.ws_active_query_ids.add(query_id)
|
|
|
|
self.send(frame)
|
|
|
|
while True:
|
|
|
|
yield self.get_ws_query_event(query_id, timeout)
|
|
|
|
|
|
|
|
def _on_open(self):
|
2019-08-12 12:44:29 +03:00
|
|
|
if not self.is_closing:
|
|
|
|
self.connected_event.set()
|
2019-04-08 10:22:38 +03:00
|
|
|
|
|
|
|
def _on_message(self, message):
|
2019-09-04 18:02:35 +03:00
|
|
|
# NOTE: make sure we preserve key ordering so we can test the ordering
|
|
|
|
# properties in the graphql spec properly
|
|
|
|
json_msg = json.loads(message, object_pairs_hook=OrderedDict)
|
2019-04-08 10:22:38 +03:00
|
|
|
if 'id' in json_msg:
|
|
|
|
query_id = json_msg['id']
|
|
|
|
if json_msg.get('type') == 'stop':
|
|
|
|
#Remove from active queries list
|
|
|
|
self.ws_active_query_ids.discard( query_id )
|
|
|
|
if not query_id in self.ws_id_query_queues:
|
|
|
|
self.ws_id_query_queues[json_msg['id']] = queue.Queue(maxsize=-1)
|
|
|
|
#Put event in the correponding query_queue
|
|
|
|
self.ws_id_query_queues[query_id].put(json_msg)
|
2019-08-12 12:44:29 +03:00
|
|
|
elif json_msg['type'] != 'ka':
|
2019-04-08 10:22:38 +03:00
|
|
|
#Put event in the main queue
|
|
|
|
self.ws_queue.put(json_msg)
|
|
|
|
|
|
|
|
def _on_close(self):
|
|
|
|
self.remote_closed = True
|
|
|
|
self.init_done = False
|
|
|
|
|
|
|
|
def teardown(self):
|
2019-08-12 12:44:29 +03:00
|
|
|
self.is_closing = True
|
2019-04-08 10:22:38 +03:00
|
|
|
if not self.remote_closed:
|
|
|
|
self._ws.close()
|
|
|
|
self.wst.join()
|
|
|
|
|
2020-02-13 20:38:23 +03:00
|
|
|
|
|
|
|
class ActionsWebhookHandler(http.server.BaseHTTPRequestHandler):
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
self.send_response(HTTPStatus.OK)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
content_len = self.headers.get('Content-Length')
|
|
|
|
req_body = self.rfile.read(int(content_len)).decode("utf-8")
|
|
|
|
self.req_json = json.loads(req_body)
|
|
|
|
req_headers = self.headers
|
|
|
|
req_path = self.path
|
|
|
|
self.log_message(json.dumps(self.req_json))
|
|
|
|
|
|
|
|
if req_path == "/create-user":
|
|
|
|
resp, status = self.create_user()
|
|
|
|
self._send_response(status, resp)
|
|
|
|
|
|
|
|
elif req_path == "/create-users":
|
|
|
|
resp, status = self.create_users()
|
|
|
|
self._send_response(status, resp)
|
|
|
|
|
|
|
|
elif req_path == "/invalid-response":
|
|
|
|
self._send_response(HTTPStatus.OK, "some-string")
|
|
|
|
|
2020-03-20 09:46:45 +03:00
|
|
|
elif req_path == "/mirror-action":
|
|
|
|
resp, status = self.mirror_action()
|
|
|
|
self._send_response(status, resp)
|
|
|
|
|
2020-02-13 20:38:23 +03:00
|
|
|
else:
|
|
|
|
self.send_response(HTTPStatus.NO_CONTENT)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def create_user(self):
|
|
|
|
email_address = self.req_json['input']['email']
|
|
|
|
name = self.req_json['input']['name']
|
|
|
|
|
|
|
|
if not self.check_email(email_address):
|
|
|
|
response = {
|
|
|
|
'message': 'Given email address is not valid',
|
|
|
|
'code': 'invalid-email'
|
|
|
|
}
|
|
|
|
return response, HTTPStatus.BAD_REQUEST
|
|
|
|
|
|
|
|
gql_query = '''
|
|
|
|
mutation ($email: String! $name: String!) {
|
|
|
|
insert_user_one(object: {email: $email, name: $name}){
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
query = {
|
|
|
|
'query': gql_query,
|
|
|
|
'variables': {
|
|
|
|
'email': email_address,
|
|
|
|
'name': name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code, resp = self.execute_query(query)
|
|
|
|
if code != 200 or 'data' not in resp:
|
|
|
|
response = {
|
|
|
|
'message': 'GraphQL query execution failed',
|
|
|
|
'code': 'unexpected'
|
|
|
|
}
|
|
|
|
return response, HTTPStatus.BAD_REQUEST
|
|
|
|
|
|
|
|
response = resp['data']['insert_user_one']
|
|
|
|
return response, HTTPStatus.OK
|
|
|
|
|
|
|
|
def create_users(self):
|
|
|
|
inputs = self.req_json['input']['users']
|
|
|
|
for input in inputs:
|
|
|
|
email_address = input['email']
|
|
|
|
if not self.check_email(email_address):
|
|
|
|
response = {
|
|
|
|
'message': 'Email address is not valid: ' + email_address,
|
|
|
|
'code': 'invalid-email'
|
|
|
|
}
|
|
|
|
return response, HTTPStatus.BAD_REQUEST
|
|
|
|
|
|
|
|
gql_query = '''
|
|
|
|
mutation ($insert_inputs: [user_insert_input!]!){
|
|
|
|
insert_user(objects: $insert_inputs){
|
|
|
|
returning{
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
query = {
|
|
|
|
'query': gql_query,
|
|
|
|
'variables': {
|
|
|
|
'insert_inputs': inputs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code, resp = self.execute_query(query)
|
|
|
|
if code != 200 or 'data' not in resp:
|
|
|
|
response = {
|
|
|
|
'message': 'GraphQL query execution failed',
|
|
|
|
'code': 'unexpected'
|
|
|
|
}
|
|
|
|
return response, HTTPStatus.BAD_REQUEST
|
|
|
|
|
|
|
|
response = resp['data']['insert_user']['returning']
|
|
|
|
return response, HTTPStatus.OK
|
|
|
|
|
2020-03-20 09:46:45 +03:00
|
|
|
def mirror_action(self):
|
|
|
|
response = self.req_json['input']['arg']
|
|
|
|
return response, HTTPStatus.OK
|
|
|
|
|
|
|
|
|
2020-02-13 20:38:23 +03:00
|
|
|
def check_email(self, email):
|
|
|
|
regex = '^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
|
|
|
|
return re.search(regex,email)
|
|
|
|
|
|
|
|
def execute_query(self, query):
|
|
|
|
headers = {}
|
|
|
|
admin_secret = self.hge_ctx.hge_key
|
|
|
|
if admin_secret is not None:
|
|
|
|
headers['X-Hasura-Admin-Secret'] = admin_secret
|
|
|
|
code, resp, _ = self.hge_ctx.anyq('/v1/graphql', query, headers)
|
|
|
|
self.log_message(json.dumps(resp))
|
|
|
|
return code, resp
|
|
|
|
|
|
|
|
def _send_response(self, status, body):
|
|
|
|
self.send_response(status)
|
|
|
|
self.send_header('Content-Type', 'application/json')
|
2020-03-20 09:46:45 +03:00
|
|
|
self.send_header('Set-Cookie', 'abcd')
|
2020-02-13 20:38:23 +03:00
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(json.dumps(body).encode("utf-8"))
|
|
|
|
|
|
|
|
|
|
|
|
class ActionsWebhookServer(http.server.HTTPServer):
|
|
|
|
def __init__(self, hge_ctx, server_address):
|
|
|
|
handler = ActionsWebhookHandler
|
|
|
|
handler.hge_ctx = hge_ctx
|
|
|
|
super().__init__(server_address, handler)
|
|
|
|
|
|
|
|
def server_bind(self):
|
|
|
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
self.socket.bind(self.server_address)
|
|
|
|
|
2019-04-08 10:22:38 +03:00
|
|
|
class EvtsWebhookHandler(http.server.BaseHTTPRequestHandler):
|
2018-09-18 09:21:57 +03:00
|
|
|
def do_GET(self):
|
|
|
|
self.send_response(HTTPStatus.OK)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_POST(self):
|
2018-11-23 16:02:46 +03:00
|
|
|
content_len = self.headers.get('Content-Length')
|
|
|
|
req_body = self.rfile.read(int(content_len)).decode("utf-8")
|
|
|
|
req_json = json.loads(req_body)
|
|
|
|
req_headers = self.headers
|
|
|
|
req_path = self.path
|
|
|
|
self.log_message(json.dumps(req_json))
|
|
|
|
if req_path == "/fail":
|
2018-09-19 15:12:57 +03:00
|
|
|
self.send_response(HTTPStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
self.end_headers()
|
2018-11-23 16:02:46 +03:00
|
|
|
self.server.error_queue.put({"path": req_path,
|
|
|
|
"body": req_json,
|
|
|
|
"headers": req_headers})
|
2019-02-14 10:37:59 +03:00
|
|
|
elif req_path == "/timeout_short":
|
|
|
|
time.sleep(5)
|
|
|
|
self.send_response(HTTPStatus.NO_CONTENT)
|
|
|
|
self.end_headers()
|
|
|
|
self.server.error_queue.put({"path": req_path,
|
|
|
|
"body": req_json,
|
|
|
|
"headers": req_headers})
|
|
|
|
elif req_path == "/timeout_long":
|
|
|
|
time.sleep(5)
|
|
|
|
self.send_response(HTTPStatus.NO_CONTENT)
|
|
|
|
self.end_headers()
|
|
|
|
self.server.resp_queue.put({"path": req_path,
|
|
|
|
"body": req_json,
|
|
|
|
"headers": req_headers})
|
2018-09-19 15:12:57 +03:00
|
|
|
else:
|
|
|
|
self.send_response(HTTPStatus.NO_CONTENT)
|
|
|
|
self.end_headers()
|
2018-11-23 16:02:46 +03:00
|
|
|
self.server.resp_queue.put({"path": req_path,
|
|
|
|
"body": req_json,
|
|
|
|
"headers": req_headers})
|
2018-09-19 15:12:57 +03:00
|
|
|
|
2020-03-11 09:27:31 +03:00
|
|
|
# A very slightly more sane/performant http server.
|
2020-03-20 09:46:45 +03:00
|
|
|
# See: https://stackoverflow.com/a/14089457/176841
|
2020-03-11 09:27:31 +03:00
|
|
|
#
|
|
|
|
# TODO use this elsewhere, or better yet: use e.g. bottle + waitress
|
|
|
|
class ThreadedHTTPServer(ThreadingMixIn, http.server.HTTPServer):
|
|
|
|
"""Handle requests in a separate thread."""
|
|
|
|
|
|
|
|
class EvtsWebhookServer(ThreadedHTTPServer):
|
2019-04-08 10:22:38 +03:00
|
|
|
def __init__(self, server_address):
|
|
|
|
self.resp_queue = queue.Queue(maxsize=1)
|
|
|
|
self.error_queue = queue.Queue()
|
|
|
|
super().__init__(server_address, EvtsWebhookHandler)
|
2018-09-18 09:21:57 +03:00
|
|
|
|
2018-10-04 15:44:15 +03:00
|
|
|
def server_bind(self):
|
|
|
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
self.socket.bind(self.server_address)
|
|
|
|
|
2019-04-08 10:22:38 +03:00
|
|
|
def get_event(self, timeout):
|
|
|
|
return self.resp_queue.get(timeout=timeout)
|
|
|
|
|
|
|
|
def get_error_queue_size(self):
|
|
|
|
sz = 0
|
|
|
|
while not self.error_queue.empty():
|
|
|
|
self.error_queue.get()
|
|
|
|
sz = sz + 1
|
|
|
|
return sz
|
|
|
|
|
|
|
|
def teardown(self):
|
|
|
|
self.evt_trggr_httpd.shutdown()
|
|
|
|
self.evt_trggr_httpd.server_close()
|
|
|
|
graphql_server.stop_server(self.graphql_server)
|
|
|
|
self.gql_srvr_thread.join()
|
|
|
|
self.evt_trggr_web_server.join()
|
|
|
|
|
|
|
|
class HGECtxGQLServer:
|
2020-03-26 14:52:20 +03:00
|
|
|
def __init__(self, hge_urls, port=5000):
|
2019-04-08 10:22:38 +03:00
|
|
|
# start the graphql server
|
2020-03-26 14:52:20 +03:00
|
|
|
self.port = port
|
|
|
|
self._hge_urls = hge_urls
|
|
|
|
self.is_running = False
|
|
|
|
self.start_server()
|
|
|
|
|
|
|
|
def start_server(self):
|
|
|
|
if not self.is_running:
|
|
|
|
self.graphql_server = graphql_server.create_server('127.0.0.1', self.port)
|
|
|
|
self.hge_urls = graphql_server.set_hge_urls(self._hge_urls)
|
|
|
|
self.gql_srvr_thread = threading.Thread(target=self.graphql_server.serve_forever)
|
|
|
|
self.gql_srvr_thread.start()
|
|
|
|
self.is_running = True
|
2019-04-08 10:22:38 +03:00
|
|
|
|
|
|
|
def teardown(self):
|
2020-03-26 14:52:20 +03:00
|
|
|
self.stop_server()
|
2019-04-08 10:22:38 +03:00
|
|
|
|
2020-03-26 14:52:20 +03:00
|
|
|
def stop_server(self):
|
|
|
|
if self.is_running:
|
|
|
|
graphql_server.stop_server(self.graphql_server)
|
|
|
|
self.gql_srvr_thread.join()
|
|
|
|
self.is_running = False
|
2018-10-30 12:21:58 +03:00
|
|
|
|
2018-09-18 09:21:57 +03:00
|
|
|
class HGECtx:
|
2019-04-08 10:22:38 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
def __init__(self, hge_url, pg_url, config):
|
2018-09-18 09:21:57 +03:00
|
|
|
|
|
|
|
self.http = requests.Session()
|
2020-04-16 09:45:21 +03:00
|
|
|
self.hge_key = config.getoption('--hge-key')
|
2019-04-08 10:22:38 +03:00
|
|
|
self.hge_url = hge_url
|
|
|
|
self.pg_url = pg_url
|
2020-02-13 12:14:02 +03:00
|
|
|
self.hge_webhook = config.getoption('--hge-webhook')
|
|
|
|
hge_jwt_key_file = config.getoption('--hge-jwt-key-file')
|
2018-10-28 21:27:49 +03:00
|
|
|
if hge_jwt_key_file is None:
|
|
|
|
self.hge_jwt_key = None
|
|
|
|
else:
|
|
|
|
with open(hge_jwt_key_file) as f:
|
|
|
|
self.hge_jwt_key = f.read()
|
2020-02-13 12:14:02 +03:00
|
|
|
self.hge_jwt_conf = config.getoption('--hge-jwt-conf')
|
2020-04-16 09:45:21 +03:00
|
|
|
if self.hge_jwt_conf is not None:
|
|
|
|
self.hge_jwt_conf_dict = json.loads(self.hge_jwt_conf)
|
2020-02-13 12:14:02 +03:00
|
|
|
self.webhook_insecure = config.getoption('--test-webhook-insecure')
|
|
|
|
self.metadata_disabled = config.getoption('--test-metadata-disabled')
|
2018-10-28 21:27:49 +03:00
|
|
|
self.may_skip_test_teardown = False
|
2018-09-18 09:21:57 +03:00
|
|
|
|
2019-04-08 10:22:38 +03:00
|
|
|
self.engine = create_engine(self.pg_url)
|
|
|
|
self.meta = MetaData()
|
2018-11-26 16:08:16 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
self.ws_read_cookie = config.getoption('--test-ws-init-cookie')
|
2019-03-04 10:46:53 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
self.hge_scale_url = config.getoption('--test-hge-scale-url')
|
|
|
|
self.avoid_err_msg_checks = config.getoption('--avoid-error-message-checks')
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2019-05-10 09:05:11 +03:00
|
|
|
self.ws_client = GQLWsClient(self, '/v1/graphql')
|
2019-04-08 10:22:38 +03:00
|
|
|
|
2020-03-20 09:46:45 +03:00
|
|
|
# HGE version
|
2018-10-28 21:27:49 +03:00
|
|
|
result = subprocess.run(['../../scripts/get-version.sh'], shell=False, stdout=subprocess.PIPE, check=True)
|
2019-11-26 15:14:21 +03:00
|
|
|
env_version = os.getenv('VERSION')
|
|
|
|
self.version = env_version if env_version else result.stdout.decode('utf-8').strip()
|
2020-02-13 12:14:02 +03:00
|
|
|
if not self.metadata_disabled and not config.getoption('--skip-schema-setup'):
|
2019-02-28 16:53:03 +03:00
|
|
|
try:
|
|
|
|
st_code, resp = self.v1q_f('queries/clear_db.yaml')
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
self.teardown()
|
|
|
|
raise HGECtxError(repr(e))
|
|
|
|
assert st_code == 200, resp
|
2018-09-18 09:21:57 +03:00
|
|
|
|
2020-03-20 09:46:45 +03:00
|
|
|
# Postgres version
|
|
|
|
pg_version_text = self.sql('show server_version_num').fetchone()['server_version_num']
|
|
|
|
self.pg_version = int(pg_version_text)
|
|
|
|
|
|
|
|
|
2018-09-18 09:21:57 +03:00
|
|
|
def reflect_tables(self):
|
|
|
|
self.meta.reflect(bind=self.engine)
|
|
|
|
|
2018-10-30 12:21:58 +03:00
|
|
|
def anyq(self, u, q, h):
|
2018-09-18 09:21:57 +03:00
|
|
|
resp = self.http.post(
|
|
|
|
self.hge_url + u,
|
|
|
|
json=q,
|
|
|
|
headers=h
|
|
|
|
)
|
2019-09-04 18:02:35 +03:00
|
|
|
# NOTE: make sure we preserve key ordering so we can test the ordering
|
|
|
|
# properties in the graphql spec properly
|
2019-12-25 06:35:32 +03:00
|
|
|
# Returning response headers to get the request id from response
|
|
|
|
return resp.status_code, resp.json(object_pairs_hook=OrderedDict), resp.headers
|
2018-09-18 09:21:57 +03:00
|
|
|
|
2019-02-28 16:53:03 +03:00
|
|
|
def sql(self, q):
|
|
|
|
conn = self.engine.connect()
|
|
|
|
res = conn.execute(q)
|
|
|
|
conn.close()
|
|
|
|
return res
|
|
|
|
|
2019-01-28 09:12:52 +03:00
|
|
|
def v1q(self, q, headers = {}):
|
|
|
|
h = headers.copy()
|
2018-10-28 21:27:49 +03:00
|
|
|
if self.hge_key is not None:
|
2019-02-14 12:37:47 +03:00
|
|
|
h['X-Hasura-Admin-Secret'] = self.hge_key
|
2018-09-18 09:21:57 +03:00
|
|
|
resp = self.http.post(
|
|
|
|
self.hge_url + "/v1/query",
|
2018-10-28 21:27:49 +03:00
|
|
|
json=q,
|
|
|
|
headers=h
|
2018-09-18 09:21:57 +03:00
|
|
|
)
|
2019-09-04 18:02:35 +03:00
|
|
|
# NOTE: make sure we preserve key ordering so we can test the ordering
|
|
|
|
# properties in the graphql spec properly
|
|
|
|
return resp.status_code, resp.json(object_pairs_hook=OrderedDict)
|
2018-09-18 09:21:57 +03:00
|
|
|
|
|
|
|
def v1q_f(self, fn):
|
|
|
|
with open(fn) as f:
|
2019-11-14 19:32:11 +03:00
|
|
|
# NOTE: preserve ordering with ruamel
|
|
|
|
yml = yaml.YAML()
|
|
|
|
return self.v1q(yml.load(f))
|
2018-09-18 09:21:57 +03:00
|
|
|
|
|
|
|
def teardown(self):
|
|
|
|
self.http.close()
|
|
|
|
self.engine.dispose()
|