Esempio n. 1
0
def test_keeperdoer():
    """
    KeeperDoer
    """
    keep0 = keeping.Keeper(name='test0', temp=True, reopen=False)
    assert keep0.opened == False
    assert keep0.path == None
    assert keep0.env == None

    kpDoer0 = keeping.KeeperDoer(keeper=keep0)
    assert kpDoer0.keeper == keep0
    assert kpDoer0.keeper.opened == False

    keep1 = keeping.Keeper(name='test1', temp=True, reopen=False)
    assert keep1.opened == False
    assert keep1.path == None
    assert keep1.env == None

    kpDoer1 = keeping.KeeperDoer(keeper=keep1)
    assert kpDoer1.keeper == keep1
    assert kpDoer1.keeper.opened == False

    limit = 0.25
    tock = 0.03125
    doist = doing.Doist(limit=limit, tock=tock)

    doers = [kpDoer0, kpDoer1]

    dogs = doist.ready(doers=doers)
    assert len(dogs) == 2
    assert [val[1] for val in dogs] == [0.0, 0.0]  #  retymes
    for doer in doers:
        assert doer._tymist == doist
        assert doer.keeper.opened
        assert "_test/keri/keep/test" in doer.keeper.path

    doist.once(dogs)
    assert doist.tyme == 0.03125  # on next cycle
    assert len(dogs) == 2
    for doer in doers:
        assert doer.keeper.opened == True

    for dog, retyme, index in dogs:
        dog.close()

    for doer in doers:
        assert doer.keeper.opened == False
        assert doer.keeper.env == None
        assert not os.path.exists(doer.keeper.path)

    #start over
    doist.tyme = 0.0
    doist.do(doers=doers)
    assert doist.tyme == limit
    for doer in doers:
        assert doer.keeper.opened == False
        assert doer.keeper.env == None
        assert not os.path.exists(doer.keeper.path)

    """End Test"""
