1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#! /usr/bin/python2.4
# Copyright 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
__author__ = "Nils Klarlund"
import os
import os.path
import tempfile
import unittest
import basics
class BasicsTest(unittest.TestCase):
def setUp(self):
basics.opt_debug_pattern = 1
def tearDown(self):
pass
def test_ClientRootKeeper(self):
os.environ['DISTCC_CLIENT_TMP'] = 'to/be'
self.assertRaises(SystemExit, basics.ClientRootKeeper)
os.environ['DISTCC_CLIENT_TMP'] = '/to/be/or'
self.assertRaises(SystemExit, basics.ClientRootKeeper)
try:
tempfile_mkdtemp = tempfile.mkdtemp
os_makedirs = os.makedirs
def Mock_tempfile_mkdtemp(pat, dir):
self.assert_((pat, dir)
in
[('.%s-%s-%d' %
(basics.ClientRootKeeper.INCLUDE_SERVER_NAME,
os.getpid(), generation),
prefix)
for generation, prefix in
[(1,'/to/be'), (2, '/to')]])
return (dir == '/to/be' and '/to/be/xxxxxx'
or dir == '/to' and '/to/xxxxxxx')
def Mock_os_makedirs(f, *unused_args):
if not f.startswith('/to/'):
raise Exception, f
tempfile.mkdtemp = Mock_tempfile_mkdtemp
os.makedirs = Mock_os_makedirs
os.environ['DISTCC_CLIENT_TMP'] = '/to/be'
client_root_keeper = basics.ClientRootKeeper()
client_root_keeper.ClientRootMakedir(1)
self.assertEqual(os.path.dirname(client_root_keeper.client_root),
"/to/be")
os.environ['DISTCC_CLIENT_TMP'] = '/to'
client_root_keeper = basics.ClientRootKeeper()
client_root_keeper.ClientRootMakedir(2)
self.assertEqual(os.path.dirname(
os.path.dirname(client_root_keeper.client_root)), "/to")
self.assertEqual(os.path.basename(client_root_keeper.client_root),
"padding")
self.assertEqual(len(
[ None for ch in client_root_keeper.client_root if ch == '/' ]), 3)
finally:
tempfile.mkdtemp = tempfile_mkdtemp
os.makedirs = os_makedirs
def test_ClientRootKeeper_Deletions(self):
"""Test whether directories emerge and go away appropriately."""
# Test with a one-level value of DISTCC_CLIENT_TMP.
os.environ['DISTCC_CLIENT_TMP'] = '/tmp'
client_root_keeper = basics.ClientRootKeeper()
client_root_keeper.ClientRootMakedir(117)
self.assert_(os.path.isdir(client_root_keeper._client_root_before_padding))
self.assert_(os.path.isdir(client_root_keeper.client_root))
self.assert_(client_root_keeper.client_root.endswith('/padding'))
client_root_keeper.ClientRootMakedir(118)
client_root_keeper.CleanOutClientRoots()
# Directories must be gone now!
self.assert_(not os.path.isdir(
client_root_keeper._client_root_before_padding))
# Test with a two-level value of DISTCC_CLIENT_TMP.
try:
os.environ['DISTCC_CLIENT_TMP'] = tempfile.mkdtemp('basics_test',
dir='/tmp')
client_root_keeper = basics.ClientRootKeeper()
client_root_keeper.ClientRootMakedir(117)
self.assert_(os.path.isdir(
client_root_keeper._client_root_before_padding))
self.assert_(os.path.isdir(client_root_keeper.client_root))
client_root_keeper.ClientRootMakedir(118)
client_root_keeper.CleanOutClientRoots()
self.assert_(os.path.isdir,
client_root_keeper._client_root_before_padding)
finally:
os.rmdir(os.environ['DISTCC_CLIENT_TMP'])
unittest.main()
|