Skip to content

Commit

Permalink
Install test requirements on startup
Browse files Browse the repository at this point in the history
closes #56
  • Loading branch information
lubosmj committed Aug 11, 2023
1 parent 4d0e000 commit 928c2c5
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/assets/docker_compose.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ DEV_SOURCE_PATH=pulpcore:pulp_file
COMPOSE_BINARY=docker
SECOND_SERVICE_PORT=8002
CUSTOM_DEFAULT2=howdy_default
INSTALL_TESTS=True
1 change: 1 addition & 0 deletions .github/assets/podman_compose.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ DEV_SOURCE_PATH=pulpcore:pulp_file
COMPOSE_BINARY=podman
SECOND_SERVICE_PORT=8002
CUSTOM_DEFAULT2=howdy_default
INSTALL_TESTS=True
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ jobs:
- name: Test functional tests
run: |
oci-env -v generate-client -i
oci-env -v test -i -p pulp_file functional -k test_labels
oci-env -v test -p pulp_file functional -k test_labels
- name: Test lint
run: oci-env -v test -i -p pulp_file lint
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,21 @@ oci-env -e custom.env compose up

## Running Tests

Test dependencies are automatically installed after the container startup when you set the `INSTALL_TESTS` variable to `True` in the compose file.

### Lint

```bash
# Re-install all test requirements for all plugins

oci-env test -i

# Install the lint requirements and run the linter for a specific plugin

oci-env test -i -p PLUGIN_NAME lint

# Run the linter without installing lint dependencies.
# Run the linter without installing lint dependencies

oci-env test -p PLUGIN_NAME lint
```

Expand Down
29 changes: 29 additions & 0 deletions base/container_scripts/install_test_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import subprocess

from django.conf import settings

projects = os.getenv("DEV_SOURCE_PATH").split(":")

# load django settings on the fly
settings.DYNACONF.configure()
api_root = settings.get("API_ROOT")


test_requirements_scripts = [
"install_functional_requirements.sh",
"install_performance_requirements.sh",
"install_unit_requirements.sh",
"install_lint_requirements.sh",
]

for project in projects:
print(f"Installing test requirements for {project}...")
for test_script in test_requirements_scripts:
test_script = os.path.join("/opt/oci_env/base/container_scripts/", test_script)
print(f"Running {test_script} for {project}...")

try:
subprocess.run(["bash", test_script, project], capture_output=False)
except subprocess.CalledProcessError as err:
print(test_script, project, err.stdout)
8 changes: 8 additions & 0 deletions base/container_scripts/run_functional_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ EOF
}
}

function check_test () {
[[ -d "${PROJECT}/tests/functional" ]] || {
echo "Skipping functional tests because they do not seem to exist..."
exit 0
}
}

source "/opt/oci_env/base/container_scripts/configure_pulp_smash.sh"

cd "/src/$PACKAGE/"

check_pytest
check_client
check_test

sudo -u pulp -E pytest -r sx --rootdir=/var/lib/pulp --color=yes --pyargs "$PROJECT.tests.functional" "${@:2}"
8 changes: 8 additions & 0 deletions base/container_scripts/run_performance_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ EOF
}
}

function check_test () {
[[ -d "${PROJECT}/tests/performance" ]] || {
echo "Skipping performance tests because they do not seem to exist..."
exit 0
}
}

source "/opt/oci_env/base/container_scripts/configure_pulp_smash.sh"

cd "/src/$PACKAGE/"

check_pytest
check_client
check_test

sudo -u pulp -E pytest -r sx --rootdir=/var/lib/pulp --color=yes --pyargs "$PROJECT.tests.performance" "${@:2}"
10 changes: 10 additions & 0 deletions base/container_scripts/run_unit_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ declare PROJECT="${PACKAGE//-/_}"

set -e

function check_test () {
[[ -d "${PROJECT}/tests/unit" ]] || {
echo "Skipping unit tests because they do not seem to exist..."
pwd
exit 0
}
}

source "/opt/oci_env/base/container_scripts/configure_pulp_smash.sh"

cd "/src/$PACKAGE/"

check_test

sudo -u pulp -E PULP_DATABASES__default__USER=postgres pytest -r sx --color=yes --pyargs "$PROJECT.tests.unit" "${@:2}"
10 changes: 9 additions & 1 deletion base/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set -o pipefail

readonly DEV_SOURCE_PATH="${DEV_SOURCE_PATH:-}"
readonly LOCK_REQUIREMENTS="${LOCK_REQUIREMENTS:-1}"
readonly INSTALL_TESTS="${INSTALL_TESTS:-False}"


