Skip to content

Commit efd383c

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 efd383c

File tree

4 files changed

+115
-69
lines changed

4 files changed

+115
-69
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.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def validate_vfs(self, fpgname=None, vfsname=None, no_vfsname=None,
408408
# Validate contents
409409
if fpgname is not None:
410410
success_message = None
411-
not_found_message = 'Invalid VFS %s\r' % vfsname
411+
not_found_message = 'Invalid VFS %s' % vfsname
412412
self.assertIn(message, (success_message, not_found_message))
413413
elif total == 0:
414414
self.assertEqual('', message)
@@ -456,7 +456,7 @@ def test_getfpg_empty(self):
456456
@print_header_and_footer
457457
def test_getfpg_bogus(self):
458458
result = self.cl.getfpg('bogus1', 'bogus2', 'bogus3')
459-
expected_message = 'File Provisioning Group: bogus1 not found\r'
459+
expected_message = 'File Provisioning Group: bogus1 not found'
460460
self.assertEqual(expected_message, result['message'])
461461
self.assertEqual(0, result['total'])
462462
self.assertEqual([], result['members'])
@@ -474,7 +474,7 @@ def test_createfpg_bogus_cpg(self):
474474
bogus_cpgname = 'thiscpgdoesnotexist'
475475
result = self.cl.createfpg(bogus_cpgname, fpgname, '1X')
476476
self.assertEqual(
477-
'Error: Invalid CPG name: %s\r' % bogus_cpgname,
477+
'Error: Invalid CPG name: %s' % bogus_cpgname,
478478
result[0])
479479

480480
self.validate_fpg(expected_count=fpg_count)
@@ -498,7 +498,7 @@ def test_createfpg_bad_size(self):
498498

499499
result = self.cl.createfpg(cpgname, fpgname, '1X')
500500
self.assertEqual(
501-
'The suffix, X, for size is invalid.\r', result[0])
501+
'The suffix, X, for size is invalid.', result[0])
502502

503503
self.validate_fpg(expected_count=fpg_count)
504504

@@ -552,7 +552,7 @@ def test_createfpg_twice_and_remove(self):
552552

