-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup batch test results every day
- Loading branch information
Showing
10 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.16 on 2024-12-03 19:56 | ||
|
||
import datetime | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("checks", "0015_add_rpki_scoring"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="webtesttls", | ||
name="timestamp", | ||
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(1, 1, 1, 0, 0)), | ||
preserve_default=False, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.16 on 2024-12-03 20:11 | ||
|
||
import datetime | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("checks", "0016_webtesttls_timestamp"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="webtestappsecpriv", | ||
name="timestamp", | ||
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(1, 1, 1, 0, 0)), | ||
preserve_default=False, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/sh | ||
|
||
# perform cleanup maintenance on database: | ||
# | ||
# - remove dangling subtests (probe results with no report) caused by periodic test or aborted single tests | ||
# - remove test reports for batch periodic tests | ||
|
||
set -e | ||
|
||
if [ ! "$CRON_DAILY_DATABASE_CLEANUP" = "True" ];then | ||
exit 0 | ||
fi | ||
|
||
docker ps --filter label=com.docker.compose.service=app --quiet | xargs -I% --no-run-if-empty docker exec % ./manage.py database_cleanup -v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.core.management.base import BaseCommand | ||
from checks.models import BatchRequest, DomainTestIpv6, DomainTestDnssec, WebTestTls, WebTestAppsecpriv, WebTestRpki | ||
import logging | ||
import datetime | ||
from django.conf import settings | ||
from django.utils import timezone | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
BATCH_PERIODIC_TESTS_PREFIX = "batch periodic tests" | ||
|
||
TEST_REPORT_PROBE_MODELS = [DomainTestIpv6, DomainTestDnssec, WebTestTls, WebTestAppsecpriv, WebTestRpki] | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Removes batch periodic test scan results and dangling probe results from database" | ||
|
||
def info(self, text): | ||
if self.v_level: | ||
self.stdout.write(f"{text}") | ||
|
||
def debug(self, text): | ||
if self.v_level > 1: | ||
self.stdout.write(f"{text}") | ||
|
||
def handle(self, *args, **options): | ||
logging.basicConfig(level=logging.INFO if options["verbosity"] > 0 else logging.ERROR) | ||
|
||
count, _ = BatchRequest.objects.filter(name__startswith=BATCH_PERIODIC_TESTS_PREFIX).delete() | ||
log.info("Deleted %s BatchRequest objects from batch periodic tests.", count) | ||
|
||
timestamp_recent_probes = timezone.make_aware(datetime.datetime.now()) - datetime.timedelta( | ||
seconds=int(settings.CACHE_TTL) | ||
) | ||
|
||
for model in TEST_REPORT_PROBE_MODELS: | ||
# >>> print(DomainTestIpv6.objects.filter(domaintestreport__isnull=True).values_list('id').query) | ||
# SELECT "checks_domaintestipv6"."id" FROM "checks_domaintestipv6" LEFT OUTER JOIN "checks_domaintestreport" | ||
# ON ("checks_domaintestipv6"."id" = "checks_domaintestreport"."ipv6_id") | ||
# WHERE "checks_domaintestreport"."id" IS NULL | ||
|
||
# find all test probe results that have no report associated, but not to recent because | ||
# those might be unfinished tests | ||
count, _ = model.objects.filter( | ||
domaintestreport__isnull=True, timestamp__lt=timestamp_recent_probes | ||
).delete() | ||
log.info("Deleted %s probes that don't have an associated report.", count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.core.management import call_command | ||
from checks.models import DomainTestIpv6, DomainTestReport | ||
import datetime | ||
import pytest | ||
|
||
def test_cleanup_aborted_or_periodic_test_results(db): | ||
"""Make sure that test results with a report are deleted on cleanup, but not if they are recent.""" | ||
ipv6_no_report = DomainTestIpv6(domain="example.com", report="{}") | ||
ipv6_no_report.save() | ||
ipv6_no_report.timestamp=datetime.datetime.now() - datetime.timedelta(seconds=200) | ||
ipv6_no_report.save() | ||
|
||
ipv6_report = DomainTestIpv6(domain="example.com", report="{}") | ||
ipv6_report.save() | ||
ipv6_report.timestamp=datetime.datetime.now() - datetime.timedelta(seconds=200) | ||
ipv6_report.save() | ||
|
||
ipv6_no_report_recent = DomainTestIpv6(domain="example.com", report="{}") | ||
ipv6_no_report_recent.save() | ||
|
||
ipv6_report_recent = DomainTestIpv6(domain="example.com", report="{}") | ||
ipv6_report_recent.save() | ||
|
||
report = DomainTestReport(domain="example.com", ipv6=ipv6_report) | ||
report.save() | ||
|
||
# run cleanup | ||
call_command("database_cleanup") | ||
|
||
with pytest.raises(DomainTestIpv6.DoesNotExist): | ||
ipv6_no_report.refresh_from_db() | ||
|
||
ipv6_report.refresh_from_db() | ||
assert ipv6_report | ||
|
||
ipv6_no_report_recent.refresh_from_db() | ||
assert ipv6_no_report_recent | ||
|
||
ipv6_report_recent.refresh_from_db() | ||
assert ipv6_report_recent |