Merge pull request #1299 from adamoutler/main
Some checks failed
Code checks / check-url-paths (push) Has been cancelled
Code checks / lint (push) Has been cancelled
Code checks / docker-tests (push) Has been cancelled
docker / docker_dev (push) Has been cancelled
Deploy MkDocs / deploy (push) Has been cancelled

feat: docker-based testing
This commit is contained in:
Jokob @NetAlertX
2025-11-20 17:40:10 +11:00
committed by GitHub
4 changed files with 96 additions and 19 deletions

View File

@@ -153,9 +153,9 @@ COPY --from=builder --chown=20212:20212 ${VIRTUAL_ENV} ${VIRTUAL_ENV}
RUN if [ -f .VERSION ]; then \
cp .VERSION ${NETALERTX_APP}/.VERSION; \
else \
echo "DEVELOPMENT $(cd /app && git rev-parse --short HEAD 2>/dev/null || echo '00000000')" > ${NETALERTX_APP}/.VERSION; \
echo "DEVELOPMENT 00000000" > ${NETALERTX_APP}/.VERSION; \
fi && \
chown ${READ_ONLY_USER}:${READ_ONLY_GROUP} ${NETALERTX_APP}/.VERSION && \
chown 20212:20212 ${NETALERTX_APP}/.VERSION && \
apk add libcap && \
setcap cap_net_raw+ep /bin/busybox && \
setcap cap_net_raw,cap_net_admin+eip /usr/bin/nmap && \

View File

@@ -21,7 +21,7 @@ jobs:
run: |
echo "🔍 Checking for incorrect absolute '/php/' URLs (should be 'php/' or './php/')..."
MATCHES=$(grep -rE "['\"]\/php\/" --include=\*.{js,php,html} ./front | grep -E "\.get|\.post|\.ajax|fetch|url\s*:") || true
MATCHES=$(grep -rE "[\"']/\/php\/" --include=*.{js,php,html} ./front | grep -E "\.get|\.post|\.ajax|fetch|url\s*:") || true
if [ -n "$MATCHES" ]; then
echo "$MATCHES"
@@ -85,25 +85,14 @@ jobs:
echo "🔍 Linting Dockerfiles..."
/tmp/hadolint Dockerfile* || true
test:
docker-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
- name: Run Docker-based tests
run: |
pip install -r requirements.txt
pip install pytest pyyaml
- name: Run unit tests
run: |
echo "🧪 Running unit tests..."
export PYTHONPATH=$PYTHONPATH:./server
pytest -m "not (docker or compose or feature_complete)"
echo "🐳 Running Docker-based tests..."
chmod +x ./run_docker_tests.sh
./run_docker_tests.sh

View File

@@ -66,6 +66,7 @@ CREATE TABLE Devices (
devIsArchived BOOLEAN NOT NULL DEFAULT (0) CHECK (devIsArchived IN (0, 1)),
devParentMAC TEXT,
devParentPort INTEGER,
devParentRelType TEXT,
devIcon TEXT,
devGUID TEXT,
devSite TEXT,

87
run_docker_tests.sh Executable file
View File

@@ -0,0 +1,87 @@
#!/bin/bash
#
# run_docker_tests.sh
#
# This script automates the entire process of testing the application
# within its intended, privileged devcontainer environment. It is
# idempotent and can be run repeatedly.
#
set -e
# --- 1. Regenerate Devcontainer Dockerfile ---
echo "--- Regenerating .devcontainer/Dockerfile from source ---"
if [ -f ".devcontainer/scripts/generate-configs.sh" ]; then
/bin/bash .devcontainer/scripts/generate-configs.sh
else
echo "ERROR: generate-configs.sh not found. Aborting."
exit 1
fi
# --- 2. Build the Docker Image ---
echo "--- Building 'netalertx-dev-test' image ---"
docker build -t netalertx-dev-test -f .devcontainer/Dockerfile . --target netalertx-devcontainer
# --- 3. Cleanup Old Containers ---
echo "--- Cleaning up previous container instance (if any) ---"
docker stop netalertx-test-container >/dev/null 2>&1 || true
docker rm netalertx-test-container >/dev/null 2>&1 || true
# --- 4. Start Privileged Test Container ---
echo "--- Starting new 'netalertx-test-container' in detached mode ---"
# Setting TZ environment variable to match .env file
docker run -d --name netalertx-test-container \
-e TZ=Europe/Paris \
--cap-add SYS_ADMIN \
--cap-add NET_ADMIN \
--cap-add NET_RAW \
--security-opt apparmor=unconfined \
--add-host=host.docker.internal:host-gateway \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$(pwd)":/workspaces/NetAlertX \
netalertx-dev-test
# --- 5. Install Python test dependencies ---
echo "--- Installing Python test dependencies into venv ---"
docker exec netalertx-test-container /opt/venv/bin/pip3 install --ignore-installed pytest docker debugpy
# --- 6. Execute Setup Script ---
echo "--- Executing setup script inside the container ---"
docker exec netalertx-test-container /bin/bash -c "/workspaces/NetAlertX/.devcontainer/scripts/setup.sh"
# --- 7. Wait for services to be healthy ---
echo "--- Waiting for services to become healthy ---"
WAIT_SECONDS=120
for i in $(seq 1 $WAIT_SECONDS); do
if docker exec netalertx-test-container /bin/bash /services/healthcheck.sh; then
echo "--- Services are healthy! ---"
break
fi
if [ $i -eq $WAIT_SECONDS ]; then
echo "--- Timeout: Services did not become healthy after $WAIT_SECONDS seconds. ---"
docker logs netalertx-test-container
exit 1
fi
echo " ... waiting ($i/$WAIT_SECONDS)"
sleep 1
done
# --- 8. Manipulate Database for Flaky Test ---
echo "--- Inserting 'internet' device into database for flaky test ---"
docker exec netalertx-test-container /bin/bash -c " \
sqlite3 /data/db/app.db \"INSERT OR IGNORE INTO Devices (devMac, devFirstConnection, devLastConnection, devLastIP, devName) VALUES ('internet', DATETIME('now'), DATETIME('now'), '0.0.0.0', 'Internet Gateway');\" \
"
# --- 9. Execute Tests ---
echo "--- Executing tests inside the container ---"
docker exec netalertx-test-container /bin/bash -c " \
cd /workspaces/NetAlertX && /opt/venv/bin/pytest -m 'not (docker or compose or feature_complete)' --cache-clear -o cache_dir=/tmp/.pytest_cache; \
"
# --- 10. Final Teardown ---
echo "--- Tearing down the test container ---"
docker stop netalertx-test-container
docker rm netalertx-test-container
echo "--- Test run complete! ---"