def test_shouldTreatLocsCorrespondingToPortsAsMinimumOptions(fixture): def locOptInfo(number): return optInfo("Loc" + str(number), types.Location) sched = fakeConfigurable("scheduler") syncer = fakeConfigurable("synchronizer", ports=[port(), port(), port(), port()], availableOptions=[ locOptInfo(1), locOptInfo(2), locOptInfo(3), locOptInfo(4) ]) loc1Through3 = { "Loc1": loc("/some-place"), "Loc2": loc("/place"), "Loc3": loc("/bar") } with pytest.raises(ConfigConsistencyException) as ex: fixture.factory.build("rulename", sched, syncer, {}, {}, mkSyncerOpts(**loc1Through3), True) strToTest(str(ex.value)).shouldIncludeInOrder("not", "minimum opt").andAlso.\ shouldInclude("Loc4") with noException(): fixture.factory.build( "rule", sched, syncer, {}, {}, mkSyncerOpts(Loc4=loc("/fourth"), **loc1Through3), True)
def test_shouldNotAllowSyncersThatAlreadySupportSSHInAnyWay(): assert not isExtensible( mockSyncer(ports=[port(["file", "ssh"]), port(["file"])])) assert not isExtensible( mockSyncer( ports=[port(["file"]), port(["file", "ssh"])]))
def test_shouldReturnVersionsGotFromSynchronizerIfFileIsWithinAPort(fixture): syncer = mockSyncer(ports=[port(), port(), port()]) syncerOptions = mkSyncerOpts(Loc1=location("/mnt/data/loc1"), Loc2=location("/mnt/backup/loc2"), Loc3=location("/mnt/foo/loc3")) rule = fixture.ruleWith(mockedSynchronizer=syncer, syncerOptions=syncerOptions) ret = orderedDateTimes(2) def check(path, expectedRelativePath, expectedLoc): syncer.expectCalls( mock.callMatching( "versionsOf", lambda options, path, locNumber: path == expectedRelativePath and locNumber == expectedLoc and options == syncerOptions, ret=ret)) assert set(versionsOf( rule, path)) == {version(rule, ret[0]), version(rule, ret[1])} syncer.checkExpectedCalls() check(location("/mnt/data/loc1/blah"), "blah", 1) check(location("/mnt/backup/loc2/one/two/"), "one/two", 2) check(location("/mnt/foo/loc3/bar/../"), ".", 3) assert len(versionsOf(rule, location("/mnt/data/quux"))) == 0
def test_shouldAddLocOptInfosToAvailableOptionsBasedOnPorts(): wrapped = fakeConfigurable("syncer", ports=[port(), port(), port()], availableOptions=[]) syncer = DefaultValueSynchronizer(wrapped) iterToTest(syncer.availableOptions).shouldContainMatching( lambda opt: opt.name == "Loc1" and opt.optionType == types.Location, lambda opt: opt.name == "Loc2", lambda opt: opt.name == "Loc3")
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")
def test_shouldAssignRestoreTargetToThePortTheFileToBeRestoredWasFoundIn( fixture): syncer = mockSyncer() syncer.ports = [port(["a"]), port(["b"])] loc1 = remoteLocation(protocol="a", path="/foo") rule = fixture.ruleWith(mockedSynchronizer=syncer, syncerOptions=mkSyncerOpts(Loc1=loc1, Loc2=remoteLocation( protocol="b", path="/bar"))) with pytest.raises(UnsupportedProtocolException) as ex: rule.restore(loc1, version(rule), remoteLocation(protocol="b"), detector()) assert ex.value.supportedProtocols == ["a"]
def test_shouldThrowAnExceptionIfLocOptionsHaveProtocolsNotSupportedBySyncer( fixture): syncer = mockSyncer() syncer.ports = [port(["a", "b"]), port(["c"])] fixture.ruleWith(mockedSynchronizer=syncer, syncerOptions=mkSyncerOpts( Loc1=remoteLocation(protocol="b"), Loc2=remoteLocation(protocol="c"))) with pytest.raises(UnsupportedProtocolException) as ex: fixture.ruleWith(mockedSynchronizer=syncer, syncerOptions=mkSyncerOpts( Loc1=remoteLocation(protocol="b"), Loc2=remoteLocation(protocol="d"))) assert ex.value.optionName == "Loc2" assert ex.value.protocol == "d" assert ex.value.supportedProtocols == ["c"]
def test_shouldEnforceSpecialInvariantThatOnePortMustHaveFileProtocol(fixture): syncer = mockSyncer() syncer.onePortMustHaveFileProtocol = True syncer.ports = [port(["file", "remote"]), port(["file", "remote"])] with pytest.raises(UnsupportedProtocolException) as ex: fixture.ruleWith(mockedSynchronizer=syncer, syncerOptions=mkSyncerOpts( Loc1=remoteLocation(protocol="remote"), Loc2=remoteLocation(protocol="remote"))) assert "at least one" in ex.value.explanation rule = fixture.ruleWith(mockedSynchronizer=syncer, syncerOptions=mkSyncerOpts( Loc1=remoteLocation(protocol="remote", path="/foo"), Loc2=remoteLocation(protocol="file", path="/bar"))) with pytest.raises(UnsupportedProtocolException) as ex: rule.restore(remoteLocation(protocol="file", path="/bar/file"), version(rule), remoteLocation(protocol="remote"), detector())
def checkWriteLocs(writtenToFlags, expectedWriteLocs, expectedNonWriteLocs, loc2=loc2): syncer = mockSyncer() syncer.ports = [port(isWrittenTo=flag) for flag in writtenToFlags] rule = fixture.ruleWith(mockedSynchronizer=syncer, syncerOptions=mkSyncerOpts(Loc1=loc1, Loc2=loc2)) assert set(rule.writeLocs) == set(expectedWriteLocs) assert set(rule.nonWriteLocs) == set(expectedNonWriteLocs)
def test_shouldNotAllowSyncersThatDontSupportLocalFiles(): assert not isExtensible(mockSyncer(ports=[port(["file"]), port(["ftp"])]))
@pytest.fixture def fixture(): return Fixture() class LoggingProcessRunner(object): def __init__(self): self.executions = [] def execute(self, *args, **kwargs): self.executions.append(args) ThreeFilePorts = [port(["file"]), port(["file"]), port(["file"])] TwoFilePorts = [port(["file"]), port(["file"]), port(["file"])] 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")