def test_shouldLogSystemExceptionsInLessDetail(self, fixture): class SysException(BaseException): pass def raiseException(logger): raise SysException() with pytest.raises(SysException): fixture.log.logExecution("foo", constantTimeClock(), raiseException) iterToTest(fixture.executionsOf("foo")).shouldContainMatching( lambda execution: strToTest(execution.output).\ shouldInclude("exception").but.shouldNotInclude("traceback"))
def test_shouldLogAndRethrowExceptions(self, fixture): class TestException(Exception): def __str__(self): return "we have a problem" def raiseException(logFile): raise TestException() with pytest.raises(TestException): fixture.log.logExecution("foo", constantTimeClock(), raiseException) iterToTest(fixture.executionsOf("foo")).shouldContainMatching( lambda execution: "internal" in execution.output and "exception" in execution.output and "we have a problem" in execution.output and not execution.succeeded)
def test_shouldRequireForceOptionToRestoreWithARuleWhoseExecutionIsTooClose( fixture): now = anyUTCDateTime() fixture.setClock(constantTimeClock(now)) _, sched = fixture.conf.aSched().withNextExecutionTimeFunc( lambda *_: now).mock() syncer = fixture.conf.syncerHavingAnyVersions().write() rule = fixture.conf.aRule().withScheduler(sched).withSynchronizer(syncer).\ withLoc1("/mnt").write() fixture.runSibtCheckingExecs("restore", "/mnt", ":") fixture.shouldHaveExitedWithStatus(1) fixture.stderr.shouldInclude("error", "execution", "less than").but.\ shouldNotInclude("warning", "matching") syncer.expectingRestore().reMakeExpectations() fixture.runSibtCheckingExecs("restore", "--force", "/mnt", ":") fixture.shouldHaveExitedWithStatus(0) fixture.stderr.shouldBeEmpty()
def test_shouldCorrectlyReadExecutionsThatAreStillInProgress(self, fixture): startTime = anyUTCDateTime() executionsInThisProcess = [] executionsInOtherProcess = [] def execute(logger): logger.write(b"output\n") executionsInThisProcess.extend(fixture.executionsOf("quux")) executionsInOtherProcess.extend(fixture.inNewProcess(r""" return fixture.executionsOf("quux")""")) return True fixture.log.logExecution("quux", constantTimeClock(startTime), execute) iterToTest(executionsInOtherProcess).shouldContainMatching( lambda execution: execution.startTime == startTime and execution.output == "output\n" and execution.finished is False) iterToTest(executionsInThisProcess).shouldContainMatching( lambda execution: execution.finished is False)
def test_shouldWarnWhenAccessingLocsIfTheNextRuleExecutionIsCloserThanAnHour( fixture): startTime, endTime, nextTime = orderedDateTimes(3) ruleName = "some-rule" nameAsSysRule = "+" + ruleName def nextExecutionTime(scheduling): assert scheduling.ruleName == nameAsSysRule assert scheduling.lastExecutionTime == endTime return nextTime syncer = fixture.conf.syncerHavingAnyVersions().asSysConfig().write() _, sched = fixture.conf.aSysSched().withNextExecutionTimeFunc( nextExecutionTime).mock() rule = fixture.conf.aSysRule(ruleName).withScheduler(sched).\ withSynchronizer(syncer).allowedForTestUser().write() fixture.setRootUserId() fixture.executeOnce(rule, startTime, endTime) fixture.setNormalUserId() syncer.reMakeExpectations() fixture.setClock(constantTimeClock(nextTime - timedelta(minutes=59))) fixture.runSibtCheckingExecs("versions-of", str(rule.loc1)) fixture.shouldHaveExitedWithStatus(0) fixture.stdout.shouldContainLinePatterns("*:*") fixture.stderr.shouldInclude("warning", "execution", "less than 1", nameAsSysRule) syncer.reMakeExpectations() fixture.runSibtCheckingExecs("versions-of", "/lala") fixture.stderr.shouldNotInclude("less than") syncer.expectingListFiles().reMakeExpectations() fixture.runSibtCheckingExecs("list-files", str(rule.loc1), ",") fixture.stderr.shouldInclude("warning", "less than") fixture.shouldHaveExitedWithStatus(0)
def make(self, clock=constantTimeClock(), prefix=""): return FileLogger(str(self.logFile), clock, prefix)
def makeFormatter(currentTime=None): if currentTime is None: currentTime = datetime.now(timezone.utc) return DateTimeFormatter(constantTimeClock(currentTime), True)
def make(minimum=timedelta(hours=1), currentTime=anyUTCDateTime()): return ExecutionClosenessDetector(constantTimeClock(currentTime), minimum)
def construct(self, wrappedSched): return LoggingScheduler(wrappedSched, constantTimeClock(), BufferingOutput(), False)
def callWithLoggerAndClose(self, func): def execute(logger): func(logger) return True self.log.logExecution(SpecialRuleName, constantTimeClock(), execute)