Revision 73027ed2

b/lib/utils.py
273 273
  return status
274 274

  
275 275

  
276
def SetCloseOnExecFlag(fd, enable):
277
  """Sets or unsets the close-on-exec flag on a file descriptor.
278

  
279
  @type fd: int
280
  @param fd: File descriptor
281
  @type enable: bool
282
  @param enable: Whether to set or unset it.
283

  
284
  """
285
  flags = fcntl.fcntl(fd, fcntl.F_GETFD)
286

  
287
  if enable:
288
    flags |= fcntl.FD_CLOEXEC
289
  else:
290
    flags &= ~fcntl.FD_CLOEXEC
291

  
292
  fcntl.fcntl(fd, fcntl.F_SETFD, flags)
293

  
294

  
276 295
def RetryOnSignal(fn, *args, **kwargs):
277 296
  """Calls a function again if it failed due to EINTR.
278 297

  
b/test/ganeti.utils_unittest.py
34 34
import re
35 35
import select
36 36
import string
37
import fcntl
37 38

  
38 39
import ganeti
39 40
import testutils
......
232 233
    self.failUnlessEqual(RunCmd(["pwd"], cwd=cwd).stdout.strip(), cwd)
233 234

  
234 235

  
236
class TestSetCloseOnExecFlag(unittest.TestCase):
237
  """Tests for SetCloseOnExecFlag"""
238

  
239
  def setUp(self):
240
    self.tmpfile = tempfile.TemporaryFile()
241

  
242
  def testEnable(self):
243
    utils.SetCloseOnExecFlag(self.tmpfile.fileno(), True)
244
    self.failUnless(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFD) &
245
                    fcntl.FD_CLOEXEC)
246

  
247
  def testDisable(self):
248
    utils.SetCloseOnExecFlag(self.tmpfile.fileno(), False)
249
    self.failIf(fcntl.fcntl(self.tmpfile.fileno(), fcntl.F_GETFD) &
250
                fcntl.FD_CLOEXEC)
251

  
252

  
235 253
class TestRemoveFile(unittest.TestCase):
236 254
  """Test case for the RemoveFile function"""
237 255

  
......
246 264
      os.unlink(self.tmpfile)
247 265
    os.rmdir(self.tmpdir)
248 266

  
249

  
250 267
  def testIgnoreDirs(self):
251 268
    """Test that RemoveFile() ignores directories"""
252 269
    self.assertEqual(None, RemoveFile(self.tmpdir))
253 270

  
254

  
255 271
  def testIgnoreNotExisting(self):
256 272
    """Test that RemoveFile() ignores non-existing files"""
257 273
    RemoveFile(self.tmpfile)
258 274
    RemoveFile(self.tmpfile)
259 275

  
260

  
261 276
  def testRemoveFile(self):
262 277
    """Test that RemoveFile does remove a file"""
263 278
    RemoveFile(self.tmpfile)
264 279
    if os.path.exists(self.tmpfile):
265 280
      self.fail("File '%s' not removed" % self.tmpfile)
266 281

  
267

  
268 282
  def testRemoveSymlink(self):
269 283
    """Test that RemoveFile does remove symlinks"""
270 284
    symlink = self.tmpdir + "/symlink"

Also available in: Unified diff