Ejemplo n.º 1
0
class MAASDjangoTestRunner(NoseTestSuiteRunner):
    """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."""
        self.cluster = ClusterFixture("db", preserve=True)
        self.cluster.setUp()
        try:
            # Create a database in the PostgreSQL cluster for each database
            # connection configured in Django's settings that points to the
            # same datadir.
            for database in settings.DATABASES.values():
                if database["HOST"] == self.cluster.datadir:
                    self.cluster.createdb(database["NAME"])
            # Call-up to super-classes.
            up = super()
            return up.setup_databases(*args, **kwargs)
        except Exception:
            # Clean-up the cluster now, or it'll be left running; django-nose
            # does not know to clean it up itself, and lacks a fixture-like
            # mechanism to aid with reverting a half-configured environment.
            self.cluster.cleanUp()
            # Now we can let the original error wreak havoc.
            raise

    def teardown_databases(self, *args, **kwargs):
        """Tear-down the test database cluster.

        This is *not* called if there's a failure during bring-up of any of
        the test databases, hence there is also tear-down code embedded in
        `setup_databases`.
        """
        super().teardown_databases(*args, **kwargs)
        self.cluster.cleanUp()
Ejemplo n.º 2
0
class TestRunner(NoseTestSuiteRunner):
    """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."""
        self.cluster = ClusterFixture("db", preserve=True)
        self.cluster.setUp()
        for database in settings.DATABASES.values():
            if database["HOST"] == self.cluster.datadir:
                self.cluster.createdb(database["NAME"])
        return super(TestRunner, self).setup_databases(*args, **kwargs)

    def teardown_databases(self, *args, **kwargs):
        super(TestRunner, self).teardown_databases(*args, **kwargs)
        self.cluster.cleanUp()
Ejemplo n.º 3
0
    def setUp(self):
        """Start the regiond service."""
        super(MAASRegionServiceFixture, self).setUp()
        # Force django DEBUG false.
        self.addCleanup(patch(settings, "DEBUG", False))

        # Create a database in the PostgreSQL cluster for each database
        # connection configured in Django"s settings that points to the same
        # datadir.
        cluster = ClusterFixture("db", preserve=True)
        self.useFixture(cluster)
        for database in settings.DATABASES.values():
            if database["HOST"] == cluster.datadir:
                cluster.createdb(database["NAME"])

        # Setup the database for testing. This is so the database is isolated
        # only for this testing.
        self.setup_databases()
        self.addCleanup(self.teardown_databases)

        # Fork the process to have regiond run in its own process.
        twistd_pid = os.fork()
        if twistd_pid == 0:
            # Redirect all output to /dev/null
            redirect_to_devnull()

            # Add command line options to start twistd.
            sys.argv[1:] = [
                "--nodaemon",
                "--pidfile",
                "",
                "maas-regiond",
            ]

            # Change the DEFAULT_PORT so it can run along side of the
            # development regiond.
            from maasserver import eventloop
            patch(eventloop, "DEFAULT_PORT", 5253)

            # Start twistd.
            try:
                twistd.run()
            except:
                traceback.print_exc()
                os._exit(2)
            finally:
                os._exit(0)
        else:
            # Add cleanup to stop the twistd service.
            self.addCleanup(self.stop_twistd, twistd_pid)

            # Check that the child process is still running after a few
            # seconds. This makes sure that everything started okay and it
            # is still running.
            time.sleep(2)
            try:
                os.kill(twistd_pid, 0)
            except OSError:
                # Not running.
                raise ServiceError(
                    "Failed to start regiond. Check that another test is "
                    "not running at the same time.")