コード例 #1
0
    def test_invalid_dir(self):

        LOG.info("Testing creation lockfile in an invalid lock directory.")

        from fb_tools.handler.lock import LockHandler
        from fb_tools.handler.lock import LockdirNotExistsError
        from fb_tools.handler.lock import LockdirNotWriteableError

        ldir = '/etc/passwd'
        locker = LockHandler(
            appname='test_lock',
            verbose=self.verbose,
            lockdir=ldir,
        )
        with self.assertRaises(LockdirNotExistsError) as cm:
            lock = locker.create_lockfile(self.lock_basename)
            lock = None
        e = cm.exception
        LOG.debug("%s raised as expected on lockdir = %r: %s.",
                  'LockdirNotExistsError', ldir, e)
        del locker

        if os.getegid():
            ldir = '/var'
            locker = LockHandler(
                appname='test_lock',
                verbose=self.verbose,
                lockdir=ldir,
            )
            with self.assertRaises(LockdirNotWriteableError) as cm:
                lock = locker.create_lockfile(self.lock_basename)  # noqa
                del lock
            e = cm.exception
            LOG.debug("%s raised as expected on lockdir = %r: %s.",
                      'LockdirNotWriteableError', ldir, e)
コード例 #2
0
    def test_invalid_lockfile3(self):

        LOG.info(
            "Testing creation lockfile with an invalid previous lockfile #3.")

        from fb_tools.handler.lock import LockHandler

        locker = LockHandler(
            appname='test_lock',
            verbose=self.verbose,
            lockdir=self.lock_dir,
        )

        content = "123456\n\n"
        lockfile = self.create_lockfile(content)
        result = None

        try:
            result = locker.create_lockfile(lockfile,
                                            delay_start=0.2,
                                            delay_increase=0.4,
                                            max_delay=5)
            locker.remove_lockfile(lockfile)
            if not result:
                self.fail(
                    "PbLockHandler should be able to create the lockfile.")
            result = None
        finally:
            self.remove_lockfile(lockfile)
コード例 #3
0
    def test_invalid_lockfile1(self):

        LOG.info(
            "Testing creation lockfile with an invalid previous lockfile #1.")

        from fb_tools.handler.lock import LockHandler
        from fb_tools.errors import CouldntOccupyLockfileError

        locker = LockHandler(
            appname='test_lock',
            verbose=self.verbose,
            lockdir=self.lock_dir,
        )

        content = "\n\n"
        lockfile = self.create_lockfile(content)
        result = None

        try:
            with self.assertRaises(CouldntOccupyLockfileError) as cm:
                result = locker.create_lockfile(  # noqa
                    lockfile,
                    delay_start=0.2,
                    delay_increase=0.4,
                    max_delay=5)
            e = cm.exception
            LOG.debug(
                "%s raised as expected on an invalid lockfile (empty lines): %s",
                e.__class__.__name__, e)
            del result

        finally:
            self.remove_lockfile(lockfile)
コード例 #4
0
    def test_valid_lockfile(self):

        LOG.info("Testing fail on creation lockfile with a valid PID.")

        from fb_tools.handler.lock import LockHandler
        from fb_tools.errors import CouldntOccupyLockfileError

        content = "%d\n" % (os.getpid())

        locker = LockHandler(
            appname='test_lock',
            verbose=self.verbose,
            lockdir=self.lock_dir,
        )

        lockfile = self.create_lockfile(content)
        result = None

        try:
            with self.assertRaises(CouldntOccupyLockfileError) as cm:
                result = locker.create_lockfile(lockfile,
                                                delay_start=0.2,
                                                delay_increase=0.4,
                                                max_delay=5)
            e = cm.exception
            LOG.debug("%s raised as expected on an valid lockfile: %s",
                      e.__class__.__name__, e)
            self.assertEqual(lockfile, e.lockfile)
            if result:
                self.fail(
                    "PbLockHandler shouldn't be able to create the lockfile.")
                result = None
        finally:
            self.remove_lockfile(lockfile)
