root / test / tempfile_fork_unittest.py @ f8c9fa5c
History | View | Annotate | Download (3.1 kB)
1 | 055f822b | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 055f822b | Michael Hanselmann | #
|
3 | 055f822b | Michael Hanselmann | |
4 | 055f822b | Michael Hanselmann | # Copyright (C) 2010 Google Inc.
|
5 | 055f822b | Michael Hanselmann | #
|
6 | 055f822b | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 055f822b | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 055f822b | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 055f822b | Michael Hanselmann | # (at your option) any later version.
|
10 | 055f822b | Michael Hanselmann | #
|
11 | 055f822b | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 055f822b | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 055f822b | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 055f822b | Michael Hanselmann | # General Public License for more details.
|
15 | 055f822b | Michael Hanselmann | #
|
16 | 055f822b | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 055f822b | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 055f822b | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 055f822b | Michael Hanselmann | # 02110-1301, USA.
|
20 | 055f822b | Michael Hanselmann | |
21 | 055f822b | Michael Hanselmann | |
22 | 055f822b | Michael Hanselmann | """Script for testing utils.ResetTempfileModule"""
|
23 | 055f822b | Michael Hanselmann | |
24 | 055f822b | Michael Hanselmann | import os |
25 | 055f822b | Michael Hanselmann | import sys |
26 | 055f822b | Michael Hanselmann | import errno |
27 | 055f822b | Michael Hanselmann | import shutil |
28 | 055f822b | Michael Hanselmann | import tempfile |
29 | 055f822b | Michael Hanselmann | import unittest |
30 | 055f822b | Michael Hanselmann | import logging |
31 | 055f822b | Michael Hanselmann | |
32 | 055f822b | Michael Hanselmann | from ganeti import utils |
33 | 055f822b | Michael Hanselmann | |
34 | 055f822b | Michael Hanselmann | import testutils |
35 | 055f822b | Michael Hanselmann | |
36 | 055f822b | Michael Hanselmann | |
37 | 055f822b | Michael Hanselmann | # This constant is usually at a much higher value. Setting it lower for test
|
38 | 055f822b | Michael Hanselmann | # purposes.
|
39 | 055f822b | Michael Hanselmann | tempfile.TMP_MAX = 3
|
40 | 055f822b | Michael Hanselmann | |
41 | 055f822b | Michael Hanselmann | |
42 | 055f822b | Michael Hanselmann | class TestResetTempfileModule(unittest.TestCase): |
43 | 055f822b | Michael Hanselmann | def setUp(self): |
44 | 055f822b | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
45 | 055f822b | Michael Hanselmann | |
46 | 055f822b | Michael Hanselmann | def tearDown(self): |
47 | 055f822b | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
48 | 055f822b | Michael Hanselmann | |
49 | 055f822b | Michael Hanselmann | def testNoReset(self): |
50 | 055f822b | Michael Hanselmann | self._Test(False) |
51 | 055f822b | Michael Hanselmann | |
52 | 055f822b | Michael Hanselmann | def testReset(self): |
53 | 055f822b | Michael Hanselmann | self._Test(True) |
54 | 055f822b | Michael Hanselmann | |
55 | 055f822b | Michael Hanselmann | def _Test(self, reset): |
56 | 055f822b | Michael Hanselmann | self.failIf(tempfile.TMP_MAX > 10) |
57 | 055f822b | Michael Hanselmann | |
58 | 055f822b | Michael Hanselmann | # Initialize tempfile module
|
59 | 055f822b | Michael Hanselmann | (fd, _) = tempfile.mkstemp(dir=self.tmpdir, prefix="init.", suffix="") |
60 | 055f822b | Michael Hanselmann | os.close(fd) |
61 | 055f822b | Michael Hanselmann | |
62 | 055f822b | Michael Hanselmann | (notify_read, notify_write) = os.pipe() |
63 | 055f822b | Michael Hanselmann | |
64 | 055f822b | Michael Hanselmann | pid = os.fork() |
65 | 055f822b | Michael Hanselmann | if pid == 0: |
66 | 055f822b | Michael Hanselmann | # Child
|
67 | 055f822b | Michael Hanselmann | try:
|
68 | 055f822b | Michael Hanselmann | try:
|
69 | 055f822b | Michael Hanselmann | if reset:
|
70 | 055f822b | Michael Hanselmann | utils.ResetTempfileModule() |
71 | 055f822b | Michael Hanselmann | |
72 | 055f822b | Michael Hanselmann | os.close(notify_write) |
73 | 055f822b | Michael Hanselmann | |
74 | 055f822b | Michael Hanselmann | # Wait for parent to close pipe
|
75 | 055f822b | Michael Hanselmann | os.read(notify_read, 1)
|
76 | 055f822b | Michael Hanselmann | |
77 | 055f822b | Michael Hanselmann | try:
|
78 | 055f822b | Michael Hanselmann | # This is a short-lived process, not caring about closing file
|
79 | 055f822b | Michael Hanselmann | # descriptors
|
80 | 055f822b | Michael Hanselmann | (_, path) = tempfile.mkstemp(dir=self.tmpdir,
|
81 | 055f822b | Michael Hanselmann | prefix="test.", suffix="") |
82 | 055f822b | Michael Hanselmann | except EnvironmentError, err: |
83 | 055f822b | Michael Hanselmann | if err.errno == errno.EEXIST:
|
84 | 055f822b | Michael Hanselmann | # Couldnt' create temporary file (e.g. because we run out of
|
85 | 055f822b | Michael Hanselmann | # retries)
|
86 | 055f822b | Michael Hanselmann | os._exit(2)
|
87 | 055f822b | Michael Hanselmann | raise
|
88 | 055f822b | Michael Hanselmann | |
89 | 055f822b | Michael Hanselmann | logging.debug("Child created %s", path)
|
90 | 055f822b | Michael Hanselmann | |
91 | 055f822b | Michael Hanselmann | os._exit(0)
|
92 | 055f822b | Michael Hanselmann | except Exception: |
93 | 055f822b | Michael Hanselmann | logging.exception("Unhandled error")
|
94 | 055f822b | Michael Hanselmann | finally:
|
95 | 055f822b | Michael Hanselmann | os._exit(1)
|
96 | 055f822b | Michael Hanselmann | |
97 | 055f822b | Michael Hanselmann | # Parent
|
98 | 055f822b | Michael Hanselmann | os.close(notify_read) |
99 | 055f822b | Michael Hanselmann | |
100 | 055f822b | Michael Hanselmann | # Create parent's temporary files
|
101 | 055f822b | Michael Hanselmann | for _ in range(tempfile.TMP_MAX): |
102 | 055f822b | Michael Hanselmann | (fd, path) = tempfile.mkstemp(dir=self.tmpdir,
|
103 | 055f822b | Michael Hanselmann | prefix="test.", suffix="") |
104 | 055f822b | Michael Hanselmann | os.close(fd) |
105 | 055f822b | Michael Hanselmann | logging.debug("Parent created %s", path)
|
106 | 055f822b | Michael Hanselmann | |
107 | 055f822b | Michael Hanselmann | # Notify child by closing pipe
|
108 | 055f822b | Michael Hanselmann | os.close(notify_write) |
109 | 055f822b | Michael Hanselmann | |
110 | 055f822b | Michael Hanselmann | (_, status) = os.waitpid(pid, 0)
|
111 | 055f822b | Michael Hanselmann | |
112 | 055f822b | Michael Hanselmann | self.failIf(os.WIFSIGNALED(status))
|
113 | 055f822b | Michael Hanselmann | |
114 | 055f822b | Michael Hanselmann | if reset:
|
115 | 055f822b | Michael Hanselmann | # If the tempfile module was reset, it should not fail to create
|
116 | 055f822b | Michael Hanselmann | # temporary files
|
117 | 055f822b | Michael Hanselmann | expected = 0
|
118 | 055f822b | Michael Hanselmann | else:
|
119 | 055f822b | Michael Hanselmann | expected = 2
|
120 | 055f822b | Michael Hanselmann | |
121 | 055f822b | Michael Hanselmann | self.assertEqual(os.WEXITSTATUS(status), expected)
|
122 | 055f822b | Michael Hanselmann | |
123 | 055f822b | Michael Hanselmann | |
124 | 055f822b | Michael Hanselmann | if __name__ == "__main__": |
125 | 055f822b | Michael Hanselmann | testutils.GanetiTestProgram() |