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 11, 2018
1 parent a6f1786 commit e0fffec
Show file tree
Hide file tree
Showing 17 changed files with 430 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
19 changes: 19 additions & 0 deletions runbot_preseed_database/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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": [
"security/ir.model.access.csv",
"wizards/runbot_preseed_database_refresh.xml",
"views/runbot_branch.xml",
"views/runbot_repo.xml",
],
}
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
23 changes: 23 additions & 0 deletions runbot_preseed_database/models/runbot_branch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# © 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,
)
database_refresh_cronjob_id = fields.Many2one(
'ir.cron', string='Refresh cronjob', copy=False,
)

@api.constrains('preseed_database')
def _check_preseed_database(self):
self.env['runbot.repo']._check_preseed_database.__func__(self)
37 changes: 37 additions & 0 deletions runbot_preseed_database/models/runbot_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# © 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 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)
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)
36 changes: 36 additions & 0 deletions runbot_preseed_database/models/runbot_repo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# © 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',
copy=False,
)
preseed_database_cronjob_id = fields.Many2one(
'ir.cron', string='Refresh cronjob', copy=False,
)

@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()

@api.multi
def _generate_preseed_database(self):
"""Generate a new preseed database"""
# TODO: find a working build, run its cmd installing
# preseed_database_module_ids in some temporary database, replace
# the current one with the result
pass
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
25 changes: 25 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,25 @@
# Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
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',
'preseed_database': 'template1',
})
branch = self.env['runbot.branch'].create({
'name': 'branch/branch/branch',
'repo_id': repo.id,
'preseed_database': self.env.cr.dbname,
})
build = self.env['runbot.build'].create({
'name': 'build',
'branch_id': branch.id,
})
self.assertTrue(build.preseed_database)
build._local_pg_createdb('preseed_database_test')
with local_pgadmin_cursor() as cr:
cr.execute('drop database preseed_database_test')
12 changes: 12 additions & 0 deletions runbot_preseed_database/views/runbot_branch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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>
</field>
</record>
</odoo>
15 changes: 15 additions & 0 deletions runbot_preseed_database/views/runbot_repo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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_cronjob_id" />
<label for="" attrs="{'invisible': [('preseed_database_cronjob_id', '!=', False)]}" />
<button name="%(action_runbot_preseed_database)s" type="action" string="Refresh automatically" class="oe_link" attrs="{'invisible': [('preseed_database_cronjob_id', '!=', 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 e0fffec

Please sign in to comment.