Skip to content

Commit a738647

Browse files
committed
test: add coverage for the run cleanup job
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent e74d2c7 commit a738647

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
11+
from datetime import timedelta
12+
13+
from django.test import TestCase
14+
from django.utils import timezone
15+
16+
from vulnerabilities import models
17+
from vulnerabilities.pipelines.management.cleanup_pipeline_runs import CleanupPipelineRuns
18+
from vulnerabilities.tests.pipelines import TestLogger
19+
20+
21+
class TestCleanupPipelineRuns(TestCase):
22+
def setUp(self):
23+
self.logger = TestLogger()
24+
25+
self.schedule1 = models.PipelineSchedule.objects.create(pipeline_id="test_pipeline")
26+
for _ in range(70):
27+
models.PipelineRun.objects.create(
28+
pipeline=self.schedule1,
29+
run_exitcode=0,
30+
)
31+
32+
def test_pipelines_management_cleanup_pipeline_runs_with_old_and_new_runs(self):
33+
cutoff = timezone.now() - timedelta(days=100)
34+
old_run_ids = models.PipelineRun.objects.all()[:60].values("run_id")
35+
models.PipelineRun.objects.filter(run_id__in=old_run_ids).update(created_date=cutoff)
36+
37+
self.assertEqual(models.PipelineRun.objects.count(), 70)
38+
pipeline = CleanupPipelineRuns()
39+
pipeline.log = self.logger.write
40+
exit_code, _ = pipeline.execute()
41+
42+
self.assertEqual(exit_code, 0)
43+
self.assertEqual(models.PipelineRun.objects.count(), 60)
44+
self.assertIn(
45+
"Successfully removed 10 runs from 1 pipelines that were older than 60 days",
46+
self.logger.getvalue(),
47+
)
48+
49+
def test_pipelines_management_cleanup_pipeline_runs_with_new_runs_only(self):
50+
self.assertEqual(models.PipelineRun.objects.count(), 70)
51+
pipeline = CleanupPipelineRuns()
52+
pipeline.log = self.logger.write
53+
exit_code, _ = pipeline.execute()
54+
55+
self.assertEqual(exit_code, 0)
56+
self.assertEqual(models.PipelineRun.objects.count(), 70)
57+
self.assertIn(
58+
"Successfully removed 0 runs from 0 pipelines that were older than 60 days",
59+
self.logger.getvalue(),
60+
)

0 commit comments

Comments
 (0)