553553
# Create same FPG again to test createfpg already exists error
554554
result = self.cl.createfpg(cpgname, fpgname, '1T', wait=True)
555-
expected = ('Error: FPG %s already exists\r' %
555+
expected = ('Error: FPG %s already exists' %
556556
fpgname)
557557
self.assertEqual(expected, result[0])
558558
self.validate_fpg(fpgname=fpgname, expected_count=fpg_count + 1)
@@ -614,7 +614,7 @@ def test_createvfs_bogus_bgrace(self):
614614
fpg=fpgname,
615615
bgrace='bogus', igrace='60',
616616
wait=True)
617-
self.assertEqual('bgrace value should be between 1 and 2147483647\r',
617+
self.assertEqual('bgrace value should be between 1 and 2147483647',
618618
result[0])
619619

620620
@unittest.skipIf(is_live_test() and skip_file_persona(), SKIP_MSG)
@@ -627,7 +627,7 @@ def test_createvfs_bogus_igrace(self):
627627
fpg=fpgname,
628628
bgrace='60', igrace='bogus',
629629
wait=True)
630-
self.assertEqual('igrace value should be between 1 and 2147483647\r',
630+
self.assertEqual('igrace value should be between 1 and 2147483647',
631631
result[0])
632632

633633
def get_fsips(self, fpgname, vfsname):
@@ -654,15 +654,15 @@ def get_fsips(self, fpgname, vfsname):
654654
result = self.cl.getfsip(vfsname, fpg='bogus')
655655
self.debug_print(result)
656656
expected = {
657-
'message': 'File Provisioning Group: bogus not found\r',
657+
'message': 'File Provisioning Group: bogus not found',
658658
'total': 0,
659659
'members': []
660660
}
661661
self.assertEqual(expected, result)
662662
result = self.cl.getfsip('bogus', fpg=fpgname)
663663
self.debug_print(result)
664664
expected = {
665-
'message': 'Invalid VFS bogus\r',
665+
'message': 'Invalid VFS bogus',
666666
'total': 0,
667667
'members': []
668668
}
@@ -736,13 +736,13 @@ def create_fsnap(self, fpgname, vfsname, fstore, tag):
736736

737737
# Test error messages with bogus names
738738
result = self.cl.createfsnap('bogus', fstore, tag, fpg=fpgname)
739-
self.assertEqual(['Virtual Server bogus does not exist on FPG %s\r' %
739+
self.assertEqual(['Virtual Server bogus does not exist on FPG %s' %
740740
fpgname], result)
741741
result = self.cl.createfsnap(vfsname, 'bogus', tag, fpg=fpgname)
742-
self.assertEqual(['File Store bogus does not exist on FPG %s\r' %
742+
self.assertEqual(['File Store bogus does not exist on FPG %s' %
743743
fpgname], result)
744744
result = self.cl.createfsnap(vfsname, fstore, tag, fpg='bogus')
745-
self.assertEqual(['FPG bogus not found\r'], result)
745+
self.assertEqual(['FPG bogus not found'], result)
746746

747747
result = self.cl.getfsnap('bogus',
748748
fpg=fpgname, vfs=vfsname, fstore=fstore,
@@ -754,7 +754,7 @@ def create_fsnap(self, fpgname, vfsname, fstore, tag):
754754
expected = {
755755
'members': [],
756756
'message': 'SnapShot bogus does not exist on FPG %s path '
757-
'%s/%s\r' % (fpgname, vfsname, fstore),
757+
'%s/%s' % (fpgname, vfsname, fstore),
758758
'total': 0}
759759
self.assertEqual(expected, result)
760760

@@ -797,7 +797,7 @@ def create_fsnap(self, fpgname, vfsname, fstore, tag):
797797
self.assertEqual([], result)
798798

799799
success = []
800-
running = ['Reclamation already running on %s\r' % fpgname]
800+
running = ['Reclamation already running on %s' % fpgname]
801801
expected_in = (success, running)
802802
# After first one expect 'running', but to avoid timing issues in
803803
# the test results accept either success or running.
@@ -833,14 +833,14 @@ def create_fsnap(self, fpgname, vfsname, fstore, tag):
833833
self.assertEqual([], result)
834834

835835
result = self.cl.startfsnapclean(fpgname, resume=True)
836-
self.assertEqual(['No reclamation task running on FPG %s\r' % fpgname],
836+
self.assertEqual(['No reclamation task running on FPG %s' % fpgname],
837837
result)
838838

839839
def remove_fstore(self, fpgname, vfsname, fstore):
840840
self.cl.removefsnap(vfsname, fstore, fpg=fpgname)
841841
result = self.cl.startfsnapclean(fpgname, reclaimStrategy='maxspeed')
842842
success = []
843-
running = ['Reclamation already running on %s\r' % fpgname]
843+
running = ['Reclamation already running on %s' % fpgname]
844844
expected_in = (success, running)
845845
self.assertIn(result, expected_in)
846846

@@ -855,7 +855,7 @@ def remove_share(self, protocol, fpgname, vfsname, share_name):
855855
fpg=fpgname, fstore=share_name)
856856
if protocol == 'nfs':
857857
expected = ['%s Delete Export failed with error: '
858-
'share %s does not exist\r' %
858+
'share %s does not exist' %
859859
(protocol.upper(), share_name)]
860860
self.assertEqual(expected, result)
861861
else:
@@ -869,9 +869,9 @@ def remove_share(self, protocol, fpgname, vfsname, share_name):
869869
if protocol == 'nfs':
870870
expected = [
871871
'%s Delete Export failed with error: '
872-
'File Store bogus was not found\r' % protocol.upper()]
872+
'File Store bogus was not found' % protocol.upper()]
873873
else:
874-
expected = ['Could not find Store=bogus\r']
874+
expected = ['Could not find Store=bogus']
875875
self.assertEqual(expected, result)
876876

877877
@unittest.skipIf(skip_file_persona(), SKIP_MSG)
@@ -890,7 +890,7 @@ def test_create_and_remove_shares(self):
890890
result = self.cl.createvfs('127.0.0.2', '255.255.255.0', vfsname,
891891
fpg=fpgname,
892892
wait=True)
893-
expected = ('VFS "%s" already exists within FPG %s\r' %
893+
expected = ('VFS "%s" already exists within FPG %s' %
894894
(vfsname, fpgname))
895895
self.assertEqual(expected, result[0])
896896
self.validate_vfs(vfsname=vfsname, fpgname=fpgname,
@@ -985,12 +985,12 @@ def test_removevfs_bogus(self):
985985
self.assertRaises(AttributeError, self.cl.removevfs, None)
986986
result = self.cl.removevfs('bogus')
987987
vfs_not_found = ('Virtual file server bogus was not found in any '
988-
'existing file provisioning group.\r')
988+
'existing file provisioning group.')
989989
self.assertEqual(vfs_not_found, result[0])
990990
self.assertRaises(AttributeError, self.cl.removevfs, None, fpg='bogus')
991991

992992
result = self.cl.removevfs('bogus', fpg='bogus')
993-
fpg_not_found = 'File Provisioning Group: bogus not found\r'
993+
fpg_not_found = 'File Provisioning Group: bogus not found'
994994
self.assertEqual(fpg_not_found, result[0])
995995

996996
# testing

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 = [

0 commit comments

Comments
 (0)