Revision 4fdedd09

b/lib/jstore.py
42 42

  
43 43
  """
44 44
  try:
45
    return int(utils.ReadFile(file_name))
45
    contents = utils.ReadFile(file_name)
46 46
  except EnvironmentError, err:
47 47
    if err.errno in (errno.ENOENT, ):
48 48
      return None
49 49
    raise
50 50

  
51
  try:
52
    return int(contents)
53
  except (ValueError, TypeError), err:
54
    # Couldn't convert to int
55
    raise errors.JobQueueError("Content of file '%s' is not numeric: %s" %
56
                               (file_name, err))
57

  
51 58

  
52 59
def ReadSerial():
53 60
  """Read the serial file.
b/test/ganeti.jstore_unittest.py
78 78
    self.assertRaises(errors.ParameterError, jstore.ParseJobId, [])
79 79

  
80 80

  
81
class TestReadNumericFile(testutils.GanetiTestCase):
82
  def testNonExistingFile(self):
83
    result = jstore._ReadNumericFile("/tmp/this/file/does/not/exist")
84
    self.assertTrue(result is None)
85

  
86
  def testValidFile(self):
87
    tmpfile = self._CreateTempFile()
88

  
89
    for (data, exp) in [("123", 123), ("0\n", 0)]:
90
      utils.WriteFile(tmpfile, data=data)
91
      result = jstore._ReadNumericFile(tmpfile)
92
      self.assertEqual(result, exp)
93

  
94
  def testInvalidContent(self):
95
    tmpfile = self._CreateTempFile()
96
    utils.WriteFile(tmpfile, data="{wrong content")
97
    self.assertRaises(errors.JobQueueError, jstore._ReadNumericFile, tmpfile)
98

  
99

  
81 100
if __name__ == "__main__":
82 101
  testutils.GanetiTestProgram()

Also available in: Unified diff