4 # Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
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.
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.
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
22 """Script for testing ganeti.utils.wrapper"""
31 from ganeti import constants
32 from ganeti import utils
37 class TestSetCloseOnExecFlag(unittest.TestCase):
38 """Tests for SetCloseOnExecFlag"""
41 self.tmpfile = tempfile.TemporaryFile()
44 utils.SetCloseOnExecFlag(self.tmpfile.fileno(), True)
45 self.failUnless(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFD) &
48 def testDisable(self):
49 utils.SetCloseOnExecFlag(self.tmpfile.fileno(), False)
50 self.failIf(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFD) &
54 class TestSetNonblockFlag(unittest.TestCase):
56 self.tmpfile = tempfile.TemporaryFile()
59 utils.SetNonblockFlag(self.tmpfile.fileno(), True)
60 self.failUnless(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFL) &
63 def testDisable(self):
64 utils.SetNonblockFlag(self.tmpfile.fileno(), False)
65 self.failIf(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFL) &
69 class TestIgnoreProcessNotFound(unittest.TestCase):
72 os.write(fd, str(os.getpid()))
77 (pid_read_fd, pid_write_fd) = os.pipe()
79 # Start short-lived process which writes its PID to pipe
80 self.assert_(utils.RunInSeparateProcess(self._WritePid, pid_write_fd))
81 os.close(pid_write_fd)
84 pid = int(os.read(pid_read_fd, 1024))
87 # Try to send signal to process which exited recently
88 self.assertFalse(utils.IgnoreProcessNotFound(os.kill, pid, 0))
91 class TestIgnoreSignals(unittest.TestCase):
92 """Test the IgnoreSignals decorator"""
95 def _Raise(exception):
102 def testIgnoreSignals(self):
103 sock_err_intr = socket.error(errno.EINTR, "Message")
104 sock_err_inval = socket.error(errno.EINVAL, "Message")
106 env_err_intr = EnvironmentError(errno.EINTR, "Message")
107 env_err_inval = EnvironmentError(errno.EINVAL, "Message")
109 self.assertRaises(socket.error, self._Raise, sock_err_intr)
110 self.assertRaises(socket.error, self._Raise, sock_err_inval)
111 self.assertRaises(EnvironmentError, self._Raise, env_err_intr)
112 self.assertRaises(EnvironmentError, self._Raise, env_err_inval)
114 self.assertEquals(utils.IgnoreSignals(self._Raise, sock_err_intr), None)
115 self.assertEquals(utils.IgnoreSignals(self._Raise, env_err_intr), None)
116 self.assertRaises(socket.error, utils.IgnoreSignals, self._Raise,
118 self.assertRaises(EnvironmentError, utils.IgnoreSignals, self._Raise,
121 self.assertEquals(utils.IgnoreSignals(self._Return, True), True)
122 self.assertEquals(utils.IgnoreSignals(self._Return, 33), 33)
125 if __name__ == "__main__":
126 testutils.GanetiTestProgram()