TT#43813 hotfix: move code from task to utils

Tasks should deal only with serialize/de-serialize parameters
work need to be external so it can be tested easily

* don't call a task directly in tests that causes:

> ======================================================================
> ERROR: test_hotfixreleased (hotfix.test.test_hotfix_released.TestHotfixReleased)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "/code/venv_dev/lib/python3.7/site-packages/kombu/utils/objects.py", line 42, in __get__
>     return obj.__dict__[self.__name__]
> KeyError: 'backend'

Change-Id: I620f10d5c58563cc4af2898429bbe1a9a60007b5
changes/90/38690/1
Victor Seva 6 years ago
parent 52854d099a
commit 261d8b8715

@ -1,23 +1,23 @@
# Copyright (C) 2016 The Sipwise Team - http://sipwise.com # Copyright (C) 2016 The Sipwise Team - http://sipwise.com
#
# This program is free software: you can redistribute it and/or modify it # 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 # 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) # Software Foundation, either version 3 of the License, or (at your option)
# any later version. # any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT # This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. # more details.
#
# 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/>.
from __future__ import absolute_import
import logging import logging
from celery import shared_task from celery import shared_task
from .utils import process_hotfix
from repoapi.models import JenkinsBuildInfo from repoapi.models import JenkinsBuildInfo
from .utils import parse_changelog, create_note
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,7 +25,4 @@ logger = logging.getLogger(__name__)
@shared_task(ignore_result=True) @shared_task(ignore_result=True)
def hotfix_released(jbi_id, path): def hotfix_released(jbi_id, path):
jbi = JenkinsBuildInfo.objects.get(pk=jbi_id) jbi = JenkinsBuildInfo.objects.get(pk=jbi_id)
logger.info('hotfix_released[%s] %s', jbi, path) process_hotfix(str(jbi), jbi.projectname, path)
wids, changelog = parse_changelog(path)
for wid in wids:
create_note(wid, jbi.projectname, changelog.full_version)

