Example #1
0
    def test_place_clears_scheduler_id(self, client_class):
        client_class.side_effect = self.create_fake_client

        client = MagicMock()
        self._clients["bar"] = client

        scheduler = LeafScheduler("foo", 9, False)
        scheduler.configure([ChildInfo(id="bar", address="bar")])
        scheduler.place(PlaceRequest(scheduler_id="foo"))

        client.host_place.assert_called_with(PlaceRequest())
Example #2
0
    def test_place_sampling(self, shuffle, client_class):
        client_class.side_effect = self.create_fake_client
        shuffle.side_effect = self.fake_shuffle(
            [ChildInfo(id="bar", address="bar"),
             ChildInfo(id="baz", address="baz"),
             ChildInfo(id="qux", address="qux")])

        bar_client = MagicMock()
        bar_response = PlaceResponse(PlaceResultCode.OK, agent_id="bar",
                                     score=Score(5, 90))
        bar_client.host_place.return_value = bar_response
        self._clients["bar"] = bar_client

        baz_client = MagicMock()
        baz_response = PlaceResponse(PlaceResultCode.OK, agent_id="baz",
                                     score=Score(30, 80))
        baz_client.host_place.return_value = baz_response
        self._clients["baz"] = baz_client

        scheduler = LeafScheduler("foo", 9, False)
        scheduler.configure([ChildInfo(id="qux", address="qux"),
                             ChildInfo(id="bar", address="bar"),
                             ChildInfo(id="baz", address="baz")])

        response = scheduler.place(self._place_request())
        assert_that(response, is_(same_instance(baz_response)))
Example #3
0
    def test_place_local_agent(self, client_class):
        client_class.side_effect = self.create_fake_client

        remote_client = MagicMock()
        self._clients["local-id"] = remote_client

        local_response = PlaceResponse(
            PlaceResultCode.OK, agent_id="local-id", score=Score(5, 90))
        self._scheduler_handler.host_place.return_value = local_response

        scheduler = LeafScheduler("foo", 9, False)
        scheduler.configure([ChildInfo(id="local-id")])

        response = scheduler.place(self._place_request())
        assert_that(remote_client.place.called, is_(False))
        assert_that(response, is_(same_instance(local_response)))
Example #4
0
    def test_place_exception(self, client_class):
        client_class.side_effect = self.create_fake_client

        client = MagicMock()
        e = TimeoutError()
        client.place.side_effect = e
        self._clients["baz"] = client

        scheduler = LeafScheduler("foo", 1.0, False)
        scheduler.configure([ChildInfo(id="baz", address="baz", port=123)])

        response = scheduler.place(self._place_request())
        assert_that(response.result, is_(PlaceResultCode.SYSTEM_ERROR))

        # must shutdown to join the futures since the patch lifecycle
        # ends when this method returns
        self._threadpool.shutdown()
Example #5
0
    def test_place_res_with_missing(self, health_checker, client_class):
        client_class.side_effect = self.create_fake_client
        _health_checker = MagicMock()
        health_checker.return_value = _health_checker
        _health_checker.get_missing_hosts = ["bar"]

        baz_client = MagicMock()
        baz_response = PlaceResponse(PlaceResultCode.OK, agent_id="baz",
                                     score=Score(30, 80))
        baz_client.host_place.return_value = baz_response
        self._clients["baz"] = baz_client

        scheduler = LeafScheduler("foo", 1.0, True)
        scheduler.configure([ChildInfo(id="bar", address="bar"),
                             ChildInfo(id="baz", address="baz")])

        response = scheduler.place(self._place_request())
        assert_that(response, is_(same_instance(baz_response)))
Example #6
0
    def test_place_resource_not_found(self, client_class):
        client_class.side_effect = self.create_fake_client

        bar_client = MagicMock()
        bar_response = PlaceResponse(PlaceResultCode.NO_SUCH_RESOURCE)
        bar_client.host_place.return_value = bar_response
        self._clients["bar"] = bar_client

        baz_client = MagicMock()
        baz_response = PlaceResponse(PlaceResultCode.SYSTEM_ERROR)
        baz_client.host_place.return_value = baz_response
        self._clients["baz"] = baz_client

        scheduler = LeafScheduler("foo", 9, False)
        scheduler.configure([ChildInfo(id="bar", address="bar"),
                             ChildInfo(id="baz", address="baz")])

        response = scheduler.place(self._place_request())
        assert_that(response.result, is_(PlaceResultCode.NO_SUCH_RESOURCE))