Esempio n. 2
0
def test_keeper():
    """
    Test Keeper creation
    """
    """
    stat.S_ISVTX  is Sticky bit. When this bit is set on a directory it means
        that a file in that directory can be renamed or deleted only by the
        owner of the file, by the owner of the directory, or by a privileged process.

    stat.S_IRUSR Owner has read permission.
    stat.S_IWUSR Owner has write permission.
    stat.S_IXUSR Owner has execute permission.
    """
    dirMode = stat.S_ISVTX | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
    assert dirMode == 0o1700

    # set mode to sticky bit plus rwx only for owner/user
    keeper = keeping.Keeper()
    assert isinstance(keeper, keeping.Keeper)
    assert keeper.name == "main"
    assert keeper.temp == False
    assert isinstance(keeper.env, lmdb.Environment)
    assert keeper.path.endswith("keri/keep/main")
    assert keeper.env.path() == keeper.path
    assert os.path.exists(keeper.path)
    assert oct(os.stat(keeper.path).st_mode)[-4:] == "1700"
    assert keeper.DirMode == dirMode

    assert isinstance(keeper.gbls, lmdb._Database)
    assert isinstance(keeper.pris, lmdb._Database)
    assert isinstance(keeper.sits, lmdb._Database)

    keeper.close(clear=True)
    assert not os.path.exists(keeper.path)
    assert not keeper.opened

    # set to unrestricted mode
    keeper = keeping.Keeper(dirMode=0o775)
    assert isinstance(keeper, keeping.Keeper)
    assert keeper.name == "main"
    assert keeper.temp == False
    assert isinstance(keeper.env, lmdb.Environment)
    assert keeper.path.endswith("keri/keep/main")
    assert keeper.env.path() == keeper.path
    assert os.path.exists(keeper.path)
    assert oct(os.stat(keeper.path).st_mode)[-4:] == "0775"

    assert isinstance(keeper.gbls, lmdb._Database)
    assert isinstance(keeper.pris, lmdb._Database)
    assert isinstance(keeper.sits, lmdb._Database)

    keeper.close(clear=True)
    assert not os.path.exists(keeper.path)
    assert not keeper.opened

    # test not opened on init
    keeper = keeping.Keeper(reopen=False)
    assert isinstance(keeper, keeping.Keeper)
    assert keeper.name == "main"
    assert keeper.temp == False
    assert keeper.opened == False
    assert keeper.path == None
    assert keeper.env == None

    keeper.reopen()
    assert keeper.opened
    assert isinstance(keeper.env, lmdb.Environment)
    assert keeper.path.endswith("keri/keep/main")
    assert keeper.env.path() == keeper.path
    assert os.path.exists(keeper.path)

    assert isinstance(keeper.gbls, lmdb._Database)
    assert isinstance(keeper.pris, lmdb._Database)
    assert isinstance(keeper.sits, lmdb._Database)

    keeper.close(clear=True)
    assert not os.path.exists(keeper.path)
    assert not keeper.opened

    # Test using context manager
    with keeping.openKS() as keeper:
        assert isinstance(keeper, keeping.Keeper)
        assert keeper.name == "test"
        assert keeper.temp == True
        assert isinstance(keeper.env, lmdb.Environment)
        assert keeper.path.startswith("/tmp/keri_keep_")
        assert keeper.path.endswith("_test/keri/keep/test")
        assert keeper.env.path() == keeper.path
        assert os.path.exists(keeper.path)

        assert isinstance(keeper.gbls, lmdb._Database)
        assert isinstance(keeper.pris, lmdb._Database)
        assert isinstance(keeper.sits, lmdb._Database)

        salta = b'0AZxWJGkCkpDcHuVG4GM1KVw'
        saltb = b'0AHuVG4GM1KVwZxWJGkCkpDc'
        pria = b'AaOa6eOCJQcgEozYb1GgV9zE2yPgBXiP6h_J2cZeCy4M'
        prib = b'AE2yPgBXiP6h_J2cZeCy4MaOa6eOCJQcgEozYb1GgV9z'
        puba = b'DGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4'
        pubb = b'DoXvbGv9IPb0foWTZvI_4GAPkzNZMtX-QiVgbRbyAIZG'
        pubc = b'DAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4G'
        prea = b'EWzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhcc'
        preb = b'EQPYGGwTmuupUhPx5_yZ-Wk1x4ejhccWzwEHHzq7K0gz'

        #  test .gbls sub db methods
        key = b'pidx'
        pidxa = b'%x' % 0  # "{:x}".format(pidx).encode("utf-8")
        pidxb = b'%x' % 1  # "{:x}".format(pidx).encode("utf-8"
        assert keeper.getPri(key) == None
        assert keeper.delPri(key) == False
        assert keeper.putPri(key, val=pidxa) == True
        assert keeper.getPri(key) == pidxa
        assert keeper.putPri(key, val=pidxb) == False
        assert keeper.getPri(key) == pidxa
        assert keeper.setPri(key, val=pidxb) == True
        assert keeper.getPri(key) == pidxb
        assert keeper.delPri(key) == True
        assert keeper.getPri(key) == None

        key = b'salt'
        assert keeper.getPri(key) == None
        assert keeper.delPri(key) == False
        assert keeper.putPri(key, val=salta) == True
        assert keeper.getPri(key) == salta
        assert keeper.putPri(key, val=saltb) == False
        assert keeper.getPri(key) == salta
        assert keeper.setPri(key, val=saltb) == True
        assert keeper.getPri(key) == saltb
        assert keeper.delPri(key) == True
        assert keeper.getPri(key) == None

        key = b'tier'
        assert keeper.getPri(key) == None
        assert keeper.delPri(key) == False
        assert keeper.putPri(key, val=coring.Tiers.low) == True
        assert keeper.getPri(key) == coring.Tiers.low.encode("utf-8")
        assert keeper.putPri(key, val=coring.Tiers.med) == False
        assert keeper.getPri(key) == coring.Tiers.low.encode("utf-8")
        assert keeper.setPri(key, val=coring.Tiers.med) == True
        assert keeper.getPri(key) == coring.Tiers.med.encode("utf-8")
        assert keeper.delPri(key) == True
        assert keeper.getPri(key) == None

        #  test .pris sub db methods
        key = puba
        assert keeper.getPri(key) == None
        assert keeper.delPri(key) == False
        assert keeper.putPri(key, val=pria) == True
        assert keeper.getPri(key) == pria
        assert keeper.putPri(key, val=prib) == False
        assert keeper.getPri(key) == pria
        assert keeper.setPri(key, val=prib) == True
        assert keeper.getPri(key) == prib
        assert keeper.delPri(key) == True
        assert keeper.getPri(key) == None

        #  test .pres sub db methods
        key = puba
        assert keeper.getPre(key) == None
        assert keeper.delPre(key) == False
        assert keeper.putPre(key, val=prea) == True
        assert keeper.getPre(key) == prea
        assert keeper.putPre(key, val=preb) == False
        assert keeper.getPre(key) == prea
        assert keeper.setPre(key, val=preb) == True
        assert keeper.getPre(key) == preb
        assert keeper.delPre(key) == True
        assert keeper.getPre(key) == None

        #  test .prms sub db methods
        key = prea
        prma = json.dumps(
            dict(
                pidx=0,
                algo='salty',
                salt=salta.decode("utf-8"),
                stem='',
                tier='low',
            )).encode("utf-8")
        prmb = json.dumps(
            dict(
                pidx=1,
                algo='randy',
                salt='',
                stem='',
                level='',
            )).encode("utf-8")
        assert keeper.getPrm(key) == None
        assert keeper.delPrm(key) == False
        assert keeper.putPrm(key, val=prma) == True
        assert keeper.getPrm(key) == prma
        assert keeper.putPrm(key, val=prmb) == False
        assert keeper.getPrm(key) == prma
        assert keeper.setPrm(key, val=prmb) == True
        assert keeper.getPrm(key) == prmb
        assert keeper.delPrm(key) == True
        assert keeper.getPrm(key) == None

        #  test .sits sub db methods with pubs
        key = prea
        sita = json.dumps(
            dict(old=dict(pubs=[], ridx=0, kidx=0, st='0', dt=''),
                 new=dict(pubs=[puba.decode("utf-8")],
                          ridx=1,
                          kidx=1,
                          st='1',
                          dt=helping.nowIso8601()),
                 nxt=dict(pubs=[pubb.decode("utf-8")],
                          ridx=2,
                          kidx=2,
                          st='1',
                          dt=helping.nowIso8601()))).encode("utf-8")
        sitb = json.dumps(
            dict(old=dict(pubs=[puba.decode("utf-8")],
                          ridx=0,
                          kidx=0,
                          st='1',
                          dt=helping.nowIso8601()),
                 new=dict(pubs=[pubb.decode("utf-8")],
                          ridx=1,
                          kidx=1,
                          st='1',
                          dt=helping.nowIso8601()),
                 nxt=dict(pubs=[pubc.decode("utf-8")],
                          ridx=2,
                          kidx=2,
                          st='1',
                          dt=helping.nowIso8601()))).encode("utf-8")
        assert keeper.getSit(key) == None
        assert keeper.delSit(key) == False
        assert keeper.putSit(key, val=sita) == True
        assert keeper.getSit(key) == sita
        assert keeper.putSit(key, val=sitb) == False
        assert keeper.getSit(key) == sita
        assert keeper.setSit(key, val=sitb) == True
        assert keeper.getSit(key) == sitb
        assert keeper.delSit(key) == True
        assert keeper.getSit(key) == None

        #  test .pubs sub db methods
        key0 = keeping.riKey(prea, 0)
        pubs1 = [puba.decode("utf-8"), pubb.decode("utf-8")]
        pubs2 = [pubc.decode("utf-8")]
        spubs1 = json.dumps(pubs1).encode("utf-8")
        assert spubs1 == (b'["DGAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4", '
                          b'"DoXvbGv9IPb0foWTZvI_4GAPkzNZMtX-QiVgbRbyAIZG"]')
        spubs2 = json.dumps(pubs2).encode("utf-8")
        assert spubs2 == b'["DAPkzNZMtX-QiVgbRbyAIZGoXvbGv9IPb0foWTZvI_4G"]'
        assert keeper.getPubs(key0) == None
        assert keeper.delPubs(key0) == False
        assert keeper.putPubs(key0, val=spubs1) == True
        assert keeper.getPubs(key0) == spubs1
        assert keeper.putPubs(key0, val=spubs2) == False
        assert keeper.getPubs(key0) == spubs1
        assert keeper.setPubs(key0, val=spubs2) == True
        assert keeper.getPubs(key0) == spubs2
        assert keeper.delPubs(key0) == True
        assert keeper.getPubs(key0) == None

    assert not os.path.exists(keeper.path)
    """ End Test """