コード例 #1
0
def TestOpenStuff():
    """    """
    import ioflo.base.storing as storing

    storing.Store.Clear()  #clear registry
    s1 = ServerTask(store=storing.Store())
    s2 = ServerTask(store=storing.Store())

    print(s1.server.reopen())
    print(s2.server.reopen())
コード例 #2
0
 def __init__(self,
              name='',
              version=raeting.VERSION,
              store=None,
              device=None,
              did=None,
              ha=("", raeting.RAET_PORT),
              rxMsgs = None,
              txMsgs = None,
              udpRxes = None,
              udpTxes = None,
              ):
     '''
     Setup StackUdp instance
     '''
     if not name:
         name = "stack{0}".format(StackUdp.Count)
         StackUdp.Count += 1
     self.name = name
     self.version = version
     self.store = store or storing.Store(stamp=0.0)
     self.devices = odict() # remote devices attached to this stack by did
     self.dids = odict() # reverse lookup did by device.name
      # local device for this stack
     self.device = device or devicing.LocalDevice(stack=self, did=did, ha=ha)
     self.transactions = odict() #transactions
     self.rxMsgs = rxMsgs if rxMsgs is not None else deque() # messages received
     self.txMsgs = txMsgs if txMsgs is not None else deque() # messages to transmit
     #(msg, ddid) ddid=0 is broadcast
     self.udpRxes = udpRxes if udpRxes is not None else deque() # udp packets received
     self.udpTxes = udpTxes if udpTxes is not None else deque() # udp packet to transmit
     self.serverUdp = aiding.SocketUdpNb(ha=self.device.ha)
     self.serverUdp.reopen()  # open socket
     self.device.ha = self.serverUdp.ha  # update device host address after open
コード例 #3
0
def Test(verbose=False):
    """Module self test



    """
    import ioflo.base.storing as storing
    import ioflo.base.tasking as tasking

    storing.Store.Clear()  #clear registry
    tasking.Tasker.Clear()

    s = Server(store=storing.Store())

    s.store.expose()

    print("ready to go")
    status = s.start()

    while (not (status == STOPPED or status == ABORTED)):
        try:
            status = s.run()

        except KeyboardInterrupt:  #CNTL-C shutdown skedder
            print("    Keyboard Interrupt manual shutdown of taskers ...")
            s.server.close()

            break
コード例 #4
0
ファイル: test_stacking.py プロジェクト: rogeriofalcone/raet
    def setUp(self):
        self.store = storing.Store(stamp=0.0)
        self.timer = StoreTimer(store=self.store, duration=1.0)

        if sys.platform == 'win32':
            self.tempDirpath = tempfile.mktemp(prefix="raet",
                                               suffix="base",
                                               dir=TEMPDIR)
        else:
            self.tempDirpath = tempfile.mkdtemp(prefix="raet",
                                                suffix="base",
                                                dir=TEMPDIR)

        self.baseDirpath = os.path.join(self.tempDirpath, 'lane', 'keep')

        # main stack
        self.main = stacking.LaneStack(name='main',
                                       uid=1,
                                       lanename='cherry',
                                       sockdirpath=self.baseDirpath)

        #other stack
        self.other = stacking.LaneStack(name='other',
                                        uid=1,
                                        lanename='cherry',
                                        sockdirpath=self.baseDirpath)
コード例 #5
0
    def testStore(self):
        """
        Test Store Class
        """
        console.terse("{0}\n".format(self.testStore.__doc__))
        storing.Store.Clear()  # clear registry of Store instance entries

        store = storing.Store()
        self.assertEqual(store.name, 'Store1')
        self.assertEqual(store.house, None)
        self.assertEqual(store.stamp, None)
        self.assertIsInstance(store.shares, storing.Node)
        self.assertIn("meta", store.shares)
        self.assertIn("realtime", store.shares)
        self.assertIn("time", store.shares)
        self.assertEqual(store.metaShr, storing.Node([]))
        self.assertIsInstance(store.timeShr, storing.Share)
        self.assertIsInstance(store.realTimeShr, storing.Share)
        self.assertIsInstance(store.dateTimeShr, storing.Share)
        for field in ("iso", "dt", "year", "month", "day", "hour", "minute", "second", "micro"):
            self.assertTrue(field in store.dateTimeShr)

        share = storing.Share(name='auto.depth')
        store.add(share).create(depth=10.0)
        self.assertIs(store.fetch('auto.depth'), share)
        self.assertIs(share.data.depth, 10.0)
        with  self.assertRaises(AttributeError) as  ex:
            share.data.value
        self.assertIs(share.value, None)

        path = 'auto.heading'
        store.create(path).create(heading=20.0)
        share = store.fetch(path)
        self.assertEqual(share.data.heading, 20.0)
        share.create(heading=25.0)
        self.assertEqual(share.data.heading, 20.0)

        shareA = store.create(path)
        self.assertIs(share, shareA)

        with self.assertRaises(ValueError) as  ex:
            store.add(storing.Share(name=path))

        shareA = storing.Share(name=path)
        store.change(shareA)
        self.assertIs(store.fetch(path), shareA)
        self.assertIsNot(store.fetch(path), share)
        shareA.value = 25.0
        self.assertEqual(shareA.value, 25.0)

        path = 'auto.speed'
        share = storing.Share(name=path)
        with self.assertRaises(ValueError) as  ex:
            store.change(share)
        store.add(share)
        self.assertIs(store.fetch(share.name), share)

        store.expose(valued=True)
        storing.Store.Clear()
コード例 #6
0
ファイル: stacking.py プロジェクト: topochan/salt
    def __init__(self,
                 name='',
                 main=False,
                 version=raeting.VERSION,
                 store=None,
                 estate=None,
                 eid=None,
                 ha=("", raeting.RAET_PORT),
                 rxMsgs=None,
                 txMsgs=None,
                 udpRxes=None,
                 udpTxes=None,
                 road=None,
                 safe=None,
                 auto=None,
                 dirpath=None,
                 ):
        '''
        Setup StackUdp instance
        '''
        if not name:
            name = "stackUdp{0}".format(StackUdp.Count)
            StackUdp.Count += 1
        self.name = name
        self.version = version
        self.store = store or storing.Store(stamp=0.0)
        self.estates = odict() # remote estates attached to this stack by eid
        self.eids = odict() # reverse lookup eid by estate.name
        self.transactions = odict() #transactions
        self.rxMsgs = rxMsgs if rxMsgs is not None else deque() # messages received
        self.txMsgs = txMsgs if txMsgs is not None else deque() # messages to transmit
        self.udpRxes = udpRxes if udpRxes is not None else deque() # udp packets received
        self.udpTxes = udpTxes if udpTxes is not None else deque() # udp packet to transmit

        self.road = road or keeping.RoadKeep(dirpath=dirpath,
                                             stackname=self.name)
        self.safe = safe or keeping.SafeKeep(dirpath=dirpath,
                                             stackname=self.name,
                                             auto=auto)
        kept = self.loadLocal() # local estate from saved data
        # local estate for this stack
        self.estate = kept or estate or estating.LocalEstate(stack=self,
                                                             eid=eid,
                                                             main=main,
                                                             ha=ha)
        self.estate.stack = self
        self.serverUdp = aiding.SocketUdpNb(ha=self.estate.ha, bufsize=raeting.MAX_MESSAGE_SIZE)
        self.serverUdp.reopen()  # open socket
        self.estate.ha = self.serverUdp.ha  # update estate host address after open
        self.dumpLocal() # save local estate data

        kepts = self.loadAllRemote() # remote estates from saved data
        for kept in kepts:
            self.addRemoteEstate(kept)
        self.dumpAllRemote() # save remote estate data
