Skip to content
Snippets Groups Projects
Commit 4fdedd09 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

jstore: Nicer error message on non-numeric file content


An error like “invalid literal for int() with base 10” can be quite
confusing.

Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 946e553b
No related branches found
No related tags found
No related merge requests found
......@@ -42,12 +42,19 @@ def _ReadNumericFile(file_name):
"""
try:
return int(utils.ReadFile(file_name))
contents = utils.ReadFile(file_name)
except EnvironmentError, err:
if err.errno in (errno.ENOENT, ):
return None
raise
try:
return int(contents)
except (ValueError, TypeError), err:
# Couldn't convert to int
raise errors.JobQueueError("Content of file '%s' is not numeric: %s" %
(file_name, err))
def ReadSerial():
"""Read the serial file.
......
......@@ -78,5 +78,24 @@ class TestParseJobId(testutils.GanetiTestCase):
self.assertRaises(errors.ParameterError, jstore.ParseJobId, [])
class TestReadNumericFile(testutils.GanetiTestCase):
def testNonExistingFile(self):
result = jstore._ReadNumericFile("/tmp/this/file/does/not/exist")
self.assertTrue(result is None)
def testValidFile(self):
tmpfile = self._CreateTempFile()
for (data, exp) in [("123", 123), ("0\n", 0)]:
utils.WriteFile(tmpfile, data=data)
result = jstore._ReadNumericFile(tmpfile)
self.assertEqual(result, exp)
def testInvalidContent(self):
tmpfile = self._CreateTempFile()
utils.WriteFile(tmpfile, data="{wrong content")
self.assertRaises(errors.JobQueueError, jstore._ReadNumericFile, tmpfile)
if __name__ == "__main__":
testutils.GanetiTestProgram()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment