diff --git a/build/management/__init__.py b/build/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/build/management/commands/__init__.py b/build/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/build/management/commands/create_fake_buildrelease.py b/build/management/commands/create_fake_buildrelease.py new file mode 100644 index 0000000..a9ad562 --- /dev/null +++ b/build/management/commands/create_fake_buildrelease.py @@ -0,0 +1,37 @@ +# Copyright (C) 2020 The Sipwise Team - http://sipwise.com +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +import uuid + +from django.core.management.base import BaseCommand +from django.core.management.base import CommandError + +from build.models import BuildRelease +from build.models.br import regex_mrXX + + +class Command(BaseCommand): + help = "generates fake BuildRelease info. Useful just for mrX.Y builds" + + def add_arguments(self, parser): + parser.add_argument("version") + + def handle(self, *args, **options): + ver = options["version"] + if not regex_mrXX.match(ver): + raise CommandError("'{}'' not mrX.Y version".format(ver)) + release = "release-{}".format(ver) + if BuildRelease.objects.release(release).count() > 0: + raise CommandError("'{}' has already instances".format(release)) + BuildRelease.objects.create_build_release(uuid.uuid4(), ver, fake=True) diff --git a/build/migrations/0004_start_date.py b/build/migrations/0004_start_date.py new file mode 100644 index 0000000..b7075a6 --- /dev/null +++ b/build/migrations/0004_start_date.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.29 on 2020-07-24 14:53 +import django.utils.timezone +from django.db import migrations +from django.db import models + + +class Migration(migrations.Migration): + + dependencies = [ + ("build", "0003_buildrelease_triggered_projects"), + ] + + operations = [ + migrations.AlterField( + model_name="buildrelease", + name="start_date", + field=models.DateTimeField( + blank=True, default=django.utils.timezone.now, editable=False + ), + ), + ] diff --git a/build/models/__init__.py b/build/models/__init__.py index 17e6748..15fc978 100644 --- a/build/models/__init__.py +++ b/build/models/__init__.py @@ -13,8 +13,10 @@ # You should have received a copy of the GNU General Public License along # with this program. If not, see . import logging +from datetime import timedelta from django.db.models import signals +from django.utils import timezone from ..conf import settings from .br import BuildRelease @@ -31,6 +33,12 @@ def br_manage(sender, **kwargs): if instance.release.endswith("-update"): build_resume.delay(instance.pk) logger.debug("BuildRelease:%s triggered", instance) + elif timezone.now() > instance.start_date + timedelta(minutes=15): + logger.debug( + "BuildRelease:%s not triggered, is from the past:%s", + instance, + instance.start_date, + ) else: build_release.delay(instance.pk) logger.debug("BuildRelease:%s triggered", instance) diff --git a/build/models/br.py b/build/models/br.py index 3e696d7..22472a3 100644 --- a/build/models/br.py +++ b/build/models/br.py @@ -12,12 +12,14 @@ # # You should have received a copy of the GNU General Public License along # with this program. If not, see . +import datetime import logging import re from django.db import models from django.db.models import Q from django.forms.models import model_to_dict +from django.utils import timezone from ..conf import settings from build.exceptions import BuildReleaseUnique @@ -41,7 +43,7 @@ class BuildReleaseManager(models.Manager): Q(release=version) | Q(release="{}-update".format(version)) ) - def create_build_release(self, uuid, release): + def create_build_release(self, uuid, release, fake=False): config = ReleaseConfig(release) qs = self.get_queryset() br = qs.filter(release=config.release) @@ -57,13 +59,24 @@ class BuildReleaseManager(models.Manager): "set {} as release" ) logger.info(msg.format(config.branch, release_ok)) + projects = ",".join(config.projects) + if fake: + start_date = timezone.make_aware(datetime.datetime(1977, 1, 1)) + built_projects = ",".join( + list(settings.BUILD_RELEASE_JOBS) + config.projects + ) + else: + start_date = timezone.now() + built_projects = None return self.create( + start_date=start_date, uuid=uuid, tag=config.tag, branch=config.branch, release=release_ok, distribution=config.debian_release, - projects=",".join(config.projects), + projects=projects, + built_projects=built_projects, ) def jbi(self, release_uuid): @@ -113,7 +126,9 @@ class BuildReleaseManager(models.Manager): class BuildRelease(models.Model): uuid = models.CharField(max_length=64, unique=True, null=False) - start_date = models.DateTimeField(auto_now_add=True) + start_date = models.DateTimeField( + blank=True, editable=False, default=timezone.now + ) tag = models.CharField(max_length=50, null=True, blank=True) branch = models.CharField(max_length=50, null=False) release = models.CharField(max_length=50, null=False, db_index=True) diff --git a/build/test/test_commands.py b/build/test/test_commands.py new file mode 100644 index 0000000..67ae7e0 --- /dev/null +++ b/build/test/test_commands.py @@ -0,0 +1,41 @@ +# Copyright (C) 2020 The Sipwise Team - http://sipwise.com +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +from django.core.management import call_command +from django.core.management.base import CommandError +from django.test import TestCase + +from build.models import BuildRelease + + +class createFakeBuildReleaseTest(TestCase): + fixtures = ["test_models"] + + def test_mrXXX_ko(self): + with self.assertRaises(CommandError): + call_command("create_fake_buildrelease", "mr8.1.1") + + def test_mrXX_ko(self): + with self.assertRaises(CommandError): + call_command("create_fake_buildrelease", "mr8.1") + + def test_mrXX_ok(self): + self.assertEqual( + BuildRelease.objects.release("release-mr7.5").count(), 0 + ) + call_command("create_fake_buildrelease", "mr7.5") + qs = BuildRelease.objects.release("release-mr7.5") + self.assertEqual(qs.count(), 1) + br = qs.first() + self.assertTrue(br.done) diff --git a/build/test/test_models.py b/build/test/test_models.py index a200140..80eaa36 100644 --- a/build/test/test_models.py +++ b/build/test/test_models.py @@ -12,10 +12,12 @@ # # You should have received a copy of the GNU General Public License along # with this program. If not, see . +import datetime from unittest.mock import MagicMock from unittest.mock import patch from django.test import override_settings +from django.utils import timezone from build.exceptions import BuildReleaseUnique from build.models import BuildRelease @@ -89,6 +91,15 @@ class BuildReleaseManagerTestCase(BaseTest): qs = BuildRelease.objects.release("release-mr8.1") self.assertEqual(qs.count(), prev.count() + 1) + def test_create_fake(self, dlf): + br = BuildRelease.objects.create_build_release( + "AAA", "trunk", fake=True + ) + self.assertEqual( + br.start_date, timezone.make_aware(datetime.datetime(1977, 1, 1)) + ) + self.assertTrue(br.done) + class BuildReleaseTestCase(BaseTest): fixtures = [