Skip to content

Commit

Permalink
Merge pull request #319 from nokia/suppoer_for_text_files_with_errors
Browse files Browse the repository at this point in the history
Support for text files with content the same as errors from commands.
  • Loading branch information
Ernold11 authored May 11, 2020
2 parents 3a90254 + f752d20 commit df42741
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 26 deletions.
67 changes: 54 additions & 13 deletions moler/cmd/unix/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@
from moler.exceptions import ParsingDone
import re

__author__ = 'Sylwester Golonka'
__copyright__ = 'Copyright (C) 2018, Nokia'
__email__ = 'sylwester.golonka@nokia.com'
__author__ = 'Sylwester Golonka, Marcin Usielski'
__copyright__ = 'Copyright (C) 2018-2020, Nokia'
__email__ = 'sylwester.golonka@nokia.com, marcin.usielski@nokia.com'


class Cat(GenericUnixCommand):
def __init__(self, connection, path, options=None, prompt=None, newline_chars=None, runner=None):
"""
:param connection: Moler connection to device, terminal when command is executed.
:param path: path to file to cat.
:param options: options passed to command cat.
:param prompt: prompt (on system where command runs).
:param newline_chars: Characters to split lines - list.
:param runner: Runner to run command.
"""
super(Cat, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.path = path
self.options = options
self.current_ret["LINES"] = []
self._line_nr = 0

def build_command_string(self):
"""
Builds string with command.
:return: String with command.
"""
cmd = "cat"
if self.options:
cmd = "{} {} {}".format(cmd, self.path, self.options)
Expand All @@ -28,9 +42,20 @@ def build_command_string(self):
return cmd

def on_new_line(self, line, is_full_line):
"""
Parses the output of the command.
:param line: Line to process, can be only part of line. New line chars are removed from line.
:param is_full_line: True if line had new line chars, False otherwise
:return: None
"""
if is_full_line:
self._line_nr += 1
try:
self._parse_error(line)
if self._line_nr > 1:
self._re_fail = None
else:
self._parse_error(line)
self._parse_line(line)
except ParsingDone:
pass
Expand All @@ -49,8 +74,7 @@ def _parse_line(self, line):
raise ParsingDone


COMMAND_OUTPUT_no_parms = """
ute@debdev:~$ cat /etc/network/interfaces
COMMAND_OUTPUT_no_parms = """cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
Expand All @@ -59,7 +83,7 @@ def _parse_line(self, line):
auto lo
iface lo inet loopback
ute@debdev:~$
user@host:~$
"""

COMMAND_RESULT_no_parms = {
Expand All @@ -69,34 +93,51 @@ def _parse_line(self, line):
'# The loopback network interface',
'auto lo',
'iface lo inet loopback',
'ute@debdev:~$']
'user@host:~$']

}
COMMAND_KWARGS_no_parms = {
"path": "/etc/network/interfaces",
}

#
COMMAND_OUTPUT_parms = """
ute@debdev:~$ cat /etc/network/interfaces -b
COMMAND_OUTPUT_cannot_open = """cat file.txt
Some output
No such file or directory
user@host:~$"""

COMMAND_RESULT_cannot_open = {
'LINES':
[
'Some output',
'No such file or directory',
],
}

COMMAND_KWARGS_cannot_open = {
"path": "file.txt",
}

COMMAND_OUTPUT_parms = """cat /etc/network/interfaces -b
1 # This file describes the network interfaces available on your system
2 # and how to activate them. For more information, see interfaces(5).
3 source /etc/network/interfaces.d/*
4 # The loopback network interface
5 auto lo
6 iface lo inet loopback
ute@debdev:~$
user@host:~$
"""

COMMAND_RESULT_parms = {
'LINES': [' 1\t# This file describes the network interfaces available on your system',
' 2\t# and how to activate them. For more information, see interfaces(5).',
' 3\tsource /etc/network/interfaces.d/*',
' 4\t# The loopback network interface',
' 5\tauto lo',
' 6\tiface lo inet loopback',
'ute@debdev:~$']
'user@host:~$']

}

