def test_with_region(self, resource_plugin_conf, service_cred_conf): # Now test with a region resource_plugin_conf.include_region_name = True service_cred_conf.os_region_name = 'test-region' index_name = self.role_plugin.alias_name_listener simple_plugin = fake_plugins.FakeSimplePlugin(self.elastic_connection) non_role = fake_plugins.NonRoleSeparatedPlugin(self.elastic_connection) # Override region name for non-role non_role.options.override_region_name = ['region1', 'region2'] non_role.index_initial_data() simple_plugin.index_initial_data() self._flush_elasticsearch(index_name) es_results = self._get_all_elasticsearch_docs() simple_res = list( filter(lambda r: r['_type'] == simple_plugin.get_document_type(), es_results['hits']['hits'])) non_role_res = list( filter(lambda r: r['_type'] == non_role.get_document_type(), es_results['hits']['hits'])) self.assertEqual(['test-region'], simple_res[0]['_source']['region_name']) self.assertEqual(['region1', 'region2'], non_role_res[0]['_source']['region_name'])
def test_non_role_separated_save_docs(self): """Test that for a plugin that doesn't specify any protected fields, ids are left alone and there's only one copy of each indexed doc """ mock_engine = mock.Mock() plugin = fake_plugins.NonRoleSeparatedPlugin(es_engine=mock_engine) indexing_helper = helper.IndexingHelper(plugin) bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk' with mock.patch(bulk_name) as mock_bulk: count = len(plugin.get_objects()) fake_versions = range(1, count + 1) indexing_helper.save_documents(plugin.get_objects(), fake_versions) self.assertEqual(1, len(mock_bulk.call_args_list)) actions = list(mock_bulk.call_args_list[0][1]['actions']) self.assertEqual(2, len(actions)) self.assertEqual(set(['non-role-fake1', 'non-role-fake2']), set(action['_id'] for action in actions)) self.assertEqual(set(fake_versions), set(action['_version'] for action in actions)) self.assertEqual(['external'] * 2, [action['_version_type'] for action in actions]) fake1 = actions[0]['_source'] self.assertEqual(['admin', 'user'], sorted(fake1.pop(ROLE_USER_FIELD))) self.assertEqual(fake_plugins.NON_ROLE_SEPARATED_DATA[0], actions[0]['_source'])
def test_index_initial_data(self, mock_vers, mock_save): mock_engine = mock.Mock() # Test #1: Index with two documents. mock_vers.return_value = '1234' plugin = fake_plugins.NonRoleSeparatedPlugin(es_engine=mock_engine) plugin.index_initial_data(index_name='fake') mock_save.assert_called_once_with(fake_plugins.NON_ROLE_SEPARATED_DATA, versions=['1234', '1234'], index='fake')
def setUp(self): super(TestReindexing, self).setUp() role_plugin = fake_plugins.RoleSeparatedPlugin( es_engine=self.elastic_connection) self.role_plugin = role_plugin non_role_plugin = fake_plugins.NonRoleSeparatedPlugin( es_engine=self.elastic_connection) self.non_role_plugin = non_role_plugin self.initialized_plugins['re-non-role-separated'] = non_role_plugin self.initialized_plugins['re-role-separated'] = role_plugin
def test_facet_counts(self): mock_engine = mock.Mock() plugin = fake_plugins.NonRoleSeparatedPlugin(es_engine=mock_engine) fake_request = unit_test_utils.get_fake_request('a', 'b', '/v1/search/facets', is_admin=True) mock_engine.search.return_value = {"hits": {"total": 1}} facets, doc_count = plugin.get_facets(fake_request.context, include_fields=False) self.assertEqual([], facets) self.assertEqual(1, doc_count) call_args = mock_engine.search.call_args_list self.assertEqual(1, len(call_args)) self.assertNotIn('aggs', call_args[0][1]['body']) mock_engine.search.reset_mock() mock_engine.search.return_value = { "aggregations": { "faceted": { "buckets": [{ "key": 100, "doc_count": 1 }] } }, "hits": { "total": 1 } } facets, doc_count = plugin.get_facets(fake_request.context, include_fields=True) self.assertEqual([{ "name": "faceted", "type": "short", "options": [{ "key": 100, "doc_count": 1 }] }], list(filter(lambda f: f["name"] == "faceted", facets))) self.assertEqual(1, doc_count) call_args = mock_engine.search.call_args_list self.assertEqual(1, len(call_args)) self.assertIn('aggs', call_args[0][1]['body'])
def test_non_role_separated_delete(self): """Test that deletion for a role-separated plugin deletes the doc""" mock_engine = mock.Mock() plugin = fake_plugins.NonRoleSeparatedPlugin(es_engine=mock_engine) indexing_helper = helper.IndexingHelper(plugin) bulk_name = 'searchlight.elasticsearch.plugins.helper.helpers.bulk' with mock.patch(bulk_name) as mock_bulk: indexing_helper.delete_document({'_id': 'non-role-fake1'}) expected_delete_actions = [{ '_op_type': 'delete', '_id': 'non-role-fake1' }] mock_bulk.assert_called_once_with(client=plugin.engine, index=plugin.alias_name_listener, doc_type=plugin.document_type, actions=expected_delete_actions)
def test_manage_type_glob(self, mock_get_plugins, mock_utcnow): mock_utcnow.return_value = datetime.datetime(year=2016, month=3, day=3) expected_index_name = 'searchlight-2016_03_03_00_00_00' simple_plugin = fake_plugins.FakeSimplePlugin(self.elastic_connection) child_plugin = fake_plugins.FakeChildPlugin(self.elastic_connection) non_role_plugin = fake_plugins.NonRoleSeparatedPlugin( self.elastic_connection) child_plugin.register_parent(simple_plugin) mock_get_plugins.return_value = { plugin.get_document_type(): test_utils.StevedoreMock(plugin) for plugin in (simple_plugin, child_plugin, non_role_plugin) } index_command = manage.IndexCommands() # Expect this to match simple plugin and child plugin, but not # non_role_plugin. Patch the index->index function call since it won't # find any data, and we want to check it's called correctly with mock.patch.object(index_command, '_es_reindex_worker') as patch_es_reindex: try: # Use two wildcard matches index_command.sync(_type='fake-sim*,fake-chi*', force=True) es_results = self._get_all_elasticsearch_docs() es_hits = self._get_hit_source(es_results) finally: es_utils.delete_index(expected_index_name) patch_es_reindex.assert_called_with( {non_role_plugin.get_document_type(): non_role_plugin}, [('searchlight', 'searchlight-search', 'searchlight-listener')], {'searchlight': expected_index_name} ) expected = ['simple1', 'child1'] self.assertEqual(len(expected), len(es_hits)) self.assertEqual( set(expected), set(hit['_id'] for hit in es_results['hits']['hits']))
def test_manage(self, mock_get_plugins, mock_utcnow): """Test that manage index sync works from end to end. Uses fake plugins because it avoids having to fake service data and is less dependent on functional tests for each service plugin. """ mock_utcnow.return_value = datetime.datetime(year=2016, month=1, day=1) expected_index_name = 'searchlight-2016_01_01_00_00_00' simple_plugin = fake_plugins.FakeSimplePlugin(self.elastic_connection) child_plugin = fake_plugins.FakeChildPlugin(self.elastic_connection) non_role_plugin = fake_plugins.NonRoleSeparatedPlugin( self.elastic_connection) child_plugin.register_parent(simple_plugin) mock_get_plugins.return_value = { plugin.get_document_type(): test_utils.StevedoreMock(plugin) for plugin in (simple_plugin, child_plugin, non_role_plugin) } index_command = manage.IndexCommands() # The fake plugins all have hardcoded data for get_objects that will # be indexed. Use force=True to avoid the 'are you sure?' prompt try: index_command.sync(force=True) es_results = self._get_all_elasticsearch_docs() es_hits = self._get_hit_source(es_results) finally: es_utils.delete_index(expected_index_name) self.assertEqual(expected_index_name, es_results['hits']['hits'][0]['_index']) expected = ['simple1', 'child1', 'non-role-fake1', 'non-role-fake2'] self.assertEqual(len(expected), len(es_hits)) self.assertEqual( set(expected), set(hit['_id'] for hit in es_results['hits']['hits']))