def test_facet_value_query(self):
        fields = ['simple', 'nested.single', 'nested.list']
        aggregation_query = plugin_utils.get_facets_query(fields, 10)

        expected = dict((
            unit_test_utils.simple_facet_field_agg('simple', size=10),
            unit_test_utils.complex_facet_field_agg('nested.single', size=10),
            unit_test_utils.complex_facet_field_agg('nested.list', size=10),
        ))
        self.assertEqual(expected, aggregation_query)
示例#2
0
    def test_facet_value_query(self):
        fields = ['simple', 'nested.single', 'nested.list']
        aggregation_query = plugin_utils.get_facets_query(fields, 10)

        expected = dict((
            unit_test_utils.simple_facet_field_agg('simple', size=10),
            unit_test_utils.complex_facet_field_agg('nested.single', size=10),
            unit_test_utils.complex_facet_field_agg('nested.list', size=10),
        ))
        self.assertEqual(expected, aggregation_query)
    def test_facets_non_admin(self):
        mock_engine = mock.Mock()
        self.plugin.engine = mock_engine
        # Don't care about the actual aggregation result; the base functions
        # are tested separately.
        mock_engine.search.return_value = {'aggregations': {}}

        fake_request = unit_test_utils.get_fake_request(
            USER1, TENANT1, '/v1/search/facets', is_admin=False
        )

        facets = self.plugin.get_facets(fake_request.context)
        facet_names = [f['name'] for f in facets]

        network_facets = ('name', 'version', 'ipv6_addr', 'ipv4_addr',
                          'OS-EXT-IPS-MAC:mac_addr', 'OS-EXT-IPS:type')
        expected_facet_names = [
            'OS-EXT-AZ:availability_zone', 'created_at', 'flavor.id', 'id',
            'image.id', 'name', 'owner', 'security_groups.name', 'status',
            'updated_at', 'user_id']
        expected_facet_names.extend(['networks.' + f for f in network_facets])

        self.assertEqual(set(expected_facet_names), set(facet_names))

        # Test fields with options
        complex_facet_option_fields = (
            'image.id', 'flavor.id', 'networks.name',
            'networks.OS-EXT-IPS:type', 'networks.version',
            'security_groups.name')
        aggs = dict(unit_test_utils.complex_facet_field_agg(name)
                    for name in complex_facet_option_fields)

        simple_facet_option_fields = (
            'status', 'OS-EXT-AZ:availability_zone'
        )
        aggs.update(dict(unit_test_utils.simple_facet_field_agg(name)
                         for name in simple_facet_option_fields))

        expected_agg_query = {
            'aggs': aggs,
            'query': {
                'filtered': {
                    'filter': {
                        'and': [
                            {'term': {ROLE_USER_FIELD: 'user'}},
                            {'term': {'tenant_id': TENANT1}}
                        ]
                    }
                }
            }
        }
        mock_engine.search.assert_called_with(
            index=self.plugin.get_index_name(),
            doc_type=self.plugin.get_document_type(),
            body=expected_agg_query,
            ignore_unavailable=True,
            search_type='count'
        )
    def test_facets_admin(self):
        mock_engine = mock.Mock()
        self.plugin.engine = mock_engine

        fake_request = unit_test_utils.get_fake_request(USER1, TENANT1, "/v1/search/facets", is_admin=True)

        mock_engine.search.return_value = {
            "aggregations": {
                "status": {"buckets": [{"key": "ACTIVE", "doc_count": 2}]},
                "OS-EXT-AZ:availability_zone": {"buckets": []},
                "image.id": {"buckets": [{"key": imagea["id"], "doc_count": 1}]},
                "flavor.id": {"buckets": [{"key": flavor1["id"], "doc_count": 1}]},
                "security_groups": {"buckets": []},
                "network.name": {"buckets": []},
            }
        }

        facets = self.plugin.get_facets(fake_request.context)

        # Check created and updated fields aren't present, even for admins
        self.assertFalse(list(filter(lambda f: f["name"] == "created", facets)))
        self.assertFalse(list(filter(lambda f: f["name"] == "updated", facets)))
        self.assertTrue(list(filter(lambda f: f["name"] == "created_at", facets)))
        self.assertTrue(list(filter(lambda f: f["name"] == "updated_at", facets)))

        # Check unprotected fields are still present
        self.assertTrue(list(filter(lambda f: f["name"] == "status", facets)))

        complex_facet_option_fields = (
            "image.id",
            "flavor.id",
            "networks.name",
            "networks.OS-EXT-IPS:type",
            "networks.version",
            "security_groups.name",
        )
        aggs = dict(unit_test_utils.complex_facet_field_agg(name) for name in complex_facet_option_fields)

        simple_facet_option_fields = ("status", "OS-EXT-AZ:availability_zone")
        aggs.update(dict(unit_test_utils.simple_facet_field_agg(name) for name in simple_facet_option_fields))

        expected_agg_query = {
            "aggs": aggs,
            "query": {"filtered": {"filter": {"and": [{"term": {"tenant_id": TENANT1}}]}}},
        }
        mock_engine.search.assert_called_with(
            index=self.plugin.get_index_name(),
            doc_type=self.plugin.get_document_type(),
            body=expected_agg_query,
            ignore_unavailable=True,
            search_type="count",
        )
    def test_facets_admin(self):
        mock_engine = mock.Mock()
        self.plugin.engine = mock_engine
        mock_engine.search.return_value = {'aggregations': {}}

        fake_request = unit_test_utils.get_fake_request(USER1,
                                                        TENANT1,
                                                        '/v1/search/facets',
                                                        is_admin=True)

        facets = self.plugin.get_facets(fake_request.context)
        facet_names = [f['name'] for f in facets]

        network_facets = ('name', 'version', 'ipv6_addr', 'ipv4_addr',
                          'OS-EXT-IPS-MAC:mac_addr', 'OS-EXT-IPS:type')
        expected_facet_names = [
            'OS-EXT-AZ:availability_zone', 'created_at', 'flavor.id', 'id',
            'image.id', 'name', 'owner', 'security_groups', 'status',
            'tenant_id', 'updated_at', 'user_id'
        ]
        expected_facet_names.extend(['networks.' + f for f in network_facets])

        self.assertEqual(set(expected_facet_names), set(facet_names))

        complex_facet_option_fields = ('image.id', 'flavor.id',
                                       'networks.name',
                                       'networks.OS-EXT-IPS:type',
                                       'networks.version')
        aggs = dict(
            unit_test_utils.complex_facet_field_agg(name)
            for name in complex_facet_option_fields)

        simple_facet_option_fields = ('status', 'OS-EXT-AZ:availability_zone',
                                      'security_groups')
        aggs.update(
            dict(
                unit_test_utils.simple_facet_field_agg(name)
                for name in simple_facet_option_fields))

        expected_agg_query = {
            'aggs': aggs,
            'query': {
                'filtered': {
                    'filter': {
                        'and': [{
                            'term': {
                                ROLE_USER_FIELD: 'admin'
                            }
                        }, {
                            'term': {
                                'tenant_id': TENANT1
                            }
                        }]
                    }
                }
            }
        }
        mock_engine.search.assert_called_with(
            index=self.plugin.alias_name_search,
            doc_type=self.plugin.get_document_type(),
            body=expected_agg_query,
            ignore_unavailable=True,
            size=0)
    def test_facets_all_projects(self):
        # For non admins, all_projects should have no effect
        mock_engine = mock.Mock()
        self.plugin.engine = mock_engine

        # Don't really care about the return values
        mock_engine.search.return_value = {"aggregations": {}}

        fake_request = unit_test_utils.get_fake_request(USER1, TENANT1, "/v1/search/facets", is_admin=False)

        self.plugin.get_facets(fake_request.context, all_projects=True)

        complex_facet_option_fields = (
            "image.id",
            "flavor.id",
            "networks.name",
            "networks.OS-EXT-IPS:type",
            "networks.version",
            "security_groups.name",
        )
        aggs = dict(unit_test_utils.complex_facet_field_agg(name) for name in complex_facet_option_fields)

        simple_facet_option_fields = ("status", "OS-EXT-AZ:availability_zone")
        aggs.update(dict(unit_test_utils.simple_facet_field_agg(name) for name in simple_facet_option_fields))

        expected_agg_query = {
            "aggs": aggs,
            "query": {"filtered": {"filter": {"and": [{"term": {"tenant_id": TENANT1}}]}}},
        }
        mock_engine.search.assert_called_with(
            index=self.plugin.get_index_name(),
            doc_type=self.plugin.get_document_type(),
            body=expected_agg_query,
            ignore_unavailable=True,
            search_type="count",
        )

        # Admins can request all_projects
        fake_request = unit_test_utils.get_fake_request(USER1, TENANT1, "/v1/search/facets", is_admin=True)

        self.plugin.get_facets(fake_request.context, all_projects=True)
        complex_facet_option_fields = (
            "image.id",
            "flavor.id",
            "networks.name",
            "networks.OS-EXT-IPS:type",
            "networks.version",
            "security_groups.name",
        )
        aggs = dict(unit_test_utils.complex_facet_field_agg(name) for name in complex_facet_option_fields)
        simple_facet_option_fields = ("status", "OS-EXT-AZ:availability_zone")
        aggs.update(dict(unit_test_utils.simple_facet_field_agg(name) for name in simple_facet_option_fields))

        # No query here
        expected_agg_query = {"aggs": aggs}
        mock_engine.search.assert_called_with(
            index=self.plugin.get_index_name(),
            doc_type=self.plugin.get_document_type(),
            body=expected_agg_query,
            ignore_unavailable=True,
            search_type="count",
        )