Skip to content

Commit 0ccf025

Browse files
committed
Improve SSH multi line splitter parser
This patch improves the SSH multi line splitter parser to take into account some cases that were not covered as well as fix some of the tests. Code fixes: - On slow SSH connections we may not get the initial dump of stdin in the response, so looking for the "exit" command we sent will never find it and the parsing will fail. We now look for the prompt and ignore whether we have the stdin data or not, since it's not really relevant. - Sometimes we get empty prompts in the SSH response, which either break the current parsing or makes us return bad data. We now remove all empty prompts. Code improvements: - We were always removing the last 2 lines from the response without actually checking if these 2 lines are the exit command and an empty line. Now we search for the final exit command that we execute to determine the end of the valid output data. - If the output data has any "\r" at the end of a line the parser returns them, which we shouldn't. They should be removed just like we remove the "\r\n" when splitting the lines. Test fixes in HPE3ParClientMockSSHTestCase::test_strip_input_from_output: - In a couple of places tests are passing [cmd, X, Y] to the parser expecting a failure, which we get, and we think everything is working as expected, but the failure is not for the reasons we want, so we are not testing anything. By passing that data we are effectively passing [['foo', '-v'], X, Y] which will break the parser because that's "garbage". - The data passed and expected in the success case can be misleading, as it doesn't include the exit prompt and doesn't expect the footer to be returned, but the footer should be returned and parsing a command that doesn't have the last exit command is not valid. Closes #78
1 parent f6b942b commit 0ccf025

File tree

3 files changed

+93
-47
lines changed

3 files changed

+93
-47
lines changed

