Skip to content

Rework test code#272

Open
jsf9k wants to merge 42 commits intodevelopfrom
rework-test-code
Open

Rework test code#272
jsf9k wants to merge 42 commits intodevelopfrom
rework-test-code

Conversation

@jsf9k
Copy link
Copy Markdown
Member

@jsf9k jsf9k commented May 1, 2026

🗣 Description

This pull request reworks the test code to use a Docker composition running an actual PostgreSQL instance. As pytest runs the tests interact with the containers in the Docker composition instead of making extensive use of mock objects.

💭 Motivation and context

These changes make testing run faster and provide for better tests. This is also the way that this code is intended to be run anyway in cisagov/guacamole-composition.

Resolves #270.

🧪 Testing

All automated tests pass.

✅ Pre-approval checklist

  • This PR has an informative and human-readable title.
  • Changes are limited to a single goal - eschew scope creep!
  • All future TODOs are captured in issues, which are referenced in code comments.
  • All relevant type-of-change labels have been added.
  • I have read the CONTRIBUTING document.
  • These code changes follow cisagov code standards.
  • Tests have been added and/or modified to cover the changes in this PR.
  • All new and existing tests pass.
  • Bump major, minor, patch, pre-release, and/or build versions as appropriate via the bump_version script if this repository is versioned and the changes in this PR warrant a version bump.
  • Create a pre-release (necessary if and only if the pre-release version was bumped).

✅ Pre-merge checklist

  • Mark Mac and Windows checks as no longer required.

jsf9k added 11 commits April 30, 2026 22:38
This makes sense, and also speeds up the execution of the test code.
The Docker composition is based on that in
cisagov/guacamole-composition but is altered to allow an instance of
guacscanner running on the Docker host to access the mock AWS IMDS
service and the PostgreSQL instance.

Note that we also have to add a library to the test requirements of
the Python library and set up some test fixtures.  We also have to
pre-populate some dummy secrets used by the Docker composition.
This is preferable to specifying them manually.
I will uncomment these tests and adapt them to the new testing Docker
composition one by one.
This is just in case the Docker host has an external IP.
Also use pytest-env to set an environment variable necessary for moto
to make use of the server we are now running in the Docker
composition; normally moto starts a local server on the host where
pytest is being run.
With alterations for new Docker composition test environment.
@jsf9k jsf9k self-assigned this May 1, 2026
@github-actions github-actions Bot added version bump This issue or pull request increments the version number dependencies Pull requests that update a dependency file python Pull requests that update Python code test This issue or pull request adds or otherwise modifies test code docker Pull requests that update Docker code labels May 1, 2026
jsf9k added 12 commits May 4, 2026 10:10
Instead just run Moto as a local library in the pytest run.
This isn't needed in this repository.
This container is unnecessary here as we have no actual instances to
connect to via guacamole.
This container doesn't bind to port 80, so there is no need fro it to
have this capability.
We want these to be class scope so that they remain up for related
tests in the same class but the PostgreSQL database gets destroyed
between test classes.
This will allow the PostgreSQL container to prepare itself for use
before it is queried by any test code.
This allows boto3 to function with Moto (our mock AWS library).
@github-actions github-actions Bot added the github-actions Pull requests that update GitHub Actions code label May 4, 2026
jsf9k added 6 commits May 4, 2026 14:47
Using a scope of function here makes the PostgreSQL container have the
same scope as the Moto mock AWS library.  The latter resets itself
after every test function.

Also remove a couple of unused fixtures and revert the scope of the
Docker composition itself to session.
I don't think this is necessary, but it certainly doesn't hurt.
This is the proper place for this code.
@jsf9k jsf9k force-pushed the rework-test-code branch from 29dba28 to 546d632 Compare May 5, 2026 15:29
jsf9k added 2 commits May 5, 2026 12:00
This should ensure that port 5432 is free on the GitHub runner.  The
fact that the PostgreSQL service was running on the GitHub runner was
previously causing issues when running pytest (with the Docker
composition) in GitHub Actions.
Also create a dummy (empty) VPC to search instead of using a dummy VPC
ID.
@jsf9k jsf9k force-pushed the rework-test-code branch from 5bd4e92 to 3c00bae Compare May 5, 2026 16:00
There are currently various issues with running Docker on the
GitHub-hosted Mac runners.  I created #274 to remind us to support Mac
when that becomes possible.

