Ejemplo n.º 1
0
class TestCleanShutdown(unittest.TestCase):
    def setUp(self):
        self.botmaster = BotMaster(mock.Mock())
        self.reactor = mock.Mock()
        self.botmaster.startService()

    def assertReactorStopped(self, _=None):
        self.assertTrue(self.reactor.stop.called)

    def assertReactorNotStopped(self, _=None):
        self.assertFalse(self.reactor.stop.called)

    def makeFakeBuild(self):
        self.fake_builder = builder = mock.Mock()
        build = mock.Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        self.build_deferred = defer.Deferred()
        build.waitUntilFinished.return_value = self.build_deferred

        self.botmaster.builders = mock.Mock()
        self.botmaster.builders.values.return_value = [builder]

    def finishFakeBuild(self):
        self.fake_builder.builder_status.getCurrentBuilds.return_value = []
        self.build_deferred.callback(None)

    # tests

    def test_shutdown_idle(self):
        """Test that the master shuts down when it's idle"""
        self.botmaster.cleanShutdown(_reactor=self.reactor)
        self.assertReactorStopped()

    def test_shutdown_busy(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # check that we haven't stopped yet, since there's a running build
        self.assertReactorNotStopped()

        # try to shut it down again, just to check that this does not fail
        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # Now we cause the build to finish
        self.finishFakeBuild()

        # And now we should be stopped
        self.assertReactorStopped()

    def test_shutdown_cancel_not_shutting_down(self):
        """Test that calling cancelCleanShutdown when none is in progress
        works"""
        # this just shouldn't fail..
        self.botmaster.cancelCleanShutdown()

    def test_shutdown_cancel(self):
        """Test that we can cancel a shutdown"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        self.assertReactorNotStopped()

        # but the BuildRequestDistributor should not be running
        self.assertFalse(self.botmaster.brd.running)

        # Cancel the shutdown
        self.botmaster.cancelCleanShutdown()

        # Now we cause the build to finish
        self.finishFakeBuild()

        # We should still be running!
        self.assertReactorNotStopped()

        # and the BuildRequestDistributor should be, as well
        self.assertTrue(self.botmaster.brd.running)
Ejemplo n.º 2
0
class TestCleanShutdown(TestReactorMixin, unittest.TestCase):
    @defer.inlineCallbacks
    def setUp(self):
        self.setUpTestReactor()
        self.master = fakemaster.make_master(self, wantData=True)
        self.botmaster = BotMaster()
        yield self.botmaster.setServiceParent(self.master)
        self.botmaster.startService()

    def assertReactorStopped(self, _=None):
        self.assertTrue(self.reactor.stop_called)

    def assertReactorNotStopped(self, _=None):
        self.assertFalse(self.reactor.stop_called)

    def makeFakeBuild(self, waitedFor=False):
        self.fake_builder = builder = mock.Mock()
        build_status = mock.Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build_status]
        self.build_deferred = defer.Deferred()

        request = mock.Mock()
        request.waitedFor = waitedFor
        build = mock.Mock()
        build.stopBuild = self.stopFakeBuild
        build.waitUntilFinished.return_value = self.build_deferred
        build.requests = [request]
        builder.building = [build]

        self.botmaster.builders = mock.Mock()
        self.botmaster.builders.values.return_value = [builder]

    def stopFakeBuild(self, reason, results):
        self.reason = reason
        self.results = results
        self.finishFakeBuild()

    def finishFakeBuild(self):
        self.fake_builder.building = []
        self.build_deferred.callback(None)

    # tests

    def test_shutdown_idle(self):
        """Test that the master shuts down when it's idle"""
        self.botmaster.cleanShutdown()
        self.assertReactorStopped()

    def test_shutdown_busy(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown()

        # check that we haven't stopped yet, since there's a running build
        self.assertReactorNotStopped()

        # try to shut it down again, just to check that this does not fail
        self.botmaster.cleanShutdown()

        # Now we cause the build to finish
        self.finishFakeBuild()

        # And now we should be stopped
        self.assertReactorStopped()

    def test_shutdown_busy_quick(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(quickMode=True)

        # And now we should be stopped
        self.assertReactorStopped()
        self.assertEqual(self.results, RETRY)

    def test_shutdown_busy_quick_cancelled(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild(waitedFor=True)

        self.botmaster.cleanShutdown(quickMode=True)

        # And now we should be stopped
        self.assertReactorStopped()
        self.assertEqual(self.results, CANCELLED)

    def test_shutdown_cancel_not_shutting_down(self):
        """Test that calling cancelCleanShutdown when none is in progress
        works"""
        # this just shouldn't fail..
        self.botmaster.cancelCleanShutdown()

    def test_shutdown_cancel(self):
        """Test that we can cancel a shutdown"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown()

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        self.assertReactorNotStopped()

        # but the BuildRequestDistributor should not be running
        self.assertFalse(self.botmaster.brd.running)

        # Cancel the shutdown
        self.botmaster.cancelCleanShutdown()

        # Now we cause the build to finish
        self.finishFakeBuild()

        # We should still be running!
        self.assertReactorNotStopped()

        # and the BuildRequestDistributor should be, as well
        self.assertTrue(self.botmaster.brd.running)
class TestCleanShutdown(unittest.TestCase):

    def setUp(self):
        self.botmaster = BotMaster(mock.Mock())
        self.reactor = mock.Mock()
        self.botmaster.startService()

    def assertReactorStopped(self, _=None):
        self.assertTrue(self.reactor.stop.called)

    def assertReactorNotStopped(self, _=None):
        self.assertFalse(self.reactor.stop.called)

    def makeFakeBuild(self):
        self.fake_builder = builder = mock.Mock()
        build = mock.Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        self.build_deferred = defer.Deferred()
        build.waitUntilFinished.return_value = self.build_deferred

        self.botmaster.builders = mock.Mock()
        self.botmaster.builders.values.return_value = [builder]

    def finishFakeBuild(self):
        self.fake_builder.builder_status.getCurrentBuilds.return_value = []
        self.build_deferred.callback(None)

    # tests

    def test_shutdown_idle(self):
        """Test that the master shuts down when it's idle"""
        self.botmaster.cleanShutdown(_reactor=self.reactor)
        self.assertReactorStopped()

    def test_shutdown_busy(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # check that we haven't stopped yet, since there's a running build
        self.assertReactorNotStopped()

        # try to shut it down again, just to check that this does not fail
        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # Now we cause the build to finish
        self.finishFakeBuild()

        # And now we should be stopped
        self.assertReactorStopped()

    def test_shutdown_cancel_not_shutting_down(self):
        """Test that calling cancelCleanShutdown when none is in progress
        works"""
        # this just shouldn't fail..
        self.botmaster.cancelCleanShutdown()

    def test_shutdown_cancel(self):
        """Test that we can cancel a shutdown"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        self.assertReactorNotStopped()

        # but the BuildRequestDistributor should not be running
        self.assertFalse(self.botmaster.brd.running)

        # Cancel the shutdown
        self.botmaster.cancelCleanShutdown()

        # Now we cause the build to finish
        self.finishFakeBuild()

        # We should still be running!
        self.assertReactorNotStopped()

        # and the BuildRequestDistributor should be, as well
        self.assertTrue(self.botmaster.brd.running)
class TestCleanShutdown(unittest.TestCase):
    def setUp(self):
        self.master = BotMaster(Mock())
        self.master.reactor = Mock()
        self.master.startService()

    def test_shutdown_idle(self):
        """Test that the master shuts down when it's idle"""
        d = self.master.cleanShutdown()
        def _check(ign):
            self.assertEquals(self.master.reactor.stop.called, True)

        d.addCallback(_check)
        return d

    def test_shutdown_busy(self):
        """Test that the master shuts down after builds finish"""
        # Fake some builds
        builder = Mock()
        build = Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        d_finished = defer.Deferred()
        build.waitUntilFinished.return_value = d_finished

        self.master.builders = Mock()
        self.master.builders.values.return_value = [builder]

        d_shutdown = self.master.cleanShutdown()

        # Trigger the loop to get things going
        self.master.loop.trigger()

        # First we wait for it to quiet down again
        d = self.master.loop.when_quiet()

        # Next we check that we haven't stopped yet, since there's a running
        # build
        def _check1(ign):
            self.assertEquals(self.master.reactor.stop.called, False)
        d.addCallback(_check1)

        # Now we cause the build to finish, then kick the loop again,
        # empty out the list of running builds, and wait for the shutdown
        # process to finish
        def _finish_build(ign):
            d_finished.callback(None)
            self.master.loop.trigger()
            self.master.builders.values.return_value = []
            return d_shutdown
        d.addCallback(_finish_build)

        # And now we should be done
        def _check2(ign):
            self.assertEquals(self.master.reactor.stop.called, True)
        d.addCallback(_check2)

        return d

    def test_shutdown_cancel(self):
        """Test that we can cancel a shutdown"""
        # Fake some builds
        builder = Mock()
        build = Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        d_finished = defer.Deferred()
        build.waitUntilFinished.return_value = d_finished

        self.master.builders = Mock()
        self.master.builders.values.return_value = [builder]

        d_shutdown = self.master.cleanShutdown()

        # Trigger the loop to get things going
        self.master.loop.trigger()

        # First we wait for it to quiet down again
        d = self.master.loop.when_quiet()

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        # We cancel the shutdown here too
        def _check1(ign):
            self.assertEquals(self.master.reactor.stop.called, False)
            self.master.cancelCleanShutdown()
        d.addCallback(_check1)

        # Now we cause the build to finish, then kick the loop again,
        # empty out the list of running builds, and wait for the shutdown
        # process to finish
        def _finish_build(ign):
            d_finished.callback(None)
            self.master.loop.trigger()
            self.master.builders.values.return_value = []
            return d_shutdown
        d.addCallback(_finish_build)

        # We should still be running!
        def _check2(ign):
            self.assertEquals(self.master.reactor.stop.called, False)
        d.addCallback(_check2)

        return d

    def test_shutdown_no_new_builds(self):
        """Test that no new builds get handed out when we're shutting down"""
        # Fake some builds
        builder = Mock()
        build = Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        d_finished = defer.Deferred()
        build.waitUntilFinished.return_value = d_finished

        self.master.builders = Mock()
        self.master.builders.values.return_value = [builder]

        self.assertEquals(self.master._get_processors(), [builder.run])

        d_shutdown = self.master.cleanShutdown()
        assert d_shutdown

        # Trigger the loop to get things going
        self.master.loop.trigger()

        # First we wait for it to quiet down again
        d = self.master.loop.when_quiet()

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        # Also check that we're not trying to hand out new builds!
        def _check1(ign):
            self.assertEquals(self.master.reactor.stop.called, False)
            self.assertEquals(self.master._get_processors(), [])
        d.addCallback(_check1)

        return d
class TestCleanShutdown(TestReactorMixin, unittest.TestCase):

    def setUp(self):
        self.setUpTestReactor()
        self.master = fakemaster.make_master(self, wantData=True)
        self.botmaster = BotMaster()
        self.botmaster.setServiceParent(self.master)
        self.botmaster.startService()

    def assertReactorStopped(self, _=None):
        self.assertTrue(self.reactor.stop_called)

    def assertReactorNotStopped(self, _=None):
        self.assertFalse(self.reactor.stop_called)

    def makeFakeBuild(self, waitedFor=False):
        self.fake_builder = builder = mock.Mock()
        build_status = mock.Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build_status]
        self.build_deferred = defer.Deferred()

        request = mock.Mock()
        request.waitedFor = waitedFor
        build = mock.Mock()
        build.stopBuild = self.stopFakeBuild
        build.waitUntilFinished.return_value = self.build_deferred
        build.requests = [request]
        builder.building = [build]

        self.botmaster.builders = mock.Mock()
        self.botmaster.builders.values.return_value = [builder]

    def stopFakeBuild(self, reason, results):
        self.reason = reason
        self.results = results
        self.finishFakeBuild()

    def finishFakeBuild(self):
        self.fake_builder.building = []
        self.build_deferred.callback(None)

    # tests

    def test_shutdown_idle(self):
        """Test that the master shuts down when it's idle"""
        self.botmaster.cleanShutdown(_reactor=self.reactor)
        self.assertReactorStopped()

    def test_shutdown_busy(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # check that we haven't stopped yet, since there's a running build
        self.assertReactorNotStopped()

        # try to shut it down again, just to check that this does not fail
        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # Now we cause the build to finish
        self.finishFakeBuild()

        # And now we should be stopped
        self.assertReactorStopped()

    def test_shutdown_busy_quick(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(quickMode=True, _reactor=self.reactor)

        # And now we should be stopped
        self.assertReactorStopped()
        self.assertEqual(self.results, RETRY)

    def test_shutdown_busy_quick_cancelled(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild(waitedFor=True)

        self.botmaster.cleanShutdown(quickMode=True, _reactor=self.reactor)

        # And now we should be stopped
        self.assertReactorStopped()
        self.assertEqual(self.results, CANCELLED)

    def test_shutdown_cancel_not_shutting_down(self):
        """Test that calling cancelCleanShutdown when none is in progress
        works"""
        # this just shouldn't fail..
        self.botmaster.cancelCleanShutdown()

    def test_shutdown_cancel(self):
        """Test that we can cancel a shutdown"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        self.assertReactorNotStopped()

        # but the BuildRequestDistributor should not be running
        self.assertFalse(self.botmaster.brd.running)

        # Cancel the shutdown
        self.botmaster.cancelCleanShutdown()

        # Now we cause the build to finish
        self.finishFakeBuild()

        # We should still be running!
        self.assertReactorNotStopped()

        # and the BuildRequestDistributor should be, as well
        self.assertTrue(self.botmaster.brd.running)
Ejemplo n.º 6
0
class TestCleanShutdown(unittest.TestCase):
    def setUp(self):
        self.master = BotMaster(Mock())
        self.master.reactor = Mock()
        self.master.startService()

    def test_shutdown_idle(self):
        """Test that the master shuts down when it's idle"""
        d = self.master.cleanShutdown()

        def _check(ign):
            self.assertEquals(self.master.reactor.stop.called, True)

        d.addCallback(_check)
        return d

    def test_shutdown_busy(self):
        """Test that the master shuts down after builds finish"""
        # Fake some builds
        builder = Mock()
        build = Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        d_finished = defer.Deferred()
        build.waitUntilFinished.return_value = d_finished

        self.master.builders = Mock()
        self.master.builders.values.return_value = [builder]

        d_shutdown = self.master.cleanShutdown()

        # Trigger the loop to get things going
        self.master.loop.trigger()

        # First we wait for it to quiet down again
        d = self.master.loop.when_quiet()

        # Next we check that we haven't stopped yet, since there's a running
        # build
        def _check1(ign):
            self.assertEquals(self.master.reactor.stop.called, False)

        d.addCallback(_check1)

        # Now we cause the build to finish, then kick the loop again,
        # empty out the list of running builds, and wait for the shutdown
        # process to finish
        def _finish_build(ign):
            d_finished.callback(None)
            self.master.loop.trigger()
            self.master.builders.values.return_value = []
            return d_shutdown

        d.addCallback(_finish_build)

        # And now we should be done
        def _check2(ign):
            self.assertEquals(self.master.reactor.stop.called, True)

        d.addCallback(_check2)

        return d

    def test_shutdown_cancel(self):
        """Test that we can cancel a shutdown"""
        # Fake some builds
        builder = Mock()
        build = Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        d_finished = defer.Deferred()
        build.waitUntilFinished.return_value = d_finished

        self.master.builders = Mock()
        self.master.builders.values.return_value = [builder]

        d_shutdown = self.master.cleanShutdown()

        # Trigger the loop to get things going
        self.master.loop.trigger()

        # First we wait for it to quiet down again
        d = self.master.loop.when_quiet()

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        # We cancel the shutdown here too
        def _check1(ign):
            self.assertEquals(self.master.reactor.stop.called, False)
            self.master.cancelCleanShutdown()

        d.addCallback(_check1)

        # Now we cause the build to finish, then kick the loop again,
        # empty out the list of running builds, and wait for the shutdown
        # process to finish
        def _finish_build(ign):
            d_finished.callback(None)
            self.master.loop.trigger()
            self.master.builders.values.return_value = []
            return d_shutdown

        d.addCallback(_finish_build)

        # We should still be running!
        def _check2(ign):
            self.assertEquals(self.master.reactor.stop.called, False)

        d.addCallback(_check2)

        return d

    def test_shutdown_no_new_builds(self):
        """Test that no new builds get handed out when we're shutting down"""
        # Fake some builds
        builder = Mock()
        build = Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        d_finished = defer.Deferred()
        build.waitUntilFinished.return_value = d_finished

        self.master.builders = Mock()
        self.master.builders.values.return_value = [builder]

        self.assertEquals(self.master._get_processors(), [builder.run])

        d_shutdown = self.master.cleanShutdown()
        assert d_shutdown

        # Trigger the loop to get things going
        self.master.loop.trigger()

        # First we wait for it to quiet down again
        d = self.master.loop.when_quiet()

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        # Also check that we're not trying to hand out new builds!
        def _check1(ign):
            self.assertEquals(self.master.reactor.stop.called, False)
            self.assertEquals(self.master._get_processors(), [])

        d.addCallback(_check1)

        return d