Skip to content

Commit 9cf33e9

Browse files
authored
[tests] Helper script to run single tests (apache#9547)
1 parent 7cefc89 commit 9cf33e9

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed

docker-compose.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ version: "3.7"
3636
services:
3737
redis:
3838
image: redis:3.2
39+
container_name: superset_cache
3940
restart: unless-stopped
4041
ports:
4142
- "127.0.0.1:6379:6379"
@@ -45,16 +46,18 @@ services:
4546
db:
4647
env_file: docker/.env
4748
image: postgres:10
49+
container_name: superset_db
4850
restart: unless-stopped
4951
ports:
5052
- "127.0.0.1:5432:5432"
5153
volumes:
5254
- db_home:/var/lib/postgresql/data
5355

5456
superset:
57+
env_file: docker/.env
5558
build: *superset-build
59+
container_name: superset_app
5660
command: ["flask", "run", "-p", "8088", "--with-threads", "--reload", "--debugger", "--host=0.0.0.0"]
57-
env_file: docker/.env
5861
restart: unless-stopped
5962
ports:
6063
- 8088:8088
@@ -63,20 +66,23 @@ services:
6366

6467
superset-init:
6568
build: *superset-build
69+
container_name: superset_init
6670
command: ["/app/docker-init.sh"]
6771
env_file: docker/.env
6872
depends_on: *superset-depends-on
6973
volumes: *superset-volumes
7074

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

7883
superset-worker:
7984
build: *superset-build
85+
container_name: superset_worker
8086
command: ["celery", "worker", "--app=superset.tasks.celery_app:app", "-Ofair"]
8187
env_file: docker/.env
8288
restart: unless-stopped

scripts/tests/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
20+
# Utility script to run tests faster
21+
22+
By default tests will be run using the Postgres container defined at the `docker-compose` file on the root of the repo,
23+
so prior to using this script make sure to launch the dev containers.
24+
25+
You can use a different DB backend by defining `SUPERSET__SQLALCHEMY_DATABASE_URI` env var.
26+
27+
## Use:
28+
29+
From the superset repo root directory:
30+
31+
- Example run a single test module:
32+
```$bash
33+
scripts/tests/run.sh tests.charts.api_tests
34+
```
35+
36+
- Example run a single test:
37+
```$bash
38+
scripts/tests/run.sh tests.charts.api_tests:ChartApiTests.test_get_charts
39+
```
40+
41+
- Example run a single test, without any init procedures. Init procedures include:
42+
resetting test database, db upgrade, superset init, loading example data. If your tests
43+
are idempotent, after the first run, subsequent runs are really fast
44+
```$bash
45+
scripts/tests/run.sh tests.charts.api_tests:ChartApiTests.test_get_charts --no-init
46+
```
47+
48+
- Example for not recreating the test DB (will still run all the tests init procedures)
49+
```$bash
50+
scripts/tests/run.sh tests.charts.api_tests:ChartApiTests.test_get_charts --no-reset-db
51+
```

scripts/tests/run.sh

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env bash
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
set -e
21+
22+
#
23+
# Reset test DATABASE
24+
#
25+
function reset_db() {
26+
echo --------------------
27+
echo Reseting test DB
28+
echo --------------------
29+
docker exec -i superset_db bash -c "/usr/bin/psql -h 127.0.0.1 -U ${DB_USER} -w -c 'DROP DATABASE ${DB_NAME};'"
30+
docker exec -i superset_db bash -c "/usr/bin/psql -h 127.0.0.1 -U ${DB_USER} -w -c 'CREATE DATABASE ${DB_NAME};'"
31+
}
32+
33+
#
34+
# Run init test procedures
35+
#
36+
function test_init() {
37+
echo --------------------
38+
echo Upgrading
39+
echo --------------------
40+
superset db upgrade
41+
echo --------------------
42+
echo Superset init
43+
echo --------------------
44+
superset init
45+
echo --------------------
46+
echo Load examples
47+
echo --------------------
48+
nosetests tests/load_examples_test.py
49+
}
50+
51+
52+
if [[ "$#" -eq "0" ]]
53+
then
54+
echo "No argument suplied"
55+
echo ------------------------
56+
echo use:
57+
echo "run.sh <test module name> [options]"
58+
echo "[options]:"
59+
echo "--no-init: Dont restart docker and no db migrations, superset init and test data"
60+
echo "--no-reset-db: Recreates test database (DROP, CREATE)"
61+
exit 1
62+
fi
63+
64+
#
65+
# Init global vars
66+
#
67+
DB_NAME="test"
68+
DB_USER="superset"
69+
DB_PASSWORD="superset"
70+
export SUPERSET__SQLALCHEMY_DATABASE_URI=${SUPERSET__SQLALCHEMY_DATABASE_URI:-postgresql+psycopg2://"${DB_USER}":"${DB_PASSWORD}"@localhost/"${DB_NAME}"}
71+
export SUPERSET_CONFIG=${SUPERSET_CONFIG:-tests.superset_test_config}
72+
RUN_INIT=1
73+
RUN_RESET_DB=1
74+
TEST_MODULE="${1}"
75+
76+
# Shift to pass the first cmd parameter for the test module
77+
shift 1
78+
79+
PARAMS=""
80+
while (( "$#" )); do
81+
case "$1" in
82+
--no-init)
83+
RUN_INIT=0
84+
RUN_RESET_DB=0
85+
shift 1
86+
;;
87+
--no-reset-db)
88+
RUN_RESET_DB=0
89+
shift 1
90+
;;
91+
--) # end argument parsing
92+
shift
93+
break
94+
;;
95+
--*) # unsupported flags
96+
echo "Error: Unsupported flag $1" >&2
97+
exit 1
98+
;;
99+
*) # preserve positional arguments
100+
PARAMS="$PARAMS $1"
101+
shift
102+
;;
103+
esac
104+
done
105+
106+
echo ------------------------------------
107+
echo DB_URI="${SUPERSET__SQLALCHEMY_DATABASE_URI}"
108+
echo Superset config module="${SUPERSET_CONFIG}"
109+
echo Run init procedures=$RUN_INIT
110+
echo Run reset DB=$RUN_RESET_DB
111+
echo Test to run:"${TEST_MODULE}"
112+
echo ------------------------------------
113+
114+
115+
if [ $RUN_RESET_DB -eq 1 ]
116+
then
117+
reset_db
118+
fi
119+
120+
if [ $RUN_INIT -eq 1 ]
121+
then
122+
test_init
123+
fi
124+
125+
nosetests --exclude=load_examples_test "${TEST_MODULE}"

0 commit comments

Comments
 (0)