root / test / ganeti.asyncnotifier_unittest.py @ e9c8deab
History | View | Annotate | Download (4.4 kB)
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 |
NOTIFIERS = [NOTIFIER_TERM, NOTIFIER_NORM] = range(2) |
45 |
|
46 |
def setUp(self): |
47 |
testutils.GanetiTestCase.setUp(self)
|
48 |
self.mainloop = daemon.Mainloop()
|
49 |
self.chk_files = [self._CreateTempFile() for i in self.NOTIFIERS] |
50 |
self.notified = [False for i in self.NOTIFIERS] |
51 |
# We need one watch manager per notifier, as those contain the file
|
52 |
# descriptor which is monitored by asyncore
|
53 |
self.wms = [pyinotify.WatchManager() for i in self.NOTIFIERS] |
54 |
self.cbk = [self.OnInotifyCallback(self, i) |
55 |
for i in range(len(self.NOTIFIERS))] |
56 |
self.ihandler = [asyncnotifier.SingleFileEventHandler(self.wms[i], |
57 |
self.cbk[i],
|
58 |
self.chk_files[i])
|
59 |
for i in range(len(self.NOTIFIERS))] |
60 |
self.notifiers = [asyncnotifier.ErrorLoggingAsyncNotifier(self.wms[i], |
61 |
self.ihandler[i])
|
62 |
for i in range(len(self.NOTIFIERS))] |
63 |
# TERM notifier is enabled by default, as we use it to get out of the loop
|
64 |
self.ihandler[self.NOTIFIER_TERM].enable() |
65 |
|
66 |
class OnInotifyCallback: |
67 |
def __init__(self, testobj, i): |
68 |
self.testobj = testobj
|
69 |
self.notified = testobj.notified
|
70 |
self.i = i
|
71 |
|
72 |
def __call__(self, enabled): |
73 |
self.notified[self.i] = True |
74 |
if self.i == self.testobj.NOTIFIER_TERM: |
75 |
os.kill(os.getpid(), signal.SIGTERM) |
76 |
|
77 |
def testReplace(self): |
78 |
utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
79 |
self.mainloop.Run()
|
80 |
self.assert_(self.notified[self.NOTIFIER_TERM]) |
81 |
self.assert_(not self.notified[self.NOTIFIER_NORM]) |
82 |
|
83 |
def testEnableDisable(self): |
84 |
self.ihandler[self.NOTIFIER_TERM].enable() |
85 |
self.ihandler[self.NOTIFIER_TERM].disable() |
86 |
self.ihandler[self.NOTIFIER_TERM].disable() |
87 |
self.ihandler[self.NOTIFIER_TERM].enable() |
88 |
self.ihandler[self.NOTIFIER_TERM].disable() |
89 |
self.ihandler[self.NOTIFIER_TERM].enable() |
90 |
utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
91 |
self.mainloop.Run()
|
92 |
self.assert_(self.notified[self.NOTIFIER_TERM]) |
93 |
self.assert_(not self.notified[self.NOTIFIER_NORM]) |
94 |
|
95 |
def testDoubleEnable(self): |
96 |
self.ihandler[self.NOTIFIER_TERM].enable() |
97 |
self.ihandler[self.NOTIFIER_TERM].enable() |
98 |
utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
99 |
self.mainloop.Run()
|
100 |
self.assert_(self.notified[self.NOTIFIER_TERM]) |
101 |
self.assert_(not self.notified[self.NOTIFIER_NORM]) |
102 |
|
103 |
def testDefaultDisabled(self): |
104 |
utils.WriteFile(self.chk_files[self.NOTIFIER_NORM], data="dummy") |
105 |
utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
106 |
self.mainloop.Run()
|
107 |
self.assert_(self.notified[self.NOTIFIER_TERM]) |
108 |
# NORM notifier is disabled by default
|
109 |
self.assert_(not self.notified[self.NOTIFIER_NORM]) |
110 |
|
111 |
def testBothEnabled(self): |
112 |
self.ihandler[self.NOTIFIER_NORM].enable() |
113 |
utils.WriteFile(self.chk_files[self.NOTIFIER_NORM], data="dummy") |
114 |
utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
115 |
self.mainloop.Run()
|
116 |
self.assert_(self.notified[self.NOTIFIER_TERM]) |
117 |
self.assert_(self.notified[self.NOTIFIER_NORM]) |
118 |
|
119 |
|
120 |
if __name__ == "__main__": |
121 |
testutils.GanetiTestProgram() |