← Back to team overview

maas-devel team mailing list archive

[Merge] lp:~jtv/maas/db-fixture into lp:maas

 

Jeroen T. Vermeulen has proposed merging lp:~jtv/maas/db-fixture into lp:maas.

Requested reviews:
  MaaS Developers (maas-devel)

For more details, see:
https://code.launchpad.net/~jtv/maas/db-fixture/+merge/88898

Set up the testing database cluster as part of the test runner.

It took me a while to find a hook into the test runner that I could use.  The one I found was: use a custom test runner based on the django one, but with a call to maasdb added to it.  I couldn't define the custom runner in development.py or maas/__init__.py etc. because it'd just lead to circular imports and a mysterious failure.

To try this out, create a new branch (so there's certainly no database cluster running in it), "make," then check "ps -ef | grep postgres" to verify that there still is no postgres running in the branch, then "./bin/test" to see that the tests can access the database.  I added some only-slightly-more-meaningful tests to make sure of that.

You may notice that database setup before a test takes a while.  That doesn't go away when I remove the custom test runner though; I guess it's something we just can't get rid of.

Something a bit nasty I did was to hard-code "./db/" as the database cluster location.  Ideally I'd have some way of specifying "main branch directory/db."

The maasdb script does nothing when asked to start a cluster that's already running.  Doesn't take very long to find out, either.


Jeroen
-- 
https://code.launchpad.net/~jtv/maas/db-fixture/+merge/88898
Your team MaaS Developers is requested to review the proposed merge of lp:~jtv/maas/db-fixture into lp:maas.
=== modified file 'Makefile'
--- Makefile	2012-01-17 15:04:53 +0000
+++ Makefile	2012-01-17 16:55:28 +0000
@@ -11,7 +11,7 @@
 dev-db:
 	bin/maasdb start ./db/ disposable
 
-test: bin/test dev-db
+test: bin/test
 	bin/test
 
 lint:

=== modified file 'src/maas/development.py'
--- src/maas/development.py	2012-01-16 17:02:09 +0000
+++ src/maas/development.py	2012-01-17 16:55:28 +0000
@@ -5,6 +5,10 @@
 
 import os
 
+# Use our custom test runner, which makes sure that a local database
+# cluster is running in the branch.
+TEST_RUNNER='maas.testing.runner.CustomTestRunner'
+
 DEBUG = True
 TEMPLATE_DEBUG = DEBUG
 YUI_DEBUG = DEBUG

=== added directory 'src/maas/testing'
=== added file 'src/maas/testing/__init__.py'
=== added file 'src/maas/testing/runner.py'
--- src/maas/testing/runner.py	1970-01-01 00:00:00 +0000
+++ src/maas/testing/runner.py	2012-01-17 16:55:28 +0000
@@ -0,0 +1,14 @@
+from subprocess import Popen
+from django.test.simple import DjangoTestSuiteRunner
+
+
+class CustomTestRunner(DjangoTestSuiteRunner):
+    """Custom test runner; ensures that the test database cluster is up."""
+
+    def setup_databases(self, *args, **kwargs):
+        """Fire up the db cluster, then punt to original implementation."""
+        process = Popen(['bin/maasdb', 'start', './db/'])
+        retval = process.wait()
+        if retval != 0:
+            raise RuntimeError("Failed to start database cluster.")
+        return super(CustomTestRunner, self).setup_databases(*args, **kwargs)

=== modified file 'src/maasserver/tests/simple_tests.py'
--- src/maasserver/tests/simple_tests.py	2012-01-17 15:35:41 +0000
+++ src/maasserver/tests/simple_tests.py	2012-01-17 16:55:28 +0000
@@ -1,16 +1,16 @@
-"""
-This file demonstrates writing tests using the unittest module. These will pass
-when you run "manage.py test".
-
-Replace this with more appropriate tests for your application.
-"""
-
 from django.test import TestCase
 
+from maasserver.models import Node
+
 
 class SimpleTest(TestCase):
-    def test_basic_addition(self):
-        """
-        Tests that 1 + 1 always equals 2.
-        """
-        self.assertEqual(1 + 1, 2)
+
+    def test_can_create_nodes(self):
+        self.assertEqual([], list(Node.objects.all()))
+        n = Node(name='foo', status='NEW')
+        n.save()
+        self.assertEqual([n], list(Node.objects.all()))
+
+    def test_no_nodes_exist_initially(self):
+        self.assertEqual([], list(Node.objects.all()))
+


Follow ups