COMMAND_KWARGS_parms = {
"path": "/etc/network/interfaces",
"options": "-b",
Expand Down
6 changes: 4 additions & 2 deletions moler/cmd/unix/genericunix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

__author__ = 'Marcin Usielski'
__copyright__ = 'Copyright (C) 2018-2019, Nokia'
__copyright__ = 'Copyright (C) 2018-2020, Nokia'
__email__ = 'marcin.usielski@nokia.com'

import re
Expand Down Expand Up @@ -57,7 +57,9 @@ def is_failure_indication(self, line):
:param line: Line from command output on device
:return: Match object if find regex in line, None otherwise.
"""
return self._regex_helper.search_compiled(GenericUnixCommand._re_fail, line)
if self._re_fail:
return self._regex_helper.search_compiled(GenericUnixCommand._re_fail, line)
return False

def _decode_line(self, line):
"""
Expand Down
39 changes: 32 additions & 7 deletions moler/cmd/unix/tail.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@
from moler.exceptions import ParsingDone
import re

__author__ = 'Sylwester Golonka'
__copyright__ = 'Copyright (C) 2018, Nokia'
__email__ = 'sylwester.golonka@nokia.com'
__author__ = 'Sylwester Golonka, Marcin Usielski'
__copyright__ = 'Copyright (C) 2018-2020, Nokia'
__email__ = 'sylwester.golonka@nokia.com, marcin.usielski@nokia.com'


class Tail(GenericUnixCommand):
def __init__(self, connection, path, options=None, prompt=None, newline_chars=None, runner=None):
"""
:param connection: Moler connection to device, terminal when command is executed.
:param path: path to file to tail.
:param options: options passed to command tail.
:param prompt: prompt (on system where command runs).
:param newline_chars: Characters to split lines - list.
:param runner: Runner to run command.
"""
super(Tail, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.path = path
self.options = options
self.current_ret["LINES"] = []
self._line_nr = 0

def build_command_string(self):
"""
Builds string with command.
:return: String with command.
"""
cmd = "tail"
if self.options:
cmd = "{} {} {}".format(cmd, self.path, self.options)
Expand All @@ -28,9 +42,20 @@ def build_command_string(self):
return cmd

def on_new_line(self, line, is_full_line):
"""
Parses the output of the command.
:param line: Line to process, can be only part of line. New line chars are removed from line.
:param is_full_line: True if line had new line chars, False otherwise
:return: None
"""
if is_full_line:
self._line_nr += 1
try:
self._parse_error(line)
if self._line_nr > 1:
self._re_fail = None
else:
self._parse_error(line)
self._parse_line(line)
except ParsingDone:
pass
Expand All @@ -50,7 +75,7 @@ def _parse_line(self, line):


COMMAND_OUTPUT = """
ute@debdev:~$ tail /proc/meminfo
user@host:~$ tail /proc/meminfo
VmallocChunk: 34359608824 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
Expand All @@ -61,7 +86,7 @@ def _parse_line(self, line):
Hugepagesize: 2048 kB
DirectMap4k: 53184 kB
DirectMap2M: 4141056 kB
ute@debdev:~$
user@host:~$
"""

COMMAND_RESULT = {'LINES': [u'VmallocChunk: 34359608824 kB',
Expand All @@ -74,7 +99,7 @@ def _parse_line(self, line):
u'Hugepagesize: 2048 kB',
u'DirectMap4k: 53184 kB',
u'DirectMap2M: 4141056 kB',
u'ute@debdev:~$']}
u'user@host:~$']}

COMMAND_KWARGS = {
"path": "/proc/meminfo"
Expand Down
19 changes: 15 additions & 4 deletions test/cmd/unix/test_cmd_tail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"""
Testing of tail command.
"""
__author__ = 'Sylwester Golonka'
__copyright__ = 'Copyright (C) 2018, Nokia'
__email__ = 'sylwester.golonka@nokia.com'
__author__ = 'Sylwester Golonka, Marcin Usielski'
__copyright__ = 'Copyright (C) 2018-2020, Nokia'
__email__ = 'sylwester.golonka@nokia.com, marcin.usielski@nokia.com'

from moler.cmd.unix.tail import Tail
from moler.exceptions import CommandFailure
Expand All @@ -16,11 +16,22 @@ def test_tail_returns_proper_command_string(buffer_connection):
assert "tail /home/ute/test -n 10" == cmd.command_string


def test_tail_not_raise_exception(buffer_connection):
command_output = """
tail test.txt
some output with other info.
tail: cannot open test.txt for reading: No such file or directory
user@host:~$"""
buffer_connection.remote_inject_response([command_output])
cmd = Tail(connection=buffer_connection.moler_connection, path="test.txt")
cmd()


def test_tail_raise_exception(buffer_connection):
command_output = """
ute@debdev:~$ tail test.txt
tail: cannot open test.txt for reading: No such file or directory
ute@debdev:~$"""
user@host:~$"""
buffer_connection.remote_inject_response([command_output])
cmd = Tail(connection=buffer_connection.moler_connection, path="test.txt")
with pytest.raises(CommandFailure):
Expand Down

0 comments on commit df42741

Please sign in to comment.