Skip to content

Commit a034dcc

Browse files
authored
Build: test for healthcheck (#12359)
Continuation of #12332
1 parent 401b375 commit a034dcc

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

readthedocs/rtd_tests/tests/test_project.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -577,62 +577,3 @@ def test_user_can_change_project_with_same_lang(self):
577577
)
578578
self.assertEqual(resp.status_code, 200)
579579
self.assertNotContains(resp, "There is already a")
580-
581-
582-
class TestFinishInactiveBuildsTask(TestCase):
583-
fixtures = ["eric", "test_data"]
584-
585-
def setUp(self):
586-
self.client.login(username="eric", password="test")
587-
self.pip = Project.objects.get(slug="pip")
588-
589-
self.taggit = Project.objects.get(slug="taggit")
590-
self.taggit.container_time_limit = 7200 # 2 hours
591-
self.taggit.save()
592-
593-
# Build just started with the default time
594-
self.build_1 = Build.objects.create(
595-
project=self.pip,
596-
version=self.pip.get_stable_version(),
597-
state=BUILD_STATE_CLONING,
598-
)
599-
600-
# Build started an hour ago with default time
601-
self.build_2 = Build.objects.create(
602-
project=self.pip,
603-
version=self.pip.get_stable_version(),
604-
state=BUILD_STATE_TRIGGERED,
605-
)
606-
self.build_2.date = timezone.now() - datetime.timedelta(hours=1)
607-
self.build_2.save()
608-
609-
# Build started an hour ago with custom time (2 hours)
610-
self.build_3 = Build.objects.create(
611-
project=self.taggit,
612-
version=self.taggit.get_stable_version(),
613-
state=BUILD_STATE_TRIGGERED,
614-
)
615-
self.build_3.date = timezone.now() - datetime.timedelta(hours=1)
616-
self.build_3.save()
617-
618-
@pytest.mark.xfail(reason="Fails while we work out Docker time limits", strict=True)
619-
def test_finish_inactive_builds_task(self):
620-
finish_inactive_builds()
621-
622-
# Legitimate build (just started) not finished
623-
self.build_1.refresh_from_db()
624-
self.assertTrue(self.build_1.success)
625-
self.assertEqual(self.build_1.error, "")
626-
self.assertEqual(self.build_1.state, BUILD_STATE_CLONING)
627-
628-
# Build with default time finished
629-
self.build_2.refresh_from_db()
630-
self.assertFalse(self.build_2.success)
631-
self.assertNotEqual(self.build_2.error, "")
632-
self.assertEqual(self.build_2.state, BUILD_STATE_FINISHED)
633-
634-
# Build with custom time not finished
635-
self.build_3.refresh_from_db()
636-
self.assertTrue(self.build_3.success)
637-
self.assertEqual(self.build_3.error, "")
638-
self.assertEqual(self.build_3.state, BUILD_STATE_TRIGGERED)

readthedocs/rtd_tests/tests/test_projects_tasks.py

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
import datetime
2+
13
from unittest.mock import patch
24

35
from django.test import TestCase
6+
from django.utils import timezone
47
from django_dynamic_fixture import get
58

6-
from readthedocs.builds.constants import BUILD_STATUS_SUCCESS, EXTERNAL
9+
from readthedocs.builds.constants import (
10+
BUILD_STATUS_SUCCESS,
11+
BUILD_STATE_CLONING,
12+
BUILD_STATE_CANCELLED,
13+
BUILD_STATE_TRIGGERED,
14+
EXTERNAL,
15+
)
716
from readthedocs.builds.models import Build, Version
8-
from readthedocs.projects.models import Project
9-
from readthedocs.projects.tasks.utils import send_external_build_status
17+
from readthedocs.projects.models import Feature, Project
18+
from readthedocs.projects.tasks.utils import finish_unhealthy_builds, send_external_build_status
1019

1120

1221
class SendBuildStatusTests(TestCase):
@@ -46,3 +55,53 @@ def test_send_external_build_status_with_internal_version(self, send_build_statu
4655
)
4756

4857
send_build_status.delay.assert_not_called()
58+
59+
class TestFinishInactiveBuildsTask(TestCase):
60+
61+
@patch("readthedocs.projects.tasks.utils.app")
62+
def test_finish_unhealthy_builds_task(self, mocked_app):
63+
project = get(Project)
64+
feature = get(Feature, feature_id=Feature.BUILD_HEALTHCHECK)
65+
feature.projects.add(project)
66+
67+
# Build just started with the default time and healthcheck now
68+
build_1 = get(
69+
Build,
70+
project=project,
71+
version=project.get_stable_version(),
72+
state=BUILD_STATE_CLONING,
73+
healthcheck=timezone.now(),
74+
)
75+
76+
# Build started an hour ago with default time and healthcheck 59s ago
77+
build_2 = get(
78+
Build,
79+
project=project,
80+
version=project.get_stable_version(),
81+
state=BUILD_STATE_TRIGGERED,
82+
date=timezone.now() - datetime.timedelta(hours=1),
83+
healthcheck=timezone.now() - datetime.timedelta(seconds=59),
84+
)
85+
86+
# Build started an hour ago with custom time (2 hours) and healthcheck 15m ago
87+
build_3 = get(
88+
Build,
89+
project=project,
90+
version=project.get_stable_version(),
91+
state=BUILD_STATE_TRIGGERED,
92+
date=timezone.now() - datetime.timedelta(hours=2),
93+
healthcheck=timezone.now() - datetime.timedelta(minutes=15),
94+
)
95+
96+
finish_unhealthy_builds()
97+
98+
build_1.refresh_from_db()
99+
self.assertEqual(build_1.state, BUILD_STATE_CLONING)
100+
101+
build_2.refresh_from_db()
102+
self.assertEqual(build_2.state, BUILD_STATE_TRIGGERED)
103+
104+
build_3.refresh_from_db()
105+
self.assertEqual(build_3.state, BUILD_STATE_CANCELLED)
106+
self.assertEqual(build_3.success, False)
107+
self.assertEqual(build_3.notifications.count(), 1)

0 commit comments

Comments
 (0)