Skip to content

Commit

Permalink
Finished two testplans (#5)
Browse files Browse the repository at this point in the history
* Completed two test plans: the normal plan and the classroom plan
* Refactored into using abstract base user types. These are implemented in test plan scenario files.
* Added new task user types: app viewer, power user, 
* Completed authentication methods.
* Created non-locust module for opening user apps which created k8s pods on cluster.
* More settings and configuration to customize the user app behaviour
* Better handling of switching between test environments.
* Added task to register new user account.
* Added the standard code linters and checkers to this repository.
* Introduced the standard logging library. Converted most prints to logging.

---------

Co-authored-by: Nikita Churikov <[email protected]>
  • Loading branch information
alfredeen and churnikov authored Feb 2, 2024
1 parent 0393ef4 commit f555f80
Show file tree
Hide file tree
Showing 23 changed files with 1,015 additions and 141 deletions.
14 changes: 14 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[flake8]
max-line-length = 120
extend-ignore =
# Whitespace before ':'
E203,
# imported but unused
F401
exclude =
.git,
__pycache__,
env,
.env,
venv,
.venv
Empty file added .github/workflows/.gitkeep
Empty file.
30 changes: 30 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Pre-commit linters and code checks

on:
pull_request:
paths-ignore:
- 'README.md'
push:
branches:
- develop
paths-ignore:
- 'README.md'

jobs:
pre_commit:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: 3.8

- name: Install dependencies
run: |
python -m pip install --upgrade pre-commit==3.3.3
- name: Run pre-commit
run: pre-commit run --all-files
52 changes: 52 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
default_stages:
- commit
repos:
# general hooks to verify or beautify code
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
args: [--maxkb=5000]
- id: trailing-whitespace
- id: check-json
- id: check-merge-conflict
- id: check-xml
- id: check-yaml
exclude: ^charts/
- id: detect-private-key
- id: mixed-line-ending
- id: end-of-file-fixer
- id: pretty-format-json
args: [--autofix]


# autoformat code with black formatter
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black

# beautify and sort imports
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
additional_dependencies: [toml]
exclude: '^(.*?/)?migrations/.*\.py$'


# check code style
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
# exclude added here because the .flake8 settings are not respected by pre-commit --all-files
exclude: __init__.py|.venv|.env|venv|env|__pycache__


# static type checking
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.0
hooks:
- id: mypy
additional_dependencies: [types-requests==2.25.9]
95 changes: 71 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,82 +23,129 @@ Make changes in the develop branch, commit and submit pull requests to merge to
## Configuration options

To configure the test runs, use the Locust configuration file ./source/locust.conf
For options, see https://docs.locust.io/en/stable/configuration.html
For options, see the [Locust docs](https://docs.locust.io/en/stable/configuration.html)

## Create tests

Create locust tests in the /source/tests directory.

## Verify the setup by running a simple test

These tests do not need access to a host (URL) to test against.

### Running tests using the command line (headlessly)

Run the command:

locust --headless -f ./tests/simple.py --users 1 --run-time 10s --html ./reports/locust-report-simple.html
locust --headless -f ./tests/test_verify_setup.py --users 1 --run-time 10s --html ./reports/locust-report-verify-setup.html

Open the generated html report and verify that there are no errors and that the statistics look reasonable. The report is created under /source/reports/

### Using the Locust UI web client

Run the commands
Run the command

cd ./tests
locust --modern-ui --class-picker -f simple.py
locust --config locust-ui.conf --modern-ui --class-picker -f ./tests/test_verify_setup.py --html ./reports/locust-report-verify-setup-ui.html

Open a browser tab at URL http://localhost:8089/

Paste in as host

https://staging.serve-dev.scilifelab.se

## Verify the setup and access to a host (URL)

Run the command:

locust --headless -f ./tests/test_verify_host.py --users 1 --run-time 10s --html ./reports/locust-report-verify-host.html

Open the generated html report and verify that there are no errors and that the statistics look reasonable. The report is created under /source/reports/


## Running tests

If desired generate html reports by editing the report name in the below commands.
### Prepare for running tests

Some of the tests require pre-existing test users that are named with the format "locust_test_user_"*
Therefore, before running the tests, run the script to create them in the test environment. This can be performed using the Django manage module while connected to the Serve studio pod. For example to add 10 test users, run:

### Run tests headlessly
python3 manage.py add_locust_users 10

When the tests are completed, you can remove the test users by running:

python3 manage.py remove_locust_users

Move into the source directory if not already there:

cd ./source

### To run only the Website tests
- Copy the template environment file .env.template as .env
- Edit the followinf values in the .env file according to your needs.

- SERVE_LOCUST_TEST_USER_PASS=(The password of the test locust users)
- SERVE_LOCUST_DO_CREATE_OBJECTS=(A boolean indicating whether to create objects in Serve such as projects and apps)

locust -f ./tests/website.py --html ./reports/locust-report-website.html
Set the environment values from the file

### To run only the API tests
set -o allexport; source .env; set +o allexport

locust -f ./tests/api.py --html ./reports/locust-report-api.html
If desired then generate html reports by editing the report name in the below commands.

### To run all tests, execute one of the below commands. Beware, please be nice to the system resources.
### To run the Normal test plan/scenario

The configuration file parameter here is not necessary but is included for extra intelligibility.
Use minimum 10 users for the Normal test plan

locust --config locust.conf --html ./reports/locust-report-all.html
locust --headless -f ./tests/test_plan_normal.py --html ./reports/locust-report-normal.html --users 10 --run-time 30s

This executes the same command as above.
### To run the Classroom test plan/scenario

locust --html ./reports/locust-report-all.html
locust --headless -f ./tests/test_plan_classroom.py --html ./reports/locust-report-classroom.html --users 1 --run-time 30s


## Tests under development

These tests are not yet ready to be used in a load testing session.
The tests are located under directory /source/tests-dev/

### To run only the AppViewer tests (using user apps as a non-authenticated user)
### To run only the AppViewer tests

This test uses a non-authenticated user

locust -f ./tests-dev/appviewer.py --html ./reports/locust-report-appviewer.html --users 1 --run-time 20s
locust --headless -f ./tests-dev/appviewer.py --html ./reports/locust-report-appviewer.html --users 1 --run-time 20s

### To run only the test class requiring authentication

These tests require a user account in Serve and a protected page such as a project page.

- Copy the template environment file .env.template as .env
- Edit the values in the .env file
Run the tests

Set the environment values from the file
locust --headless -f ./tests-dev/authenticated.py --html ./reports/locust-report-authenticated.html --users 1 --run-time 10s

set -o allexport; source .env; set +o allexport

Run the tests
## To run tests using a Docker base image

Using provided Locust base image. To select which tests to execute, edit the file parameter -f as shown above.

cd ./source

docker run -p 8089:8089 -v $PWD:/mnt/locust locustio/locust -f /mnt/locust/tests/simple.py --config /mnt/locust/locust.conf --html /mnt/locust/reports/locust-report-from-docker.html


## To run k8s pod-creating tests

The Locust app-viewer tests do not currently create pods on the cluster. In order to run such tests, configure and execute appviewer_requestshtml.py

python3 ./tests-dev/appviewer_requestshtml.py


## Use the shell script to run tests

The shell script can be used to execute multiple simultaneous types of tests (such as Locust plus a scripted test).

Edit the configuration in locust.conf and in .env. If running the appviewer_requestshtml.py module, then also configure settings in this file.

locust -f ./tests-dev/authenticated.py --html ./reports/locust-report-authenticated.html --users 1 --run-time 10s
```
$ cd ./source
$ chmod +x run_test_plan.sh
$ ./run_test_plan.sh
```
53 changes: 53 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This file contains project information.
# It also contains settings for linters and code checkers isort, black and mypy.
# Note that these settings are not respected with pre-commit run --all-files
# In that case add configurations to the .pre-commit-config.yaml file.

[project]
name = "serve-load-testing"
version = "1.0.0"
description = "Load testing of the SciLifeLab Serve platform."
requires-python = "=3.8"
keywords = ["load testing", "locust", "python"]

[tool.isort]
profile = 'black'

[tool.black]
line-length = 120
target-version = ['py38']
include = '\.pyi?$'
extend-exclude = '''
/(
\.git
| \.mypy_cache
| \.venv
| venv
| migrations
)/
'''

[tool.mypy]
strict = false
python_version = "3.8"
ignore_missing_imports = true
warn_return_any = true
exclude = ["venv", ".venv", "examples"]

[[tool.mypy.overrides]]
module = "*.migrations.*"
ignore_errors = true

[[tool.mypy.overrides]]
module = [
"flatten_json.*",
"guardian.*",
"tagulous.*",
"dash.*",
"markdown.*",
"pytz.*",
"requests.*",
"setuptools.*",
"yaml.*",
]
ignore_missing_imports = true
4 changes: 3 additions & 1 deletion source/.env.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[email protected]
SERVE_PASS=a_pass
PROTECTED_PAGE_RELATIVE_URL=/a/relative/url
SERVE_LOCUST_TEST_USER_PASS=a_pass2
SERVE_LOCUST_DO_CREATE_OBJECTS=True
PROTECTED_PAGE_RELATIVE_URL=/a/relative/url
12 changes: 12 additions & 0 deletions source/locust-ui.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# locust.conf
locustfile = tests
headless = false
#master = true
#expect-workers = 5
host = https://staging.serve-dev.scilifelab.se
users = 1
spawn-rate = 1
run-time = 10s
only-summary = true
csv = stats/locust
loglevel = INFO
9 changes: 5 additions & 4 deletions source/locust.conf
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# locust.conf
locustfile = tests
headless = true
#headless = true
#master = true
#expect-workers = 5
host = https://staging.serve-dev.scilifelab.se
host = https://serve-dev.scilifelab.se
users = 1
spawn-rate = 1
run-time = 10s
only-summary = true
csv = stats/locust
#only-summary = true
csv = stats/locust
loglevel = INFO
2 changes: 1 addition & 1 deletion source/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
locust>=2.20.0
requests-html>=0.10.0
requests-html>=0.10.0
14 changes: 14 additions & 0 deletions source/run_test_plan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -o errexit

# Activate the virtual env
source ./.venv/bin/activate

# Set the env variables from this project
set -o allexport; source .env; set +o allexport

# Run 2 sets of tests in parallel: the non-locust user apps and the locust test plan

python3 ./tests-dev/appviewer_requestshtml.py & \
locust --headless -f ./tests/test_plan_normal.py --users 10 --run-time 60s --html ./reports/locust-report-normal.html
Loading

0 comments on commit f555f80

Please sign in to comment.