The official Guacamole images currently only support x86_64 and ARM64
on Linux; therefore, we cannot currently run the tests on Windows.  I
created #273 to remind us to again support Windows when that becomes
possible.
@jsf9k jsf9k force-pushed the rework-test-code branch from 2578abc to c4407b0 Compare May 5, 2026 18:03
jsf9k added 4 commits May 5, 2026 14:07
This is a better home for these testing-specific files.
This is a better location for this testing-specific file.
@jsf9k jsf9k marked this pull request as ready for review May 5, 2026 18:57
@jsf9k jsf9k requested a review from dav3r as a code owner May 5, 2026 18:57
Copilot AI review requested due to automatic review settings May 5, 2026 18:57
@jsf9k jsf9k requested review from felddy and mcdonnnj as code owners May 5, 2026 18:57
@jsf9k jsf9k requested a review from a team May 5, 2026 19:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reworks the test strategy to run against a real PostgreSQL instance in Docker instead of heavily mocking psycopg, aligning test behavior more closely with production usage of guacscanner.

Changes:

  • Replaced most database-mocking tests with Docker-backed integration tests using python-on-whales.
  • Added Docker Compose and secret fixtures for a PostgreSQL/Guacamole test environment.
  • Updated the CLI to support overriding the PostgreSQL hostname and trimmed newline characters when reading secrets from files.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/test_guacscanner.py Reorganized tests into classes and converted many cases to Docker-backed integration tests.
tests/secrets/postgres-username Added test secret for the PostgreSQL username.
tests/secrets/postgres-password Added test secret for the PostgreSQL password.
tests/conftest.py Added pytest fixtures to start/stop the Docker composition and expose Postgres details.
tests/compose.yml Added Docker Compose stack for PostgreSQL and Guacamole-based test setup.
src/guacscanner/guacscanner.py Added --postgres-hostname, stripped file-based secret values, and adjusted loop timing.
src/guacscanner/_version.py Bumped version to 3.0.2-rc.1.
pyproject.toml Added python-on-whales to test dependencies.
.github/workflows/build.yml Updated test matrices and CI setup for Docker-backed tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_guacscanner.py
Comment on lines +16 to +26
LOG_LEVELS: list[str] = []
if sys.version_info >= (3, 11):
LOG_LEVELS = [*logging.getLevelNamesMapping()]
else:
# The logging.getLevelNamesMapping method was only introduced in
# Python 3.11.
LOG_LEVELS = [
logging.getLevelName(x)
for x in range(0, 101)
if not logging.getLevelName(x).startswith("Level")
]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see commit 7cfa943.

Comment thread tests/test_guacscanner.py Outdated
Comment thread tests/compose.yml
Comment on lines +69 to +72
volumes:
- read_only: true
source: initdb
target: /opt/guacamole/extensions/guacamole-auth-jdbc/postgresql/schema
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to work, and I followed the official instructions from Guacamole.

Comment thread tests/compose.yml
Comment on lines +26 to +27
ports:
- 127.0.0.1:5432:5432/tcp
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is OK. We want our PostgreSQL to be listening on localhost:5432 for testing purposes.

run: sudo systemctl stop postgresql
- name: Run tests
env:
RELEASE_TAG: ${{ github.event.release.tag_name }}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see commit 84b9671.

jsf9k and others added 5 commits May 5, 2026 15:11
We can't call another member function here since that will reset Moto (the mock AWS library we are using).

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This gets rid of some errors from our flake8 pre-commit linter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file docker Pull requests that update Docker code github-actions Pull requests that update GitHub Actions code python Pull requests that update Python code test This issue or pull request adds or otherwise modifies test code version bump This issue or pull request increments the version number

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use pytest-docker to simplify and speed up testing

2 participants