From 1cd9fc376de6c69baf5d54a210621c2875f1bfa2 Mon Sep 17 00:00:00 2001 From: Abby Sassel <3883855+sassela@users.noreply.github.com> Date: Wed, 21 Jul 2021 18:22:08 +0100 Subject: [PATCH] server/mysql: integrate MySQL tests into dev.sh workflow https://github.com/hasura/graphql-engine-mono/pull/1848 GitOrigin-RevId: 2bd7c0a18448f042a1e35d21aaf42232acf48a46 --- scripts/containers/mysql.sh | 50 +++++++++++++++++++ scripts/data-sources-util.sh | 15 ++++++ scripts/dev.sh | 36 +++++++++++++ scripts/parse-pytest-backend | 3 ++ server/src-lib/Hasura/Backends/MySQL/Types.hs | 2 +- .../graphql_query/mysql/replace_metadata.yaml | 6 +-- server/tests-py/test_graphql_queries.py | 10 ++-- 7 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 scripts/containers/mysql.sh diff --git a/scripts/containers/mysql.sh b/scripts/containers/mysql.sh new file mode 100644 index 00000000000..5819ebc5110 --- /dev/null +++ b/scripts/containers/mysql.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +### This file is not meant to be run directly, but to be sourced from +### the dev script. It defines all the functions required to run an +### MySQL docker container. + + +###################### +# Configuration # +###################### + +if [ "$MODE" = "test" ]; then + MYSQL_PORT=33306 +else + MYSQL_PORT=23306 +fi + +MYSQL_USER=root +MYSQL_PASSWORD=hasuraMySQL1 +MYSQL_CONTAINER_NAME="hasura-dev-mysql-$MYSQL_PORT" +# space deliberately omitted between -u and -p params https://hub.docker.com/_/mysql +MYSQL_DOCKER="docker exec -it $MYSQL_CONTAINER_NAME mysql -u$MYSQL_USER -p$MYSQL_PASSWORD" + +###################### +# Functions # +###################### + +function mysql_launch_container(){ + echo "Launching MySQL container: $MYSQL_CONTAINER_NAME" + docker run --name $MYSQL_CONTAINER_NAME \ + -e MYSQL_ROOT_PASSWORD=$MYSQL_PASSWORD \ + -e MYSQL_DATABASE=hasura \ + -p 127.0.0.1:$MYSQL_PORT:3306 \ + -d mysql:8.0 -h "127.0.0.1" +} + +function mysql_wait { + echo -n "Waiting for mysql to come up" + until ( $MYSQL_DOCKER -e 'SELECT 1' ) &>/dev/null; do + echo -n '.' && sleep 0.2 + done + echo " Ok" +} + +function mysql_cleanup(){ + echo "Removing $MYSQL_CONTAINER_NAME and its volumes in 5 seconds!" + echo " PRESS CTRL-C TO ABORT removal of all containers, or ENTER to clean up right away" + read -rt5 || true + docker stop "$MYSQL_CONTAINER_NAME" + docker rm -v "$MYSQL_CONTAINER_NAME" +} diff --git a/scripts/data-sources-util.sh b/scripts/data-sources-util.sh index 941c4e24053..ca330dbdd1e 100644 --- a/scripts/data-sources-util.sh +++ b/scripts/data-sources-util.sh @@ -13,6 +13,9 @@ function add_sources() { mssql) add_mssql_source "$hasura_graphql_server_port" "$MSSQL_DB_URL" ;; + mysql) + add_mysql_source "$hasura_graphql_server_port" "$MSSQL_DB_URL" + ;; # bigquery deliberately omitted as its test setup is atypical. See: # https://github.com/hasura/graphql-engine/blob/master/server/CONTRIBUTING.md#running-the-python-test-suite-on-bigquery esac @@ -52,3 +55,15 @@ function add_mssql_source() { curl "$metadata_url" \ --data-raw '{"type":"mssql_add_source","args":{"name":"mssql","configuration":{"connection_info":{"connection_string":"'"$connection_string"'"}}}}' } + +function add_mysql_source() { + hasura_graphql_server_port=${1} + # connection_string currently unused as mysql_add_source not yet supported + # connection_string=${2} + metadata_url=http://127.0.0.1:$hasura_graphql_server_port/v1/metadata + + echo "" + echo "Adding MySQL source" + curl "$metadata_url" \ + --data-raw '{"type":"replace_metadata","args":{"version":3,"sources":[{"name":"mysql","kind":"mysql","tables":[],"configuration":{"database":"hasura","user":"'"$MYSQL_USER"'","password":"'"$MYSQL_PASSWORD"'","host":"127.0.0.1","port":'"$MYSQL_PORT"',"pool_settings":{}}}]}}' +} diff --git a/scripts/dev.sh b/scripts/dev.sh index 1a9b4df4734..d4aff48cdc8 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -50,6 +50,10 @@ Available COMMANDs: Launch a Citus single-node container suitable for use with graphql-engine, watch its logs, clean up nicely after + mysql + Launch a MySQL container suitable for use with graphql-engine, watch its + logs, clean up nicely after + test [--integration [pytest_args...] | --unit | --hlint] Run the unit and integration tests, handling spinning up all dependencies. This will force a recompile. A combined code coverage report will be @@ -96,6 +100,8 @@ case "${1-}" in ;; citus) ;; + mysql) + ;; test) case "${2-}" in --unit) @@ -161,11 +167,13 @@ fi source scripts/containers/postgres source scripts/containers/mssql source scripts/containers/citus +source scripts/containers/mysql.sh source scripts/data-sources-util.sh PG_RUNNING=0 MSSQL_RUNNING=0 CITUS_RUNNING=0 +MYSQL_RUNNING=0 function cleanup { echo @@ -178,6 +186,7 @@ function cleanup { if [ $PG_RUNNING -eq 1 ]; then pg_cleanup; fi if [ $MSSQL_RUNNING -eq 1 ]; then mssql_cleanup; fi if [ $CITUS_RUNNING -eq 1 ]; then citus_cleanup; fi + if [ $MYSQL_RUNNING -eq 1 ]; then mysql_cleanup; fi echo_pretty "Done" } @@ -202,6 +211,13 @@ function citus_start() { citus_wait } +function mysql_start() { + mysql_launch_container + MYSQL_RUNNING=1 + mysql_wait +} + + function start_dbs() { # always launch the postgres container pg_start @@ -213,6 +229,9 @@ function start_dbs() { mssql) mssql_start ;; + mysql) + mysql_start + ;; # bigquery deliberately omitted as its test setup is atypical. See: # https://github.com/hasura/graphql-engine/blob/master/server/CONTRIBUTING.md#running-the-python-test-suite-on-bigquery esac @@ -388,6 +407,23 @@ elif [ "$MODE" = "citus" ]; then echo_pretty "" docker logs -f --tail=0 "$CITUS_CONTAINER_NAME" +################################# +### MySQL Container ### +################################# + +elif [ "$MODE" = "mysql" ]; then + mysql_start + echo_pretty "MYSQL logs will start to show up in realtime here. Press CTRL-C to exit and " + echo_pretty "shutdown this container." + echo_pretty "" + echo_pretty "You can use the following to connect to the running instance:" + echo_pretty " $ $MYSQL_DOCKER" + echo_pretty "" + echo_pretty "If you want to import a SQL file into MYSQL:" + echo_pretty " $ $MYSQL_DOCKER -i " + echo_pretty "" + docker logs -f --tail=0 "$MYSQL_CONTAINER_NAME" + elif [ "$MODE" = "test" ]; then ######################################## diff --git a/scripts/parse-pytest-backend b/scripts/parse-pytest-backend index 62563da8038..64661554bcd 100644 --- a/scripts/parse-pytest-backend +++ b/scripts/parse-pytest-backend @@ -9,6 +9,7 @@ Invalid --backend argument. Available backends: bigquery citus mssql + mysql EOL exit 1 @@ -47,6 +48,8 @@ case "$BACKEND" in ;; mssql) ;; + mysql) + ;; *) die_backends ;; diff --git a/server/src-lib/Hasura/Backends/MySQL/Types.hs b/server/src-lib/Hasura/Backends/MySQL/Types.hs index bbfeb8bdbfe..c5d539c5e97 100644 --- a/server/src-lib/Hasura/Backends/MySQL/Types.hs +++ b/server/src-lib/Hasura/Backends/MySQL/Types.hs @@ -57,7 +57,7 @@ $(J.deriveToJSON hasuraJSON ''ConnPoolSettings) -- | Partial of Database.MySQL.Simple.ConnectInfo data ConnSourceConfig = ConnSourceConfig - { _cscHost :: !Text -- ^ Works with @127.0.0.1@ but not with @localhost@ for some reason + { _cscHost :: !Text -- ^ Works with @127.0.0.1@ but not with @localhost@: https://mariadb.com/kb/en/troubleshooting-connection-issues/#localhost-and , _cscPort :: !Word16 , _cscUser :: !Text , _cscPassword :: !Text diff --git a/server/tests-py/queries/graphql_query/mysql/replace_metadata.yaml b/server/tests-py/queries/graphql_query/mysql/replace_metadata.yaml index f1f9232c616..892268956a0 100644 --- a/server/tests-py/queries/graphql_query/mysql/replace_metadata.yaml +++ b/server/tests-py/queries/graphql_query/mysql/replace_metadata.yaml @@ -12,9 +12,9 @@ query: kind: mysql configuration: host: '127.0.0.1' - port: 3306 - user: hasura - password: password + port: 33306 + user: root + password: hasuraMySQL1 database: hasura pool_settings: {} tables: diff --git a/server/tests-py/test_graphql_queries.py b/server/tests-py/test_graphql_queries.py index 8c41eaf4d07..fac043be704 100644 --- a/server/tests-py/test_graphql_queries.py +++ b/server/tests-py/test_graphql_queries.py @@ -9,15 +9,13 @@ pytestmark = pytest.mark.allow_server_upgrade_test usefixtures = pytest.mark.usefixtures -@pytest.mark.parametrize("transport", ['http', 'websocket']) @pytest.mark.parametrize("backend", ['mysql']) @usefixtures('per_class_tests_db_state') class TestGraphQLQueryBasicMySQL: - # initialize the metadata - def test_replace_metadata(self, hge_ctx, transport): - if transport == 'http': - check_query_f(hge_ctx, self.dir() + '/replace_metadata.yaml') + # initialize the metadata with default 'http' transport fixture + def test_replace_metadata(self, hge_ctx): + check_query_f(hge_ctx, self.dir() + '/replace_metadata.yaml') @classmethod def dir(cls): @@ -1114,7 +1112,7 @@ class TestGraphQLExplain: if req_st != 200: # return early in case we're testing for failures return - + # Comparing only with generated 'sql' since the 'plan' may differ resp_sql = resp_json[0]['sql'] exp_sql = conf['response'][0]['sql']