Example #1
0
  def test(self):
    """Test wrapping a function in a file-based lock."""
    lock_file = self.mox.CreateMock(file)
    self.mox.StubOutWithMock(utils, 'acquireLock')
    utils.acquireLock('debmarshal', fcntl.LOCK_EX).AndReturn(lock_file)

    self.mox.ReplayAll()

    @utils.withLockfile('debmarshal', fcntl.LOCK_EX)
    def hasALock():
      return True

    self.assertEqual(hasALock(), True)
Example #2
0
    def test(self):
        """Test wrapping a function in a file-based lock."""
        lock_file = self.mox.CreateMock(file)
        self.mox.StubOutWithMock(utils, 'acquireLock')
        utils.acquireLock('debmarshal', fcntl.LOCK_EX).AndReturn(lock_file)

        self.mox.ReplayAll()

        @utils.withLockfile('debmarshal', fcntl.LOCK_EX)
        def hasALock():
            return True

        self.assertEqual(hasALock(), True)
Example #3
0
  def test(self):
    """Test acquiring a lockfile.

    Since acquiring a lock is purely procedural with no branching,
    this is a bit of a dumb test.
    """
    # When run from within a test setUp method, mox.StubOutWithMock
    # doesn't seem to be able to stub out __builtins__, so we'll hack
    # around it ourselves
    self.open = self.mox.CreateMockAnything()
    utils.open = self.open
    self.mox.StubOutWithMock(fcntl, 'lockf')

    lock_file = self.mox.CreateMock(file)
    self.open('/var/lock/debmarshal-networks', 'w+').AndReturn(lock_file)
    fcntl.lockf(lock_file, fcntl.LOCK_SH)

    self.mox.ReplayAll()

    self.assertEqual(utils.acquireLock('debmarshal-networks', fcntl.LOCK_SH),
                     lock_file)

    self.mox.VerifyAll()

    del utils.open
Example #4
0
    def test(self):
        """Test acquiring a lockfile.

    Since acquiring a lock is purely procedural with no branching,
    this is a bit of a dumb test.
    """
        # When run from within a test setUp method, mox.StubOutWithMock
        # doesn't seem to be able to stub out __builtins__, so we'll hack
        # around it ourselves
        self.open = self.mox.CreateMockAnything()
        utils.open = self.open
        self.mox.StubOutWithMock(fcntl, 'lockf')

        lock_file = self.mox.CreateMock(file)
        self.open('/var/lock/debmarshal-networks', 'w+').AndReturn(lock_file)
        fcntl.lockf(lock_file, fcntl.LOCK_SH)

        self.mox.ReplayAll()

        self.assertEqual(
            utils.acquireLock('debmarshal-networks', fcntl.LOCK_SH), lock_file)

        self.mox.VerifyAll()

        del utils.open
Example #5
0
def storeState(state, filename):
    """Store state to a file in /var/run.

  This stores state to a pickle in /var/run, including locking in
  /var/lock that should be compatible with loadState.

  Args:
    state: The state to pickle and store.
    filename: The basename of the state file to save to.
  """
    lock = utils.acquireLock(filename, fcntl.LOCK_EX)
    state_file = open('/var/run/%s' % filename, 'w')
    pickle.dump(state, state_file)
Example #6
0
def storeState(state, filename):
    """Store state to a file in /var/run.

  This stores state to a pickle in /var/run, including locking in
  /var/lock that should be compatible with loadState.

  Args:
    state: The state to pickle and store.
    filename: The basename of the state file to save to.
  """
    lock = utils.acquireLock(filename, fcntl.LOCK_EX)
    state_file = open("/var/run/%s" % filename, "w")
    pickle.dump(state, state_file)
Example #7
0
def loadState(filename):
    """Load state from a file in /var/run.

  debmarshal stores state as pickles in /var/run, using files with
  corresponding names in /var/lock for locking.

  Args:
    filename: The basename of the state file to load.

  Returns:
    The depickled contents of the state file, or None if the state
      file does not yet exist.
  """
    lock = utils.acquireLock(filename, fcntl.LOCK_SH)
    try:
        state_file = open('/var/run/%s' % filename)
        return pickle.load(state_file)
    except EnvironmentError, e:
        if e.errno == errno.ENOENT:
            return
        else:
            raise
Example #8
0
def loadState(filename):
    """Load state from a file in /var/run.

  debmarshal stores state as pickles in /var/run, using files with
  corresponding names in /var/lock for locking.

  Args:
    filename: The basename of the state file to load.

  Returns:
    The depickled contents of the state file, or None if the state
      file does not yet exist.
  """
    lock = utils.acquireLock(filename, fcntl.LOCK_SH)
    try:
        state_file = open("/var/run/%s" % filename)
        return pickle.load(state_file)
    except EnvironmentError, e:
        if e.errno == errno.ENOENT:
            return
        else:
            raise