root / test / ganeti.asyncnotifier_unittest.py @ 49f986e7
History | View | Annotate | Download (4.4 kB)
1 | 17496050 | Guido Trotter | #!/usr/bin/python
|
---|---|---|---|
2 | 17496050 | Guido Trotter | #
|
3 | 17496050 | Guido Trotter | |
4 | 17496050 | Guido Trotter | # Copyright (C) 2010 Google Inc.
|
5 | 17496050 | Guido Trotter | #
|
6 | 17496050 | Guido Trotter | # This program is free software; you can redistribute it and/or modify
|
7 | 17496050 | Guido Trotter | # it under the terms of the GNU General Public License as published by
|
8 | 17496050 | Guido Trotter | # the Free Software Foundation; either version 2 of the License, or
|
9 | 17496050 | Guido Trotter | # (at your option) any later version.
|
10 | 17496050 | Guido Trotter | #
|
11 | 17496050 | Guido Trotter | # This program is distributed in the hope that it will be useful, but
|
12 | 17496050 | Guido Trotter | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 17496050 | Guido Trotter | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 17496050 | Guido Trotter | # General Public License for more details.
|
15 | 17496050 | Guido Trotter | #
|
16 | 17496050 | Guido Trotter | # You should have received a copy of the GNU General Public License
|
17 | 17496050 | Guido Trotter | # along with this program; if not, write to the Free Software
|
18 | 17496050 | Guido Trotter | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 17496050 | Guido Trotter | # 02110-1301, USA.
|
20 | 17496050 | Guido Trotter | |
21 | 17496050 | Guido Trotter | |
22 | 17496050 | Guido Trotter | """Script for unittesting the asyncnotifier module"""
|
23 | 17496050 | Guido Trotter | |
24 | 17496050 | Guido Trotter | import unittest |
25 | 17496050 | Guido Trotter | import signal |
26 | 17496050 | Guido Trotter | import os |
27 | 17496050 | Guido Trotter | |
28 | 17496050 | Guido Trotter | try:
|
29 | 17496050 | Guido Trotter | # pylint: disable-msg=E0611
|
30 | 17496050 | Guido Trotter | from pyinotify import pyinotify |
31 | 17496050 | Guido Trotter | except ImportError: |
32 | 17496050 | Guido Trotter | import pyinotify |
33 | 17496050 | Guido Trotter | |
34 | 17496050 | Guido Trotter | from ganeti import asyncnotifier |
35 | 17496050 | Guido Trotter | from ganeti import daemon |
36 | 17496050 | Guido Trotter | from ganeti import utils |
37 | 17496050 | Guido Trotter | |
38 | 17496050 | Guido Trotter | import testutils |
39 | 17496050 | Guido Trotter | |
40 | 17496050 | Guido Trotter | |
41 | 17496050 | Guido Trotter | class TestSingleFileEventHandler(testutils.GanetiTestCase): |
42 | 17496050 | Guido Trotter | """Test daemon.Mainloop"""
|
43 | 17496050 | Guido Trotter | |
44 | 49f986e7 | Guido Trotter | NOTIFIERS = [NOTIFIER_TERM, NOTIFIER_NORM] = range(2) |
45 | 49f986e7 | Guido Trotter | |
46 | 17496050 | Guido Trotter | def setUp(self): |
47 | 17496050 | Guido Trotter | testutils.GanetiTestCase.setUp(self)
|
48 | 17496050 | Guido Trotter | self.mainloop = daemon.Mainloop()
|
49 | 49f986e7 | Guido Trotter | self.chk_files = [self._CreateTempFile() for i in self.NOTIFIERS] |
50 | 49f986e7 | Guido Trotter | self.notified = [False for i in self.NOTIFIERS] |
51 | 17496050 | Guido Trotter | # We need one watch manager per notifier, as those contain the file
|
52 | 17496050 | Guido Trotter | # descriptor which is monitored by asyncore
|
53 | 49f986e7 | Guido Trotter | self.wms = [pyinotify.WatchManager() for i in self.NOTIFIERS] |
54 | 49f986e7 | Guido Trotter | self.cbk = [self.OnInotifyCallback(self, i) |
55 | 49f986e7 | Guido Trotter | for i in range(len(self.NOTIFIERS))] |
56 | 17496050 | Guido Trotter | self.ihandler = [asyncnotifier.SingleFileEventHandler(self.wms[i], |
57 | 17496050 | Guido Trotter | self.cbk[i],
|
58 | 17496050 | Guido Trotter | self.chk_files[i])
|
59 | 49f986e7 | Guido Trotter | for i in range(len(self.NOTIFIERS))] |
60 | 17496050 | Guido Trotter | self.notifiers = [asyncnotifier.AsyncNotifier(self.wms[i], |
61 | 17496050 | Guido Trotter | self.ihandler[i])
|
62 | 49f986e7 | Guido Trotter | for i in range(len(self.NOTIFIERS))] |
63 | 49f986e7 | Guido Trotter | # TERM notifier is enabled by default, as we use it to get out of the loop
|
64 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].enable() |
65 | 17496050 | Guido Trotter | |
66 | 17496050 | Guido Trotter | class OnInotifyCallback: |
67 | 49f986e7 | Guido Trotter | def __init__(self, testobj, i): |
68 | 49f986e7 | Guido Trotter | self.testobj = testobj
|
69 | 49f986e7 | Guido Trotter | self.notified = testobj.notified
|
70 | 17496050 | Guido Trotter | self.i = i
|
71 | 17496050 | Guido Trotter | |
72 | 17496050 | Guido Trotter | def __call__(self, enabled): |
73 | 17496050 | Guido Trotter | self.notified[self.i] = True |
74 | 49f986e7 | Guido Trotter | if self.i == self.testobj.NOTIFIER_TERM: |
75 | 17496050 | Guido Trotter | os.kill(os.getpid(), signal.SIGTERM) |
76 | 17496050 | Guido Trotter | |
77 | 17496050 | Guido Trotter | def testReplace(self): |
78 | 49f986e7 | Guido Trotter | utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
79 | 17496050 | Guido Trotter | self.mainloop.Run()
|
80 | 49f986e7 | Guido Trotter | self.assert_(self.notified[self.NOTIFIER_TERM]) |
81 | 49f986e7 | Guido Trotter | self.assert_(not self.notified[self.NOTIFIER_NORM]) |
82 | 17496050 | Guido Trotter | |
83 | 17496050 | Guido Trotter | def testEnableDisable(self): |
84 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].enable() |
85 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].disable() |
86 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].disable() |
87 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].enable() |
88 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].disable() |
89 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].enable() |
90 | 49f986e7 | Guido Trotter | utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
91 | 17496050 | Guido Trotter | self.mainloop.Run()
|
92 | 49f986e7 | Guido Trotter | self.assert_(self.notified[self.NOTIFIER_TERM]) |
93 | 49f986e7 | Guido Trotter | self.assert_(not self.notified[self.NOTIFIER_NORM]) |
94 | 17496050 | Guido Trotter | |
95 | 17496050 | Guido Trotter | def testDoubleEnable(self): |
96 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].enable() |
97 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_TERM].enable() |
98 | 49f986e7 | Guido Trotter | utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
99 | 17496050 | Guido Trotter | self.mainloop.Run()
|
100 | 49f986e7 | Guido Trotter | self.assert_(self.notified[self.NOTIFIER_TERM]) |
101 | 49f986e7 | Guido Trotter | self.assert_(not self.notified[self.NOTIFIER_NORM]) |
102 | 17496050 | Guido Trotter | |
103 | 17496050 | Guido Trotter | def testDefaultDisabled(self): |
104 | 49f986e7 | Guido Trotter | utils.WriteFile(self.chk_files[self.NOTIFIER_NORM], data="dummy") |
105 | 49f986e7 | Guido Trotter | utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
106 | 17496050 | Guido Trotter | self.mainloop.Run()
|
107 | 49f986e7 | Guido Trotter | self.assert_(self.notified[self.NOTIFIER_TERM]) |
108 | 49f986e7 | Guido Trotter | # NORM notifier is disabled by default
|
109 | 49f986e7 | Guido Trotter | self.assert_(not self.notified[self.NOTIFIER_NORM]) |
110 | 17496050 | Guido Trotter | |
111 | 17496050 | Guido Trotter | def testBothEnabled(self): |
112 | 49f986e7 | Guido Trotter | self.ihandler[self.NOTIFIER_NORM].enable() |
113 | 49f986e7 | Guido Trotter | utils.WriteFile(self.chk_files[self.NOTIFIER_NORM], data="dummy") |
114 | 49f986e7 | Guido Trotter | utils.WriteFile(self.chk_files[self.NOTIFIER_TERM], data="dummy") |
115 | 17496050 | Guido Trotter | self.mainloop.Run()
|
116 | 49f986e7 | Guido Trotter | self.assert_(self.notified[self.NOTIFIER_TERM]) |
117 | 49f986e7 | Guido Trotter | self.assert_(self.notified[self.NOTIFIER_NORM]) |
118 | 17496050 | Guido Trotter | |
119 | 17496050 | Guido Trotter | |
120 | 17496050 | Guido Trotter | if __name__ == "__main__": |
121 | 17496050 | Guido Trotter | testutils.GanetiTestProgram() |