Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
[ADD] runbot_preseed_database
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Jun 18, 2018
1 parent a6f1786 commit d444d75
Show file tree
Hide file tree
Showing 18 changed files with 593 additions and 0 deletions.
66 changes: 66 additions & 0 deletions runbot_preseed_database/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: https://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

===============================
Preseed test database in runbot
===============================

This module allows you to have runbot use a database template that already has some modules installed. This allows for much faster builds in case you're only interested in a subset of the tests.

Configuration
=============

To configure this module, you need to:

#. go to your repository (all builds in the repo) or a branch (just builds for this branch)
#. fill in a database name to be used as template. This database must exist in the cluster and be accessible for the user running runbot

Usage
=====

You will have to regenerate the database template from time, and you should also take care to have one branch tested with a fresh database. This is meant to speedup builds for which most tests are irrelevant.

Known issues / Roadmap
======================

* it would be nice to be able to upload the database via the interface
* also helpful would be a wizard to just generate a database with a couple of user defined modules from some build

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/runbot-addons/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.

Credits
=======

Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Holger Brunn <[email protected]>

Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list <mailto:[email protected]>`_ or the `appropriate specialized mailinglist <https://odoo-community.org/groups>`_ for help, and the bug tracker linked in `Bug Tracker`_ above for technical issues.

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
4 changes: 4 additions & 0 deletions runbot_preseed_database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import models
from . import wizards
20 changes: 20 additions & 0 deletions runbot_preseed_database/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Preseed test database in runbot",
"version": "11.0.1.0.0",
"author": "Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Runbot",
"summary": "Allows to run tests with a partially initialized database",
"depends": [
'runbot',
],
"data": [
"data/ir_cron.xml",
"security/ir.model.access.csv",
"wizards/runbot_preseed_database_refresh.xml",
"views/runbot_branch.xml",
"views/runbot_repo.xml",
],
}
14 changes: 14 additions & 0 deletions runbot_preseed_database/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo noupdate="1">
<record id="server_action_cron" model="ir.actions.server">
<field name="name">Regenerate preseed databases</field>
<field name="state">code</field>
<field name="model_id" ref="runbot_preseed_database.model_runbot_preseed_database_refresh" />
<field name="code">model._refresh_preseed_database()</field>
</record>
<record id="cron" model="ir.cron">
<field name="ir_actions_server_id" ref="server_action_cron" />
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
</record>
</odoo>
6 changes: 6 additions & 0 deletions runbot_preseed_database/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import runbot_repo
from . import runbot_build
from . import runbot_branch
from . import runbot_preseed_database_module
24 changes: 24 additions & 0 deletions runbot_preseed_database/models/runbot_branch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# © 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models


class RunbotBranch(models.Model):
_inherit = 'runbot.branch'

preseed_database = fields.Char(
help='Fill in the name of a database to use as template for the all '
'build', copy=False,
)
preseed_database_module_ids = fields.Many2many(
'runbot.preseed.database.module', string='Modules to install',
copy=False,
)
preseed_database_last_refresh = fields.Date(
'Last update of preseed database',
)
preseed_database_refresh_pid = fields.Integer('Refresh process pid')

@api.constrains('preseed_database')
def _check_preseed_database(self):
self.env['runbot.repo']._check_preseed_database.__func__(self)
47 changes: 47 additions & 0 deletions runbot_preseed_database/models/runbot_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# © 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import inspect
from psycopg2.extensions import AsIs
from odoo import api, fields, models
from odoo.addons.runbot.common import local_pgadmin_cursor


class RunbotBuild(models.Model):
_inherit = 'runbot.build'

preseed_database = fields.Char(
compute=lambda self: [
this.update({
'preseed_database':
this.branch_id.preseed_database or
this.branch_id.repo_id.preseed_database
})
for this in self
],
)

def _local_pg_createdb(self, dbname):
if dbname.endswith('-base'):
# no need to do anything here
return super(RunbotBuild, self)._local_pg_createdb(dbname)
stack = inspect.stack()
# there should be a build in the callee, don't recurse through stack
build = stack[1][0].f_locals.get('build')
if isinstance(build, models.BaseModel) and build.preseed_database:
with local_pgadmin_cursor() as cr:
cr.execute(
'create database "%s" TEMPLATE "%s"',
(AsIs(dbname), AsIs(build.preseed_database)),
)
else:
super(RunbotBuild, self)._local_pg_createdb(dbname)

@api.multi
def _refresh_preseed_database_cmd(self, database, modules):
self.ensure_one()
cmd, dummy = self._cmd()
return cmd + [
'--test-enable', '--no-xmlrpc', '-d', database,
'-i', ','.join(modules.mapped('name')),
'--stop-after-init', '--log-level=test', '--max-cron-threads=0'
]
10 changes: 10 additions & 0 deletions runbot_preseed_database/models/runbot_preseed_database_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# © 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import fields, models


class RunbotPreseedDatabaseModule(models.Model):
_name = 'runbot.preseed.database.module'
_description = 'Preseed database module'

name = fields.Char(required=True)
31 changes: 31 additions & 0 deletions runbot_preseed_database/models/runbot_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# © 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import psycopg2
from odoo import api, fields, models