コード例 #7
0
ファイル: stacking.py プロジェクト: KennethWilke/raet
    def __init__(
        self,
        name='',
        version=raeting.VERSION,
        store=None,
        keep=None,
        dirpath='',
        local=None,
        server=None,
        rxMsgs=None,
        txMsgs=None,
        rxes=None,
        txes=None,
        stats=None,
    ):
        '''
        Setup StackUdp instance
        '''
        if not name:
            name = "stack{0}".format(Stack.Count)
            Stack.Count += 1
        self.name = name
        self.version = version
        self.store = store or storing.Store(stamp=0.0)

        self.keep = keep or keeping.LotKeep(dirpath=dirpath,
                                            stackname=self.name)
        self.loadLocal(
            local)  # load local data from saved data else passed in local
        self.remotes = odict()  # remotes indexed by uid
        self.uids = odict()  # remote uids indexed by name
        self.loadRemotes()  # load remotes from saved data
        self.server = server
        if self.server:
            if not self.server.reopen():  # open socket
                raise raeting.StackError("Stack {0}: Can't open server at"
                                         " {1}\n".format(
                                             self.name, self.server.ha))
            if self.local:
                self.local.ha = self.server.ha  # update local host address after open

        self.rxMsgs = rxMsgs if rxMsgs is not None else deque(
        )  # messages received
        self.txMsgs = txMsgs if txMsgs is not None else deque(
        )  # messages to transmit
        self.rxes = rxes if rxes is not None else deque(
        )  # udp packets received
        self.txes = txes if txes is not None else deque(
        )  # udp packet to transmit
        self.stats = stats if stats is not None else odict()  # udp statistics
        self.statTimer = aiding.StoreTimer(self.store)

        self.dumpLocal()  # save local data
        self.dumpRemotes()  # save remote data
コード例 #8
0
def testApp():
    """

    setup falcon and load REST endpoints

    """

    store = storing.Store(stamp=0.0)
    testApp = falcon.API()
    loadEndPoints(testApp, store=store, mode="method")

    return testApp
コード例 #9
0
def Test(rule=UPDATE):
    """Module Common self test

    """

    storing.Store.Clear()
    logging.Logger.Clear()
    logging.Log.Clear()

    store = storing.Store(name='Test', stamp=0.0)

    heading = store.create('pose.heading').create(value=0.0)
    position = store.create('pose.position').create(north=10.0, east=5.0)

    log = logging.Log(name='test',
                      store=store,
                      kind='text',
                      prefix='log',
                      path='./logs/',
                      rule=rule)
    log.addLoggee(tag='heading', loggee='pose.heading')
    log.addLoggee(tag='pos', loggee='pose.position')
    log.resolve()

    logger = logging.Logger(name='Test', store=store)
    logger.addLog(log)

    status = logger.runner.send(START)  #also prepares logs
    status = logger.runner.send(RUN)

    for i in range(20):
        store.advanceStamp(0.125)
        if i == 5:
            heading.value += 0.0
            position.data.north += 0.0
            position.data.east -= 0.0
        elif i == 10:
            pass
        else:
            heading.value = float(i)
            position.data.north += 2.0
            position.data.east -= 1.5

        status = logger.runner.send(RUN)

    status = logger.runner.send(STOP)

    status = logger.runner.send(START)
    store.advanceStamp(0.125)
    heading.value += 5.0
    status = logger.runner.send(RUN)
    status = logger.runner.send(STOP)
コード例 #10
0
ファイル: test_saltsafe.py プロジェクト: baijian/salt
    def setUp(self):
        self.store = storing.Store(stamp=0.0)
        self.timer = StoreTimer(store=self.store, duration=1.0)

        self.mainDirpath = tempfile.mkdtemp(prefix="salt", suffix='main', dir='/tmp')
        opts = self.createOpts(self.mainDirpath, openMode=True, autoAccept=True)
        self.mainSafe = salting.SaltSafe(opts=opts)

        self.otherDirpath = tempfile.mkdtemp(prefix="salt", suffix='other', dir='/tmp')
        opts = self.createOpts(self.otherDirpath, openMode=True, autoAccept=True)
        self.otherSafe = salting.SaltSafe(opts=opts)

        self.baseDirpath = tempfile.mkdtemp(prefix="raet",  suffix="base", dir='/tmp')
コード例 #11
0
    def setUp(self):
        self.store = storing.Store(stamp=0.0)

        dirpathBase='/tmp/raet/'

        #main stack
        mainName = "main"
        mainDirpath = os.path.join(dirpathBase, 'road', 'keep', mainName)
        signer = nacling.Signer()
        mainSignKeyHex = signer.keyhex
        privateer = nacling.Privateer()
        mainPriKeyHex = privateer.keyhex


        #other stack
        otherName = "other"
        otherDirpath = os.path.join(dirpathBase, 'road', 'keep', otherName)
        signer = nacling.Signer()
        otherSignKeyHex = signer.keyhex
        privateer = nacling.Privateer()
        otherPriKeyHex = privateer.keyhex


        keeping.clearAllKeepSafe(mainDirpath)
        keeping.clearAllKeepSafe(otherDirpath)

        local = estating.LocalEstate(eid=1,
                                     name=mainName,
                                     sigkey=mainSignKeyHex,
                                     prikey=mainPriKeyHex,)

        self.main = stacking.RoadStack(name=mainName,
                                         local=local,
                                         auto=True,
                                         main=True,
                                         dirpath=mainDirpath,
                                         store=self.store)

        local = estating.LocalEstate(eid=0,
                                     name=otherName,
                                     ha=("", raeting.RAET_TEST_PORT),
                                     sigkey=otherSignKeyHex,
                                     prikey=otherPriKeyHex,)

        self.other = stacking.RoadStack(name=otherName,
                                         local=local,
                                         dirpath=otherDirpath,
                                         store=self.store)

        self.timer = StoreTimer(store=self.store, duration=1.0)
コード例 #12
0
 def __init__(
     self,
     name='',
     version=raeting.VERSION,
     store=None,
     lanename='lane',
     yard=None,
     yid=None,
     yardname='',
     ha='',
     rxMsgs=None,
     txMsgs=None,
     uxdRxes=None,
     uxdTxes=None,
     lane=None,
     accept=None,
     dirpath=None,
 ):
     '''
     Setup StackUxd instance
     '''
     if not name:
         name = "stackUxd{0}".format(StackUxd.Count)
         StackUxd.Count += 1
     self.name = name
     self.version = version
     self.store = store or storing.Store(stamp=0.0)
     self.yards = odict()  # remote uxd yards attached to this stack by name
     self.names = odict()  # remote uxd yard names  by ha
     self.yard = yard or yarding.Yard(stack=self,
                                      name=yardname,
                                      yid=yid,
                                      ha=ha,
                                      prefix=lanename,
                                      dirpath=dirpath)
     self.rxMsgs = rxMsgs if rxMsgs is not None else deque(
     )  # messages received
     self.txMsgs = txMsgs if txMsgs is not None else deque(
     )  # messages to transmit
     self.uxdRxes = uxdRxes if uxdRxes is not None else deque(
     )  # uxd packets received
     self.uxdTxes = uxdTxes if uxdTxes is not None else deque(
     )  # uxd packets to transmit
     self.lane = lane  # or keeping.LaneKeep()
     self.accept = self.Accept if accept is None else accept  #accept uxd msg if not in lane
     self.serverUxd = aiding.SocketUxdNb(ha=self.yard.ha,
                                         bufsize=raeting.MAX_MESSAGE_SIZE)
     self.serverUxd.reopen()  # open socket
     self.yard.ha = self.serverUxd.ha  # update estate host address after open
コード例 #13
0
ファイル: test_stacking.py プロジェクト: rogeriofalcone/raet
    def setUp(self):
        self.store = storing.Store(stamp=0.0)
        self.timer = StoreTimer(store=self.store, duration=1.0)

        self.baseDirpath = tempfile.mkdtemp(prefix="raet",
                                            suffix="base",
                                            dir=TEMPDIR)
        stacking.RoadStack.Bk = raeting.bodyKinds.json

        #main stack
        mainName = "main"
        mainDirpath = os.path.join(self.baseDirpath, 'road', 'keep', mainName)
        signer = nacling.Signer()
        mainSignKeyHex = signer.keyhex
        privateer = nacling.Privateer()
        mainPriKeyHex = privateer.keyhex

        #other stack
        otherName = "other"
        otherDirpath = os.path.join(self.baseDirpath, 'road', 'keep',
                                    otherName)
        signer = nacling.Signer()
        otherSignKeyHex = signer.keyhex
        privateer = nacling.Privateer()
        otherPriKeyHex = privateer.keyhex

        keeping.clearAllKeep(mainDirpath)
        keeping.clearAllKeep(otherDirpath)

        self.main = stacking.RoadStack(
            store=self.store,
            name=mainName,
            main=True,
            auto=raeting.autoModes.once,
            sigkey=mainSignKeyHex,
            prikey=mainPriKeyHex,
            dirpath=mainDirpath,
        )

        self.other = stacking.RoadStack(
            store=self.store,
            name=otherName,
            auto=raeting.autoModes.once,
            ha=("", raeting.RAET_TEST_PORT),
            sigkey=otherSignKeyHex,
            prikey=otherPriKeyHex,
            dirpath=otherDirpath,
        )
コード例 #14
0
    def setUp(self):
        self.store = storing.Store(stamp=0.0)
        self.timer = StoreTimer(store=self.store, duration=1.0)

        self.saltDirpath = tempfile.mkdtemp(prefix="salt",
                                            suffix="main",
                                            dir='/tmp')

        pkiDirpath = os.path.join(self.saltDirpath, 'pki')
        if not os.path.exists(pkiDirpath):
            os.makedirs(pkiDirpath)

        acceptedDirpath = os.path.join(pkiDirpath, 'accepted')
        if not os.path.exists(acceptedDirpath):
            os.makedirs(acceptedDirpath)

        pendingDirpath = os.path.join(pkiDirpath, 'pending')
        if not os.path.exists(pendingDirpath):
            os.makedirs(pendingDirpath)

        rejectedDirpath = os.path.join(pkiDirpath, 'rejected')
        if not os.path.exists(rejectedDirpath):
            os.makedirs(rejectedDirpath)

        self.localFilepath = os.path.join(pkiDirpath, 'local.key')
        if os.path.exists(self.localFilepath):
            mode = os.stat(self.localFilepath).st_mode
            os.chmod(self.localFilepath, mode | stat.S_IWUSR | stat.S_IWUSR)

        self.cacheDirpath = os.path.join(self.saltDirpath, 'cache')
        self.sockDirpath = os.path.join(self.saltDirpath, 'sock')

        self.opts = dict(
            __role='master',
            id='master',
            pki_dir=pkiDirpath,
            sock_dir=self.sockDirpath,
            cachedir=self.cacheDirpath,
            open_mode=False,
            auto_accept=True,
            transport='raet',
        )

        self.mainKeeper = RaetKey(opts=self.opts)
        self.baseDirpath = tempfile.mkdtemp(prefix="salt",
                                            suffix="base",
                                            dir='/tmp')
コード例 #15
0
 def __init__(
     self,
     name='',
     version=raeting.VERSION,
     store=None,
     estate=None,
     eid=None,
     ha=("", raeting.RAET_PORT),
     rxMsgs=None,
     txMsgs=None,
     udpRxes=None,
     udpTxes=None,
     road=None,
     safe=None,
 ):
     '''
     Setup StackUdp instance
     '''
     if not name:
         name = "stackUdp{0}".format(StackUdp.Count)
         StackUdp.Count += 1
     self.name = name
     self.version = version
     self.store = store or storing.Store(stamp=0.0)
     self.estates = odict()  # remote estates attached to this stack by eid
     self.eids = odict()  # reverse lookup eid by estate.name
     # local estate for this stack
     self.estate = estate or estating.LocalEstate(
         stack=self, eid=eid, ha=ha)
     self.transactions = odict()  #transactions
     self.rxMsgs = rxMsgs if rxMsgs is not None else deque(
     )  # messages received
     self.txMsgs = txMsgs if txMsgs is not None else deque(
     )  # messages to transmit
     #(msg, deid) deid=0 is broadcast
     self.udpRxes = udpRxes if udpRxes is not None else deque(
     )  # udp packets received
     self.udpTxes = udpTxes if udpTxes is not None else deque(
     )  # udp packet to transmit
     self.road = road or keeping.RoadKeep()
     self.safe = safe or keeping.SafeKeep()
     self.serverUdp = aiding.SocketUdpNb(ha=self.estate.ha,
                                         bufsize=raeting.MAX_MESSAGE_SIZE)
     self.serverUdp.reopen()  # open socket
     self.estate.ha = self.serverUdp.ha  # update estate host address after open
コード例 #16
0
def TestLog(rule=UPDATE):
    """Module Common self test

    """
    storing.Store.Clear()  #clear registry
    logging.Log.Clear()

    store = storing.Store(name='Test', stamp=0.0)
    heading = store.create('pose.heading').create(value=0.0)
    position = store.create('pose.position').create(north=10.0, east=5.0)

    log = logging.Log(name='test',
                      store=store,
                      kind='console',
                      prefix='log',
                      path='./logs/',
                      rule=rule)
    log.addLoggee(tag='heading', loggee='pose.heading')
    log.addLoggee(tag='pos', loggee='pose.position')
    log.resolve()
    log.prepare()

    print("logging log %s to file %s" % (log.name, log.fileName))
    log()  #log
    for i in range(20):
        store.advanceStamp(0.125)
        if i == 5:
            heading.value += 0.0
            position.data.north += 0.0
            position.data.east -= 0.0
        elif i == 10:
            pass
        else:
            heading.value = float(i)
            position.data.north += 2.0
            position.data.east -= 1.5

        log()  #log

    log.close()
コード例 #17
0
    def setUp(self):
        self.store = storing.Store(stamp=0.0)
        self.timer = StoreTimer(store=self.store, duration=1.0)

        self.baseDirpath = os.path.join(
            tempfile.mkdtemp(prefix="raet", suffix="base", dir='/tmp'), 'lane',
            'keep')

        # main stack
        self.main = stacking.LaneStack(name='main',
                                       yid=1,
                                       localname='main',
                                       lanename='cherry',
                                       basedirpath=self.baseDirpath,
                                       sockdirpath=self.baseDirpath)

        #other stack
        self.other = stacking.LaneStack(name='other',
                                        yid=1,
                                        localname='other',
                                        lanename='cherry',
                                        basedirpath=self.baseDirpath,
                                        sockdirpath=self.baseDirpath)
コード例 #18
0
ファイル: signTests.py プロジェクト: westonb7/Reputation
try:
    import simplejson as json
except ImportError:
    import json

sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from src.Reputation.Resource import resource
from src.Reputation.Resource import priming
from src.Reputation.Resource import helping
from src.Reputation.Resource.db import dbing
"""
This script is written to be run from the command line like this:
python3 signTests.py
"""

store = storing.Store(stamp=0.0)


@pytest.fixture
def client():
    return testing.TestClient(resource.create(store=store))


