Revision 740c5aab

b/test/ganeti.utils_unittest.py
28 28
import os.path
29 29
import os
30 30
import md5
31
import signal
31 32
import socket
32 33
import shutil
33 34
import re
......
43 44
     SetEtcHostsEntry, RemoveEtcHostsEntry
44 45
from ganeti.errors import LockError, UnitParseError
45 46

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

  
47 51
class TestIsProcessAlive(unittest.TestCase):
48 52
  """Testing case for IsProcessAlive"""
53

  
49 54
  def setUp(self):
50
    # create a zombie and a (hopefully) non-existing process id
51
    self.pid_zombie = os.fork()
52
    if self.pid_zombie == 0:
53
      os._exit(0)
54
    elif self.pid_zombie < 0:
55
      raise SystemError("can't fork")
55
    global _ChildFlag
56
    # create a (most probably) non-existing process-id
56 57
    self.pid_non_existing = os.fork()
57 58
    if self.pid_non_existing == 0:
58 59
      os._exit(0)
......
60 61
      os.waitpid(self.pid_non_existing, 0)
61 62
    else:
62 63
      raise SystemError("can't fork")
64
    _ChildFlag = False
65
    # Use _ChildHandler for SIGCHLD
66
    self.chldOrig = signal.signal(signal.SIGCHLD, _ChildHandler)
67
    # create a zombie
68
    self.pid_zombie = os.fork()
69
    if self.pid_zombie == 0:
70
      os._exit(0)
71
    elif self.pid_zombie < 0:
72
      raise SystemError("can't fork")
63 73

  
74
  def tearDown(self):
75
    signal.signal(signal.SIGCHLD, self.chldOrig)
64 76

  
65 77
  def testExists(self):
66 78
    mypid = os.getpid()
......
68 80
                 "can't find myself running")
69 81

  
70 82
  def testZombie(self):
83
    global _ChildFlag
84
    timeout = 10
85

  
86
    while not _ChildFlag:
87
      if timeout >= 0:
88
        time.sleep(0.2)
89
        timeout -= 0.2
90
      else:
91
        self.fail("timed out waiting for child's signal")
92
        break # not executed...
93

  
71 94
    self.assert_(not IsProcessAlive(self.pid_zombie),
72 95
                 "zombie not detected as zombie")
73 96

  
74

  
75 97
  def testNotExisting(self):
76 98
    self.assert_(not IsProcessAlive(self.pid_non_existing),
77 99
                 "noexisting process detected")

Also available in: Unified diff