hpe3parclient/ssh.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -208,52 +208,54 @@ def strip_input_from_output(cmd, output):
208208
in the output so that it knows what it is stripping (or else it
209209
raises an exception).
210210
"""
211-
212-
# Keep output lines after the 'exit'.
213-
# 'exit' is the last of the stdin.
211+
# Search for the prompt. It may or may not be the first line, because
212+
# on fast connections we get a "dump" of stdin with all the commands
213+
# we've sent, but we don't on slow connections.
214214
for i, line in enumerate(output):
215-
if line == 'exit':
215+
prompt_pct = line.find('% setclienv csvtable 1')
216+
if prompt_pct >= 0:
217+
prompt = line[:prompt_pct + 1] + ' '
216218
output = output[i + 1:]
217219
break
218220
else:
219-
reason = "Did not find 'exit' in output."
220-
HPE3PARSSHClient.raise_stripper_error(reason, output)
221-
222-
if not output:
223-
reason = "Did not find any output after 'exit'."
224-
HPE3PARSSHClient.raise_stripper_error(reason, output)
225-
226-
# The next line is prompt plus setclienv command.
227-
# Use this to get the prompt string.
228-
prompt_pct = output[0].find('% setclienv csvtable 1')
229-
if prompt_pct < 0:
230221
reason = "Did not find '% setclienv csvtable 1' in output."
231222
HPE3PARSSHClient.raise_stripper_error(reason, output)
232-
prompt = output[0][0:prompt_pct + 1]
233-
del output[0]
234223

235-
# Next find the prompt plus the command.
224+
# Some systems return additional empty prompts, others return an
225+
# extra \r after commands. In both cases this prevents us from finding
226+
# the output delimiters, and makes us return useless data to caller, so
227+
# fix them.
228+
output = [line.rstrip('\r\n') for line in output if line != prompt]
229+
230+
# Output starts right after the command we sent.
236231
# It might be broken into multiple lines, so loop and
237232
# append until we find the whole prompt plus command.
238233
command_string = ' '.join(cmd)
239234
if re.match('|'.join(tpd_commands), command_string):
240235
escp_command_string = command_string.replace('"', '\\"')
241236
command_string = "Tpd::rtpd " + '"' + escp_command_string + '"'
242-
seek = ' '.join((prompt, command_string))
237+
seek = prompt + command_string
243238
found = ''
244239
for i, line in enumerate(output):
245-
found = ''.join((found, line.rstrip('\r\n')))
240+
found = found + line
246241
if found == seek:
247-
# Found the whole thing. Use the rest as output now.
248-
output = output[i + 1:]
242+
# Output is right after this line, drop everything before it
243+
del output[:i + 1]
249244
break
250245
else:
251246
HPE3PARSSHClient._logger.debug("Command: %s" % command_string)
252247
reason = "Did not find match for command in output"
253248
HPE3PARSSHClient.raise_stripper_error(reason, output)
254249

255-
# Always strip the last 2
256-
return output[:len(output) - 2]
250+
# Output stops right before exit command is executed
251+
try:
252+
exit_index = output.index(prompt + 'exit')
253+
del output[exit_index:]
254+
except ValueError:
255+
reason = "Did not find 'exit' in output."
256+
HPE3PARSSHClient.raise_stripper_error(reason, output)
257+
258+
return output
257259

258260
def run(self, cmd, multi_line_stripper=False):
259261
"""Runs a CLI command over SSH, without doing any result parsing."""

test/test_HPE3ParClient_FilePersona_Mock.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,17 @@ def test_strip_input_from_output(self):
258258
'CSIM-EOS08_1611165 cli% createvfs -fpg marktestfpg -wait '
259259
'127.0.0.2 255.255.255.\r',
260260
'0 UT5_VFS_150651\r',
261-
'VFS UT5_VFS_150651 already exists within FPG marktestfpg\r',
261+
'VFS UT5_VFS_150651 already exists within FPG marktestfpg',
262262
'CSIM-EOS08_1611165 cli% exit\r',
263263
''
264264
]
265265
expected = [
266-
'VFS UT5_VFS_150651 already exists within FPG marktestfpg\r']
266+
'VFS UT5_VFS_150651 already exists within FPG marktestfpg']
267267

268268
actual = ssh.HPE3PARSSHClient.strip_input_from_output(cmd, out)
269269
self.assertEqual(expected, actual)
270270

271-
def test_strip_input_from_output_no_exit(self):
271+
def test_strip_input_from_output_no_stdin(self):
272272
cmd = [
273273
'createvfs',
274274
'-fpg',
@@ -279,10 +279,6 @@ def test_strip_input_from_output_no_exit(self):
279279
'UT5_VFS_150651'
280280
]
281281
out = [
282-
'setclienv csvtable 1',
283-
'createvfs -fpg marktestfpg -wait 127.0.0.2 255.255.255.0 '
284-
'UT5_VFS_150651',
285-
'XXXt', # Don't match
286282
'CSIM-EOS08_1611165 cli% setclienv csvtable 1\r',
287283
'CSIM-EOS08_1611165 cli% createvfs -fpg marktestfpg -wait '
288284
'127.0.0.2 255.255.255.\r',
@@ -291,9 +287,11 @@ def test_strip_input_from_output_no_exit(self):
291287
'CSIM-EOS08_1611165 cli% exit\r',
292288
''
293289
]
294-
self.assertRaises(exceptions.SSHException,
295-
ssh.HPE3PARSSHClient.strip_input_from_output,
296-
cmd, out)
290+
expected = [
291+
'VFS UT5_VFS_150651 already exists within FPG marktestfpg']
292+
293+
actual = ssh.HPE3PARSSHClient.strip_input_from_output(cmd, out)
294+
self.assertEqual(expected, actual)
297295

298296
def test_strip_input_from_output_no_setclienv(self):
299297
cmd = [

test/test_HPE3ParClient_MockSSH.py

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ def test_sanitize_cert(self):
210210
self.assertEqual(expected, out)
211211

212212
def test_strip_input_from_output(self):
213+
def gen_output(*args):
214+
result = []
215+
for arg in args:
216+
if isinstance(arg, list):
217+
result.extend(arg)
218+
else:
219+
result.append(arg)
220+
return result
213221
cmd = ['foo', '-v']
214222
# nothing after exit
215223
output = ['exit']
@@ -224,30 +232,68 @@ def test_strip_input_from_output(self):
224232
cmd,
225233
output)
226234
# no setclienv csv
227-
output = [cmd, 'exit', 'out']
235+
output = cmd + ['exit', 'out']
228236
self.assertRaises(exceptions.SSHException,
229237
ssh.HPE3PARSSHClient.strip_input_from_output,
230238
cmd,
231239
output)
232240
# command not in output after exit
233-
output = [cmd, 'exit', 'PROMPT% setclienv csvtable 1']
241+
output = gen_output('setclienv csvtable 1',
242+
cmd,
243+
'exit',
244+
'PROMPT% setclienv csvtable 1')
245+
self.assertRaises(exceptions.SSHException,
246+
ssh.HPE3PARSSHClient.strip_input_from_output,
247+
cmd,
248+
output)
249+
# Missing exit prompt because output was cut
250+
output = gen_output('setclienv csvtable 1',
251+
cmd,
252+
'exit',
253+
'PROMPT% setclienv csvtable 1',
254+
'PROMPT% foo -v',
255+
'out1',
256+
'out2')
234257
self.assertRaises(exceptions.SSHException,
235258
ssh.HPE3PARSSHClient.strip_input_from_output,
236259
cmd,
237260
output)
238261
# success
239-
output = [cmd,
240-
'setclienv csvtable 1',
241-
'exit',
242-
'PROMPT% setclienv csvtable 1',
243-
'PROMPT% foo -v',
244-
'out1',
245-
'out2',
246-
'out3',
247-
'------',
248-
'totals']
262+
expected = ['out1', 'out2', 'out3', '------', 'totals']
263+
output = gen_output('setclienv csvtable 1',
264+
cmd,
265+
'exit',
266+
'PROMPT% setclienv csvtable 1',
267+
'PROMPT% foo -v',
268+
'out1',
269+
'out2',
270+
'out3',
271+
'------',
272+
'totals',
273+
'PROMPT% exit')
274+
result = ssh.HPE3PARSSHClient.strip_input_from_output(cmd, output)
275+
self.assertEqual(expected, result)
276+
277+
# succeed even on slow connection that's missing the stdin dump
278+
output = output[4:]
279+
result = ssh.HPE3PARSSHClient.strip_input_from_output(cmd, output)
280+
self.assertEqual(expected, result)
281+
282+
# succeed with empty prompts and extra \r on commands
283+
output = gen_output('setclienv csvtable 1',
284+
cmd,
285+
'exit',
286+
'PROMPT% setclienv csvtable 1\r',
287+
'PROMPT% ',
288+
'PROMPT% foo -v\r',
289+
'out1',
290+
'out2',
291+
'out3',
292+
'------',
293+
'totals',
294+
'PROMPT% exit\r')
249295
result = ssh.HPE3PARSSHClient.strip_input_from_output(cmd, output)
250-
self.assertEqual(['out1', 'out2', 'out3'], result)
296+
self.assertEqual(expected, result)
251297

252298
@mock.patch('hpe3parclient.client.ssh.HPE3PARSSHClient', spec=True)
253299
def test_verify_get_port(self, mock_ssh_client):

0 commit comments

Comments
 (0)