Skip to content
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

Overhaul test suite with fixtures #2675

Conversation

lunkwill42
Copy link
Member

@lunkwill42 lunkwill42 commented Sep 5, 2023

This PR has an overarching goal of making the test suite easier to run locally, rather then requiring devs to run all tests inside a very specifically defined Docker container (which is NOT how the test suite is run on GitHub actions).

Goals:

  • Rather than spin up all kinds of third party services as part of either tox or conftest.py, such as gunicorn, PostgreSQL, Xvfb, while expecting that these are available in the local environment, these services should be defined as test fixtures. Any test that depends on PostgreSQL should declare a dependency to this fixture, in order for it to be able to use PostgreSQL.
  • A PostgreSQL fixture should be able to work with an already setup PostgreSQL server (such as in GitHub Actions), or spin up its own using Docker, rather than relying on being run inside a very specific container environment.
  • Once all tests are properly "fixturized", the separation of the "unit", "integration" and "functional" test suites into separate tox environments should go away. These test environments are essentially all the same, when it comes to Python dependencies, and are a source of a lot of redundancies that lead to time-consuming test runs.

Some of the changes herein would probably stand to be factored out into separate PRs, as there are many fixes to the existing test suite that are not dependent on the fixture changes made.

@sonarcloud
Copy link

sonarcloud bot commented Sep 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@github-actions
Copy link

github-actions bot commented Sep 5, 2023

Test results

       9 files         9 suites   3m 33s ⏱️
2 070 tests 1 506 ✔️ 13 💤 114  437 🔥
4 678 runs  3 547 ✔️ 36 💤 228  867 🔥

For more details on these failures and errors, see this check.

Results for commit b8a3582.

♻️ This comment has been updated with latest results.

@lunkwill42 lunkwill42 force-pushed the test/fixture-experiments branch from 1d4791a to b8a3582 Compare December 11, 2023 09:43
Copy link

sonarcloud bot commented Dec 11, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@lunkwill42 lunkwill42 force-pushed the test/fixture-experiments branch from b8a3582 to da5c940 Compare February 27, 2024 14:21
Copy link

sonarcloud bot commented Feb 27, 2024

Quality Gate Passed Quality Gate passed

Issues
1 New issue

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

codecov bot commented Sep 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 38.14%. Comparing base (936aa34) to head (ee65d2c).

❗ There is a different number of reports uploaded between BASE (936aa34) and HEAD (ee65d2c). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (936aa34) HEAD (ee65d2c)
2 1
Additional details and impacted files
@@                   Coverage Diff                    @@
##           test/fix-skip-issue    #2675       +/-   ##
========================================================
- Coverage                60.47%   38.14%   -22.33%     
========================================================
  Files                      603      603               
  Lines                    43771    43777        +6     
  Branches                    48       48               
========================================================
- Hits                     26469    16700     -9769     
- Misses                   17290    27065     +9775     
  Partials                    12       12               

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lunkwill42
Copy link
Member Author

These items have been factored out as separate PRs and this PR has been rebased on the latest master:

  • Webcrawler used without test generation (single test for validation, single test for reachability)
  • Gunicorn as a fixture
  • Adaptations for non-Debian OSes such as MacOS or NixOS

@lunkwill42 lunkwill42 force-pushed the test/fixture-experiments branch from 57c8096 to 7e7ea25 Compare September 24, 2024 11:08
@lunkwill42 lunkwill42 force-pushed the test/fixture-experiments branch from 7e7ea25 to 94d9e4e Compare September 24, 2024 11:56
@lunkwill42 lunkwill42 changed the base branch from master to test/fix-skip-issue September 24, 2024 11:57
@lunkwill42
Copy link
Member Author

Extracted everything to do with skip logic issues to #3015 and rebased this on that.

@lunkwill42 lunkwill42 force-pushed the test/fix-skip-issue branch 2 times, most recently from bf85f9f to 936aa34 Compare September 24, 2024 13:06
This creates a PostgreSQL fixture that can be depended on by tests that
require a running PostgreSQL server with the NAV database to be present.

The fixture will work with externally set up PostgreSQL servers, like in
the case of GitHub Actions, while it can also provision a PostgreSQL
server with the NAV schema using Docker if run locally.
This ensures any tests that rely on the various pre-existing db-related
fixtures will have a running PostgreSQL server available.
This ensures the postgres fixture is started for all tests that inherit
from DjangoTransactionTestCase
Change TestCase implementations that should really be transactional test
cases (which in turn ensures they have PostgreSQL available through the
postgres fixture, and that transactions are properly rolled back after
the tests have run)
Declares a dependency on the postgresql fixture to ensure the NAV
database is actually available for these tests
This ensures that any test that depends on a logged in web client also
automatically depends on having the NAV database running.
This ensure the snmpsim fixture can run both on Linux and MacOS, by
replacing Linux specific parts (like looking at /proc/net/udp).

This slightly changes the logic of verifying whether the snmpsimd
process is up and running before yielding the fixture value: Instead of
waiting for something to appear to listen to port 1024, this verifies
the presence of the SNMP agent by performing an actual SNMP query.
These imports made the entire test module depend on the PostgreSQL
database actually running. Moving the imports to be local to the
tests (that depend directly or indirectly) on the postgresql fixture
ensures that the imports only take place after PostgreSQL is actually
running.
The admin_navlet fixture is dynamic/parametrized, which is why its
implementation is a bit weird. Unfortunately, the tests that depend on
it currently fail under the new regime of the postgresql fixture. Adding
this note so we know where to go back and fix it.
While pytest can accomplish a lot of exciting things, it cannot use
fixtures as input to test parametrization.  While can make a test
depend on a fixture for getting a postgresql instance, the test
discovery phase that generates the tests cannot.

I.e. we cannot get our test parameters from a database unless the
database was already up and running when the test discovery phase
started.  Sad, but true,.

This changes the navlet test to iterate over the available admin navlets
itself.
These tests would only work incidentally; i.e. if they ran after
another test that depended on the postgresql fixture
The postgresql fixture is fine to depend on if you just need a
database to be present.  But if you need to write data during the test,
you should probably use the db fixture.
For now, at least.  If the Open-LDAP libraries aren't available locally,
the test suite will fail because python-ldap will not build.
@lunkwill42 lunkwill42 force-pushed the test/fixture-experiments branch from 94d9e4e to ee65d2c Compare September 24, 2024 13:52
@lunkwill42 lunkwill42 deleted the branch Uninett:test/fix-skip-issue September 27, 2024 11:46
@lunkwill42 lunkwill42 closed this Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant