Skip to content

Commit

Permalink
Merge d2d8819 into 29071bc
Browse files Browse the repository at this point in the history
  • Loading branch information
palli committed Dec 12, 2014
2 parents 29071bc + d2d8819 commit 7aec615
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
17 changes: 9 additions & 8 deletions pynag/Parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2299,10 +2299,13 @@ def __init__(self, query, *args, **kwargs):
>>> query.get_query()
'GET services\\nColumns: service_description\\nFilter: host_name = localhost\\n\\n'
"""
# `query´ here represents a *single* query, obviously.
# But we don't know from where it comes or if it can be trusted..
# If it would contains some empty line then we must filter them out:
self._query = [ line for line in query.splitlines() if line ]
self._query = []

# We purposefully strip white space, extra line breaks will
# be added to the query string when get_query() is called.
for header_line in query.strip().splitlines():
self.add_header_line(header_line)

for header_line in args:
self.add_header_line(header_line)
self.add_filters(**kwargs)
Expand Down Expand Up @@ -2411,9 +2414,7 @@ def add_header_line(self, header_line):
>>> query.get_query()
'GET services\\nFilter: host_name = foo\\n\\n'
"""
# from same reason than in __init__, we have to skip empty header:
if header_line:
self._query.append(header_line)
self._query.append(header_line)

def add_header(self, keyword, arguments):
"""Add a new header to our livestatus query.
Expand Down Expand Up @@ -3120,7 +3121,7 @@ def query(self, query, *args, **kwargs):
# This is we actually send our query into livestatus. livestatus_response is the raw response
# from livestatus socket (string):
try:
livestatus_response = self.raw_query(livestatus_query)
livestatus_response = self.write(livestatus_query.get_query())
except LivestatusError:
time.sleep(self._RETRY_INTERVAL)
livestatus_response = self.raw_query(livestatus_query)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,15 @@ def test_query_stats_have_no_headers(self):
stats_for_state_0 = response[0]
self.assertIsInstance(stats_for_state_0, int)

@mock.patch('pynag.Parsers.Livestatus.raw_query')
def test_query_empty_response_raises(self, mock_raw_query):
mock_raw_query.return_value = ''
@mock.patch('pynag.Parsers.Livestatus.write')
def test_query_empty_response_raises(self, mock_write):
mock_write.return_value = ''
with self.assertRaises(pynag.Parsers.InvalidResponseFromLivestatus):
self.livestatus.query('GET services')

@mock.patch('pynag.Parsers.Livestatus.raw_query')
def test_query_invalid_response_raises(self, mock_raw_query):
mock_raw_query.return_value = '200\ngarbage data from livestatus['
@mock.patch('pynag.Parsers.Livestatus.write')
def test_query_invalid_response_raises(self, mock_write):
mock_write.return_value = '200\ngarbage data from livestatus['
with self.assertRaises(pynag.Parsers.InvalidResponseFromLivestatus):
self.livestatus.query('GET services')

Expand Down

0 comments on commit 7aec615

Please sign in to comment.