-
Notifications
You must be signed in to change notification settings - Fork 12
#569 - add check for minimum netbox/branching versions for branching support #570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arthanson
wants to merge
3
commits into
feature
Choose a base branch
from
569-check
base: feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Django system checks. | ||
|
|
||
| Enforces conditional version floors that PluginConfig's static | ||
| ``min_version``/``max_version`` can't express: when netbox-branching is | ||
| installed, NetBox and netbox-branching versions are pinned tighter because | ||
| the branching integration relies on APIs that only landed in those releases: | ||
|
|
||
| - ``serializer_resolver`` / ``register_serializer_resolver`` — NetBox 4.6.2 | ||
| - ``register_branching_resolver``, ``register_objectchange_field_migrator``, | ||
| and the ``squash_dependency_graph_built`` signal — netbox-branching 1.0.4 | ||
|
|
||
| Users who never install netbox-branching keep the broader compatibility | ||
| window declared by PluginConfig. | ||
| """ | ||
|
|
||
| from importlib.metadata import PackageNotFoundError, version as _pkg_version | ||
|
|
||
| from django.apps import apps | ||
| from django.conf import settings | ||
| from django.core.checks import Error, Warning, register | ||
| from packaging.version import InvalidVersion, Version | ||
|
|
||
|
|
||
| # Version floors enforced only when netbox-branching is installed. | ||
| REQUIRED_NETBOX_VERSION = '4.6.2' | ||
| REQUIRED_BRANCHING_VERSION = '1.0.4' | ||
|
|
||
|
|
||
| @register() | ||
| def check_branching_compatibility(app_configs, **kwargs): | ||
| """Enforce branching-only version floors; no-op without netbox-branching.""" | ||
| if not apps.is_installed('netbox_branching'): | ||
| return [] | ||
|
|
||
| errors = [] | ||
|
|
||
| try: | ||
| netbox_version = Version(settings.RELEASE.version) | ||
| if netbox_version < Version(REQUIRED_NETBOX_VERSION): | ||
| errors.append(Error( | ||
| f'netbox-custom-objects requires NetBox >= {REQUIRED_NETBOX_VERSION} ' | ||
| f'when netbox-branching is installed (detected {netbox_version}).', | ||
| hint='Upgrade NetBox, or remove netbox-branching from PLUGINS ' | ||
| 'if you do not need branching support for custom objects.', | ||
| id='netbox_custom_objects.E001', | ||
| )) | ||
| except (AttributeError, InvalidVersion): | ||
| pass # settings.RELEASE missing/unparseable — other checks surface it | ||
|
|
||
| try: | ||
| branching_version = Version(_pkg_version('netbox-branching')) | ||
| if branching_version < Version(REQUIRED_BRANCHING_VERSION): | ||
| errors.append(Error( | ||
| f'netbox-custom-objects requires netbox-branching >= ' | ||
| f'{REQUIRED_BRANCHING_VERSION} (detected {branching_version}).', | ||
| hint=f'Upgrade with: pip install "netbox-branching>={REQUIRED_BRANCHING_VERSION}"', | ||
| id='netbox_custom_objects.E002', | ||
| )) | ||
| except PackageNotFoundError: | ||
| # netbox-branching is an installed app but its distribution metadata | ||
| # isn't discoverable (e.g. an editable checkout without dist-info), so | ||
| # the version floor can't be enforced. Warn rather than silently pass | ||
| # so an incompatible checkout doesn't slip through unnoticed. | ||
| errors.append(Warning( | ||
| 'netbox-branching is installed but its version could not be ' | ||
| f'determined, so the >= {REQUIRED_BRANCHING_VERSION} requirement ' | ||
| 'cannot be verified.', | ||
| hint='If using an editable install, ensure its dist-info metadata ' | ||
| 'is present (reinstall with `pip install -e`).', | ||
| id='netbox_custom_objects.W001', | ||
| )) | ||
| except InvalidVersion: | ||
| pass # unparseable version string — skip rather than emit a confusing error | ||
|
|
||
| return errors | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """ | ||
| Tests for the conditional netbox-branching version system check | ||
| (``netbox_custom_objects.checks.check_branching_compatibility``). | ||
|
|
||
| The check is pure version-comparison logic gated on whether netbox-branching | ||
| is installed, so every branch is exercised by mocking its three inputs: | ||
| ``apps.is_installed``, ``settings.RELEASE``, and the importlib.metadata | ||
| ``version`` lookup. No database is required, hence ``SimpleTestCase``. | ||
| """ | ||
| from importlib.metadata import PackageNotFoundError | ||
| from types import SimpleNamespace | ||
| from unittest.mock import patch | ||
|
|
||
| from django.test import SimpleTestCase | ||
|
|
||
| from netbox_custom_objects import checks | ||
|
|
||
|
|
||
| def _run(*, branching_installed=True, netbox_version='4.6.2', branching_version='1.0.4'): | ||
| """Invoke the check with each input mocked. | ||
|
|
||
| ``netbox_version`` / ``branching_version`` may be a version string, or an | ||
| Exception instance/class to simulate the failure paths (a missing | ||
| ``settings.RELEASE``, an unparseable version, or ``PackageNotFoundError``). | ||
| """ | ||
| if isinstance(netbox_version, str): | ||
| release = SimpleNamespace(version=netbox_version) | ||
| else: | ||
| # No RELEASE attribute -> accessing settings.RELEASE raises AttributeError. | ||
| release = None | ||
|
|
||
| def fake_pkg_version(_name): | ||
| if isinstance(branching_version, str): | ||
| return branching_version | ||
| raise branching_version # Exception instance or class | ||
|
|
||
| settings_stub = SimpleNamespace() | ||
| if release is not None: | ||
| settings_stub.RELEASE = release | ||
|
|
||
| with patch.object(checks.apps, 'is_installed', return_value=branching_installed), \ | ||
| patch.object(checks, 'settings', settings_stub), \ | ||
| patch.object(checks, '_pkg_version', fake_pkg_version): | ||
| return checks.check_branching_compatibility(app_configs=None) | ||
|
|
||
|
|
||
| def _ids(errors): | ||
| return {e.id for e in errors} | ||
|
|
||
|
|
||
| class CheckBranchingCompatibilityTest(SimpleTestCase): | ||
| def test_no_branching_is_noop(self): | ||
| """Without netbox-branching installed, the check returns no messages.""" | ||
| self.assertEqual(_run(branching_installed=False), []) | ||
|
|
||
| def test_both_versions_satisfied(self): | ||
| """Versions at the floor produce no errors.""" | ||
| self.assertEqual(_run(netbox_version='4.6.2', branching_version='1.0.4'), []) | ||
|
|
||
| def test_newer_versions_satisfied(self): | ||
| self.assertEqual(_run(netbox_version='4.7.0', branching_version='1.1.0'), []) | ||
|
|
||
| def test_netbox_too_old(self): | ||
| errors = _run(netbox_version='4.6.1', branching_version='1.0.4') | ||
| self.assertEqual(_ids(errors), {'netbox_custom_objects.E001'}) | ||
|
|
||
| def test_branching_too_old(self): | ||
| errors = _run(netbox_version='4.6.2', branching_version='1.0.3') | ||
| self.assertEqual(_ids(errors), {'netbox_custom_objects.E002'}) | ||
|
|
||
| def test_both_too_old(self): | ||
| errors = _run(netbox_version='4.5.0', branching_version='0.9.0') | ||
| self.assertEqual(_ids(errors), {'netbox_custom_objects.E001', 'netbox_custom_objects.E002'}) | ||
|
|
||
| def test_missing_release_skips_netbox_check(self): | ||
| """A missing settings.RELEASE skips E001 but still evaluates branching.""" | ||
| errors = _run(netbox_version=AttributeError, branching_version='1.0.3') | ||
| self.assertEqual(_ids(errors), {'netbox_custom_objects.E002'}) | ||
|
|
||
| def test_unparseable_netbox_version_skips_netbox_check(self): | ||
| errors = _run(netbox_version='not-a-version', branching_version='1.0.4') | ||
| self.assertEqual(errors, []) | ||
|
|
||
| def test_unresolvable_branching_version_warns(self): | ||
| """An installed-but-unmeasurable branching dist warns rather than passing silently.""" | ||
| errors = _run(netbox_version='4.6.2', branching_version=PackageNotFoundError()) | ||
| self.assertEqual(_ids(errors), {'netbox_custom_objects.W001'}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this one pull from the min_version on the PluginConfig? Or else we'll have to keep this in sync too. Or if this is relevant only when branching is present, maybe
REQUIRED_NETBOX_VERSION_FOR_BRANCHINGor something? (If it's truly unrelated to the custom-objectsmin_versionthen I don't mind this.)