def test_resource_constraints_zero_constraints_min_fanout(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        result = strategy.filter_child(
            [self.child_1, self.child_2, self.child_3], self.request, [])
        assert_that(result, has_length(2))
    def test_resource_constraints_negative_two_select_two(self):
        # Test that both children are picked as
        # child 4 has more than just "host1"
        # while child 5 has "host4" which is
        # not included in the negative constraints
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host1'], True),
                       ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host7'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(2))
        assert_that(result, contains_inanyorder(self.child_4, self.child_5))

        # Now try with a single constraint
        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host1', 'host7'], True)]
        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(2))
        assert_that(result, contains_inanyorder(self.child_4, self.child_5))
    def test_resource_constraints_negative_two_select_one(self):
        # Test that child 4 is the only one picked as
        # child 5 has only one host ("host4") which is included
        # in the negative constraints
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host1'], True),
                       ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host4'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(1))
        assert_that(result, contains_inanyorder(self.child_4))

        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host1', 'host4'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(1))
        assert_that(result, contains_inanyorder(self.child_4))
    def test_resource_constraints_zero_constraints_min_fanout(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        result = strategy.filter_child(
            [self.child_1, self.child_2, self.child_3], self.request, [])
        assert_that(result, has_length(2))
    def test_resource_constraints_negative_two_select_one(self):
        # Test that child 4 is the only one picked as
        # child 5 has only one host ("host4") which is included
        # in the negative constraints
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host1'], True),
                       ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host4'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(1))
        assert_that(result, contains_inanyorder(self.child_4))

        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host1', 'host4'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(1))
        assert_that(result, contains_inanyorder(self.child_4))
    def test_resource_constraints_negative_two_select_two(self):
        # Test that both children are picked as
        # child 4 has more than just "host1"
        # while child 5 has "host4" which is
        # not included in the negative constraints
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host1'], True),
                       ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host7'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(2))
        assert_that(result, contains_inanyorder(self.child_4, self.child_5))

        # Now try with a single constraint
        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host1', 'host7'], True)]
        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(2))
        assert_that(result, contains_inanyorder(self.child_4, self.child_5))
    def test_resource_constraints_no_match(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        result = strategy.filter_child(
            [self.child_1, self.child_2, self.child_3],
            self.request,
            [ResourceConstraint(ResourceConstraintType.DATASTORE,
                                ['never_found'])])
        assert_that(result, has_length(0))
    def test_resource_constraints_no_match(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        result = strategy.filter_child(
            [self.child_1, self.child_2, self.child_3],
            self.request,
            [ResourceConstraint(ResourceConstraintType.DATASTORE,
                                ['never_found'])])
        assert_that(result, has_length(0))
    def test_resource_constraints_one_constraints(self):
        strategy = RandomSubsetStrategy(0.5, 2)

        strategy._get_constraints = self._get_constraints

        result = strategy.filter_child(
            [self.child_1, self.child_2, self.child_3],
            self.request,
            [ResourceConstraint(ResourceConstraintType.DATASTORE,
                                ['datastore1'])])
        assert_that(result, has_length(2))
        assert_that(result, contains_inanyorder(self.child_1, self.child_2))
    def test_resource_constraints_negative_all_match(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host6', 'host7'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(2))
        assert_that(result, contains_inanyorder(self.child_4, self.child_5))
    def test_resource_constraints_negative_no_match(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [ResourceConstraint(
            ResourceConstraintType.HOST,
            ['host1', 'host2', 'host3', 'host4'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(0))
    def test_resource_constraints_one_constraints(self):
        strategy = RandomSubsetStrategy(0.5, 2)

        strategy._get_constraints = self._get_constraints

        result = strategy.filter_child(
            [self.child_1, self.child_2, self.child_3],
            self.request,
            [ResourceConstraint(ResourceConstraintType.DATASTORE,
                                ['datastore1'])])
        assert_that(result, has_length(2))
        assert_that(result, contains_inanyorder(self.child_1, self.child_2))
    def test_resource_constraints_negative_all_match(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [ResourceConstraint(ResourceConstraintType.HOST,
                                          ['host6', 'host7'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(2))
        assert_that(result, contains_inanyorder(self.child_4, self.child_5))
    def test_resource_constraints_negative_no_match(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [ResourceConstraint(
            ResourceConstraintType.HOST,
            ['host1', 'host2', 'host3', 'host4'], True)]

        result = strategy.filter_child(
            [self.child_4, self.child_5],
            self.request, constraints)
        assert_that(result, has_length(0))
    def test_resource_constraints_availability_zone_no_match(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [
            ResourceConstraint(ResourceConstraintType.AVAILABILITY_ZONE,
                               ['zone1'], False),
            ResourceConstraint(ResourceConstraintType.DATASTORE,
                               ['datastore2'], False)]
        result = strategy.filter_child(
            [self.child_1, self.child_6, self.child_7],
            self.request, constraints)
        assert_that(result, has_length(0))
    def test_resource_constraints_availability_zone_no_match(self):
        strategy = RandomSubsetStrategy(0.5, 2)
        strategy._get_constraints = self._get_constraints

        constraints = [
            ResourceConstraint(ResourceConstraintType.AVAILABILITY_ZONE,
                               ['zone1'], False),
            ResourceConstraint(ResourceConstraintType.DATASTORE,
                               ['datastore2'], False)]
        result = strategy.filter_child(
            [self.child_1, self.child_6, self.child_7],
            self.request, constraints)
        assert_that(result, has_length(0))