Testing

Python Testing

All python tests are carried out in tox a standardized testing framework. All python tests can be run with any of the tox environments, via,

  1. tox -e <environment>

For example,

  1. tox -e py38

Alternatively, you can run all tests in a single file via,

  1. tox -e <environment> -- tests/test_file.py

or for a specific test via,

  1. tox -e <environment> -- tests/test_file.py::TestClassName::test_method_name

Note that the test environment uses a temporary directory for defining the SQLite databases which will be cleared each time before the group of test commands are invoked.

There is also a utility script included in the Superset codebase to run python integration tests. The readme can be found here

To run all integration tests for example, run this script from the root directory:

  1. scripts/tests/run.sh

You can run unit tests found in ‘./tests/unit_tests’ for example with pytest. It is a simple way to run an isolated test that doesn’t need any database setup

  1. pytest ./link_to_test.py

Frontend Testing

We use Jest and Enzyme to test TypeScript/JavaScript. Tests can be run with:

  1. cd superset-frontend
  2. npm run test

To run a single test file:

  1. npm run test -- path/to/file.js

Integration Testing

We use Cypress for integration tests. Tests can be run by tox -e cypress. To open Cypress and explore tests first setup and run test server:

  1. export SUPERSET_CONFIG=tests.integration_tests.superset_test_config
  2. export SUPERSET_TESTENV=true
  3. export CYPRESS_BASE_URL="http://localhost:8081"
  4. superset db upgrade
  5. superset load_test_users
  6. superset load-examples --load-test-data
  7. superset init
  8. superset run --port 8081

Run Cypress tests:

  1. cd superset-frontend
  2. npm run build-instrumented
  3. cd cypress-base
  4. npm install
  5. # run tests via headless Chrome browser (requires Chrome 64+)
  6. npm run cypress-run-chrome
  7. # run tests from a specific file
  8. npm run cypress-run-chrome -- --spec cypress/integration/explore/link.test.ts
  9. # run specific file with video capture
  10. npm run cypress-run-chrome -- --spec cypress/integration/dashboard/index.test.js --config video=true
  11. # to open the cypress ui
  12. npm run cypress-debug
  13. # to point cypress to a url other than the default (http://localhost:8088) set the environment variable before running the script
  14. # e.g., CYPRESS_BASE_URL="http://localhost:9000"
  15. CYPRESS_BASE_URL=<your url> npm run cypress open

See superset-frontend/cypress_build.sh.

As an alternative you can use docker-compose environment for testing:

Make sure you have added below line to your /etc/hosts file: 127.0.0.1 db

If you already have launched Docker environment please use the following command to assure a fresh database instance: docker-compose down -v

Launch environment:

CYPRESS_CONFIG=true docker-compose up

It will serve backend and frontend on port 8088.

Run Cypress tests:

  1. cd cypress-base
  2. npm install
  3. npm run cypress open

Debugging Server App

Follow these instructions to debug the Flask app running inside a docker container.

First add the following to the ./docker-compose.yaml file

  1. superset:
  2. env_file: docker/.env
  3. image: *superset-image
  4. container_name: superset_app
  5. command: ["/app/docker/docker-bootstrap.sh", "app"]
  6. restart: unless-stopped
  7. + cap_add:
  8. + - SYS_PTRACE
  9. ports:
  10. - 8088:8088
  11. + - 5678:5678
  12. user: "root"
  13. depends_on: *superset-depends-on
  14. volumes: *superset-volumes
  15. environment:
  16. CYPRESS_CONFIG: "${CYPRESS_CONFIG}"

Start Superset as usual

  1. docker-compose up

Install the required libraries and packages to the docker container

Enter the superset_app container

  1. docker exec -it superset_app /bin/bash
  2. root@39ce8cf9d6ab:/app#

Run the following commands inside the container

  1. apt update
  2. apt install -y gdb
  3. apt install -y net-tools
  4. pip install debugpy

Find the PID for the Flask process. Make sure to use the first PID. The Flask app will re-spawn a sub-process every time you change any of the python code. So it’s important to use the first PID.

  1. ps -ef
  2. UID PID PPID C STIME TTY TIME CMD
  3. root 1 0 0 14:09 ? 00:00:00 bash /app/docker/docker-bootstrap.sh app
  4. root 6 1 4 14:09 ? 00:00:04 /usr/local/bin/python /usr/bin/flask run -p 8088 --with-threads --reload --debugger --host=0.0.0.0
  5. root 10 6 7 14:09 ? 00:00:07 /usr/local/bin/python /usr/bin/flask run -p 8088 --with-threads --reload --debugger --host=0.0.0.0

Inject debugpy into the running Flask process. In this case PID 6.

  1. python3 -m debugpy --listen 0.0.0.0:5678 --pid 6

Verify that debugpy is listening on port 5678

  1. netstat -tunap
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:5678 0.0.0.0:* LISTEN 462/python
  5. tcp 0 0 0.0.0.0:8088 0.0.0.0:* LISTEN 6/python

You are now ready to attach a debugger to the process. Using VSCode you can configure a launch configuration file .vscode/launch.json like so.

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Attach to Superset App in Docker Container",
  6. "type": "python",
  7. "request": "attach",
  8. "connect": {
  9. "host": "127.0.0.1",
  10. "port": 5678
  11. },
  12. "pathMappings": [
  13. {
  14. "localRoot": "${workspaceFolder}",
  15. "remoteRoot": "/app"
  16. }
  17. ]
  18. },
  19. ]
  20. }

VSCode will not stop on breakpoints right away. We’ve attached to PID 6 however it does not yet know of any sub-processes. In order to “wakeup” the debugger you need to modify a python file. This will trigger Flask to reload the code and create a new sub-process. This new sub-process will be detected by VSCode and breakpoints will be activated.

Debugging Server App in Kubernetes Environment

To debug Flask running in POD inside kubernetes cluster. You’ll need to make sure the pod runs as root and is granted the SYS_TRACE capability.These settings should not be used in production environments.

  1. securityContext:
  2. capabilities:
  3. add: ["SYS_PTRACE"]

See (set capabilities for a container)[https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-capabilities-for-a-container\] for more details.

Once the pod is running as root and has the SYS_PTRACE capability it will be able to debug the Flask app.

You can follow the same instructions as in the docker-compose. Enter the pod and install the required library and packages; gdb, netstat and debugpy.

Often in a Kubernetes environment nodes are not addressable from outside the cluster. VSCode will thus be unable to remotely connect to port 5678 on a Kubernetes node. In order to do this you need to create a tunnel that port forwards 5678 to your local machine.

  1. kubectl port-forward pod/superset-<some random id> 5678:5678

You can now launch your VSCode debugger with the same config as above. VSCode will connect to to 127.0.0.1:5678 which is forwarded by kubectl to your remote kubernetes POD.

Storybook

Superset includes a Storybook to preview the layout/styling of various Superset components, and variations thereof. To open and view the Storybook:

  1. cd superset-frontend
  2. npm run storybook

When contributing new React components to Superset, please try to add a Story alongside the component’s jsx/tsx file.