Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Nov 22, 2017. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions sheer/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def index_processor(es, index_name, processor, reindex=False):
es.indices.put_mapping(index=index_name,
doc_type=processor.name,
body={processor.name: mapping_supplied})

# Keep track of whether the indexing process is successful
# This is so the end user and/or Jenkins knows the job failed if everything
# didn't index 100%
index_success = True
try:
# Get the document iterator from the processor.
document_iterator = processor.documents()
Expand All @@ -120,6 +123,7 @@ def index_processor(es, index_name, processor, reindex=False):
# can't connect to the API endpoint its getting the JSON from.
document_iterator = []
sys.stderr.write("error making connection for %s" % processor.name)
index_success = False

try:
# Iterate over the documents
Expand All @@ -133,8 +137,10 @@ def index_processor(es, index_name, processor, reindex=False):
# ValueError) raised by json.loads() with the API's supposedly JSON
# output.
sys.stderr.write("error reading documents for %s" % processor.name)
index_success = False
else:
sys.stdout.write("indexed %s %s \n" % (i + 1, processor.name))
return index_success


def index_location(args, config):
Expand Down Expand Up @@ -199,5 +205,15 @@ def index_location(args, config):
if args.processors and len(args.processors) > 0:
selected_processors = [p for p in processors if p.name in args.processors]

failed_processors = []
for processor in selected_processors:
index_processor(es, index_name, processor, reindex=args.reindex)
index_sucess = index_processor(es,
index_name,
processor,
reindex=args.reindex)
if not index_sucess:
failed_processors.append(processor.name)
# Exit with an error code != 0 if there were any issues with indexing
if failed_processors:
sys.exit("Indexing the following processor(s) failed: {}".format(
", ".join(failed_processors)))
15 changes: 8 additions & 7 deletions sheer/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from .indexer import ContentProcessor, index_location
from elasticsearch.exceptions import TransportError


class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self


class TestIndexing(object):
"""
Test Sheer content indexing.
Expand All @@ -32,7 +34,7 @@ def setup(self):

self.config = {'location': '.',
'elasticsearch': '',
'index': 'content',}
'index': 'content'}

# This is our mock ContentProcessor. It will return mappings and
# documents for a particular document type, 'posts' in our mock
Expand All @@ -44,7 +46,6 @@ def setup(self):
self.mock_processor.mapping.return_value = {}
self.mock_processor.documents.return_value = iter([self.mock_document])


@mock.patch('sheer.indexer.Elasticsearch')
@mock.patch('sheer.indexer.ContentProcessor')
@mock.patch('sheer.indexer.read_json_file')
Expand Down Expand Up @@ -85,7 +86,6 @@ def test_indexing(self, mock_exists, mock_read_json_file,
id=self.mock_document['_id'],
body=self.mock_document)


@mock.patch('sheer.indexer.Elasticsearch')
@mock.patch('sheer.indexer.ContentProcessor')
@mock.patch('sheer.indexer.read_json_file')
Expand Down Expand Up @@ -127,7 +127,6 @@ def test_reindexing(self, mock_exists, mock_read_json_file,
id=self.mock_document['_id'],
body=self.mock_document)


@mock.patch('sheer.indexer.Elasticsearch')
@mock.patch('sheer.indexer.ContentProcessor')
@mock.patch('sheer.indexer.read_json_file')
Expand Down Expand Up @@ -212,7 +211,6 @@ def test_partial_reindexing(self, mock_exists, mock_read_json_file,
id=self.mock_document['_id'],
body=self.mock_document)


@mock.patch('sheer.indexer.Elasticsearch')
@mock.patch('sheer.indexer.ContentProcessor')
@mock.patch('sheer.indexer.read_json_file')
Expand Down Expand Up @@ -286,7 +284,11 @@ def test_indexing_failure(self, mock_exists, mock_read_json_file,
mock_es.indices.get_mapping.return_value = None

test_args = AttrDict(processors=[], reindex=False)
index_location(test_args, self.config)
try:
index_location(test_args, self.config)
except SystemExit, s:
assert s.code == \
'Indexing the following processor(s) failed: ioerrs, valueerrs'

# Ensure that we got the error messages.
assert 'error making connection' in sys.stderr.getvalue()
Expand All @@ -301,4 +303,3 @@ def test_indexing_failure(self, mock_exists, mock_read_json_file,
doc_type='posts',
id=self.mock_document['_id'],
body=self.mock_document)