Exemplo n.º 1
0
    def get_queryset(self):
        """When the parent instance of the host query set has a `kind=smart` and a `host_filter`
        set. Use the `host_filter` to generate the queryset for the hosts.
        """
        qs = super(HostManager, self).get_queryset().defer(
            'last_job__extra_vars',
            'last_job_host_summary__job__extra_vars',
            'last_job__artifacts',
            'last_job_host_summary__job__artifacts',
        )

        if (hasattr(self, 'instance') and
           hasattr(self.instance, 'host_filter') and
           hasattr(self.instance, 'kind')):
            if self.instance.kind == 'smart' and self.instance.host_filter is not None:
                q = SmartFilter.query_from_string(self.instance.host_filter)
                if self.instance.organization_id:
                    q = q.filter(inventory__organization=self.instance.organization_id)
                # If we are using host_filters, disable the core_filters, this allows
                # us to access all of the available Host entries, not just the ones associated
                # with a specific FK/relation.
                #
                # If we don't disable this, a filter of {'inventory': self.instance} gets automatically
                # injected by the related object mapper.
                self.core_filters = {}

                qs = qs & q
                return qs.order_by('name', 'pk').distinct('name')
        return qs
Exemplo n.º 2
0
 def test_host_distinctness(self, setup_inventory_groups, organization):
     """
     two criteria would both yield the same host, check that we only get 1 copy here
     """
     assert (list(
         SmartFilter.query_from_string(
             'name=single_host or name__startswith=single_')) == [
                 Host.objects.get(name='single_host')
             ])
Exemplo n.º 3
0
 def test_does_not_invoke_db(self, filter_string, q_expected):
     q = SmartFilter.query_from_string(filter_string)
     assert str(q.query) == str(Host.objects.filter(q_expected).query)
Exemplo n.º 4
0
 def test_query_generated(self, mock_get_host_model, filter_string, q_expected):
     q = SmartFilter.query_from_string(filter_string)
     assert str(q) == str(q_expected)
Exemplo n.º 5
0
 def test_boolean_parenthesis(self, mock_get_host_model, filter_string, q_expected):
     q = SmartFilter.query_from_string(filter_string)
     assert str(q) == str(q_expected)
Exemplo n.º 6
0
 def test_search_related_fields(self, mock_get_host_model, filter_string, q_expected):
     q = SmartFilter.query_from_string(filter_string)
     assert str(q) == str(q_expected)
Exemplo n.º 7
0
 def test_forbidden_filter_string(self, mock_get_host_model, filter_string):
     with pytest.raises(Exception) as e:
         SmartFilter.query_from_string(filter_string)
     "Filtering on password is not allowed." in str(e)
Exemplo n.º 8
0
 def test_invalid_filter_strings(self, mock_get_host_model, filter_string):
     with pytest.raises(RuntimeError) as e:
         SmartFilter.query_from_string(filter_string)
     assert str(e.value) == u"Invalid query " + filter_string
Exemplo n.º 9
0
 def test_unicode(self, mock_get_host_model, filter_string, q_expected):
     q = SmartFilter.query_from_string(filter_string)
     assert unicode(q) == unicode(q_expected)
Exemplo n.º 10
0
 def test_contains_query_generated_null(self, mock_get_host_model,
                                        filter_string, q_expected):
     q = SmartFilter.query_from_string(filter_string)
     assert unicode(q) == unicode(q_expected)
Exemplo n.º 11
0
 def test_contains_query_generated(self, mock_get_host_model, filter_string,
                                   q_expected):
     q = SmartFilter.query_from_string(filter_string)
     assert six.text_type(q) == six.text_type(q_expected)