-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add isolated testing settings for awx testing #16110
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
tyraziel
wants to merge
1
commit into
ansible:devel
Choose a base branch
from
tyraziel:pytest_isolated_testing_settings
base: devel
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """ | ||
| AWX Settings for Isolated Testing | ||
|
|
||
| This settings module provides a fully isolated testing environment that: | ||
| - Uses SQLite instead of PostgreSQL for faster, isolated test runs | ||
| - Disables DAB (django-ansible-base) resource sync to prevent 401 errors | ||
| - Mocks DAB ServiceID dependencies to avoid database table requirements | ||
| - Provides in-memory caching for test isolation | ||
|
|
||
| Usage: | ||
| pytest --ds=awx.main.tests.settings_for_isolated_testing [test_file] | ||
|
|
||
| Enhanced Usage Notes: | ||
| 1. ServiceID Mocking Pattern: | ||
| Sample fixture code pattern to add to test files to prevent: "AttributeError: 'NoneType' object has no attribute 'pk'" | ||
|
|
||
| # Mock the service_id field default for testing when DAB resource registry tries to access ServiceID.objects.first().pk | ||
| @pytest.fixture(autouse=True) | ||
| def mock_dab_service_id(): | ||
| from django.conf import settings | ||
| import uuid | ||
|
|
||
| if getattr(settings, 'MOCK_DAB_SERVICE_ID', False): | ||
| with patch('ansible_base.resource_registry.models.service_identifier.ServiceID.objects.first') as mock_first: | ||
| mock_service = MagicMock() | ||
| mock_service.pk = uuid.uuid4() | ||
| mock_first.return_value = mock_service | ||
| yield | ||
| else: | ||
| yield | ||
|
|
||
| - MOCK_DAB_SERVICE_ID setting to enable/disable mocking | ||
|
|
||
| 2. DAB Resource Server Isolation: | ||
| - RESOURCE_SERVER = {} setting disables DAB sync during testing | ||
| - Prevents 401 authentication errors in test environments | ||
|
|
||
| 3. SQLite Testing Configuration: | ||
| - Fast, isolated test database setup | ||
| - No PostgreSQL permissions or setup required | ||
|
|
||
| AIA: Primarily AI, New content, Human-initiated, Reviewed, Claude (Anthropic AI) via Claude Code | ||
| AIA PAI Nc Hin R Claude Code - https://aiattribution.github.io/interpret-attribution | ||
| """ | ||
|
|
||
| # Python | ||
| import uuid | ||
|
|
||
| # Load development settings for base variables. | ||
| from awx.settings.development import * # NOQA | ||
|
|
||
| # Some things make decisions based on settings.SETTINGS_MODULE, so this is done for that | ||
| SETTINGS_MODULE = 'awx.settings.development' | ||
|
|
||
| # Turn off task submission, because sqlite3 does not have pg_notify | ||
| DISPATCHER_MOCK_PUBLISH = True | ||
|
|
||
| # Use SQLite for unit tests instead of PostgreSQL. If the lines below are | ||
| # commented out, Django will create the test_awx-dev database in PostgreSQL to | ||
| # run unit tests. | ||
| CACHES = {'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'unique-{}'.format(str(uuid.uuid4()))}} | ||
| DATABASES = { | ||
| 'default': { | ||
| 'ENGINE': 'django.db.backends.sqlite3', | ||
| 'NAME': os.path.join(BASE_DIR, 'awx.sqlite3'), # noqa | ||
| 'TEST': { | ||
| # Test database cannot be :memory: for inventory tests. | ||
| 'NAME': os.path.join(BASE_DIR, 'awx_test.sqlite3') # noqa | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| # Disable DAB resource sync to Resource Server for testing | ||
| # This prevents 401 errors when creating RoleUserAssignment objects in tests | ||
| # Set empty dict to disable sync (code checks for URL key) | ||
| RESOURCE_SERVER = {} | ||
|
|
||
| # This flag enables DAB ServiceID mocking in test files via autouse fixtures (see block comment above) | ||
| MOCK_DAB_SERVICE_ID = True | ||
Oops, something went wrong.
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.
I see this fixure in the comments. Why not include it in the PR? We aren't seeing tests fail in our CI because of ServiceID record not existing. Wondering why you are seeing it.
As I understand it a ServiceId record is supposed to be created via DAB migrations here https://github.com/ansible/django-ansible-base/blob/964c531cdb465ca9b0511ee75e2772bd3847ddc6/ansible_base/resource_registry/migrations/0001_initial.py#L31