Ejemplo n.º 1
0
    def test_get_schedulers(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        common.services.register(Host.Iface, MagicMock())
        scheduler_handler = SchedulerHandler()
        common.services.register(Scheduler.Iface, scheduler_handler)
        agent_config = MagicMock()
        agent_config.reboot_required = False
        common.services.register(ServiceName.AGENT_CONFIG, agent_config)
        common.services.register(ServiceName.REQUEST_ID, MagicMock())

        agent_control_handler = AgentControlHandler()
        request = GetSchedulersRequest()
        response = agent_control_handler.get_schedulers(request)

        child_host = ChildInfo(id="foo", address="address", port=12345)

        assert_that(response.schedulers, is_(empty()))
        leaf_scheduler_id = "leaf1"
        scheduler_handler.configure(
            [SchedulerRole(leaf_scheduler_id, "parent1",
                           host_children=[child_host])])
        leaf_scheduler._get_hosts.return_value = [child_host]

        response = agent_control_handler.get_schedulers(request)
        assert_that(response.schedulers, has_length(1))

        scheduler = response.schedulers[0]
        assert_that(scheduler.role.id, is_(leaf_scheduler_id))
        assert_that(scheduler.role.host_children[0], is_(child_host))
        assert_that(len(scheduler.role.host_children), is_(1))
        assert_that(response.result, is_(GetSchedulersResultCode.OK))
    def test_place_during_reconfigure(self):
        # Create leaf scheduler which will return RESOURCE_CONSTRAINT
        scheduler = LeafScheduler("foo", 1, False)
        scheduler._placement_hosts = MagicMock()
        scheduler._placement_hosts.return_value = []

        # Create scheduler handler
        handler = SchedulerHandler()
        handler._create_scheduler = MagicMock()
        handler._create_scheduler.return_value = scheduler

        # Configure scheduler handler with leaf scheduler
        handler.configure([
            SchedulerRole("foo",
                          "parent-id",
                          scheduler_children=[ChildInfo(id="child")])
        ])
        assert_that(handler._schedulers, has_length(1))

        concurrency = 5
        threads = []
        results = {}
        done = [False]

        # Define the thread which keeps calling place, to make sure it
        # always return RESOURCE_CONSTRAINT
        def _loop():
            while True:
                actual_response = handler.place(PlaceRequest(
                    Resource(), "foo"))
                assert_that(actual_response.result,
                            is_not(PlaceResultCode.SYSTEM_ERROR))

                if done[0]:
                    results[threading.current_thread().name] = True
                    break

        for _ in xrange(concurrency):
            thread = threading.Thread(target=_loop)
            thread.start()
            threads.append(thread)

        # Reconfigure scheduler to demote it. To test when reconfiguring,
        # there is no race when new schedulers replacing the old ones.
        handler.configure([])
        assert_that(handler._schedulers, has_length(0))
        time.sleep(0.1)
        done[0] = True

        for thread in threads:
            thread.join()

        assert_that(len(results), equal_to(concurrency))
    def test_place_during_reconfigure(self):
        # Create leaf scheduler which will return RESOURCE_CONSTRAINT
        scheduler = LeafScheduler("foo", 1, False)
        scheduler._placement_hosts = MagicMock()
        scheduler._placement_hosts.return_value = []

        # Create scheduler handler
        handler = SchedulerHandler()
        handler._create_scheduler = MagicMock()
        handler._create_scheduler.return_value = scheduler

        # Configure scheduler handler with leaf scheduler
        handler.configure(
            [SchedulerRole("foo", "parent-id",
                           scheduler_children=[ChildInfo(id="child")])])
        assert_that(handler._schedulers, has_length(1))

        concurrency = 5
        threads = []
        results = {}
        done = [False]

        # Define the thread which keeps calling place, to make sure it
        # always return RESOURCE_CONSTRAINT
        def _loop():
            while True:
                actual_response = handler.place(
                    PlaceRequest(Resource(), "foo"))
                assert_that(actual_response.result,
                            is_not(PlaceResultCode.SYSTEM_ERROR))

                if done[0]:
                    results[threading.current_thread().name] = True
                    break

        for _ in xrange(concurrency):
            thread = threading.Thread(target=_loop)
            thread.start()
            threads.append(thread)

        # Reconfigure scheduler to demote it. To test when reconfiguring,
        # there is no race when new schedulers replacing the old ones.
        handler.configure([])
        assert_that(handler._schedulers, has_length(0))
        time.sleep(0.1)
        done[0] = True

        for thread in threads:
            thread.join()

        assert_that(len(results), equal_to(concurrency))
    def test_configure_create_leaf_scheduler(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        handler = SchedulerHandler()
        assert_that(handler._schedulers, is_(empty()))

        handler.configure(
            [SchedulerRole("leaf-scheduler", "parent-id",
                           host_children=[ChildInfo(id="foo")])])
        assert_that(handler._schedulers, has_length(1))

        scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(scheduler.id, is_(leaf_scheduler.id))
        scheduler.configure.assert_called_once_with([ChildInfo(id="foo")])
    def test_configure_cleanup_scheduler(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        handler = SchedulerHandler()

        handler.configure(
            [SchedulerRole("leaf-scheduler", "parent-id",
                           host_children=[ChildInfo(id="foo")])])
        scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(scheduler, is_(leaf_scheduler))

        handler.configure([])

        assert_that(scheduler.cleanup.called, is_(True))
    def test_configure_update_scheduler(self):
        handler = SchedulerHandler()

        handler.configure(
            [SchedulerRole("leaf-scheduler", "parent-id",
                           host_children=[ChildInfo(id="foo")])], False)
        old_scheduler = handler._schedulers["leaf-scheduler"]

        handler.configure(
            [SchedulerRole("leaf-scheduler", "parent-id",
                           host_children=[ChildInfo(id="bar")])], False)
        new_scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(new_scheduler._scheduler_id,
                    equal_to(old_scheduler._scheduler_id))
        assert_that(new_scheduler._hosts, has_length(1))
        assert_that(new_scheduler._hosts[0].id, equal_to("bar"))
    def test_configure_create_leaf_scheduler(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        handler = SchedulerHandler()
        assert_that(handler._schedulers, is_(empty()))

        handler.configure([
            SchedulerRole("leaf-scheduler",
                          "parent-id",
                          host_children=[ChildInfo(id="foo")])
        ])
        assert_that(handler._schedulers, has_length(1))

        scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(scheduler.id, is_(leaf_scheduler.id))
        scheduler.configure.assert_called_once_with([ChildInfo(id="foo")])
    def test_configure_cleanup_scheduler(self, leaf_scheduler_cls):
        leaf_scheduler = MagicMock()
        leaf_scheduler_cls.return_value = leaf_scheduler

        handler = SchedulerHandler()

        handler.configure([
            SchedulerRole("leaf-scheduler",
                          "parent-id",
                          host_children=[ChildInfo(id="foo")])
        ])
        scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(scheduler, is_(leaf_scheduler))

        handler.configure([])

        assert_that(scheduler.cleanup.called, is_(True))
    def test_configure_update_scheduler(self):
        handler = SchedulerHandler()

        handler.configure([
            SchedulerRole("leaf-scheduler",
                          "parent-id",
                          host_children=[ChildInfo(id="foo")])
        ], False)
        old_scheduler = handler._schedulers["leaf-scheduler"]

        handler.configure([
            SchedulerRole("leaf-scheduler",
                          "parent-id",
                          host_children=[ChildInfo(id="bar")])
        ], False)
        new_scheduler = handler._schedulers["leaf-scheduler"]
        assert_that(new_scheduler._scheduler_id,
                    equal_to(old_scheduler._scheduler_id))
        assert_that(new_scheduler._hosts, has_length(1))
        assert_that(new_scheduler._hosts[0].id, equal_to("bar"))