Skip to content

Commit 2ca8086

Browse files
authored
fix: update tests, update interface name conflict, support isolated test db (#47)
1 parent eefc835 commit 2ca8086

9 files changed

Lines changed: 533 additions & 153 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# Copyright (C) 2025 Marcin Zieba <marcinpsk@gmail.com>
3+
#
4+
# Isolated test-database settings shim.
5+
#
6+
# Django names the test database ``test_<DB_NAME>`` (here: ``test_netbox``), so two
7+
# ``manage.py test`` runs in the same devcontainer collide on a single test DB and
8+
# corrupt each other's migrations (and a crashed run can leave an
9+
# ``idle in transaction`` connection holding locks that wedges every later run).
10+
#
11+
# This shim imports the fully-assembled NetBox settings and, when ``TEST_DB_NAME``
12+
# is set, points the test database at that name — so each session/plugin can run on
13+
# its own isolated test DB inside the same Postgres/devcontainer. With no env var it
14+
# falls back to the default ``test_netbox`` and behaves exactly like before.
15+
#
16+
# Use it via ``--settings=isolated_test_settings`` with this directory on PYTHONPATH
17+
# (the ``netbox-test-isolated`` helper in load-aliases.sh wires both up for you).
18+
import os as _os
19+
20+
from netbox.settings import * # noqa: F401,F403
21+
22+
_name = _os.environ.get("TEST_DB_NAME")
23+
if _name:
24+
DATABASES["default"].setdefault("TEST", {})["NAME"] = _name # noqa: F405

.devcontainer/scripts/load-aliases.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ netbox-test() {
123123
cd /opt/netbox/netbox && source /opt/netbox/venv/bin/activate && python manage.py test netbox_interface_name_rules "$@"
124124
}
125125

126+
# Run tests on a per-session ISOLATED test database, so concurrent suites in the
127+
# shared devcontainer don't collide on test_netbox (which corrupts migrations and
128+
# can leave lock-holding zombie connections). Pass the app(s) + any test flags,
129+
# e.g. netbox-test-isolated netbox_nso_plugin --keepdb
130+
# Override the DB name with TEST_DB_NAME=...; otherwise it's derived from the first
131+
# app argument (a stable name so --keepdb can reuse it).
132+
netbox-test-isolated() {
133+
local first_app="netbox_interface_name_rules"
134+
case "${1:-}" in -*|"") ;; *) first_app="$1" ;; esac
135+
local db_name="${TEST_DB_NAME:-test_$(printf '%s' "$first_app" | tr -c 'a-zA-Z0-9' '_')}"
136+
cd /opt/netbox/netbox && source /opt/netbox/venv/bin/activate && \
137+
PYTHONPATH="$PLUGIN_DIR/.devcontainer/config${PYTHONPATH:+:$PYTHONPATH}" \
138+
TEST_DB_NAME="$db_name" \
139+
python manage.py test "$@" --settings=isolated_test_settings
140+
}
141+
126142
netbox-manage() {
127143
cd /opt/netbox/netbox && source /opt/netbox/venv/bin/activate && python manage.py "$@"
128144
}
@@ -199,6 +215,7 @@ dev-help() {
199215
echo "🛠️ Development Tools:"
200216
echo " netbox-shell : Open NetBox Django shell"
201217
echo " netbox-test : Run plugin tests"
218+
echo " netbox-test-isolated: Run tests on a per-session isolated test DB (concurrency-safe)"
202219
echo " netbox-manage : Run Django management commands"
203220
echo " plugin-install : Reinstall plugin in development mode"
204221
echo ""

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,30 @@ Apache 2.0
8787
See [CONTRIBUTING.md](CONTRIBUTING.md) for how to submit code or interface name rules.
8888

8989
Community-contributed rules for various vendors are in the [`contrib/`](contrib/) directory.
90+
91+
## Testing
92+
93+
Inside the devcontainer:
94+
95+
```bash
96+
netbox-test # run this plugin's tests (shared test_netbox DB)
97+
netbox-test-isolated <app> [flags] # run on a per-session ISOLATED test DB
98+
```
99+
100+
`netbox-test-isolated` is concurrency-safe: Django otherwise names the test
101+
database `test_<DB_NAME>` (`test_netbox`), so two `manage.py test` runs in the
102+
same devcontainer collide on one DB and corrupt each other's migrations (a
103+
crashed run can even leave an `idle in transaction` connection holding locks that
104+
wedges every later run). The helper points the test DB at a unique name —
105+
derived from the first app argument, or `TEST_DB_NAME=...` — via the
106+
[`isolated_test_settings`](.devcontainer/config/isolated_test_settings.py) shim
107+
(`--settings=isolated_test_settings` with `.devcontainer/config` on `PYTHONPATH`).
108+
Plain `netbox-test` keeps using the shared default (`test_netbox`);
109+
`netbox-test-isolated` instead derives a per-app DB name by default — `test_<app>`
110+
from the first app argument (this plugin when none is given) — unless you set
111+
`TEST_DB_NAME` explicitly. Example — test a sibling plugin without disturbing a
112+
parallel run:
113+
114+
```bash
115+
netbox-test-isolated netbox_nso_plugin --keepdb
116+
```

0 commit comments

Comments
 (0)