TT#86350 build: create_fake_buildrelease command

to be able to build mrX.Y as release-mrX.Y-update
we need to have a previous build. This tools allows
to create a fake build

* don't trigger old BuildRelease instance
* create_build_release() add fake option

Change-Id: I99293a1c41a567d920a086bcaec329defdcf235b
pull/3/head
Victor Seva 6 years ago
parent 821c4aff49
commit 8dc31922b0

@ -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 <http://www.gnu.org/licenses/>.
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)

@ -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
),
),
]

@ -13,8 +13,10 @@
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>. # with this program. If not, see <http://www.gnu.org/licenses/>.
import logging import logging
from datetime import timedelta
from django.db.models import signals from django.db.models import signals
from django.utils import timezone
from ..conf import settings from ..conf import settings
from .br import BuildRelease from .br import BuildRelease
@ -31,6 +33,12 @@ def br_manage(sender, **kwargs):
if instance.release.endswith("-update"): if instance.release.endswith("-update"):
build_resume.delay(instance.pk) build_resume.delay(instance.pk)
logger.debug("BuildRelease:%s triggered", instance) 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: else:
build_release.delay(instance.pk) build_release.delay(instance.pk)
logger.debug("BuildRelease:%s triggered", instance) logger.debug("BuildRelease:%s triggered", instance)

@ -12,12 +12,14 @@
# #
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>. # with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
import logging import logging
import re import re
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
from django.forms.models import model_to_dict from django.forms.models import model_to_dict
from django.utils import timezone
from ..conf import settings from ..conf import settings
from build.exceptions import BuildReleaseUnique from build.exceptions import BuildReleaseUnique
@ -41,7 +43,7 @@ class BuildReleaseManager(models.Manager):
Q(release=version) | Q(release="{}-update".format(version)) 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) config = ReleaseConfig(release)
qs = self.get_queryset() qs = self.get_queryset()
br = qs.filter(release=config.release) br = qs.filter(release=config.release)
@ -57,13 +59,24 @@ class BuildReleaseManager(models.Manager):
"set {} as release" "set {} as release"
) )
logger.info(msg.format(config.branch, release_ok)) 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( return self.create(
start_date=start_date,
uuid=uuid, uuid=uuid,
tag=config.tag, tag=config.tag,
branch=config.branch, branch=config.branch,
release=release_ok, release=release_ok,
distribution=config.debian_release, distribution=config.debian_release,
projects=",".join(config.projects), projects=projects,
built_projects=built_projects,
) )
def jbi(self, release_uuid): def jbi(self, release_uuid):
@ -113,7 +126,9 @@ class BuildReleaseManager(models.Manager):
class BuildRelease(models.Model): class BuildRelease(models.Model):
uuid = models.CharField(max_length=64, unique=True, null=False) 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) tag = models.CharField(max_length=50, null=True, blank=True)
branch = models.CharField(max_length=50, null=False) branch = models.CharField(max_length=50, null=False)
release = models.CharField(max_length=50, null=False, db_index=True) release = models.CharField(max_length=50, null=False, db_index=True)

@ -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 <http://www.gnu.org/licenses/>.
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)

@ -12,10 +12,12 @@
# #
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>. # with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from unittest.mock import MagicMock from unittest.mock import MagicMock
from unittest.mock import patch from unittest.mock import patch
from django.test import override_settings from django.test import override_settings
from django.utils import timezone
from build.exceptions import BuildReleaseUnique from build.exceptions import BuildReleaseUnique
from build.models import BuildRelease from build.models import BuildRelease
@ -89,6 +91,15 @@ class BuildReleaseManagerTestCase(BaseTest):
qs = BuildRelease.objects.release("release-mr8.1") qs = BuildRelease.objects.release("release-mr8.1")
self.assertEqual(qs.count(), prev.count() + 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): class BuildReleaseTestCase(BaseTest):
fixtures = [ fixtures = [

Loading…
Cancel
Save