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 Jul 12, 2023
1 parent 4d0e000 commit d5a868f
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 24 deletions.
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.

### 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}"
2 changes: 2 additions & 0 deletions base/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ echo 'Defaults env_keep += "DJANGO_SETTINGS_MODULE PULP_SETTINGS XDG_CONFIG_H
# Add user pulp to sudoers so tests can use sudo
echo '%wheel ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/nopasswd
usermod -aG wheel pulp && echo 'Adding the pulp user to the wheel group...'

python3 /opt/oci_env/base/container_scripts/install_test_requirements.py
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

0 comments on commit d5a868f

Please sign in to comment.