log_message() {
Expand Down Expand Up @@ -36,6 +37,12 @@ install_local_deps() {
done
}

install_test_deps() {
if [ "$INSTALL_TESTS" = "True" ]; then
python3 /opt/oci_env/base/container_scripts/install_test_requirements.py
fi
}

create_super_user() {
pulpcore-manager createsuperuser --no-input --email admin@example.com
}
Expand All @@ -54,11 +61,12 @@ set_nginx_port() {

init_container() {
install_local_deps
install_test_deps
set_nginx_port
}

run_profile_init_scripts() {
bash /opt/oci_env/.compiled/${COMPOSE_PROJECT_NAME}/init.sh
}

$1
$1
82 changes: 60 additions & 22 deletions client/oci_env/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,74 @@ def shell(args, client):


def test(args, client):
if args.install_deps:
test_script = f"install_{args.test}_requirements.sh"
tr = TestRunner(args, client)
tr.install_requirements()
tr.run_tests()

if args.plugin:
exit_if_failed(
client.exec_container_script(
test_script,
args=[args.plugin],
privileged=args.privileged,
).returncode
)
else:
for project in client.config["DEV_SOURCE_PATH"].split(":"):

class TestRunner:
test_requirements_scripts = [
"install_functional_requirements.sh",
"install_performance_requirements.sh",
"install_unit_requirements.sh",
"install_lint_requirements.sh",
]

test_run_scripts = [
"run_functional_tests.sh",
"run_performance_tests.sh",
"run_unit_tests.sh",
"run_lint_tests.sh",
]

def __init__(self, args, client):
self.args = args
self.client = client
self.projects = client.config["DEV_SOURCE_PATH"].split(":")

def install_requirements(self):
if self.args.install_deps:
if self.args.test != "all":
test_script = f"install_{self.args.test}_requirements.sh"
if self.args.plugin:
self.install_test_requirements([self.args.plugin], [test_script])
else:
self.install_test_requirements(self.projects, [test_script])
else:
if self.args.plugin:
self.install_test_requirements([self.args.plugin], self.test_requirements_scripts)
else:
self.install_test_requirements(self.projects, self.test_requirements_scripts)

def install_test_requirements(self, projects, test_requirements_scripts):
for project in projects:
for test_script in test_requirements_scripts:
print(f"Running {test_script} for {project}...")
exit_if_failed(
client.exec_container_script(
self.client.exec_container_script(
test_script,
args=[project],
privileged=args.privileged,
privileged=self.args.privileged,
).returncode
)

if args.plugin:
exit_if_failed(
client.exec_container_script(
f"run_{args.test}_tests.sh",
args=[args.plugin] + args.args,
interactive=True,
privileged=args.privileged,
def run_tests(self):
if self.args.plugin:
if self.args.test != "all":
self.run_test_command(self.args.plugin, [f"run_{self.args.test}_tests.sh"])
else:
self.run_test_command(self.args.plugin, self.test_run_scripts)

def run_test_command(self, project, tests):
for test_script in tests:
exit_if_failed(
self.client.exec_container_script(
test_script,
args=[project] + self.args.args,
interactive=True,
privileged=self.args.privileged,
)
)
)


def generate_client(args, client):
Expand Down
2 changes: 1 addition & 1 deletion client/oci_env/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def parse_shell_command(subparsers):

def parse_test_command(subparsers):
parser = subparsers.add_parser('test', help='Run tests and install requirements.')
parser.add_argument('test', choices=["functional", "unit", "lint", "performance"])
parser.add_argument('test', choices=["functional", "unit", "lint", "performance", "all"], nargs="?", default="all")
parser.add_argument('-i', action='store_true', dest='install_deps', help="Install the python dependencies for the selected test instead of running it. If -p is not specified this will install all the test dependencies for each plugin in DEV_SOURCE_PATH.")
parser.add_argument('-p', type=str, default="", dest='plugin', help="Plugin to test. Tests won't run unless this is specified.")
parser.add_argument('args', nargs=argparse.REMAINDER, help='Arguments to pass to pytest.')
Expand Down
3 changes: 3 additions & 0 deletions client/oci_env/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def get_config(env_file):
# Directory on the host where the DEV_SOURCE_PATH projects are checked out
"SRC_DIR": os.path.abspath(os.path.join(path, "..")),

# Install tests automatically on startup
"INSTALL_TESTS": "False",

# List of : separated profiles to use
"COMPOSE_PROFILE": "",

Expand Down
3 changes: 3 additions & 0 deletions compose.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ COMPOSE_PROFILE=
# : separate list of python dependencies to include from source
DEV_SOURCE_PATH=

# Install test dependencies automatically
# INSTALL_TESTS=False
#
# Program to use for compose. This defaults to podman. Uncomment this to use docker-compose.
# COMPOSE_BINARY=docker

Expand Down

0 comments on commit 928c2c5

Please sign in to comment.