~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to src/maastesting/celery.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""A fixture to make Celery run tasks in a synchronous fashion."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = [
 
14
    'CeleryFixture',
 
15
    ]
 
16
 
 
17
from celery import (
 
18
    current_app,
 
19
    signals,
 
20
    )
 
21
from fixtures import Fixture
 
22
from testtools.monkey import MonkeyPatcher
 
23
 
 
24
 
 
25
class CeleryFixture(Fixture):
 
26
    """This fixture will make Celery run tasks in a synchronous fashion.
 
27
 
 
28
    This fixture can be used directly::
 
29
 
 
30
    >>> class CeleryTest1(TestCase):
 
31
    >>>
 
32
    >>>     def setUp(self):
 
33
    >>>         super(CeleryTest1, self).setUp()
 
34
    >>>         self.useFixture(CeleryFixture())
 
35
 
 
36
    It can also be converted into a FixtureResource::
 
37
 
 
38
    >>> from testresources import FixtureResource
 
39
    >>>
 
40
    >>> class CeleryTest2(TestCase):
 
41
    >>>
 
42
    >>>     resources = (
 
43
    >>>         ("celery", FixtureResource(CeleryFixture())),
 
44
    >>>         )
 
45
    >>>
 
46
    """
 
47
 
 
48
    def setUp(self):
 
49
        super(CeleryFixture, self).setUp()
 
50
        self.configure()
 
51
        self.record_tasks()
 
52
 
 
53
    def configure(self):
 
54
        patcher = MonkeyPatcher()
 
55
        patcher.add_patch(current_app.conf, 'CELERY_ALWAYS_EAGER', True)
 
56
        patcher.add_patch(
 
57
            current_app.conf, 'CELERY_EAGER_PROPAGATES_EXCEPTIONS', True)
 
58
        self.addCleanup(patcher.restore)
 
59
        patcher.patch()
 
60
 
 
61
    def record_tasks(self):
 
62
        self.tasks = []
 
63
 
 
64
        def on_task_postrun(**kwargs):
 
65
            self.tasks.append(kwargs)
 
66
        signals.task_postrun.connect(on_task_postrun, weak=False)
 
67
        self.addCleanup(lambda: self.cleanup_tasks())
 
68
 
 
69
    def cleanup_tasks(self):
 
70
        self.tasks = []