|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import django_dynamic_fixture as fixture |
| 4 | +import pytest |
| 5 | + |
| 6 | +from readthedocs.builds.constants import ( |
| 7 | + BUILD_FINAL_STATES, |
| 8 | + BUILD_STATE_BUILDING, |
| 9 | + BUILD_STATE_CANCELLED, |
| 10 | + BUILD_STATE_CLONING, |
| 11 | + BUILD_STATE_FINISHED, |
| 12 | + BUILD_STATE_INSTALLING, |
| 13 | + BUILD_STATE_TRIGGERED, |
| 14 | + BUILD_STATE_UPLOADING, |
| 15 | +) |
| 16 | +from readthedocs.builds.models import Build, Version |
| 17 | +from readthedocs.core.utils import trigger_build |
| 18 | +from readthedocs.projects.models import Feature, Project |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.django_db |
| 22 | +class TestCancelOldBuilds: |
| 23 | + @pytest.fixture(autouse=True) |
| 24 | + def setup(self): |
| 25 | + self.project = fixture.get(Project) |
| 26 | + self.version = fixture.get(Version, project=self.project) |
| 27 | + self.feature = fixture.get(Feature, feature_id=Feature.CANCEL_OLD_BUILDS) |
| 28 | + self.feature.projects.add(self.project) |
| 29 | + |
| 30 | + @pytest.mark.parametrize( |
| 31 | + "state", |
| 32 | + [ |
| 33 | + BUILD_STATE_TRIGGERED, |
| 34 | + BUILD_STATE_CLONING, |
| 35 | + BUILD_STATE_INSTALLING, |
| 36 | + BUILD_STATE_BUILDING, |
| 37 | + BUILD_STATE_UPLOADING, |
| 38 | + ], |
| 39 | + ) |
| 40 | + @mock.patch("readthedocs.core.utils.cancel_build") |
| 41 | + @mock.patch("readthedocs.projects.tasks.builds.update_docs_task") |
| 42 | + def test_cancel_old_running_build(self, update_docs_task, cancel_build, state): |
| 43 | + # Create a running build |
| 44 | + build = fixture.get( |
| 45 | + Build, project=self.project, version=self.version, state=state |
| 46 | + ) |
| 47 | + |
| 48 | + builds_count_before = Build.objects.count() |
| 49 | + |
| 50 | + # Trigger a new build for the same project/version |
| 51 | + result = trigger_build( |
| 52 | + project=self.project, |
| 53 | + version=self.version, |
| 54 | + ) |
| 55 | + |
| 56 | + triggered_build = Build.objects.first() |
| 57 | + builds_count_after = Build.objects.count() |
| 58 | + |
| 59 | + cancel_build.assert_called_once_with(build) |
| 60 | + assert result == (mock.ANY, triggered_build) |
| 61 | + assert builds_count_before == builds_count_after - 1 |
| 62 | + assert update_docs_task.signature.called |
| 63 | + assert update_docs_task.signature().apply_async.called |
| 64 | + |
| 65 | + @pytest.mark.parametrize( |
| 66 | + "state", |
| 67 | + [ |
| 68 | + BUILD_STATE_CANCELLED, |
| 69 | + BUILD_STATE_FINISHED, |
| 70 | + ], |
| 71 | + ) |
| 72 | + @mock.patch("readthedocs.core.utils.cancel_build") |
| 73 | + @mock.patch("readthedocs.projects.tasks.builds.update_docs_task") |
| 74 | + def test_not_cancel_old_finished_build(self, update_docs_task, cancel_build, state): |
| 75 | + # Create a running build |
| 76 | + build = fixture.get( |
| 77 | + Build, project=self.project, version=self.version, state=state |
| 78 | + ) |
| 79 | + |
| 80 | + builds_count_before = Build.objects.count() |
| 81 | + |
| 82 | + # Trigger a new build for the same project/version |
| 83 | + result = trigger_build( |
| 84 | + project=self.project, |
| 85 | + version=self.version, |
| 86 | + ) |
| 87 | + |
| 88 | + triggered_build = Build.objects.first() |
| 89 | + builds_count_after = Build.objects.count() |
| 90 | + |
| 91 | + cancel_build.assert_not_called() |
| 92 | + assert result == (mock.ANY, triggered_build) |
| 93 | + assert builds_count_before == builds_count_after - 1 |
| 94 | + assert update_docs_task.signature.called |
| 95 | + assert update_docs_task.signature().apply_async.called |
0 commit comments