@ -1,21 +1,21 @@
# Copyright (C) 2015 The Sipwise Team - http://sipwise.com # Copyright (C) 2015 The Sipwise Team - http://sipwise.com
# This program is free software: you can redistribute it and/or modify it # 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 # 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) # Software Foundation, either version 3 of the License, or (at your option)
# any later version. # any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT # This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. # more details.
# 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/>.
from django.test import override_settings from django.test import override_settings
from mock import patch, call, mock_open from mock import call
from hotfix import tasks, utils, models from mock import mock_open
from mock import patch
from hotfix import models
from hotfix import utils
from repoapi.models import JenkinsBuildInfo from repoapi.models import JenkinsBuildInfo
from repoapi.test.base import BaseTest from repoapi.test.base import BaseTest
@ -36,25 +36,24 @@ debian_changelog = """ngcp-fake (3.8.7.4+0~mr3.8.7.4) unstable; urgency=medium
@override_settings(CELERY_EAGER_PROPAGATES_EXCEPTIONS=True) @override_settings(CELERY_EAGER_PROPAGATES_EXCEPTIONS=True)
class TestHotfixReleased(BaseTest): class TestHotfixReleased(BaseTest):
def get_defaults(self): def get_defaults(self):
defaults = { defaults = {
'tag': "edc90cd9-37f3-4613-9748-ed05a32031c2", "tag": "edc90cd9-37f3-4613-9748-ed05a32031c2",
'projectname': "fake", "projectname": "fake",
'jobname': "release-tools-runner", "jobname": "release-tools-runner",
'buildnumber': 1, "buildnumber": 1,
'result': "SUCCESS", "result": "SUCCESS",
'job_url': "https://jenkins.mgm.sipwise.com/job/real-fake-gerrit/", "job_url": "https://jenkins.mgm.sipwise.com/job/real-fake-gerrit/",
'param_tag': "none", "param_tag": "none",
'param_branch': "mr4.5", "param_branch": "mr4.5",
'param_release': "none", "param_release": "none",
'param_distribution': "wheezy", "param_distribution": "wheezy",
'param_ppa': "gerrit_MT10339_review2054", "param_ppa": "gerrit_MT10339_review2054",
'git_commit_msg': "7fg4567 TT#0001 whatever", "git_commit_msg": "7fg4567 TT#0001 whatever",
} }
return defaults return defaults
@patch('builtins.open', mock_open(read_data=debian_changelog)) @patch("builtins.open", mock_open(read_data=debian_changelog))
def test_parse_changelog(self): def test_parse_changelog(self):
ids, changelog = utils.parse_changelog("/tmp/fake.txt") ids, changelog = utils.parse_changelog("/tmp/fake.txt")
self.assertCountEqual(ids, ["345", "123"]) self.assertCountEqual(ids, ["345", "123"])
@ -69,35 +68,36 @@ class TestHotfixReleased(BaseTest):
val = utils.get_target_release("3.8.7.4-1") val = utils.get_target_release("3.8.7.4-1")
self.assertIsNone(val) self.assertIsNone(val)
@patch('builtins.open', mock_open(read_data=debian_changelog)) @patch("builtins.open", mock_open(read_data=debian_changelog))
@patch('repoapi.utils.dlfile') @patch("repoapi.utils.dlfile")
@patch('repoapi.utils.workfront_set_release_target') @patch("repoapi.utils.workfront_set_release_target")
@patch('repoapi.utils.workfront_note_send') @patch("repoapi.utils.workfront_note_send")
def test_hotfixreleased(self, wns, wsrt, dlfile): def test_hotfixreleased(self, wns, wsrt, dlfile):
param = self.get_defaults() param = self.get_defaults()
jbi = JenkinsBuildInfo.objects.create(**param) jbi = JenkinsBuildInfo.objects.create(**param)
tasks.hotfix_released.delay(jbi.pk, "/tmp/fake.txt") utils.process_hotfix(str(jbi), jbi.projectname, "/tmp/fake.txt")
projectname = "fake" projectname = "fake"
version = "3.8.7.4+0~mr3.8.7.4" version = "3.8.7.4+0~mr3.8.7.4"
gri = models.WorkfrontNoteInfo.objects.filter( gri = models.WorkfrontNoteInfo.objects.filter(
projectname=projectname, projectname=projectname, version=version
version=version) )
self.assertEquals(gri.count(), 2) self.assertEquals(gri.count(), 2)
gri = models.WorkfrontNoteInfo.objects.filter( gri = models.WorkfrontNoteInfo.objects.filter(
workfront_id="345", workfront_id="345", projectname=projectname, version=version
projectname=projectname, )
version=version)
self.assertEquals(gri.count(), 1) self.assertEquals(gri.count(), 1)
msg = "hotfix %s.git %s triggered" % (projectname, version) msg = "hotfix %s.git %s triggered" % (projectname, version)
calls = [call("345", msg), ] calls = [
call("345", msg),
]
gri = models.WorkfrontNoteInfo.objects.filter( gri = models.WorkfrontNoteInfo.objects.filter(
workfront_id="123", workfront_id="123", projectname=projectname, version=version
projectname=projectname, )
version=version)
self.assertEquals(gri.count(), 1) self.assertEquals(gri.count(), 1)
msg = "hotfix %s.git %s triggered" % (projectname, version) msg = "hotfix %s.git %s triggered" % (projectname, version)
calls.append(call("123", msg)) calls.append(call("123", msg))
wns.assert_has_calls(calls, any_order=True) wns.assert_has_calls(calls, any_order=True)
wsrt.assert_has_calls( wsrt.assert_has_calls(
[call("345", "mr3.8.7.4"), call("123", "mr3.8.7.4")], [call("345", "mr3.8.7.4"), call("123", "mr3.8.7.4")],
any_order=True) any_order=True,
)

@ -1,32 +1,39 @@
# Copyright (C) 2016 The Sipwise Team - http://sipwise.com # Copyright (C) 2016 The Sipwise Team - http://sipwise.com
#
# This program is free software: you can redistribute it and/or modify it # 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 # 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) # Software Foundation, either version 3 of the License, or (at your option)
# any later version. # any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT # This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. # more details.
#
# 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/>.
from __future__ import absolute_import
import re
import logging import logging
import re
from .models import WorkfrontNoteInfo
from debian.changelog import Changelog from debian.changelog import Changelog
from repoapi import utils from repoapi import utils
from .models import WorkfrontNoteInfo
hotfix_re_release = re.compile('.+~(mr[0-9]+\.[0-9]+\.[0-9]+.[0-9]+)$') hotfix_re_release = re.compile(r".+~(mr[0-9]+\.[0-9]+\.[0-9]+.[0-9]+)$")
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def process_hotfix(jbi_info, projectname, path):
logger.info("hotfix_released[%s] %s", jbi_info, path)
wids, changelog = parse_changelog(path)
for wid in wids:
create_note(wid, projectname, changelog.full_version)
def parse_changelog(path): def parse_changelog(path):
changelog = Changelog() changelog = Changelog()
with open(path, 'r') as file_changelog: with open(path, "r") as file_changelog:
changelog.parse_changelog(file_changelog.read()) changelog.parse_changelog(file_changelog.read())
set_ids = set() set_ids = set()
for block in changelog: for block in changelog:
@ -45,9 +52,8 @@ def create_note(wid, projectname, version):
wni = WorkfrontNoteInfo.objects wni = WorkfrontNoteInfo.objects
note, created = wni.get_or_create( note, created = wni.get_or_create(
workfront_id=wid, workfront_id=wid, projectname=projectname, version=version
projectname=projectname, )
version=version)
if created: if created:
msg = "hotfix %s.git %s triggered" % (note.projectname, note.version) msg = "hotfix %s.git %s triggered" % (note.projectname, note.version)
utils.workfront_note_send(wid, msg) utils.workfront_note_send(wid, msg)

Loading…
Cancel
Save