Skip to content

Commit

Permalink
functional test for migrations on sqlite
Browse files Browse the repository at this point in the history
* We commonly subvert running migrations in test land. Test land uses
  sqlite. By not constantly exercising this code path it atrophies. The
  smoke test here is to continuously exercise that code path.
* Add ci test to run migration tests separately, they take =~ 2-3
  minutes each on my laptop.
* The smoke tests also serves as an example of how to write migration
  tests.
  • Loading branch information
chrismeyersfsu committed Nov 15, 2023
1 parent efc44db commit fb42b7b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
tests:
- name: api-test
command: /start_tests.sh
- name: api-test-migrations
command: /start_tests.sh
env:
PYTEST_ARGS: -n auto --migrations -m migration_test
- name: api-lint
command: /var/lib/awx/venv/awx/bin/tox -e linters
- name: api-swagger
Expand All @@ -43,7 +47,10 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Run check ${{ matrix.tests.name }}
run: AWX_DOCKER_CMD='${{ matrix.tests.command }}' make docker-runner
run: make docker-runner
env:
AWX_DOCKER_CMD: ${{ matrix.tests.command }}
PYTEST_ARGS: ${{ matrix.tests.env.PYTEST_ARGS }}

dev-env:
runs-on: ubuntu-latest
Expand Down
2 changes: 0 additions & 2 deletions awx/main/migrations/_sqlite_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import sys

from django.db import migrations


Expand Down
44 changes: 44 additions & 0 deletions awx/main/tests/functional/test_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

from django_test_migrations.plan import all_migrations, nodes_to_tuples

"""
Most tests that live in here can probably be deleted at some point. They are mainly
for a developer. When AWX versions that users upgrade from falls out of support that
is when migration tests can be deleted. This is also a good time to squash. Squashing
will likely mess with the tests that live here.
The smoke test should be kept in here. The smoke test ensures that our migrations
continue to work when sqlite is the backing database (vs. the default DB of postgres).
"""


@pytest.mark.django_db
class TestMigrationSmoke:
def test_happy_path(self, migrator):
"""
This smoke test runs all the migrations.
Example of how to use django-test-migration to invoke particular migration(s)
while weaving in object creation and assertions.
Note that this is more than just an example. It is a smoke test because it runs ALL
the migrations. Our "normal" unit tests subvert the migrations running because it is slow.
"""
migration_nodes = all_migrations('default')
migration_tuples = nodes_to_tuples(migration_nodes)
final_migration = migration_tuples[-1]

migrator.apply_initial_migration(('main', None))
# I just picked a newish migration at the time of writing this.
# If someone from the future finds themselves here because the are squashing migrations
# it is fine to change the 0180_... below to some other newish migration
intermediate_state = migrator.apply_tested_migration(('main', '0180_add_hostmetric_fields'))

Instance = intermediate_state.apps.get_model('main', 'Instance')
# Create any old object in the database
Instance.objects.create(hostname='foobar', node_type='control')

final_state = migrator.apply_tested_migration(final_migration)
Instance = final_state.apps.get_model('main', 'Instance')
assert Instance.objects.filter(hostname='foobar').count() == 1
1 change: 1 addition & 0 deletions requirements/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
build
coreapi
django-debug-toolbar==3.2.4
django-test-migrations
drf-yasg
# pprofile - re-add once https://github.com/vpelletier/pprofile/issues/41 is addressed
ipython>=7.31.1 # https://github.com/ansible/awx/security/dependabot/30
Expand Down

0 comments on commit fb42b7b

Please sign in to comment.