Skip to content

Commit

Permalink
Transports: fix ok_codes getter for empty list
Browse files Browse the repository at this point in the history
Bug: T167394
Change-Id: Ifaac41881c7c34e88d0240ac207525c9d8879604
  • Loading branch information
volans- committed Jun 8, 2017
1 parent 49edf36 commit fad5209
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 11 additions & 2 deletions cumin/tests/unit/transports/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ def test_timeout_setter(self):

def test_ok_codes_getter(self):
"""Should return the ok_codes set, [0] otherwise."""
# Test empty list
command = transports.Command('command1')
command.ok_codes = []
self.assertListEqual(command.ok_codes, [])

for command in self.commands:
self.assertListEqual(command['obj'].ok_codes, command.get('ok_codes', [0]))

Expand All @@ -168,16 +173,20 @@ def test_ok_codes_setter(self):
command.ok_codes = None
self.assertIsNone(command._ok_codes)

command.ok_codes = []
self.assertListEqual(command._ok_codes, [])

with self.assertRaisesRegexp(transports.WorkerError, r'ok_codes must be a list or None'):
command.ok_codes = 'invalid_value'

message_regex = r'must be a list of integers in the range'
for i in (-1, 0.0, 100.0, 256, 'invalid_value'):
codes = [i]
with self.assertRaisesRegexp(transports.WorkerError, r'must be a list of integers in the range'):
with self.assertRaisesRegexp(transports.WorkerError, message_regex):
command.ok_codes = codes

codes.insert(0, 0)
with self.assertRaisesRegexp(transports.WorkerError, r'must be a list of integers in the range'):
with self.assertRaisesRegexp(transports.WorkerError, message_regex):
command.ok_codes = codes


Expand Down
12 changes: 8 additions & 4 deletions cumin/transports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,20 @@ def timeout(self, value):
@property
def ok_codes(self):
"""Getter for the command's ok_codes property, return a list with only the element 0 if not set."""
return self._ok_codes or [0]
ok_codes = self._ok_codes
if ok_codes is None:
ok_codes = [0]

return ok_codes

@ok_codes.setter
def ok_codes(self, value):
"""Setter for the command's ok_codes property with validation, raise WorkerError if not valid.
Arguments:
value -- the command's list of exit codes to be considered successful for the execution. Must be a list integers
in the range 0-255 or None to unset it. The exit code 0 is considered successful by default, it can be
overriden setting this property.
value -- the command's list of exit codes to be considered successful for the execution. Must be a list of
integers in the range 0-255 or None to unset it. The exit code 0 is considered successful by default,
but it can be overriden setting this property. An empty list is also accepted.
"""
if value is None:
self._ok_codes = value
Expand Down

0 comments on commit fad5209

Please sign in to comment.