def testOnGet(client):
    priming.setupTest()
    dbing.testDbSetup()
    docA = {'title': 'Error', 'description': 'A valid query is required.'}
    docB = {'title': 'Error', 'description': 'Reputee could not be found.'}
    docC = {
        "reputee": "foo",
        "clout": {
コード例 #19
0
def test(preClearMaster=False,
         preClearMinion=False,
         postClearMaster=False,
         postClearMinion=False):
    '''
    initially
    master on port 7530 with eid of 1
    minion on port 7531 with eid of 0
    eventually
    master eid of 1
    minion eid of 2
    '''
    console.reinit(verbosity=console.Wordage.concise)

    store = storing.Store(stamp=0.0)

    #master stack
    masterName = "master"
    signer = nacling.Signer()
    masterSignKeyHex = signer.keyhex
    privateer = nacling.Privateer()
    masterPriKeyHex = privateer.keyhex
    masterDirpath = os.path.join(os.getcwd(), 'keep', masterName)

    #minion0 stack
    minionName0 = "minion0"
    signer = nacling.Signer()
    minionSignKeyHex = signer.keyhex
    privateer = nacling.Privateer()
    minionPriKeyHex = privateer.keyhex
    m0Dirpath = os.path.join(os.getcwd(), 'keep', minionName0)

    if preClearMaster:
        keeping.clearAllKeepSafe(masterDirpath)
    if preClearMinion:
        keeping.clearAllKeepSafe(m0Dirpath)

    estate = estating.LocalEstate(
        eid=1,
        name=masterName,
        sigkey=masterSignKeyHex,
        prikey=masterPriKeyHex,
    )
    stack0 = stacking.RoadStack(name=masterName,
                                estate=estate,
                                store=store,
                                main=True,
                                dirpath=masterDirpath)

    estate = estating.LocalEstate(
        eid=0,
        name=minionName0,
        ha=("", raeting.RAET_TEST_PORT),
        sigkey=minionSignKeyHex,
        prikey=minionPriKeyHex,
    )
    stack1 = stacking.RoadStack(name=minionName0,
                                estate=estate,
                                store=store,
                                dirpath=m0Dirpath)

    print "\n********* Join Transaction **********"
    stack1.join()
    #timer = StoreTimer(store=store, duration=3.0)
    while stack1.transactions or stack0.transactions:
        stack1.serviceAll()
        stack0.serviceAll()
        if store.stamp >= 0.3:
            for estate in stack0.estates.values():
                if estate.acceptance == raeting.acceptances.pending:
                    stack0.safe.acceptRemote(estate)
        store.advanceStamp(0.1)

    for estate in stack0.estates.values():
        print "Remote Estate {0} joined= {1}".format(estate.eid, estate.joined)
    for estate in stack1.estates.values():
        print "Remote Estate {0} joined= {1}".format(estate.eid, estate.joined)

    print "{0} eid={1}".format(stack0.name, stack0.estate.uid)
    print "{0} estates=\n{1}".format(stack0.name, stack0.estates)
    print "{0} transactions=\n{1}".format(stack0.name, stack0.transactions)
    print "{0} eid={1}".format(stack1.name, stack1.estate.uid)
    print "{0} estates=\n{1}".format(stack1.name, stack1.estates)
    print "{0} transactions=\n{1}".format(stack1.name, stack1.transactions)

    print "Road {0}".format(stack0.name)
    print stack0.road.loadLocalData()
    print stack0.road.loadAllRemoteData()
    print "Safe {0}".format(stack0.name)
    print stack0.safe.loadLocalData()
    print stack0.safe.loadAllRemoteData()
    print

    print "Road {0}".format(stack1.name)
    print stack1.road.loadLocalData()
    print stack1.road.loadAllRemoteData()
    print "Safe {0}".format(stack1.name)
    print stack1.safe.loadLocalData()
    print stack1.safe.loadAllRemoteData()
    print

    print "\n********* Allow Transaction **********"
    if not stack1.estates.values()[0].joined:
        return
    stack1.allow()
    #timer = StoreTimer(store=store, duration=3.0)
    while stack1.transactions or stack0.transactions:
        stack1.serviceAll()
        stack0.serviceAll()
        store.advanceStamp(0.1)

    for estate in stack0.estates.values():
        print "Remote Estate {0} allowed= {1}".format(estate.eid,
                                                      estate.allowed)
    for estate in stack1.estates.values():
        print "Remote Estate {0} allowed= {1}".format(estate.eid,
                                                      estate.allowed)

    print "{0} eid={1}".format(stack0.name, stack0.estate.uid)
    print "{0} estates=\n{1}".format(stack0.name, stack0.estates)
    print "{0} transactions=\n{1}".format(stack0.name, stack0.transactions)
    print "{0} eid={1}".format(stack1.name, stack1.estate.uid)
    print "{0} estates=\n{1}".format(stack1.name, stack1.estates)
    print "{0} transactions=\n{1}".format(stack1.name, stack1.transactions)

    while stack1.transactions or stack0.transactions:
        stack1.serviceAll()
        stack0.serviceAll()
        store.advanceStamp(0.1)

    print "{0} Stats".format(stack0.name)
    for key, val in stack0.stats.items():
        print "   {0}={1}".format(key, val)
    print
    print "{0} Stats".format(stack1.name)
    for key, val in stack1.stats.items():
        print "   {0}={1}".format(key, val)
    print

    stack0.server.close()
    stack1.server.close()

    if postClearMaster:
        keeping.clearAllKeepSafe(masterDirpath)
    if postClearMinion:
        keeping.clearAllKeepSafe(m0Dirpath)
コード例 #20
0
def test():
    '''
    initially
    master on port 7530 with eid of 1
    minion on port 7531 with eid of 0
    eventually
    master eid of 1
    minion eid of 2
    '''
    console.reinit(verbosity=console.Wordage.concise)

    store = storing.Store(stamp=0.0)

    #master stack
    masterName = "master"
    signer = nacling.Signer()
    masterSignKeyHex = signer.keyhex
    privateer = nacling.Privateer()
    masterPriKeyHex = privateer.keyhex
    masterDirpath = os.path.join(os.getcwd(), 'keep', masterName)

    #minion0 stack
    minionName0 = "minion0"
    signer = nacling.Signer()
    minionSignKeyHex = signer.keyhex
    privateer = nacling.Privateer()
    minionPriKeyHex = privateer.keyhex
    m0Dirpath = os.path.join(os.getcwd(), 'keep', minionName0)

    keeping.clearAllKeepSafe(masterDirpath)
    keeping.clearAllKeepSafe(m0Dirpath)

    estate = estating.LocalEstate(
        eid=1,
        name=masterName,
        sigkey=masterSignKeyHex,
        prikey=masterPriKeyHex,
    )
    stack0 = stacking.RoadStack(name=masterName,
                                estate=estate,
                                store=store,
                                main=True,
                                dirpath=masterDirpath)

    estate = estating.LocalEstate(
        eid=0,
        name=minionName0,
        ha=("", raeting.RAET_TEST_PORT),
        sigkey=minionSignKeyHex,
        prikey=minionPriKeyHex,
    )
    stack1 = stacking.RoadStack(name=minionName0,
                                estate=estate,
                                store=store,
                                dirpath=m0Dirpath)

    print "\n********* Join Transaction **********"
    stack1.join()
    #timer = StoreTimer(store=store, duration=3.0)
    while store.stamp < 2.0:
        stack1.serviceAll()
        stack0.serviceAll()
        if store.stamp >= 0.3:
            for estate in stack0.estates.values():
                if estate.acceptance == raeting.acceptances.pending:
                    stack0.safe.acceptRemote(estate)
        store.advanceStamp(0.1)
        time.sleep(0.1)

    for estate in stack0.estates.values():
        print "Remote Estate {0} joined= {1}".format(estate.eid, estate.joined)
    for estate in stack1.estates.values():
        print "Remote Estate {0} joined= {1}".format(estate.eid, estate.joined)

    print "{0} eid={1}".format(stack0.name, stack0.estate.uid)
    print "{0} estates=\n{1}".format(stack0.name, stack0.estates)
    print "{0} transactions=\n{1}".format(stack0.name, stack0.transactions)
    print "{0} eid={1}".format(stack1.name, stack1.estate.uid)
    print "{0} estates=\n{1}".format(stack1.name, stack1.estates)
    print "{0} transactions=\n{1}".format(stack1.name, stack1.transactions)

    print "Road {0}".format(stack0.name)
    print stack0.road.loadLocalData()
    print stack0.road.loadAllRemoteData()
    print "Safe {0}".format(stack0.name)
    print stack0.safe.loadLocalData()
    print stack0.safe.loadAllRemoteData()
    print

    print "Road {0}".format(stack1.name)
    print stack1.road.loadLocalData()
    print stack1.road.loadAllRemoteData()
    print "Safe {0}".format(stack1.name)
    print stack1.safe.loadLocalData()
    print stack1.safe.loadAllRemoteData()
    print

    print "\n********* Allow Transaction **********"
    if not stack1.estates.values()[0].joined:
        return
    stack1.allow()
    #timer = StoreTimer(store=store, duration=3.0)
    while store.stamp < 4.0:
        stack1.serviceAll()
        stack0.serviceAll()
        store.advanceStamp(0.1)
        time.sleep(0.1)

    for estate in stack0.estates.values():
        print "Remote Estate {0} allowed= {1}".format(estate.eid,
                                                      estate.allowed)
    for estate in stack1.estates.values():
        print "Remote Estate {0} allowed= {1}".format(estate.eid,
                                                      estate.allowed)

    print "{0} eid={1}".format(stack0.name, stack0.estate.uid)
    print "{0} estates=\n{1}".format(stack0.name, stack0.estates)
    print "{0} transactions=\n{1}".format(stack0.name, stack0.transactions)
    print "{0} eid={1}".format(stack1.name, stack1.estate.uid)
    print "{0} estates=\n{1}".format(stack1.name, stack1.estates)
    print "{0} transactions=\n{1}".format(stack1.name, stack1.transactions)

    #while stack1.transactions or stack0.transactions:
    #stack1.serviceAll()
    #stack0.serviceAll()
    #store.advanceStamp(0.1)

    print "{0} Stats".format(stack0.name)
    for key, val in stack0.stats.items():
        print "   {0}={1}".format(key, val)
    print
    print "{0} Stats".format(stack1.name)
    for key, val in stack1.stats.items():
        print "   {0}={1}".format(key, val)
    print

    print "\n********* Message Transactions Both Ways Again **********"

    #stack1.transmit(odict(house="Oh Boy1", queue="Nice"))
    #stack1.transmit(odict(house="Oh Boy2", queue="Mean"))
    #stack1.transmit(odict(house="Oh Boy3", queue="Ugly"))
    #stack1.transmit(odict(house="Oh Boy4", queue="Pretty"))

    #stack0.transmit(odict(house="Yeah Baby1", queue="Good"))
    #stack0.transmit(odict(house="Yeah Baby2", queue="Bad"))
    #stack0.transmit(odict(house="Yeah Baby3", queue="Fast"))
    #stack0.transmit(odict(house="Yeah Baby4", queue="Slow"))

    #segmented packets
    stuff = []
    for i in range(300):
        stuff.append(str(i).rjust(10, " "))
    stuff = "".join(stuff)

    stack1.transmit(odict(house="Snake eyes", queue="near stuff", stuff=stuff))
    stack0.transmit(odict(house="Craps", queue="far stuff", stuff=stuff))

    #timer.restart(duration=3)
    while store.stamp < 8.0:  #not timer.expired
        stack1.serviceAll()
        stack0.serviceAll()
        store.advanceStamp(0.1)
        time.sleep(0.1)

    print "{0} eid={1}".format(stack0.name, stack0.estate.uid)
    print "{0} estates=\n{1}".format(stack0.name, stack0.estates)
    print "{0} transactions=\n{1}".format(stack0.name, stack0.transactions)
    print "{0} Received Messages".format(stack0.name)
    for msg in stack0.rxMsgs:
        print msg
    print "{0} Stats".format(stack0.name)
    for key, val in stack0.stats.items():
        print "   {0}={1}".format(key, val)
    print
    print "{0} eid={1}".format(stack1.name, stack1.estate.uid)
    print "{0} estates=\n{1}".format(stack1.name, stack1.estates)
    print "{0} transactions=\n{1}".format(stack1.name, stack1.transactions)
    print "{0} Received Messages".format(stack1.name)
    for msg in stack1.rxMsgs:
        print msg
    print "{0} Stats".format(stack1.name)
    for key, val in stack1.stats.items():
        print "   {0}={1}".format(key, val)
    print

    stack0.server.close()
    stack1.server.close()

    stack0.clearLocal()
    stack0.clearRemoteKeeps()
    stack1.clearLocal()
    stack1.clearRemoteKeeps()
コード例 #21
0
 def setUp(self):
     self.store = storing.Store(stamp=0.0)
     self.timer = StoreTimer(store=self.store, duration=1.0)
コード例 #22
0
 def setUp(self):
     self.store = storing.Store(stamp=0.0)
     self.timer = StoreTimer(store=self.store, duration=1.0)
     self.port = 4506
     self.opts = dict(master_port=self.port)
コード例 #23
0
    def __init__(
        self,
        store=None,
        version=raeting.VERSION,
        main=None,
        puid=None,
        local=None,  #passed up from subclass
        name='',
        uid=None,
        server=None,
        ha=None,
        bufcnt=2,
        rxMsgs=None,
        txMsgs=None,
        rxes=None,
        txes=None,
        stats=None,
    ):
        '''
        Setup Stack instance
        '''
        self.store = store or storing.Store(stamp=0.0)

        self.version = version
        self.main = main

        if getattr(self, 'puid', None) is None:
            self.puid = puid if puid is not None else self.Uid

        self.local = local or lotting.Lot(
            stack=self,
            name=name,
            uid=uid,
            ha=ha,
        )
        self.local.stack = self

        self.remotes = self.uidRemotes = odict()  # remotes indexed by uid
        self.nameRemotes = odict()  # remotes indexed by name

        self.bufcnt = bufcnt
        if not server:
            server = self.serverFromLocal()

        self.server = server
        if self.server:
            if not self.server.reopen():  # open socket
                raise raeting.StackError(
                    "Stack '{0}': Failed opening server at"
                    " '{1}'\n".format(self.name, self.server.ha))

            self.ha = self.server.ha  # update local host address after open

            console.verbose("Stack '{0}': Opened server at '{1}'\n".format(
                self.name, self.ha))

        self.rxMsgs = rxMsgs if rxMsgs is not None else deque(
        )  # messages received
        self.txMsgs = txMsgs if txMsgs is not None else deque(
        )  # messages to transmit
        self.rxes = rxes if rxes is not None else deque(
        )  # udp packets received
        self.txes = txes if txes is not None else deque(
        )  # udp packet to transmit
        self.stats = stats if stats is not None else odict()  # udp statistics
        self.statTimer = aiding.StoreTimer(self.store)
コード例 #24
0
ファイル: test_keeping.py プロジェクト: da4089/raet
    def setUp(self):
        self.store = storing.Store(stamp=0.0)
        self.timer = StoreTimer(store=self.store, duration=1.0)

        self.base = tempfile.mkdtemp(prefix="raet",  suffix="base", dir='/tmp')
コード例 #25
0
    def testValetServiceBottleStream(self):
        """
        Test Valet WSGI service request response stream sse
        """
        console.terse("{0}\n".format(
            self.testValetServiceBottleStream.__doc__))

        try:
            import bottle
        except ImportError as ex:
            console.terse("Bottle not available.\n")
            return

        store = storing.Store(stamp=0.0)

        app = bottle.default_app()  # create bottle app

        @app.get('/stream')
        def streamGet():
            """
            Create test server sent event stream that sends count events
            """
            timer = StoreTimer(store, duration=2.0)
            bottle.response.set_header('Content-Type',
                                       'text/event-stream')  #text
            bottle.response.set_header('Cache-Control', 'no-cache')
            # HTTP 1.1 servers detect text/event-stream and use Transfer-Encoding: chunked
            # Set client-side auto-reconnect timeout, ms.
            yield 'retry: 1000\n\n'
            i = 0
            yield 'id: {0}\n'.format(i)
            i += 1
            yield 'data: START\n\n'
            n = 1
            while not timer.expired:
                yield 'id: {0}\n'.format(i)
                i += 1
                yield 'data: {0}\n\n'.format(n)
                n += 1
            yield "data: END\n\n"

        console.terse("{0}\n".format("Building Valet ...\n"))
        wireLogAlpha = wiring.WireLog(buffify=True, same=True)
        result = wireLogAlpha.reopen()

        alpha = serving.Valet(port=6101,
                              bufsize=131072,
                              wlog=wireLogAlpha,
                              store=store,
                              app=app)
        self.assertIs(alpha.servant.reopen(), True)
        self.assertEqual(alpha.servant.ha, ('0.0.0.0', 6101))
        self.assertEqual(alpha.servant.eha, ('127.0.0.1', 6101))

        console.terse("{0}\n".format("Building Patron ...\n"))
        wireLogBeta = wiring.WireLog(buffify=True, same=True)
        result = wireLogBeta.reopen()

        path = "http://{0}:{1}/".format('localhost', alpha.servant.eha[1])

        beta = clienting.Patron(
            bufsize=131072,
            wlog=wireLogBeta,
            store=store,
            path=path,
            reconnectable=True,
        )

        self.assertIs(beta.connector.reopen(), True)
        self.assertIs(beta.connector.accepted, False)
        self.assertIs(beta.connector.connected, False)
        self.assertIs(beta.connector.cutoff, False)

        request = odict([
            ('method', u'GET'),
            ('path', u'/stream'),
            ('qargs', odict()),
            ('fragment', u''),
            ('headers',
             odict([('Accept', 'application/json'), ('Content-Length', 0)])),
            ('body', None),
        ])

        beta.requests.append(request)
        timer = StoreTimer(store, duration=1.0)
        while (not timer.expired):
            alpha.serviceAll()
            time.sleep(0.05)
            beta.serviceAll()
            time.sleep(0.05)
            store.advanceStamp(0.1)

        self.assertIs(beta.connector.accepted, True)
        self.assertIs(beta.connector.connected, True)
        self.assertIs(beta.connector.cutoff, False)

        self.assertEqual(len(alpha.servant.ixes), 1)
        self.assertEqual(len(alpha.reqs), 1)
        self.assertEqual(len(alpha.reps), 1)
        requestant = alpha.reqs.values()[0]
        self.assertEqual(requestant.method, request['method'])
        self.assertEqual(requestant.url, request['path'])
        self.assertEqual(
            requestant.headers, {
                'accept': 'application/json',
                'accept-encoding': 'identity',
                'content-length': '0',
                'host': 'localhost:6101'
            })

        #timed out while stream still open so no responses in .responses
        self.assertIs(beta.waited, True)
        self.assertIs(beta.respondent.ended, False)
        self.assertEqual(len(beta.responses), 0)
        self.assertIn('content-type', beta.respondent.headers)
        self.assertEqual(beta.respondent.headers['content-type'],
                         'text/event-stream')
        self.assertIn('transfer-encoding', beta.respondent.headers)
        self.assertEqual(beta.respondent.headers['transfer-encoding'],
                         'chunked')

        self.assertTrue(len(beta.events) >= 3)
        self.assertEqual(beta.respondent.retry, 1000)
        self.assertTrue(int(beta.respondent.leid) >= 2)
        event = beta.events.popleft()
        self.assertEqual(event, {'id': '0', 'name': '', 'data': 'START'})
        event = beta.events.popleft()
        self.assertEqual(event, {'id': '1', 'name': '', 'data': '1'})
        event = beta.events.popleft()
        self.assertEqual(event, {'id': '2', 'name': '', 'data': '2'})
        beta.events.clear()

        #keep going until ended
        timer.restart(duration=1.5)
        while (not timer.expired):
            alpha.serviceAll()
            time.sleep(0.05)
            beta.serviceAll()
            time.sleep(0.05)
            store.advanceStamp(0.1)

        self.assertTrue(len(beta.events) >= 3)
        self.assertEqual(beta.respondent.leid, '9')
        self.assertEqual(beta.events[-2], {'id': '9', 'name': '', 'data': '9'})
        self.assertEqual(beta.events[-1], {
            'id': '9',
            'name': '',
            'data': 'END'
        })
        beta.events.clear()

        alpha.servant.closeAll()
        beta.connector.close()

        wireLogAlpha.close()
        wireLogBeta.close()
コード例 #26
0
ファイル: test_stalenack.py プロジェクト: KennethWilke/raet
def test():
    '''
    initially
    master on port 7530 with eid of 1
    minion on port 7531 with eid of 0
    eventually
    master eid of 1
    minion eid of 2
    '''
    console.reinit(verbosity=console.Wordage.concise)

    store = storing.Store(stamp=0.0)

    #master stack
    masterName = "master"
    signer = nacling.Signer()
    masterSignKeyHex = signer.keyhex
    privateer = nacling.Privateer()
    masterPriKeyHex = privateer.keyhex
    dirpathMaster = os.path.join(os.getcwd(), 'keep', masterName)

    #minion0 stack
    minionName0 = "minion0"
    signer = nacling.Signer()
    minionSignKeyHex = signer.keyhex
    privateer = nacling.Privateer()
    minionPriKeyHex = privateer.keyhex
    dirpathMinion0 = os.path.join(os.getcwd(), 'keep', minionName0)

    keeping.clearAllKeepSafe(dirpathMaster)
    keeping.clearAllKeepSafe(dirpathMinion0)

    estate = estating.LocalEstate(
        eid=1,
        name=masterName,
        sigkey=masterSignKeyHex,
        prikey=masterPriKeyHex,
    )
    stack0 = stacking.RoadStack(estate=estate,
                                store=store,
                                auto=True,
                                main=True,
                                dirpath=dirpathMaster)

    estate = estating.LocalEstate(
        eid=0,
        name=minionName0,
        ha=("", raeting.RAET_TEST_PORT),
        sigkey=minionSignKeyHex,
        prikey=minionPriKeyHex,
    )
    stack1 = stacking.RoadStack(estate=estate,
                                store=store,
                                dirpath=dirpathMinion0)

    print "\n********* Join Transaction **********"
    stack1.join()
    timer = Timer(duration=2)
    timer.restart(duration=2)
    while not timer.expired:
        stack1.serviceAll()
        stack0.serviceAll()
    for estate in stack0.estates.values():
        print "Remote Estate {0} joined= {1}".format(estate.eid, estate.joined)
    for estate in stack1.estates.values():
        print "Remote Estate {0} joined= {1}".format(estate.eid, estate.joined)

    print "\n********* Allow Transaction **********"
    stack1.allow()
    timer.restart(duration=2)
    while not timer.expired:
        stack1.serviceAll()
        stack0.serviceAll()
        store.advanceStamp(0.1)

    for estate in stack0.estates.values():
        print "Remote Estate {0} allowed= {1}".format(estate.eid,
                                                      estate.allowed)
    for estate in stack1.estates.values():
        print "Remote Estate {0} allowed= {1}".format(estate.eid,
                                                      estate.allowed)

    print "\n********* Message Transactions Both Ways **********"
    #stack1.transmit(odict(house="Oh Boy1", queue="Nice"))
    stack0.transmit(odict(house="Yeah Baby1", queue="Good"))

    timer.restart(duration=1)
    while not timer.expired:
        stack0.serviceAllTx()

    timer.restart(duration=1)
    while not timer.expired:
        stack1.serviceRx()

    print "{0} Received Messages".format(stack1.name)
    for msg in stack1.rxMsgs:
        print msg
    print

    stack0.transactions = odict()  #clear transactions

    timer.restart(duration=2)
    while not timer.expired:
        stack1.serviceAllTx()
        stack0.serviceRx()

    print "{0} Received Messages".format(stack0.name)
    for msg in stack0.rxMsgs:
        print msg
    print

    print "{0} Stats".format(stack0.name)
    for key, val in stack0.stats.items():
        print "   {0}={1}".format(key, val)
    print
    print "{0} Stats".format(stack1.name)
    for key, val in stack1.stats.items():
        print "   {0}={1}".format(key, val)
    print

    stack0.server.close()
    stack1.server.close()

    stack0.clearLocal()
    stack0.clearRemoteKeeps()
    stack1.clearLocal()
    stack1.clearRemoteKeeps()
コード例 #27
0
    def testPorterServiceEcho(self):
        """
        Test Porter service request response of echo non blocking
        """
        console.terse("{0}\n".format(self.testPorterServiceEcho.__doc__))

        store = storing.Store(stamp=0.0)

        console.terse("{0}\n".format("Building Valet ...\n"))
        wireLogAlpha = wiring.WireLog(buffify=True, same=True)
        result = wireLogAlpha.reopen()

        alpha = serving.Porter(port=6101,
                               bufsize=131072,
                               wlog=wireLogAlpha,
                               store=store)
        self.assertIs(alpha.servant.reopen(), True)
        self.assertEqual(alpha.servant.ha, ('0.0.0.0', 6101))
        self.assertEqual(alpha.servant.eha, ('127.0.0.1', 6101))

        console.terse("{0}\n".format("Building Patron ...\n"))
        wireLogBeta = wiring.WireLog(buffify=True, same=True)
        result = wireLogBeta.reopen()

        path = "http://{0}:{1}/".format('localhost', alpha.servant.eha[1])

        beta = clienting.Patron(
            bufsize=131072,
            wlog=wireLogBeta,
            store=store,
            path=path,
            reconnectable=True,
        )

        self.assertIs(beta.connector.reopen(), True)
        self.assertIs(beta.connector.accepted, False)
        self.assertIs(beta.connector.connected, False)
        self.assertIs(beta.connector.cutoff, False)

        request = odict([
            ('method', u'GET'),
            ('path', u'/echo?name=fame'),
            ('qargs', odict()),
            ('fragment', u''),
            ('headers',
             odict([('Accept', 'application/json'), ('Content-Length', 0)])),
        ])

        beta.requests.append(request)

        while (beta.requests or beta.connector.txes or not beta.responses
               or not alpha.servant.ixes or not alpha.idle()):
            alpha.serviceAll()
            time.sleep(0.05)
            beta.serviceAll()
            time.sleep(0.05)

        self.assertIs(beta.connector.accepted, True)
        self.assertIs(beta.connector.connected, True)
        self.assertIs(beta.connector.cutoff, False)

        self.assertEqual(len(alpha.servant.ixes), 1)
        self.assertEqual(len(alpha.stewards), 1)
        requestant = alpha.stewards.values()[0].requestant
        self.assertEqual(requestant.method, request['method'])
        self.assertEqual(requestant.url, request['path'])
        self.assertEqual(
            requestant.headers, {
                'accept': 'application/json',
                'accept-encoding': 'identity',
                'content-length': '0',
                'host': 'localhost:6101'
            })

        self.assertEqual(len(beta.responses), 1)
        response = beta.responses.popleft()
        self.assertEqual(
            response['data'], {
                'body': '',
                'data': None,
                'fragment': '',
                'headers': {
                    'accept': 'application/json',
                    'accept-encoding': 'identity',
                    'content-length': '0',
                    'host': 'localhost:6101'
                },
                'method': 'GET',
                'path': '/echo',
                'qargs': {
                    'name': 'fame'
                },
                'version': 'HTTP/1.1'
            })

        responder = alpha.stewards.values()[0].responder
        self.assertEqual(responder.status, response['status'])
        self.assertEqual(responder.headers, response['headers'])

        alpha.servant.closeAll()
        beta.connector.close()

        wireLogAlpha.close()
        wireLogBeta.close()
コード例 #28
0
    def __init__(self,
                 name='',
                 version=raeting.VERSION,
                 store=None,
                 keep=None,
                 dirpath='',
                 basedirpath='',
                 local=None,
                 localname='',
                 bufcnt=2,
                 server=None,
                 rxMsgs=None,
                 txMsgs=None,
                 rxes=None,
                 txes=None,
                 stats=None,
                 clean=False,
                 ):
        '''
        Setup Stack instance
        '''
        if not name:
            name = "stack{0}".format(Stack.Count)
            Stack.Count += 1

        self.name = name
        self.version = version
        self.store = store or storing.Store(stamp=0.0)

        self.keep = keep or keeping.LotKeep(dirpath=dirpath,
                                            basedirpath=basedirpath,
                                            stackname=self.name)

        if clean: # clear persisted data so uses provided or default data
            self.clearLocal()
            self.clearRemoteKeeps()

        self.loadLocal(local=local, name=localname) # load local data from saved data else passed in local
        self.remotes = odict() # remotes indexed by uid
        self.uids = odict() # remote uids indexed by name
        self.loadRemotes() # load remotes from saved data

        for remote in self.remotes.values():
            remote.nextSid()

        self.bufcnt = bufcnt
        if not server:
            server = self.serverFromLocal()

        self.server = server
        if self.server:
            if not self.server.reopen():  # open socket
                raise raeting.StackError("Stack '{0}': Failed opening server at"
                            " '{1}'\n".format(self.name, self.server.ha))
            if self.local:
                self.local.ha = self.server.ha  # update local host address after open

            console.verbose("Stack '{0}': Opened server at '{1}'\n".format(self.name, self.local.ha))

        self.rxMsgs = rxMsgs if rxMsgs is not None else deque() # messages received
        self.txMsgs = txMsgs if txMsgs is not None else deque() # messages to transmit
        self.rxes = rxes if rxes is not None else deque() # udp packets received
        self.txes = txes if txes is not None else deque() # udp packet to transmit
        self.stats = stats if stats is not None else odict() # udp statistics
        self.statTimer = aiding.StoreTimer(self.store)

        self.dumpLocal() # save local data
        self.dumpRemotes() # save remote data
コード例 #29
0
    def testValetServiceBasicSecure(self):
        """
        Test Valet WSGI service with secure TLS request response
        """
        console.terse("{0}\n".format(self.testValetServiceBasicSecure.__doc__))

        store = storing.Store(stamp=0.0)

        def wsgiApp(environ, start_response):
            start_response('200 OK', [('Content-type', 'text/plain'),
                                      ('Content-length', '12')])
            return [b"Hello World!"]

        console.terse("{0}\n".format("Building Valet ...\n"))
        wireLogAlpha = wiring.WireLog(buffify=True, same=True)
        result = wireLogAlpha.reopen()

        serverCertCommonName = 'localhost'  # match hostname uses servers's cert commonname
        #serverKeypath = '/etc/pki/tls/certs/server_key.pem'  # local server private key
        #serverCertpath = '/etc/pki/tls/certs/server_cert.pem'  # local server public cert
        #clientCafilepath = '/etc/pki/tls/certs/client.pem' # remote client public cert

        serverKeypath = self.certdirpath + '/server_key.pem'  # local server private key
        serverCertpath = self.certdirpath + '/server_cert.pem'  # local server public cert
        clientCafilepath = self.certdirpath + '/client.pem'  # remote client public cert

        alpha = serving.Valet(
            port=6101,
            bufsize=131072,
            wlog=wireLogAlpha,
            store=store,
            app=wsgiApp,
            scheme='https',
            keypath=serverKeypath,
            certpath=serverCertpath,
            cafilepath=clientCafilepath,
        )

        self.assertIs(alpha.servant.reopen(), True)
        self.assertEqual(alpha.servant.ha, ('0.0.0.0', 6101))
        self.assertEqual(alpha.servant.eha, ('127.0.0.1', 6101))

        console.terse("{0}\n".format("Building Patron ...\n"))
        wireLogBeta = wiring.WireLog(buffify=True, same=True)
        result = wireLogBeta.reopen()

        #clientKeypath = '/etc/pki/tls/certs/client_key.pem'  # local client private key
        #clientCertpath = '/etc/pki/tls/certs/client_cert.pem'  # local client public cert
        #serverCafilepath = '/etc/pki/tls/certs/server.pem' # remote server public cert

        clientKeypath = self.certdirpath + '/client_key.pem'  # local client private key
        clientCertpath = self.certdirpath + '/client_cert.pem'  # local client public cert
        serverCafilepath = self.certdirpath + '/server.pem'  # remote server public cert

        path = "https://{0}:{1}/".format('localhost', alpha.servant.eha[1])

        beta = clienting.Patron(bufsize=131072,
                                wlog=wireLogBeta,
                                store=store,
                                path=path,
                                reconnectable=True,
                                scheme='https',
                                certedhost=serverCertCommonName,
                                keypath=clientKeypath,
                                certpath=clientCertpath,
                                cafilepath=serverCafilepath)

        self.assertIs(beta.connector.reopen(), True)
        self.assertIs(beta.connector.accepted, False)
        self.assertIs(beta.connector.connected, False)
        self.assertIs(beta.connector.cutoff, False)

        request = odict([
            ('method', u'GET'),
            ('path', u'/echo?name=fame'),
            ('qargs', odict()),
            ('fragment', u''),
            ('headers',
             odict([('Accept', 'application/json'), ('Content-Length', 0)])),
        ])

        beta.requests.append(request)

        while (beta.requests or beta.connector.txes or not beta.responses
               or not alpha.idle()):
            alpha.serviceAll()
            time.sleep(0.05)
            beta.serviceAll()
            time.sleep(0.05)

        self.assertIs(beta.connector.accepted, True)
        self.assertIs(beta.connector.connected, True)
        self.assertIs(beta.connector.cutoff, False)

        self.assertEqual(len(alpha.servant.ixes), 1)
        self.assertEqual(len(alpha.reqs), 1)
        self.assertEqual(len(alpha.reps), 1)
        requestant = alpha.reqs.values()[0]
        self.assertEqual(requestant.method, request['method'])
        self.assertEqual(requestant.url, request['path'])
        self.assertEqual(
            requestant.headers, {
                'accept': 'application/json',
                'accept-encoding': 'identity',
                'content-length': '0',
                'host': 'localhost:6101'
            })

        self.assertEqual(len(beta.responses), 1)
        response = beta.responses.popleft()
        self.assertEqual(response['body'], bytearray(b'Hello World!'))
        self.assertEqual(response['status'], 200)

        responder = alpha.reps.values()[0]
        self.assertTrue(responder.status.startswith, str(response['status']))
        self.assertEqual(responder.headers, response['headers'])

        alpha.servant.closeAll()
        beta.connector.close()

        wireLogAlpha.close()
        wireLogBeta.close()
コード例 #30
0
    def testValetServiceBottleSecure(self):
        """
        Test Valet WSGI service secure TLS request response
        """
        console.terse("{0}\n".format(
            self.testValetServiceBottleSecure.__doc__))

        try:
            import bottle
        except ImportError as ex:
            console.terse("Bottle not available.\n")
            return

        store = storing.Store(stamp=0.0)

        app = bottle.default_app()  # create bottle app

        @app.get('/echo')
        @app.get('/echo/<action>')
        @app.post('/echo')
        @app.post('/echo/<action>')
        def echoGet(action=None):
            """
            Echo back request data
            """
            query = dict(bottle.request.query.items())
            body = bottle.request.json
            raw = bottle.request.body.read()
            form = odict(bottle.request.forms)

            data = odict(verb=bottle.request.method,
                         url=bottle.request.url,
                         action=action,
                         query=query,
                         form=form,
                         content=body)
            return data

        console.terse("{0}\n".format("Building Valet ...\n"))
        wireLogAlpha = wiring.WireLog(buffify=True, same=True)
        result = wireLogAlpha.reopen()

        serverCertCommonName = 'localhost'  # match hostname uses servers's cert commonname
        #serverKeypath = '/etc/pki/tls/certs/server_key.pem'  # local server private key
        #serverCertpath = '/etc/pki/tls/certs/server_cert.pem'  # local server public cert
        #clientCafilepath = '/etc/pki/tls/certs/client.pem' # remote client public cert

        serverKeypath = self.certdirpath + '/server_key.pem'  # local server private key
        serverCertpath = self.certdirpath + '/server_cert.pem'  # local server public cert
        clientCafilepath = self.certdirpath + '/client.pem'  # remote client public cert

        alpha = serving.Valet(
            port=6101,
            bufsize=131072,
            wlog=wireLogAlpha,
            store=store,
            app=app,
            scheme='https',
            keypath=serverKeypath,
            certpath=serverCertpath,
            cafilepath=clientCafilepath,
        )
        self.assertIs(alpha.servant.reopen(), True)
        self.assertEqual(alpha.servant.ha, ('0.0.0.0', 6101))
        self.assertEqual(alpha.servant.eha, ('127.0.0.1', 6101))

        console.terse("{0}\n".format("Building Patron ...\n"))
        wireLogBeta = wiring.WireLog(buffify=True, same=True)
        result = wireLogBeta.reopen()

        #clientKeypath = '/etc/pki/tls/certs/client_key.pem'  # local client private key
        #clientCertpath = '/etc/pki/tls/certs/client_cert.pem'  # local client public cert
        #serverCafilepath = '/etc/pki/tls/certs/server.pem' # remote server public cert

        clientKeypath = self.certdirpath + '/client_key.pem'  # local client private key
        clientCertpath = self.certdirpath + '/client_cert.pem'  # local client public cert
        serverCafilepath = self.certdirpath + '/server.pem'  # remote server public cert

        path = "https://{0}:{1}/".format('localhost', alpha.servant.eha[1])

        beta = clienting.Patron(bufsize=131072,
                                wlog=wireLogBeta,
                                store=store,
                                path=path,
                                reconnectable=True,
                                scheme='https',
                                certedhost=serverCertCommonName,
                                keypath=clientKeypath,
                                certpath=clientCertpath,
                                cafilepath=serverCafilepath)

        self.assertIs(beta.connector.reopen(), True)
        self.assertIs(beta.connector.accepted, False)
        self.assertIs(beta.connector.connected, False)
        self.assertIs(beta.connector.cutoff, False)

        request = odict([
            ('method', u'GET'),
            ('path', u'/echo?name=fame'),
            ('qargs', odict()),
            ('fragment', u''),
            ('headers',
             odict([('Accept', 'application/json'), ('Content-Length', 0)])),
        ])

        beta.requests.append(request)
        timer = StoreTimer(store, duration=1.0)
        while (beta.requests or beta.connector.txes or not beta.responses
               or not alpha.idle()):
            alpha.serviceAll()
            time.sleep(0.05)
            beta.serviceAll()
            time.sleep(0.05)
            store.advanceStamp(0.1)

        self.assertIs(beta.connector.accepted, True)
        self.assertIs(beta.connector.connected, True)
        self.assertIs(beta.connector.cutoff, False)

        self.assertEqual(len(alpha.servant.ixes), 1)
        self.assertEqual(len(alpha.reqs), 1)
        self.assertEqual(len(alpha.reps), 1)
        requestant = alpha.reqs.values()[0]
        self.assertEqual(requestant.method, request['method'])
        self.assertEqual(requestant.url, request['path'])
        self.assertEqual(
            requestant.headers, {
                'accept': 'application/json',
                'accept-encoding': 'identity',
                'content-length': '0',
                'host': 'localhost:6101'
            })

        self.assertEqual(len(beta.responses), 1)
        response = beta.responses.popleft()
        self.assertEqual(response['status'], 200)
        self.assertEqual(response['reason'], 'OK')
        self.assertEqual(response['body'], bytearray(b''))
        self.assertEqual(
            response['data'],
            {
                'action': None,
                'content': None,
                'form': {},
                'query': {
                    'name': 'fame'
                },
                'url': 'https://localhost:6101/echo?name=fame',
                'verb': 'GET'
            },
        )

        responder = alpha.reps.values()[0]
        self.assertTrue(responder.status.startswith, str(response['status']))
        self.assertEqual(responder.headers, response['headers'])

        alpha.servant.closeAll()
        beta.connector.close()

        wireLogAlpha.close()
        wireLogBeta.close()