コード例 #5
0
    def test_simple_lockfile(self):

        LOG.info("Testing creation and removing of a simple lockfile.")

        from fb_tools.handler.lock import LockHandler

        locker = LockHandler(
            appname='test_base_object',
            verbose=self.verbose,
            lockdir=self.lock_dir,
        )
        LOG.debug("Creating lockfile %r ...", self.lock_file)
        lock = locker.create_lockfile(self.lock_basename)
        LOG.debug("Removing lockfile %r ...", self.lock_file)
        del lock
        locker.remove_lockfile(self.lock_basename)
コード例 #6
0
    def test_lockobject(self):

        LOG.info("Testing lock object on creation of a simple lockfile.")

        from fb_tools.handler.lock import LockHandler

        locker = LockHandler(
            appname='test_lock',
            verbose=self.verbose,
            lockdir=self.lock_dir,
        )
        try:
            LOG.debug("Creating lockfile %r ...", self.lock_file)
            lock = locker.create_lockfile(self.lock_basename)
            LOG.debug("PbLock object %%r: %r", lock)
            LOG.debug("PbLock object %%s:\n%s", str(lock))
            lock = None
        finally:
            LOG.debug("Removing lockfile %r ...", self.lock_file)
            locker.remove_lockfile(self.lock_basename)
コード例 #7
0
    def test_invalid_lockfile2(self):

        LOG.info(
            "Testing creation lockfile with an invalid previous lockfile #2.")

        from fb_tools.handler.lock import LockHandler
        from fb_tools.errors import CouldntOccupyLockfileError

        locker = LockHandler(
            appname='test_lock',
            verbose=self.verbose,
            lockdir=self.lock_dir,
        )

        content = "Bli bla blub\n\n"
        lockfile = self.create_lockfile(content)
        result = None

        try:
            with self.assertRaises(CouldntOccupyLockfileError) as cm:
                result = locker.create_lockfile(lockfile,
                                                delay_start=0.2,
                                                delay_increase=0.4,
                                                max_delay=5)
            e = cm.exception
            LOG.debug(
                "%s raised as expected on an invalid lockfile (non-numeric): %s",
                e.__class__.__name__, e)

            locker.remove_lockfile(lockfile)
            if result:
                self.fail(
                    "LockHandler should not be able to create the lockfile.")
                result = None

        finally:
            self.remove_lockfile(lockfile)
コード例 #8
0
    def test_refresh_lockobject(self):

        LOG.info("Testing refreshing of a lock object.")

        from fb_tools.handler.lock import LockHandler

        locker = LockHandler(
            appname='test_lock',
            verbose=self.verbose,
            lockdir=self.lock_dir,
        )
        try:
            LOG.debug("Creating lockfile %r ...", self.lock_file)
            lock = locker.create_lockfile(self.lock_basename)
            LOG.debug("Current ctime: %s" % (lock.ctime.isoformat(' ')))
            LOG.debug("Current mtime: %s" % (lock.mtime.isoformat(' ')))
            if self.verbose > 2:
                LOG.debug("Current lock object before refreshing:\n{}".format(
                    pp(lock.as_dict())))
            mtime1 = lock.stat().st_mtime
            LOG.debug("Sleeping two seconds ...")
            time.sleep(2)
            lock.refresh()
            LOG.debug("New mtime: %s" % (lock.mtime.isoformat(' ')))
            if self.verbose > 2:
                LOG.debug("Current lock object after refreshing:\n{}".format(
                    pp(lock.as_dict())))
            mtime2 = lock.stat().st_mtime
            tdiff = mtime2 - mtime1
            LOG.debug(
                "Got a time difference between mtimes of %0.3f seconds." %
                (tdiff))
            self.assertGreater(mtime2, mtime1)
            lock = None
        finally:
            LOG.debug("Removing lockfile %r ...", self.lock_file)
            locker.remove_lockfile(self.lock_basename)