4 # Copyright (C) 2010 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 utils.ResetTempfileModule"""
32 from ganeti import utils
37 # This constant is usually at a much higher value. Setting it lower for test
42 class TestResetTempfileModule(unittest.TestCase):
44 self.tmpdir = tempfile.mkdtemp()
47 shutil.rmtree(self.tmpdir)
49 def testNoReset(self):
55 def _Test(self, reset):
56 self.failIf(tempfile.TMP_MAX > 10)
58 # Initialize tempfile module
59 (fd, _) = tempfile.mkstemp(dir=self.tmpdir, prefix="init.", suffix="")
62 (notify_read, notify_write) = os.pipe()
70 utils.ResetTempfileModule()
72 os.close(notify_write)
74 # Wait for parent to close pipe
75 os.read(notify_read, 1)
78 # This is a short-lived process, not caring about closing file
80 (_, path) = tempfile.mkstemp(dir=self.tmpdir,
81 prefix="test.", suffix="")
82 except EnvironmentError, err:
83 if err.errno == errno.EEXIST:
84 # Couldnt' create temporary file (e.g. because we run out of
89 logging.debug("Child created %s", path)
93 logging.exception("Unhandled error")
100 # Create parent's temporary files
101 for _ in range(tempfile.TMP_MAX):
102 (fd, path) = tempfile.mkstemp(dir=self.tmpdir,
103 prefix="test.", suffix="")
105 logging.debug("Parent created %s", path)
107 # Notify child by closing pipe
108 os.close(notify_write)
110 (_, status) = os.waitpid(pid, 0)
112 self.failIf(os.WIFSIGNALED(status))
115 # If the tempfile module was reset, it should not fail to create
121 self.assertEqual(os.WEXITSTATUS(status), expected)
124 if __name__ == "__main__":
125 testutils.GanetiTestProgram()