Пример #1
0
    def test_shouldCheckIfAllSharedOptionValuesOfAllRulesOfASchedulerAreEqual(
            self, fix):
        schedName = "sched-with-shared-opts"
        sched = mockSched(schedName,
                          sharedOptions=[
                              optInfo("ConfDir"),
                              optInfo("DestFile"),
                              optInfo("Version")
                          ])

        def rule(**schedOpts):
            return mockRule(scheduler=sched, schedOpts=schedOpts)

        rules = [
            rule(Version="1", ConfDir="/home", DestFile="/etc/foo"),
            rule(Version="1", ConfDir="/etc", DestFile="/etc/foo"),
            rule(Version="1", ConfDir="/usr/share")
        ]

        validator = self.construct()

        iterToTest(validator.validate(ruleSet(*rules))).\
            shouldContainMatchingInAnyOrder(
                stringThat.shouldInclude("ConfDir", "/home", "/etc", "/usr/share"),
                stringThat.shouldInclude("DestFile", "‘/etc/foo’", "‘’"))
Пример #2
0
def test_shouldConvertStringsIntoSuitablePythonTypesBasedOnOptionInfo(fixture):
    opts = {"NoOfCopies": "23", "Comment": "foo bar  "}
    expected = {"NoOfCopies": 23, "Comment": "foo bar  "}
    assert fixture.parser.parseOptions([
        optInfo("NoOfCopies", types.Positive),
        optInfo("Comment", types.String)
    ], opts) == expected

    assert parse(types.Positive, " 35 ") == 35

    assert parse(types.Bool, " YeS") == True
    assert parse(types.Bool, "FaLse ") == False
    assert parse(types.Bool, "on") == True

    assert parse(types.TimeDelta, "1s 2h  3w 25m ") == timedelta(seconds=1,
                                                                 hours=2,
                                                                 weeks=3,
                                                                 minutes=25)
    assert parse(types.TimeDelta, "4 wEeKs 12.5min") == timedelta(minutes=12.5,
                                                                  weeks=4)

    loc = parse(types.File, "/tmp//abc/")
    assert loc.protocol == "file"
    assert str(loc) == "/tmp/abc"

    loc = parse(types.Location, "host:/foo")
    assert loc.host == "host"
    assert loc.path == "/foo"

    enum = types.Enum("First", "Second")
    assert parse(enum, " fIrst") is enum.First
    assert parse(enum, "SECOND") is enum.Second
Пример #3
0
def test_shouldCollectParseErrorsBeforeThrowingException(fixture):
    with pytest.raises(OptionParseException) as ex:
        fixture.parser.parseOptions(
            [optInfo("Opt1", types.Positive),
             optInfo("Opt2", types.Bool)], dict(Opt1="-2", Opt2="foo"))

    iterToTest(ex.value.errors).shouldContainMatchingInAnyOrder(
        lambda error: error.optionName == "Opt1" and "negative" in error.
        message, lambda error: error.optionName == "Opt2" and "truth" in error.
        expectedType)
Пример #4
0
    def test_shouldRoundTheIntervalsToDaysAndWarnAboutLostParts(self, fixture):
        assert optInfo("Interval", types.TimeDelta) in fixture.optionInfos

        logger = BufferingErrorLogger()
        fixture.init(logger=logger)

        schedulings = [
            buildScheduling("one-day", Interval=timedelta(hours=22)),
            buildScheduling("two-days", Interval=timedelta(days=2, hours=6)),
            buildScheduling("three-weeks", Interval=timedelta(weeks=3)),
            buildScheduling("no-interval")
        ]

        def shouldHaveWarned():
            logger.string.shouldInclude("one-day", "rounding", "two-days").andAlso.\
                shouldNotInclude("three-weeks")
            logger.clear()

        assert fixture.check(schedulings, logger=logger) == []
        shouldHaveWarned()

        def checkTab(tabPath):
            strToTest(local(tabPath).read()).shouldIncludeLinePatterns(
                "3 0 no-interval*", "1 0 one-day*", "2 0 two-days*",
                "21 0 three-weeks*")
            return True

        fixture.checkOption("-t", schedulings, checkTab)
        shouldHaveWarned()
Пример #5
0
def test_shouldThrowExceptionIfAnOptionIsNotSupported(fixture):
    scheduler = fakeConfigurable("sched",
                                 availableOptions=[optInfo("sched-supported")])
    synchronizer = fakeConfigurable(
        "syncer", ports=[], availableOptions=[optInfo("syncer-supported")])

    def callBuild(ruleOptions, schedulerOptions, synchronizerOptions):
        fixture.factory.build("rule", scheduler, synchronizer, ruleOptions,
                              schedulerOptions,
                              mkSyncerOpts(**synchronizerOptions), True)

    with pytest.raises(ConfigConsistencyException):
        callBuild({}, {"sched-supported": 1}, {"not": 1})
    with pytest.raises(ConfigConsistencyException):
        callBuild({}, {"not": 1}, {"syncer-supported": 1})

    with noException():
        callBuild({}, {"sched-supported": 1}, {"syncer-supported": 1})

    with pytest.raises(ConfigConsistencyException):
        callBuild({"Blah": 2}, {"sched-supported": 1}, {"syncer-supported": 1})
def test_shouldAddRemoteShellCommandOptionAndSSHProtocolToEachPort(fixture):
    option = object()
    fixture.init(availableOptions=[option],
                 ports=[port(["file", "ftp"]),
                        port(["file"])])

    assert fixture.syncer.availableOptions == [
        option, optInfo("RemoteShellCommand", types.String)
    ]

    fixture.protocolsOfPort(1).shouldContainInAnyOrder("file", "ftp", "ssh")
    fixture.protocolsOfPort(2).shouldContainInAnyOrder("file", "ssh")
Пример #7
0
    def test_shouldReturnTheErrorsOfTheFirstSchedulerThatHasSome(self, fix):
        sched = mockSched(sharedOptions=[optInfo("UseVarDir")])
        rule1, rule2 = mockRule(scheduler=sched, schedOpts=dict(UseVarDir=True)), \
            mockRule(scheduler=sched, schedOpts=dict(UseVarDir=False))

        def visitSchedulers(visitFunc):
            assert visitFunc(sched, [rule1]) == None
            ret = visitFunc(sched, [rule1, rule2])
            assert len(ret) == 1
            return ret

        ruleSet = lambda x: x
        ruleSet.visitSchedulers = visitSchedulers
        validator = self.construct()
        validator.validate(ruleSet)
Пример #8
0
def parse(optInfoType, string, optName="Opt"):
    parser = OptionValuesParser()
    return parser.parseOptions([optInfo(optName, optInfoType)],
                               {optName: string})[optName]
Пример #9
0
 def locOptInfo(number):
     return optInfo("Loc" + str(number), types.Location)