Revision a01b500b test/ganeti.utils_unittest.py
b/test/ganeti.utils_unittest.py | ||
---|---|---|
79 | 79 |
"nonexisting process detected") |
80 | 80 |
|
81 | 81 |
|
82 |
class TestGetProcStatusPath(unittest.TestCase): |
|
83 |
def test(self): |
|
84 |
self.assert_("/1234/" in utils._GetProcStatusPath(1234)) |
|
85 |
self.assertNotEqual(utils._GetProcStatusPath(1), |
|
86 |
utils._GetProcStatusPath(2)) |
|
87 |
|
|
88 |
|
|
89 |
class TestIsProcessHandlingSignal(unittest.TestCase): |
|
90 |
def setUp(self): |
|
91 |
self.tmpdir = tempfile.mkdtemp() |
|
92 |
|
|
93 |
def tearDown(self): |
|
94 |
shutil.rmtree(self.tmpdir) |
|
95 |
|
|
96 |
def testParseSigsetT(self): |
|
97 |
self.assertEqual(len(utils._ParseSigsetT("0")), 0) |
|
98 |
self.assertEqual(utils._ParseSigsetT("1"), set([1])) |
|
99 |
self.assertEqual(utils._ParseSigsetT("1000a"), set([2, 4, 17])) |
|
100 |
self.assertEqual(utils._ParseSigsetT("810002"), set([2, 17, 24, ])) |
|
101 |
self.assertEqual(utils._ParseSigsetT("0000000180000202"), |
|
102 |
set([2, 10, 32, 33])) |
|
103 |
self.assertEqual(utils._ParseSigsetT("0000000180000002"), |
|
104 |
set([2, 32, 33])) |
|
105 |
self.assertEqual(utils._ParseSigsetT("0000000188000002"), |
|
106 |
set([2, 28, 32, 33])) |
|
107 |
self.assertEqual(utils._ParseSigsetT("000000004b813efb"), |
|
108 |
set([1, 2, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 17, |
|
109 |
24, 25, 26, 28, 31])) |
|
110 |
self.assertEqual(utils._ParseSigsetT("ffffff"), set(range(1, 25))) |
|
111 |
|
|
112 |
def testGetProcStatusField(self): |
|
113 |
for field in ["SigCgt", "Name", "FDSize"]: |
|
114 |
for value in ["", "0", "cat", " 1234 KB"]: |
|
115 |
pstatus = "\n".join([ |
|
116 |
"VmPeak: 999 kB", |
|
117 |
"%s: %s" % (field, value), |
|
118 |
"TracerPid: 0", |
|
119 |
]) |
|
120 |
result = utils._GetProcStatusField(pstatus, field) |
|
121 |
self.assertEqual(result, value.strip()) |
|
122 |
|
|
123 |
def test(self): |
|
124 |
sp = utils.PathJoin(self.tmpdir, "status") |
|
125 |
|
|
126 |
utils.WriteFile(sp, data="\n".join([ |
|
127 |
"Name: bash", |
|
128 |
"State: S (sleeping)", |
|
129 |
"SleepAVG: 98%", |
|
130 |
"Pid: 22250", |
|
131 |
"PPid: 10858", |
|
132 |
"TracerPid: 0", |
|
133 |
"SigBlk: 0000000000010000", |
|
134 |
"SigIgn: 0000000000384004", |
|
135 |
"SigCgt: 000000004b813efb", |
|
136 |
"CapEff: 0000000000000000", |
|
137 |
])) |
|
138 |
|
|
139 |
self.assert_(utils.IsProcessHandlingSignal(1234, 10, status_path=sp)) |
|
140 |
|
|
141 |
def testNoSigCgt(self): |
|
142 |
sp = utils.PathJoin(self.tmpdir, "status") |
|
143 |
|
|
144 |
utils.WriteFile(sp, data="\n".join([ |
|
145 |
"Name: bash", |
|
146 |
])) |
|
147 |
|
|
148 |
self.assertRaises(RuntimeError, utils.IsProcessHandlingSignal, |
|
149 |
1234, 10, status_path=sp) |
|
150 |
|
|
151 |
def testNoSuchFile(self): |
|
152 |
sp = utils.PathJoin(self.tmpdir, "notexist") |
|
153 |
|
|
154 |
self.assertFalse(utils.IsProcessHandlingSignal(1234, 10, status_path=sp)) |
|
155 |
|
|
156 |
@staticmethod |
|
157 |
def _TestRealProcess(): |
|
158 |
signal.signal(signal.SIGUSR1, signal.SIG_DFL) |
|
159 |
if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): |
|
160 |
raise Exception("SIGUSR1 is handled when it should not be") |
|
161 |
|
|
162 |
signal.signal(signal.SIGUSR1, lambda signum, frame: None) |
|
163 |
if not utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): |
|
164 |
raise Exception("SIGUSR1 is not handled when it should be") |
|
165 |
|
|
166 |
signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
|
167 |
if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): |
|
168 |
raise Exception("SIGUSR1 is not handled when it should be") |
|
169 |
|
|
170 |
signal.signal(signal.SIGUSR1, signal.SIG_DFL) |
|
171 |
if utils.IsProcessHandlingSignal(os.getpid(), signal.SIGUSR1): |
|
172 |
raise Exception("SIGUSR1 is handled when it should not be") |
|
173 |
|
|
174 |
return True |
|
175 |
|
|
176 |
def testRealProcess(self): |
|
177 |
self.assert_(utils.RunInSeparateProcess(self._TestRealProcess)) |
|
178 |
|
|
179 |
|
|
82 | 180 |
class TestPidFileFunctions(unittest.TestCase): |
83 | 181 |
"""Tests for WritePidFile, RemovePidFile and ReadPidFile""" |
84 | 182 |
|
Also available in: Unified diff