class RunbotRepo(models.Model):
_inherit = 'runbot.repo'

preseed_database = fields.Char(
help='Fill in the name of a database to use as template for the all '
'build', copy=False,
)
preseed_database_module_ids = fields.Many2many(
'runbot.preseed.database.module', string='Modules to install',
help='Fill in some modules with long running tests like stock or '
'accounting. Do not fill in modules here that should be tested.',
copy=False,
)
preseed_database_last_refresh = fields.Date(
'Last update of preseed database',
)
preseed_database_refresh_pid = fields.Integer('Refresh process pid')

@api.constrains('preseed_database')
def _check_preseed_database(self):
for this in self:
if not this.preseed_database:
continue
connection = psycopg2.connect('dbname=%s' % this.preseed_database)
connection.close()
3 changes: 3 additions & 0 deletions runbot_preseed_database/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_runbot_preseed_database_module,access_runbot_preseed_database_module,model_runbot_preseed_database_module,runbot.group_runbot_admin,1,1,1,1
access_runbot_preseed_database_module_user,access_runbot_preseed_database_module,model_runbot_preseed_database_module,runbot.group_user,1,0,0,0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions runbot_preseed_database/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import test_runbot_preseed_database
54 changes: 54 additions & 0 deletions runbot_preseed_database/tests/test_runbot_preseed_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from psycopg2.extensions import AsIs
from openerp.tests.common import TransactionCase
from odoo.addons.runbot.common import local_pgadmin_cursor


class TestRunbotPreseedDatabase(TransactionCase):
def test_runbot_preseed_database(self):
repo = self.env['runbot.repo'].create({
'name': 'repo',
})
branch = self.env['runbot.branch'].create({
'name': 'branch/branch/branch',
'repo_id': repo.id,
})
build = self.env['runbot.build'].create({
'name': 'build',
'branch_id': branch.id,
'result': 'ok',
})
# first test the function
cmd = build._refresh_preseed_database_cmd(
'database', self.env['runbot.preseed.database.module'],
)
self.assertIn('--no-xmlrpc', cmd)
# then patch it away order not to run a real server
self.env['runbot.build']._refresh_preseed_database_cmd = (
lambda self, dummy, modules: ['ls'] + modules.mapped('name')
)
wizard = self.env['runbot.preseed.database.refresh'].with_context(
active_model=repo._name,
active_id=repo.id,
).create({
'build_id': build.id,
'preseed_database_module_ids': [
(
4,
self.env['runbot.preseed.database.module'].create({
'name': 'base',
}).id,
),
],
})
self.assertTrue(wizard.preseed_database)
wizard.action_get_modules()
wizard.action_write_result()
self.assertTrue(build.preseed_database)
wizard._refresh_preseed_database()
self.assertTrue(repo.preseed_database_refresh_pid)
wizard._refresh_preseed_database()
self.assertFalse(repo.preseed_database_refresh_pid)
with local_pgadmin_cursor() as cr:
cr.execute('drop database "%s"', (AsIs(build.preseed_database),))
16 changes: 16 additions & 0 deletions runbot_preseed_database/views/runbot_branch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="branch_form" model="ir.ui.view">
<field name="model">runbot.branch</field>
<field name="inherit_id" ref="runbot.branch_form" />
<field name="arch" type="xml">
<field name="modules" position="after">
<field name="preseed_database" />
<field name="preseed_database_last_refresh" invisible="1" />
<field name="preseed_database_module_ids" attrs="{'invisible': [('preseed_database', '=', False)]}" widget="many2many_tags" />
<label for="" attrs="{'invisible': [('preseed_database_last_refresh', '=', False), ('preseed_database_module_ids', '=', False)]}" />
<button name="%(action_runbot_preseed_database)s" type="action" string="Refresh automatically" class="oe_link" attrs="{'invisible': [('preseed_database_last_refresh', '=', False), ('preseed_database_module_ids', '=', False)]}" />
</field>
</field>
</record>
</odoo>
16 changes: 16 additions & 0 deletions runbot_preseed_database/views/runbot_repo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="repo_form" model="ir.ui.view">
<field name="model">runbot.repo</field>
<field name="inherit_id" ref="runbot.repo_form" />
<field name="arch" type="xml">
<field name="modules_auto" position="after">
<field name="preseed_database" />
<field name="preseed_database_last_refresh" invisible="1" />
<field name="preseed_database_module_ids" attrs="{'invisible': [('preseed_database', '=', False)]}" />
<label for="" attrs="{'invisible': [('preseed_database_last_refresh', '=', False), ('preseed_database_module_ids', '=', False)]}" />
<button name="%(action_runbot_preseed_database)s" type="action" string="Refresh automatically" class="oe_link" attrs="{'invisible': [('preseed_database_last_refresh', '=', False), ('preseed_database_module_ids', '=', False)]}" />
</field>
</field>
</record>
</odoo>
3 changes: 3 additions & 0 deletions runbot_preseed_database/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import runbot_preseed_database_refresh
Loading

0 comments on commit d444d75

Please sign in to comment.