Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] Helper script to run single tests #9547

Merged
merged 6 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ version: "3.7"
services:
redis:
image: redis:3.2
container_name: superset_cache
restart: unless-stopped
ports:
- "127.0.0.1:6379:6379"
Expand All @@ -45,16 +46,18 @@ services:
db:
env_file: docker/.env
image: postgres:10
container_name: superset_db
restart: unless-stopped
ports:
- "127.0.0.1:5432:5432"
volumes:
- db_home:/var/lib/postgresql/data

superset:
env_file: docker/.env
build: *superset-build
container_name: superset_app
command: ["flask", "run", "-p", "8088", "--with-threads", "--reload", "--debugger", "--host=0.0.0.0"]
env_file: docker/.env
restart: unless-stopped
ports:
- 8088:8088
Expand All @@ -63,20 +66,23 @@ services:

superset-init:
build: *superset-build
container_name: superset_init
command: ["/app/docker-init.sh"]
env_file: docker/.env
depends_on: *superset-depends-on
volumes: *superset-volumes

superset-node:
image: node:10-jessie
container_name: superset_node
command: ["bash", "-c", "cd /app/superset-frontend && npm install --global webpack webpack-cli && npm install && npm run dev"]
env_file: docker/.env
depends_on: *superset-depends-on
volumes: *superset-volumes

superset-worker:
build: *superset-build
container_name: superset_worker
command: ["celery", "worker", "--app=superset.tasks.celery_app:app", "-Ofair"]
env_file: docker/.env
restart: unless-stopped
Expand Down
51 changes: 51 additions & 0 deletions scripts/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

# Utility script to run tests faster

By default tests will be run using the Postgres container defined at the `docker-compose` file on the root of the repo,
so prior to using this script make sure to launch the dev containers.

You can use a different DB backend by defining `SUPERSET__SQLALCHEMY_DATABASE_URI` env var.

## Use:

From the superset repo root directory:

- Example run a single test module:
```$bash
scripts/tests/run.sh tests.charts.api_tests
```

- Example run a single test:
```$bash
scripts/tests/run.sh tests.charts.api_tests:ChartApiTests.test_get_charts
```

- Example run a single test, without any init procedures. Init procedures include:
resetting test database, db upgrade, superset init, loading example data. If your tests
are idempotent, after the first run, subsequent runs are really fast
```$bash
scripts/tests/run.sh tests.charts.api_tests:ChartApiTests.test_get_charts --no-init
```

- Example for not recreating the test DB (will still run all the tests init procedures)
```$bash
scripts/tests/run.sh tests.charts.api_tests:ChartApiTests.test_get_charts --no-reset-db
```
125 changes: 125 additions & 0 deletions scripts/tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

set -e

#
# Reset test DATABASE
#
function reset_db() {
echo --------------------
echo Reseting test DB
echo --------------------
docker exec -i superset_db bash -c "/usr/bin/psql -h 127.0.0.1 -U ${DB_USER} -w -c 'DROP DATABASE ${DB_NAME};'"
docker exec -i superset_db bash -c "/usr/bin/psql -h 127.0.0.1 -U ${DB_USER} -w -c 'CREATE DATABASE ${DB_NAME};'"
}

#
# Run init test procedures
#
function test_init() {
echo --------------------
echo Upgrading
echo --------------------
superset db upgrade
echo --------------------
echo Superset init
echo --------------------
superset init
echo --------------------
echo Load examples
echo --------------------
nosetests tests/load_examples_test.py
}


if [[ "$#" -eq "0" ]]
then
echo "No argument suplied"
echo ------------------------
echo use:
echo "run.sh <test module name> [options]"
echo "[options]:"
echo "--no-init: Dont restart docker and no db migrations, superset init and test data"
echo "--no-reset-db: Recreates test database (DROP, CREATE)"
exit 1
fi

#
# Init global vars
#
DB_NAME="test"
DB_USER="superset"
DB_PASSWORD="superset"
export SUPERSET__SQLALCHEMY_DATABASE_URI=${SUPERSET__SQLALCHEMY_DATABASE_URI:-postgresql+psycopg2://"${DB_USER}":"${DB_PASSWORD}"@localhost/"${DB_NAME}"}
export SUPERSET_CONFIG=${SUPERSET_CONFIG:-tests.superset_test_config}
RUN_INIT=1
RUN_RESET_DB=1
TEST_MODULE="${1}"

# Shift to pass the first cmd parameter for the test module
shift 1

PARAMS=""
while (( "$#" )); do
case "$1" in
--no-init)
RUN_INIT=0
RUN_RESET_DB=0
shift 1
;;
--no-reset-db)
RUN_RESET_DB=0
shift 1
;;
--) # end argument parsing
shift
break
;;
--*) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done

echo ------------------------------------
echo DB_URI="${SUPERSET__SQLALCHEMY_DATABASE_URI}"
echo Superset config module="${SUPERSET_CONFIG}"
echo Run init procedures=$RUN_INIT
echo Run reset DB=$RUN_RESET_DB
echo Test to run:"${TEST_MODULE}"
echo ------------------------------------


if [ $RUN_RESET_DB -eq 1 ]
then
reset_db
fi

if [ $RUN_INIT -eq 1 ]
then
test_init
fi

nosetests --exclude=load_examples_test "${TEST_MODULE}"