runbot/runbot/tests/test_cron.py
Christophe Monniez 5dd889de3c [IMP] runbot: split branch creation and pending builds creation
When searching for new builds by parsing git refs, the new branches are
created as well as the pending builds in the same _find_new_commits
method.

With this commit, this behavior is splitted into two methods, that way,
it's now possible to create missing branches without creating new
builds. The closest_branch detection is enhanced because all the new
branches are created before the builds (separated loops).

The find_new_commits method uses an optimized way to search for
existsing builds. Before this commit, a build search was performed for
each git reference, potentially a huge number.

With this commit, a raw sql query is performed to create a set of tuples
(branch_id, sha) which is enough to decide if a build already exists.

A test was added to verify that new refs leads to pending builds.

Also, a performance test was added but is skipped by default because it
needs an existing repo with 20000 branches in the database so it will
not work with an empty database. This test showed a gain of performance
from 8sec to 2sec when creating builds from new commits.

co-authored by @Xavier-Do
2019-05-03 10:34:49 +02:00

61 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
from unittest.mock import patch
from odoo.tests import common
class Test_Cron(common.TransactionCase):
def setUp(self):
super(Test_Cron, self).setUp()
self.Repo = self.env['runbot.repo']
@patch('odoo.addons.runbot.models.repo.config.get')
def test_cron_period(self, mock_config_get):
""" Test that the random cron period stays below margin
Assuming a configuration of 10 minutes cron limit
"""
mock_config_get.return_value = 600
period = self.Repo._get_cron_period(min_margin=200)
for i in range(200):
self.assertLess(period, 400)
@patch('odoo.addons.runbot.models.repo.fqdn')
def test_crons_returns(self, mock_fqdn):
""" test that cron_fetch_and_schedule and _cron_fetch_and_build
return directly when called on wrong host
"""
mock_fqdn.return_value = 'runboty.foo.com'
ret = self.Repo._cron_fetch_and_schedule('runbotx.foo.com')
self.assertEqual(ret, 'Not for me')
ret = self.Repo._cron_fetch_and_build('runbotx.foo.com')
self.assertEqual(ret, 'Not for me')
@patch('odoo.addons.runbot.models.repo.runbot_repo._get_cron_period')
@patch('odoo.addons.runbot.models.repo.runbot_repo._create_pending_builds')
@patch('odoo.addons.runbot.models.repo.runbot_repo._update')
@patch('odoo.addons.runbot.models.repo.fqdn')
def test_cron_schedule(self, mock_fqdn, mock_update, mock_create, mock_cron_period):
""" test that cron_fetch_and_schedule do its work """
mock_fqdn.return_value = 'runbotx.foo.com'
mock_cron_period.return_value = 2
self.env['ir.config_parameter'].sudo().set_param('runbot.runbot_update_frequency', 1)
ret = self.Repo._cron_fetch_and_schedule('runbotx.foo.com')
self.assertEqual(None, ret)
mock_update.assert_called_with(force=False)
mock_create.assert_called_with()
@patch('odoo.addons.runbot.models.repo.runbot_repo._get_cron_period')
@patch('odoo.addons.runbot.models.repo.runbot_repo._reload_nginx')
@patch('odoo.addons.runbot.models.repo.runbot_repo._scheduler')
@patch('odoo.addons.runbot.models.repo.fqdn')
def test_cron_build(self, mock_fqdn, mock_scheduler, mock_reload, mock_cron_period):
""" test that cron_fetch_and_build do its work """
mock_fqdn.return_value = 'runbotx.foo.com'
mock_cron_period.return_value = 2
self.env['ir.config_parameter'].sudo().set_param('runbot.runbot_update_frequency', 1)
ret = self.Repo._cron_fetch_and_build('runbotx.foo.com')
self.assertEqual(None, ret)
mock_scheduler.assert_called()
self.assertTrue(mock_reload.called)