← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/power-job2 into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/power-job2 into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~rvb/maas/power-job2/+merge/107028

This branch adds recording capabilities to CeleryFixture.  This way, it will be easy to test what tasks have been executed.

Drive-by fix: cleanup test_tasks.py.
-- 
https://code.launchpad.net/~rvb/maas/power-job2/+merge/107028
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/power-job2 into lp:maas.
=== modified file 'src/maastesting/celery.py'
--- src/maastesting/celery.py	2012-05-21 15:51:33 +0000
+++ src/maastesting/celery.py	2012-05-23 14:52:20 +0000
@@ -14,7 +14,10 @@
     'CeleryFixture',
     ]
 
-from celery import current_app
+from celery import (
+    current_app,
+    signals,
+    )
 from fixtures import Fixture
 from testtools.monkey import MonkeyPatcher
 
@@ -44,9 +47,24 @@
 
     def setUp(self):
         super(CeleryFixture, self).setUp()
+        self.configure()
+        self.record_tasks()
+
+    def configure(self):
         patcher = MonkeyPatcher()
         patcher.add_patch(current_app.conf, 'CELERY_ALWAYS_EAGER', True)
         patcher.add_patch(
             current_app.conf, 'CELERY_EAGER_PROPAGATES_EXCEPTIONS', True)
         self.addCleanup(patcher.restore)
         patcher.patch()
+
+    def record_tasks(self):
+        self.tasks = []
+
+        def on_task_postrun(**kwargs):
+            self.tasks.append(kwargs)
+        signals.task_postrun.connect(on_task_postrun, weak=False)
+        self.addCleanup(lambda: self.cleanup_tasks())
+
+    def cleanup_tasks(self):
+        self.tasks = []

=== modified file 'src/maastesting/tests/test_celery.py'
--- src/maastesting/tests/test_celery.py	2012-05-21 15:51:33 +0000
+++ src/maastesting/tests/test_celery.py	2012-05-23 14:52:20 +0000
@@ -19,7 +19,6 @@
 from celery.result import EagerResult
 from maastesting.celery import CeleryFixture
 from maastesting.testcase import TestCase
-from testresources import FixtureResource
 
 
 @task()
@@ -35,16 +34,16 @@
 class TestCeleryFixture(TestCase):
     """Tests `CeleryFixture`."""
 
-    resources = (
-        ("celery", FixtureResource(CeleryFixture())),
-        )
+    def setUp(self):
+        super(TestCeleryFixture, self).setUp()
+        self.celery = self.useFixture(CeleryFixture())
 
     def test_celery_config(self):
         self.assertTrue(current_app.conf.CELERY_ALWAYS_EAGER)
         self.assertTrue(current_app.conf.CELERY_EAGER_PROPAGATES_EXCEPTIONS)
 
     def test_celery_eagerresult_contains_result(self):
-       # The result is an instance of EagerResult and it contains the actual
+        # The result is an instance of EagerResult and it contains the actual
         # result.
         x = random.randrange(100)
         y = random.randrange(100)
@@ -54,3 +53,18 @@
 
     def test_celery_exception_raised(self):
         self.assertRaises(RuntimeError, task_exception.delay, 1, 2)
+
+    def test_celery_records_tasks(self):
+        x = random.randrange(100)
+        y = random.randrange(100)
+        task_add.delay(x=x, y=y)
+        z = random.randrange(100)
+        t = random.randrange(100)
+        task_add.delay(x=z, y=t)
+        tasks = self.celery.tasks
+        self.assertEqual(2, len(tasks))
+        self.assertEqual(
+            ['maastesting.tests.test_celery.task_add'] * 2,
+            [task['task'].name for task in tasks])
+        self.assertEqual({'x': x, 'y': y}, tasks[0]['kwargs'])
+        self.assertEqual({'x': z, 'y': t}, tasks[1]['kwargs'])

=== modified file 'src/provisioningserver/tasks.py'
--- src/provisioningserver/tasks.py	2012-05-23 08:57:08 +0000
+++ src/provisioningserver/tasks.py	2012-05-23 14:52:20 +0000
@@ -10,11 +10,12 @@
     )
 
 __metaclass__ = type
-__all__ = []
+__all__ = [
+    'power_on'
+    ]
 
 
 from celery.decorators import task
-
 from provisioningserver.power.poweraction import (
     PowerAction,
     PowerActionFail,

=== modified file 'src/provisioningserver/tests/test_tasks.py'
--- src/provisioningserver/tests/test_tasks.py	2012-05-23 08:57:08 +0000
+++ src/provisioningserver/tests/test_tasks.py	2012-05-23 14:52:20 +0000
@@ -12,22 +12,15 @@
 __metaclass__ = type
 __all__ = []
 
-from testresources import FixtureResource
-
 from maastesting.celery import CeleryFixture
 from maastesting.testcase import TestCase
 from provisioningserver.enum import POWER_TYPE
 from provisioningserver.power.poweraction import PowerActionFail
 from provisioningserver.tasks import power_on
-
-
-class TaskTestCase(TestCase):
-
-    def assertSuccess(self, task_result):
-        self.assertEqual("SUCCESS", task_result.status)
-
-
-class TestPowerTasks(TaskTestCase):
+from testresources import FixtureResource
+
+
+class TestPowerTasks(TestCase):
 
     resources = (
         ("celery", FixtureResource(CeleryFixture())),
@@ -43,4 +36,4 @@
     def test_ether_wake_power_on(self):
         mac = "AA:BB:CC:DD:EE:FF"
         result = power_on.delay(POWER_TYPE.WAKE_ON_LAN, mac=mac)
-        self.assertSuccess(result)
+        self.assertTrue(result.successful())