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="bar", score=Score(5, 90)) bar_client.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.place.return_value = baz_response self._clients["baz"] = baz_client scheduler = BranchScheduler("foo", 9) scheduler.configure([ ChildInfo(id="bar", address="bar", constraints=[ ResourceConstraint(ResourceConstraintType.DATASTORE, ["1"])]), ChildInfo(id="baz", address="baz", constraints=[ ResourceConstraint(ResourceConstraintType.DATASTORE, ["2"])])]) request = self._place_request() request.resource.vm.resource_constraints = [ResourceConstraint( ResourceConstraintType.DATASTORE, ["1"])] response = scheduler.place(request) assert_that(response.result, is_(PlaceResultCode.OK)) assert_that(response.agent_id, is_("bar"))
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.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.place.return_value = baz_response self._clients["baz"] = baz_client scheduler = BranchScheduler("foo", 9) 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)))
def test_new_resource_score(self): response = self.scorer.score([ PlaceResponse(score=Score(40, 0)), PlaceResponse(score=Score(50, 0)) ]) # should pick one with higher utilization score assert_that(response, is_(PlaceResponse(score=Score(50, 0))))
def test_place(self): handler = HostHandler(MagicMock()) score = Score(100, 100) place_list = [MagicMock()] address = ServerAddress(host="localhost", port=1234) request = PlaceRequest(resource=Resource(self._sample_vm(), [])) handler.hypervisor.placement_manager.place.return_value = (score, place_list) response = handler.place(request) assert_that(response.result, is_(PlaceResultCode.OK)) assert_that(response.score, is_(score)) assert_that(response.placementList.placements, is_([item.to_thrift() for item in place_list])) assert_that(response.address, is_(address)) common.services.get(ServiceName.MODE).set_mode( MODE.ENTERING_MAINTENANCE) response = handler.place(request) assert_that(response.result, is_(PlaceResultCode.INVALID_STATE)) common.services.get(ServiceName.MODE).set_mode(MODE.MAINTENANCE) response = handler.place(request) assert_that(response.result, is_(PlaceResultCode.INVALID_STATE))
def test_highest_score_random(self): place_responses = [] for i in xrange(10): resp = PlaceResponse( score=Score(random.randint(0, 100), random.randint(0, 100))) place_responses.append(resp) self._test_highest_score(place_responses)
def test_same_score_pick_random(self): responseA = PlaceResponse(score=Score(98, 100)) responseB = PlaceResponse(score=Score(98, 100)) a_freq = 0 b_freq = 0 # Still can fail with 1/2^30 probability, should be negligible for i in xrange(0, 30): response = self.scorer.score([responseA, responseB]) if response is responseA: a_freq += 1 else: b_freq += 1 assert_that(a_freq, greater_than(0)) assert_that(b_freq, greater_than(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)))
def _create_scheduler_with_hosts(self, name, number_of_hosts): scheduler = LeafScheduler(name, 1.0, False) hosts = [] for num in range(number_of_hosts): child = name + "_host_" + str(num) child_id = "id_" + child child_address = "address_" + child hosts.append( ChildInfo(id=child_id, address=child_address, port=num)) client = MagicMock() response = PlaceResponse(PlaceResultCode.OK, agent_id=child_id, score=Score(100, 90)) client.host_place.return_value = response self._clients[child_address] = client scheduler.configure(hosts) return scheduler
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)))
def to_thrift(self): thrift_score = Score(utilization=self.utilization, transfer=self.transfer) return thrift_score
class ScorerTestCase(unittest.TestCase): def setUp(self): self.scorer = DefaultScorer(ut_ratio=9) def test_highest_score_random(self): place_responses = [] for i in xrange(10): resp = PlaceResponse( score=Score(random.randint(0, 100), random.randint(0, 100))) place_responses.append(resp) self._test_highest_score(place_responses) @parameterized.expand([ ([Score(40, 90), Score(49, 78), Score(50, 70)], 9, Score(50, 70)), ([Score(40, 90), Score(49, 80), Score(50, 70)], 9, Score(49, 80)), ([Score(40, 90), Score(45, 80), Score(50, 70)], 1, Score(40, 90)), ([Score(40, 90), Score(45, 86), Score(50, 70)], 1, Score(45, 86)), ([Score(40, 60), Score(59, 69), Score(50, 70)], 0.1, Score(50, 70)), ([Score(40, 60), Score(61, 69), Score(50, 70)], 0.1, Score(61, 69)), ]) def test_ratio(self, scores, ratio, expected): scorer = DefaultScorer(ut_ratio=ratio) place_responses = [PlaceResponse(score=score) for score in scores] response = scorer.score(place_responses) assert_that(response.score, is_(expected)) def _test_highest_score(self, place_responses): response = self.scorer.score(place_responses) scores = [ self.scorer.score_formula(resp.score) for resp in place_responses ] assert_that(self.scorer.score_formula(response.score), is_(max(scores))) def test_new_resource_score(self): response = self.scorer.score([ PlaceResponse(score=Score(40, 0)), PlaceResponse(score=Score(50, 0)) ]) # should pick one with higher utilization score assert_that(response, is_(PlaceResponse(score=Score(50, 0)))) def test_same_score_pick_random(self): responseA = PlaceResponse(score=Score(98, 100)) responseB = PlaceResponse(score=Score(98, 100)) a_freq = 0 b_freq = 0 # Still can fail with 1/2^30 probability, should be negligible for i in xrange(0, 30): response = self.scorer.score([responseA, responseB]) if response is responseA: a_freq += 1 else: b_freq += 1 assert_that(a_freq, greater_than(0)) assert_that(b_freq, greater_than(0)) @parameterized.expand([ ([Score(60, 0), Score(61, 0), Score(62, 0)], 1, [Score(62, 0)]), ([Score(50, 90), Score(50, 90), Score(49, 90)], 1, [Score(50, 90)]), ([Score(60, 0), Score(70, 0), Score(69, 0)], 1, [Score(69, 0), Score(70, 0)]), ([Score(50, 90), Score(45, 80), Score(10, 70)], 9, [Score(50, 90), Score(45, 80)]), ]) def test_random_scorer_close_scores(self, scores, ut_ratio, expected): scorer = RandomScorer(ut_ratio=ut_ratio) place_responses = [PlaceResponse(score=score) for score in scores] response = scorer.score(place_responses) assert_that(expected, has_item(response.score))
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))