diff --git a/runbot_preseed_database/README.rst b/runbot_preseed_database/README.rst new file mode 100644 index 00000000..7dc85a29 --- /dev/null +++ b/runbot_preseed_database/README.rst @@ -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 +`_. 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 `_. + +Contributors +------------ + +* Holger Brunn + +Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list `_ or the `appropriate specialized mailinglist `_ 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. diff --git a/runbot_preseed_database/__init__.py b/runbot_preseed_database/__init__.py new file mode 100644 index 00000000..be857487 --- /dev/null +++ b/runbot_preseed_database/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2018 Therp BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/runbot_preseed_database/__manifest__.py b/runbot_preseed_database/__manifest__.py new file mode 100644 index 00000000..770638c5 --- /dev/null +++ b/runbot_preseed_database/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2018 Therp BV +# 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": [ + "views/runbot_branch.xml", + "views/runbot_repo.xml", + ], +} diff --git a/runbot_preseed_database/models/__init__.py b/runbot_preseed_database/models/__init__.py new file mode 100644 index 00000000..784d7c2f --- /dev/null +++ b/runbot_preseed_database/models/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2018 Therp BV +# 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 diff --git a/runbot_preseed_database/models/runbot_branch.py b/runbot_preseed_database/models/runbot_branch.py new file mode 100644 index 00000000..d45fc0cd --- /dev/null +++ b/runbot_preseed_database/models/runbot_branch.py @@ -0,0 +1,16 @@ +# © 2018 Therp BV +# 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', + ) + + @api.constrains('preseed_database') + def _check_preseed_database(self): + self.env['runbot.repo']._check_preseed_database.__func__(self) diff --git a/runbot_preseed_database/models/runbot_build.py b/runbot_preseed_database/models/runbot_build.py new file mode 100644 index 00000000..f2fbc891 --- /dev/null +++ b/runbot_preseed_database/models/runbot_build.py @@ -0,0 +1,37 @@ +# © 2018 Therp BV +# 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) diff --git a/runbot_preseed_database/models/runbot_repo.py b/runbot_preseed_database/models/runbot_repo.py new file mode 100644 index 00000000..21b55351 --- /dev/null +++ b/runbot_preseed_database/models/runbot_repo.py @@ -0,0 +1,21 @@ +# © 2018 Therp BV +# 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', + ) + + @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() diff --git a/runbot_preseed_database/static/description/icon.png b/runbot_preseed_database/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/runbot_preseed_database/static/description/icon.png differ diff --git a/runbot_preseed_database/tests/__init__.py b/runbot_preseed_database/tests/__init__.py new file mode 100644 index 00000000..0c771133 --- /dev/null +++ b/runbot_preseed_database/tests/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2018 Therp BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import test_runbot_preseed_database diff --git a/runbot_preseed_database/tests/test_runbot_preseed_database.py b/runbot_preseed_database/tests/test_runbot_preseed_database.py new file mode 100644 index 00000000..debc2e65 --- /dev/null +++ b/runbot_preseed_database/tests/test_runbot_preseed_database.py @@ -0,0 +1,25 @@ +# Copyright 2018 Therp BV +# 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') diff --git a/runbot_preseed_database/views/runbot_branch.xml b/runbot_preseed_database/views/runbot_branch.xml new file mode 100644 index 00000000..467e05d9 --- /dev/null +++ b/runbot_preseed_database/views/runbot_branch.xml @@ -0,0 +1,12 @@ + + + + runbot.branch + + + + + + + + diff --git a/runbot_preseed_database/views/runbot_repo.xml b/runbot_preseed_database/views/runbot_repo.xml new file mode 100644 index 00000000..0fed5b6b --- /dev/null +++ b/runbot_preseed_database/views/runbot_repo.xml @@ -0,0 +1,12 @@ + + + + runbot.repo + + + + + + + +