Compare commits

..

1 Commits

Author SHA1 Message Date
Lila
5cf242c8af Fix awxkit tox test failure caused by root pytest.ini config bleed
Forward-port of ansible/tower#7537 for the devel branch.

When running awxkit's tox tests, pytest picks up the root pytest.ini
which pulls in pytest-django options (--reuse-db, --nomigrations,
DJANGO_SETTINGS_MODULE) and Django-specific filterwarnings.  Since the
awxkit tox environment does not install Django or pytest-django, these
cause test collection to fail.

The root cause is that pytest.ini has absolute priority in pytest's
config discovery — it searches all ancestor directories for pytest.ini
before falling back to tox.ini's [pytest] section.  A [pytest] section
in awxkit/tox.ini alone cannot prevent the root config from being used.

Fix by:
- Adding awxkit/pytest.ini to act as the primary config boundary
  (pytest.ini has the highest priority in config discovery, so its
  presence in awxkit/ stops the upward search before reaching root)
- Adding explicit `test` path argument to the pytest command in
  awxkit/tox.ini so pytest discovers tests correctly
- Adding `testpaths` and `python_files` to the [pytest] section in
  awxkit/tox.ini as a secondary config boundary
- Adding awxkit/conftest.py that registers the Django-specific CLI
  options and INI keys as harmless no-ops, as a further safety net

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-24 09:40:26 -04:00
5 changed files with 20 additions and 29 deletions

View File

@@ -68,7 +68,7 @@ class RecordedQueryLog(object):
progname = match
break
else:
progname = 'unknown'
progname = os.path.basename(sys.argv[0])
filepath = os.path.join(self.dest, '{}.sqlite'.format(progname))
version = _get_version('awx')
log = sqlite3.connect(filepath, timeout=3)

View File

@@ -20,9 +20,6 @@ from dispatcherd.factories import get_control_from_settings
# Django
from django.conf import settings
from django.db import models, connection, transaction
# psycopg
from psycopg import sql
from django.db.models.constraints import UniqueConstraint
from django.core.exceptions import NON_FIELD_ERRORS
from django.utils.translation import gettext_lazy as _
@@ -1182,23 +1179,17 @@ class UnifiedJob(
raise StdoutMaxBytesExceeded(total, max_supported)
tbl = self._meta.db_table + 'event'
where_parts = [
sql.SQL('{} = {}').format(sql.Identifier(self.event_parent_key), sql.Literal(self.id)),
sql.SQL("stdout != ''"),
]
created_by_cond = ''
if self.has_unpartitioned_events:
tbl = '_unpartitioned_' + tbl
tbl = f'_unpartitioned_{tbl}'
else:
where_parts.insert(0, sql.SQL('job_created = {}').format(sql.Literal(self.created)))
created_by_cond = f"job_created='{self.created.isoformat()}' AND "
copy_sql = sql.SQL('COPY (SELECT stdout FROM {} WHERE {} ORDER BY start_line) TO STDOUT').format(
sql.Identifier(tbl),
sql.SQL(' AND ').join(where_parts),
)
sql = f"copy (select stdout from {tbl} where {created_by_cond}{self.event_parent_key}={self.id} and stdout != '' order by start_line) to stdout" # nosql
# psycopg3's copy writes bytes, but callers of this
# function assume a str-based fd will be returned; decode
# .write() calls on the fly to maintain this interface
with cursor.copy(copy_sql) as copy:
with cursor.copy(sql) as copy:
while data := copy.read():
fd.write(smart_str(bytes(data)))

View File

@@ -830,13 +830,14 @@ class MockCopy:
events = []
index = -1
def __init__(self):
def __init__(self, sql):
self.events = []
parts = sql.split(' ')
tablename = parts[parts.index('from') + 1]
for cls in (JobEvent, AdHocCommandEvent, ProjectUpdateEvent, InventoryUpdateEvent, SystemJobEvent):
events = list(cls.objects.order_by('start_line').values_list('stdout', flat=True))
if events:
self.events = events
break
if cls._meta.db_table == tablename:
for event in cls.objects.order_by('start_line').all():
self.events.append(event.stdout)
def read(self):
self.index = self.index + 1
@@ -857,8 +858,9 @@ def sqlite_copy(request, mocker):
# copy is postgres-specific, and SQLite doesn't support it; mock its
# behavior to test that it writes a file that contains stdout from events
def write_stdout(self, sql, params=None):
return MockCopy()
def write_stdout(self, sql):
mock_copy = MockCopy(sql)
return mock_copy
mocker.patch.object(SQLiteCursorWrapper, 'copy', write_stdout, create=True)

View File

@@ -1,6 +1,7 @@
import collections
import os
import sqlite3
import sys
import unittest
import pytest
@@ -124,7 +125,7 @@ def test_sql_above_threshold(tmpdir):
args, kw = _call
assert args == ('EXPLAIN VERBOSE {}'.format(QUERY['sql']),)
path = os.path.join(tmpdir, 'unknown.sqlite')
path = os.path.join(tmpdir, '{}.sqlite'.format(os.path.basename(sys.argv[0])))
assert os.path.exists(path)
# verify the results

View File

@@ -438,7 +438,7 @@ class ControllerAPIModule(ControllerModule):
raise RuntimeError('Expected list from API at {0}, got: {1}'.format(endpoint, response))
next_page = response['json']['next']
if response['json'].get('count', 0) > 10000:
if response['json']['count'] > 10000:
self.fail_json(msg='The number of items being queried for is higher than 10,000.')
while next_page is not None:
@@ -493,11 +493,8 @@ class ControllerAPIModule(ControllerModule):
fail_msg += ', detail: {0}'.format(response['json']['detail'])
self.fail_json(msg=fail_msg)
if 'results' not in response['json']:
self.fail_json(msg="The endpoint did not provide a results list")
if 'count' not in response['json']:
response['json']['count'] = len(response['json']['results'])
if 'count' not in response['json'] or 'results' not in response['json']:
self.fail_json(msg="The endpoint did not provide count and results")
if response['json']['count'] == 0:
if allow_none: