Revision 17496050

b/Makefile.am
320 320
	test/data/proc_drbd83.txt
321 321

  
322 322
python_tests = \
323
	test/ganeti.asyncnotifier_unittest.py \
323 324
	test/ganeti.backend_unittest.py \
324 325
	test/ganeti.bdev_unittest.py \
325 326
	test/ganeti.cli_unittest.py \
b/test/ganeti.asyncnotifier_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2010 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for unittesting the asyncnotifier module"""
23

  
24
import unittest
25
import signal
26
import os
27

  
28
try:
29
  # pylint: disable-msg=E0611
30
  from pyinotify import pyinotify
31
except ImportError:
32
  import pyinotify
33

  
34
from ganeti import asyncnotifier
35
from ganeti import daemon
36
from ganeti import utils
37

  
38
import testutils
39

  
40

  
41
class TestSingleFileEventHandler(testutils.GanetiTestCase):
42
  """Test daemon.Mainloop"""
43

  
44
  def setUp(self):
45
    testutils.GanetiTestCase.setUp(self)
46
    self.mainloop = daemon.Mainloop()
47
    notifier_count = 2
48
    self.chk_files = [self._CreateTempFile() for i in range(notifier_count)]
49
    self.notified = [False for i in range(notifier_count)]
50
    # We need one watch manager per notifier, as those contain the file
51
    # descriptor which is monitored by asyncore
52
    self.wms = [pyinotify.WatchManager() for i in range(notifier_count)]
53
    self.cbk = [self.OnInotifyCallback(self.notified, i)
54
                  for i in range(notifier_count)]
55
    self.ihandler = [asyncnotifier.SingleFileEventHandler(self.wms[i],
56
                                                          self.cbk[i],
57
                                                          self.chk_files[i])
58
                      for i in range(notifier_count)]
59
    self.notifiers = [asyncnotifier.AsyncNotifier(self.wms[i],
60
                                                  self.ihandler[i])
61
                       for i in range(notifier_count)]
62
    # notifier 0 is enabled by default, as we use it to get out of the loop
63
    self.ihandler[0].enable()
64

  
65
  class OnInotifyCallback:
66
    def __init__(self, notified, i):
67
      self.notified = notified
68
      self.i = i
69

  
70
    def __call__(self, enabled):
71
      self.notified[self.i] = True
72
      # notifier 0 is special as we use it to terminate the mainloop
73
      if self.i == 0:
74
        os.kill(os.getpid(), signal.SIGTERM)
75

  
76
  def testReplace(self):
77
    utils.WriteFile(self.chk_files[0], data="dummy")
78
    self.mainloop.Run()
79
    self.assert_(self.notified[0])
80
    self.assert_(not self.notified[1])
81

  
82
  def testEnableDisable(self):
83
    self.ihandler[0].enable()
84
    self.ihandler[0].disable()
85
    self.ihandler[0].disable()
86
    self.ihandler[0].enable()
87
    self.ihandler[0].disable()
88
    self.ihandler[0].enable()
89
    utils.WriteFile(self.chk_files[0], data="dummy")
90
    self.mainloop.Run()
91
    self.assert_(self.notified[0])
92
    self.assert_(not self.notified[1])
93

  
94
  def testDoubleEnable(self):
95
    self.ihandler[0].enable()
96
    self.ihandler[0].enable()
97
    utils.WriteFile(self.chk_files[0], data="dummy")
98
    self.mainloop.Run()
99
    self.assert_(self.notified[0])
100
    self.assert_(not self.notified[1])
101

  
102
  def testDefaultDisabled(self):
103
    utils.WriteFile(self.chk_files[1], data="dummy")
104
    utils.WriteFile(self.chk_files[0], data="dummy")
105
    self.mainloop.Run()
106
    self.assert_(self.notified[0])
107
    # notifier 1 is disabled by default
108
    self.assert_(not self.notified[1])
109

  
110
  def testBothEnabled(self):
111
    self.ihandler[1].enable()
112
    utils.WriteFile(self.chk_files[1], data="dummy")
113
    utils.WriteFile(self.chk_files[0], data="dummy")
114
    self.mainloop.Run()
115
    self.assert_(self.notified[0])
116
    self.assert_(self.notified[1])
117

  
118

  
119
if __name__ == "__main__":
120
  testutils.GanetiTestProgram()

Also available in: Unified diff