예제 #1
0
 def testPidFileFunctions(self):
     pid_file = self.f_dpn("test")
     fd = utils.WritePidFile(self.f_dpn("test"))
     self.failUnless(os.path.exists(pid_file),
                     "PID file should have been created")
     read_pid = utils.ReadPidFile(pid_file)
     self.failUnlessEqual(read_pid, os.getpid())
     self.failUnless(utils.IsProcessAlive(read_pid))
     self.failUnlessRaises(errors.PidFileLockError, utils.WritePidFile,
                           self.f_dpn("test"))
     os.close(fd)
     utils.RemoveFile(self.f_dpn("test"))
     self.failIf(os.path.exists(pid_file),
                 "PID file should not exist anymore")
     self.failUnlessEqual(
         utils.ReadPidFile(pid_file), 0,
         "ReadPidFile should return 0 for missing pid file")
     fh = open(pid_file, "w")
     fh.write("blah\n")
     fh.close()
     self.failUnlessEqual(
         utils.ReadPidFile(pid_file), 0,
         "ReadPidFile should return 0 for invalid pid file")
     # but now, even with the file existing, we should be able to lock it
     fd = utils.WritePidFile(self.f_dpn("test"))
     os.close(fd)
     utils.RemoveFile(self.f_dpn("test"))
     self.failIf(os.path.exists(pid_file),
                 "PID file should not exist anymore")
예제 #2
0
    def testKill(self):
        pid_file = self.f_dpn("child")
        r_fd, w_fd = os.pipe()
        new_pid = os.fork()
        if new_pid == 0:  #child
            utils.WritePidFile(self.f_dpn("child"))
            os.write(w_fd, "a")
            signal.pause()
            os._exit(0)
            return
        # else we are in the parent
        # wait until the child has written the pid file
        os.read(r_fd, 1)
        read_pid = utils.ReadPidFile(pid_file)
        self.failUnlessEqual(read_pid, new_pid)
        self.failUnless(utils.IsProcessAlive(new_pid))

        # Try writing to locked file
        try:
            utils.WritePidFile(pid_file)
        except errors.PidFileLockError, err:
            errmsg = str(err)
            self.assertTrue(errmsg.endswith(" %s" % new_pid),
                            msg=("Error message ('%s') didn't contain correct"
                                 " PID (%s)" % (errmsg, new_pid)))