Revision d9f311d7 test/ganeti.utils_unittest.py

b/test/ganeti.utils_unittest.py
43 43
     ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \
44 44
     ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \
45 45
     SetEtcHostsEntry, RemoveEtcHostsEntry, FirstFree
46
from ganeti.errors import LockError, UnitParseError
46
from ganeti.errors import LockError, UnitParseError, GenericError
47 47

  
48 48
def _ChildHandler(signal, stack):
49 49
  global _ChildFlag
50 50
  _ChildFlag = True
51 51

  
52

  
52 53
class TestIsProcessAlive(unittest.TestCase):
53 54
  """Testing case for IsProcessAlive"""
54 55

  
......
99 100
    self.assert_(not IsProcessAlive(self.pid_non_existing),
100 101
                 "noexisting process detected")
101 102

  
103

  
102 104
class TestPidFileFunctions(unittest.TestCase):
103
  """Tests for WritePidFile, RemovePidFile and IsPidFileAlive"""
105
  """Tests for WritePidFile, RemovePidFile and ReadPidFile"""
104 106

  
105 107
  def setUp(self):
106 108
    self.dir = tempfile.mkdtemp()
......
108 110
    utils._DaemonPidFileName = self.f_dpn
109 111

  
110 112
  def testPidFileFunctions(self):
113
    pid_file = self.f_dpn('test')
111 114
    utils.WritePidFile('test')
112
    self.assert_(os.path.exists(self.f_dpn('test')))
113
    self.assert_(utils.IsPidFileAlive(self.f_dpn('test')))
115
    self.failUnless(os.path.exists(pid_file),
116
                    "PID file should have been created")
117
    read_pid = utils.ReadPidFile(pid_file)
118
    self.failUnlessEqual(read_pid, os.getpid())
119
    self.failUnless(utils.IsProcessAlive(read_pid))
120
    self.failUnlessRaises(GenericError, utils.WritePidFile, 'test')
121
    utils.RemovePidFile('test')
122
    self.failIf(os.path.exists(pid_file),
123
                "PID file should not exist anymore")
124
    self.failUnlessEqual(utils.ReadPidFile(pid_file), 0,
125
                         "ReadPidFile should return 0 for missing pid file")
126
    fh = open(pid_file, "w")
127
    fh.write("blah\n")
128
    fh.close()
129
    self.failUnlessEqual(utils.ReadPidFile(pid_file), 0,
130
                         "ReadPidFile should return 0 for invalid pid file")
114 131
    utils.RemovePidFile('test')
115
    self.assert_(not os.path.exists(self.f_dpn('test')))
132
    self.failIf(os.path.exists(pid_file),
133
                "PID file should not exist anymore")
116 134

  
117 135
  def tearDown(self):
136
    for name in os.listdir(self.dir):
137
      os.unlink(os.path.join(self.dir, name))
118 138
    os.rmdir(self.dir)
119 139

  
120 140

  

Also available in: Unified diff