Example #7
0
    def test_place_timeout(self, client_class,
                           wait_fn, serial_fn):
        client_class.side_effect = self.create_fake_client

        client = MagicMock()
        response = PlaceResponse(PlaceResultCode.OK)
        client.place.return_value = response
        self._clients["baz"] = client

        wait_fn.return_value = set(), set()
        serial_fn.return_value = set()

        scheduler = LeafScheduler("foo", 9, False)
        scheduler.configure([ChildInfo(id="baz", address="baz")])

        response = scheduler.place(self._place_request())
        assert_that(response.result, is_(PlaceResultCode.SYSTEM_ERROR))

        # must shutdown to join the futures since the patch lifecycle
        # ends when this method returns
        self._threadpool.shutdown()
Example #8
0
    def test_place_with_resource_constraints(self, client_class):
        client_class.side_effect = self.create_fake_client

        bar_client = MagicMock()
        bar_response = PlaceResponse(PlaceResultCode.OK, agent_id="baz_1",
                                     score=Score(5, 90))
        bar_client.host_place.return_value = bar_response
        self._clients["baz_1"] = bar_client

        baz_client = MagicMock()
        baz_response = PlaceResponse(PlaceResultCode.OK, agent_id="baz_2",
                                     score=Score(30, 80))
        baz_client.host_place.return_value = baz_response
        self._clients["baz_2"] = baz_client

        baz_client = MagicMock()
        baz_response = PlaceResponse(PlaceResultCode.OK, agent_id="baz_3",
                                     score=Score(35, 85))
        baz_client.host_place.return_value = baz_response
        self._clients["baz_3"] = baz_client

        scheduler = LeafScheduler("foo", 8, False)
        scheduler.configure(self._build_scheduler_configure())

        # Check OR among values, should succeed
        request = self._place_request()
        request.resource.vm.resource_constraints = [ResourceConstraint(
            ResourceConstraintType.NETWORK, ["net_1", "net_17"])]
        response = scheduler.place(request)
        assert_that(response.result, is_(PlaceResultCode.OK))
        assert_that(response.agent_id, is_("baz_1"))

        # Check AND among constraints, should fail
        request = self._place_request()
        request.resource.vm.resource_constraints = [
            ResourceConstraint(
                ResourceConstraintType.NETWORK, ["net_1"]),
            ResourceConstraint(
                ResourceConstraintType.NETWORK, ["net_4"])]

        response = scheduler.place(request)
        assert_that(response.result, is_(PlaceResultCode.NO_SUCH_RESOURCE))

        # Check AND among constraints, should succeed
        request = self._place_request()
        request.resource.vm.resource_constraints = [
            ResourceConstraint(
                ResourceConstraintType.DATASTORE, ["ds_1"]),
            ResourceConstraint(
                ResourceConstraintType.NETWORK, ["net_2"])]

        response = scheduler.place(request)
        assert_that(response.result, is_(PlaceResultCode.OK))
        assert_that(response.agent_id, is_("baz_1"))

        # Check OR among values + AND among constraints, should succeed
        request = self._place_request()
        request.resource.vm.resource_constraints = [
            ResourceConstraint(
                ResourceConstraintType.NETWORK,
                ["net_1", "net_17"]),
            ResourceConstraint(
                ResourceConstraintType.DATASTORE_TAG,
                ["ds_tag_18", "ds_tag_1"])]

        response = scheduler.place(request)
        assert_that(response.result, is_(PlaceResultCode.OK))
        assert_that(response.agent_id, is_("baz_1"))

        # Check OR among values + AND among constraints, should fail
        request = self._place_request()
        request.resource.vm.resource_constraints = [
            ResourceConstraint(
                ResourceConstraintType.NETWORK,
                ["net_1", "net_17"]),
            ResourceConstraint(
                ResourceConstraintType.DATASTORE_TAG,
                ["ds_tag_18", "ds_tag_19"])]

        response = scheduler.place(request)
        assert_that(response.result, is_(PlaceResultCode.NO_SUCH_RESOURCE))