def joinAll(self, initiator, correspondentAddresses, timeout=None, retryCount=1): ''' Utility method to do join. :param initiator: join initiator stack :param correspondentAddresses: an iterable object containing ha tuples :param timeout: join timeout, will be passed to stack :param retryCount: retry join max the specified count of times until success ''' console.terse("\nJoin Transaction **************\n") addresses = list(correspondentAddresses) for ha in addresses: initiator.addRemote(estating.RemoteEstate(stack=initiator, fuid=0, # vacuous join sid=0, # always 0 for join ha=ha)) while retryCount > 0: done = True for remote in initiator.remotes.values(): if not remote.joined: done = False initiator.join(uid=remote.uid, timeout=timeout) self.serviceOne(initiator) if done: break retryCount -= 1
def join(self, timeout=None): ''' Utility method to do join. Call from test method. ''' console.terse("\nJoin Transaction **************\n") if not self.other.remotes: self.other.addRemote(estating.RemoteEstate(stack=self.other, #name=self.main.local.name, fuid=0, # vacuous join sid=0, # always 0 for join ha=self.main.local.ha) ) self.other.join(timeout=timeout) self.service()
def join(self, initiator, correspondent, deid=None, duration=1.0, cascade=False, renewal=False): ''' Utility method to do join. Call from test method. ''' console.terse("\nJoin Transaction **************\n") if not initiator.remotes: remote = initiator.addRemote( estating.RemoteEstate( stack=initiator, fuid=0, # vacuous join sid=0, # always 0 for join ha=correspondent.local.ha)) deid = remote.uid initiator.join(uid=deid, cascade=cascade, renewal=renewal) self.serviceStacks([correspondent, initiator], duration=duration)
def setUp(self): self.store = Store(stamp=0.0) self.timer = StoreTimer(store=self.store, duration=1.0) self.dirpathBase = tempfile.mkdtemp(prefix="raet", suffix="base", dir=TEMPDIR) stacking.RoadStack.Bk = raeting.BodyKind.json.value #main stack mainName = "main" mainDirpath = os.path.join(self.dirpathBase, 'road', 'keep', mainName) signer = nacling.Signer() mainSignKeyHex = signer.keyhex mainVerKeyHex = signer.verhex privateer = nacling.Privateer() mainPriKeyHex = privateer.keyhex mainPubKeyHex = privateer.pubhex #other stack otherName = "other" otherDirpath = os.path.join(self.dirpathBase, 'road', 'keep', otherName) signer = nacling.Signer() otherSignKeyHex = signer.keyhex otherVerKeyHex = signer.verhex privateer = nacling.Privateer() otherPriKeyHex = privateer.keyhex otherPubKeyHex = privateer.pubhex keeping.clearAllKeep(mainDirpath) keeping.clearAllKeep(otherDirpath) self.main = stacking.RoadStack(name=mainName, uid=1, sigkey=mainSignKeyHex, prikey=mainPriKeyHex, auto=raeting.AutoMode.once.value, main=True, dirpath=mainDirpath, store=self.store) remote1 = estating.RemoteEstate( stack=self.main, uid=2, name=otherName, ha=("127.0.0.1", raeting.RAET_TEST_PORT), verkey=otherVerKeyHex, pubkey=otherPubKeyHex, ) self.main.addRemote(remote1) self.other = stacking.RoadStack(name=otherName, uid=2, auto=raeting.AutoMode.once.value, ha=("", raeting.RAET_TEST_PORT), sigkey=otherSignKeyHex, prikey=otherPriKeyHex, dirpath=otherDirpath, store=self.store) remote0 = estating.RemoteEstate( stack=self.other, uid=3, name=mainName, ha=('127.0.0.1', raeting.RAET_PORT), verkey=mainVerKeyHex, pubkey=mainPubKeyHex, ) self.other.addRemote(remote0) remote0.publee = nacling.Publican(key=remote1.privee.pubhex) remote1.publee = nacling.Publican(key=remote0.privee.pubhex) stuff = [] for i in range(300): stuff.append(str(i).rjust(4, " ")) self.stuff = ns2b("".join(stuff)) self.data = odict(hk=raeting.HeadKind.raet.value)
def test(): ''' Test keeping. ''' pkiDirpath = os.path.join('/tmp', 'raet', 'keyo', 'master', '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) localFilepath = os.path.join(pkiDirpath, 'local.key') if os.path.exists(localFilepath): mode = os.stat(localFilepath).st_mode print mode os.chmod(localFilepath, mode | stat.S_IWUSR | stat.S_IRUSR) mode = os.stat(localFilepath).st_mode print mode cacheDirpath = os.path.join('/tmp/raet', 'cache', 'master') if not os.path.exists(cacheDirpath): os.makedirs(cacheDirpath) sockDirpath = os.path.join('/tmp/raet', 'sock', 'master') if not os.path.exists(sockDirpath): os.makedirs(sockDirpath) opts = dict( pki_dir=pkiDirpath, sock_dir=sockDirpath, cachedir=cacheDirpath, open_mode=True, auto_accept=True, transport='raet', ) masterSafe = salting.SaltSafe(opts=opts) print("masterSafe local =\n{0}".format(masterSafe.loadLocalData())) print("masterSafe remote =\n{0}".format(masterSafe.loadAllRemoteData())) pkiDirpath = os.path.join('/tmp', 'raet', 'keyo', 'minion', '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) localFilepath = os.path.join(pkiDirpath, 'local.key') if os.path.exists(localFilepath): mode = os.stat(localFilepath).st_mode print mode os.chmod(localFilepath, mode | stat.S_IWUSR | stat.S_IRUSR) mode = os.stat(localFilepath).st_mode print mode cacheDirpath = os.path.join('/tmp/raet', 'cache', 'minion') sockDirpath = os.path.join('/tmp/raet', 'sock', 'minion') opts = dict( pki_dir=pkiDirpath, sock_dir=sockDirpath, cachedir=cacheDirpath, open_mode=True, auto_accept=True, transport='raet', ) minionSafe = salting.SaltSafe(opts=opts) print("minionSafe local =\n{0}".format(minionSafe.loadLocalData())) print("minionSafe remote =\n{0}".format(minionSafe.loadAllRemoteData())) masterName = 'master' masterDirpath = os.path.join('/tmp', 'raet', 'keep', masterName) signer = nacling.Signer() masterSignKeyHex = signer.keyhex masterVerKeyHex = signer.verhex privateer = nacling.Privateer() masterPriKeyHex = privateer.keyhex masterPubKeyHex = privateer.pubhex m1Name = 'minion1' m1Dirpath = os.path.join('/tmp', 'raet', 'keep', m1Name) signer = nacling.Signer() m1SignKeyHex = signer.keyhex m1VerKeyHex = signer.verhex privateer = nacling.Privateer() m1PriKeyHex = privateer.keyhex m1PubKeyHex = privateer.pubhex m2Name = 'minion2' signer = nacling.Signer() m2SignKeyHex = signer.keyhex m2VerKeyHex = signer.verhex privateer = nacling.Privateer() m2PriKeyHex = privateer.keyhex m2PubKeyHex = privateer.pubhex m3Name = 'minion3' signer = nacling.Signer() m3SignKeyHex = signer.keyhex m3VerKeyHex = signer.verhex privateer = nacling.Privateer() m3PriKeyHex = privateer.keyhex m3PubKeyHex = privateer.pubhex keeping.clearAllKeepSafe(masterDirpath) keeping.clearAllKeepSafe(m1Dirpath) #saltsafe = salting.SaltSafe(opts=opts) #print saltsafe.loadLocalData() #print saltsafe.loadAllRemoteData() #master stack local = estating.LocalEstate( eid=1, name=masterName, sigkey=masterSignKeyHex, prikey=masterPriKeyHex, ) stack0 = stacking.RoadStack(local=local, dirpath=masterDirpath, safe=masterSafe) stack0.addRemote( estating.RemoteEstate( eid=2, name=m1Name, ha=('127.0.0.1', 7532), verkey=m1VerKeyHex, pubkey=m1PubKeyHex, )) stack0.addRemote( estating.RemoteEstate( eid=3, name=m2Name, ha=('127.0.0.1', 7533), verkey=m2VerKeyHex, pubkey=m2PubKeyHex, )) #minion stack local = estating.LocalEstate( eid=2, name=m1Name, ha=("", raeting.RAET_TEST_PORT), sigkey=m1SignKeyHex, prikey=m1PriKeyHex, ) stack1 = stacking.RoadStack(local=local, dirpath=m1Dirpath, safe=minionSafe) stack1.addRemote( estating.RemoteEstate( eid=1, name=masterName, ha=('127.0.0.1', 7532), verkey=masterVerKeyHex, pubkey=masterPubKeyHex, )) stack1.addRemote( estating.RemoteEstate( eid=4, name=m3Name, ha=('127.0.0.1', 7534), verkey=m3VerKeyHex, pubkey=m3PubKeyHex, )) #stack0.clearLocal() stack0.clearRemoteKeeps() #stack1.clearLocal() stack1.clearRemoteKeeps() stack0.dumpLocal() stack0.dumpRemotes() stack1.dumpLocal() stack1.dumpRemotes() print "Road {0}".format(stack0.name) print stack0.keep.loadLocalData() print stack0.keep.loadAllRemoteData() print "Safe {0}".format(stack0.name) print stack0.safe.loadLocalData() print stack0.safe.loadAllRemoteData() print print "Road {0}".format(stack1.name) print stack1.keep.loadLocalData() print stack1.keep.loadAllRemoteData() print "Safe {0}".format(stack1.name) print stack1.safe.loadLocalData() print stack1.safe.loadAllRemoteData() stack0.server.close() stack1.server.close() #master stack dirpath = os.path.join('/tmp/raet', 'keep', 'master') local = estating.LocalEstate( eid=1, name='master', sigkey=masterSignKeyHex, prikey=masterPriKeyHex, ) stack0 = stacking.RoadStack(local=local, dirpath=masterDirpath, safe=masterSafe) #minion stack dirpath = os.path.join('/tmp/raet', 'keep', 'minion1') local = estating.LocalEstate( eid=2, name='minion1', ha=("", raeting.RAET_TEST_PORT), sigkey=m1SignKeyHex, prikey=m1PriKeyHex, ) stack1 = stacking.RoadStack(local=local, dirpath=m1Dirpath, safe=minionSafe) stack0.loadLocal() print stack0.local.name, stack0.local.eid, stack0.local.sid, stack0.local.ha, stack0.local.signer, stack0.local.priver stack1.loadLocal() print stack1.local.name, stack1.local.eid, stack1.local.sid, stack1.local.ha, stack1.local.signer, stack1.local.priver stack0.clearLocal() stack0.clearRemoteKeeps() stack1.clearLocal() stack1.clearRemoteKeeps()
def setUp(self): self.store = storing.Store(stamp=0.0) self.timer = StoreTimer(store=self.store, duration=1.0) self.dirpathBase = tempfile.mkdtemp(prefix="raet", suffix="base", dir='/tmp') stacking.RoadStack.Bk = raeting.bodyKinds.json #main stack mainName = "main" mainDirpath = os.path.join(self.dirpathBase, 'road', 'keep', mainName) signer = nacling.Signer() mainSignKeyHex = signer.keyhex mainVerKeyHex = signer.verhex privateer = nacling.Privateer() mainPriKeyHex = privateer.keyhex mainPubKeyHex = privateer.pubhex #other stack otherName = "other" otherDirpath = os.path.join(self.dirpathBase, 'road', 'keep', otherName) signer = nacling.Signer() otherSignKeyHex = signer.keyhex otherVerKeyHex = signer.verhex privateer = nacling.Privateer() otherPriKeyHex = privateer.keyhex otherPubKeyHex = privateer.pubhex 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) remote1 = estating.RemoteEstate( stack=self.main, eid=2, name=otherName, ha=("127.0.0.1", raeting.RAET_TEST_PORT), verkey=otherVerKeyHex, pubkey=otherPubKeyHex, period=self.main.period, offset=self.main.offset, ) self.main.addRemote(remote1) local = estating.LocalEstate( eid=2, name=otherName, ha=("", raeting.RAET_TEST_PORT), sigkey=otherSignKeyHex, prikey=otherPriKeyHex, ) self.other = stacking.RoadStack(name=otherName, local=local, dirpath=otherDirpath, store=self.store) remote0 = estating.RemoteEstate( stack=self.other, eid=1, name=mainName, ha=('127.0.0.1', raeting.RAET_PORT), verkey=mainVerKeyHex, pubkey=mainPubKeyHex, period=self.other.period, offset=self.other.offset, ) self.other.addRemote(remote0) remote0.publee = nacling.Publican(key=remote1.privee.pubhex) remote1.publee = nacling.Publican(key=remote0.privee.pubhex) stuff = [] for i in range(300): stuff.append(str(i).rjust(4, " ")) self.stuff = "".join(stuff) self.data = odict(hk=raeting.headKinds.raet)
def testBasic(self): ''' Basic keep setup for stack keep and safe persistence load and dump ''' console.terse("{0}\n".format(self.testBasic.__doc__)) base = '/tmp/raet/' auto = True data = self.createRoadData(name='main', base=base) stack = self.createRoadStack(data=data, eid=1, main=True, auto=auto, ha=None) #default ha is ("", raeting.RAET_PORT) console.terse("{0} keep dirpath = {1} safe dirpath = {0}\n".format( stack.name, stack.keep.dirpath, stack.safe.dirpath)) self.assertEqual(stack.keep.dirpath, '/tmp/raet/road/keep/main') self.assertEqual(stack.safe.dirpath, '/tmp/raet/road/keep/main') self.assertEqual(stack.local.ha, ("0.0.0.0", raeting.RAET_PORT)) # test round trip stack.clearLocal() stack.clearRemoteKeeps() stack.dumpLocal() stack.dumpRemotes() localKeepData = stack.keep.loadLocalData() console.terse("Local keep data = '{0}'\n".format(localKeepData)) validLocalKeepData = odict([ ('eid', 1), ('name', 'main'), ('main', True), ('host', '0.0.0.0'), ('port', 7530), ('sid', 0), ]) self.assertDictEqual(localKeepData, validLocalKeepData) remoteKeepData = stack.keep.loadAllRemoteData() console.terse("Remote keep data = '{0}'\n".format(remoteKeepData)) self.assertDictEqual(remoteKeepData, {}) localSafeData = stack.safe.loadLocalData() console.terse("Local safe data = '{0}'\n".format(localSafeData)) validLocalSafeData = odict([ ('eid', 1), ('name', 'main'), ('sighex', data['sighex']), ('prihex', data['prihex']), ]) self.assertDictEqual(localSafeData, validLocalSafeData) remoteSafeData = stack.safe.loadAllRemoteData() console.terse("Remote safe data = '{0}'\n".format(remoteSafeData)) self.assertDictEqual(remoteSafeData, {}) # test round trip with stack methods stack.loadLocal() localKeepData = odict([ ('eid', stack.local.eid), ('name', stack.local.name), ('main', stack.local.main), ('host', stack.local.host), ('port', stack.local.port), ('sid', stack.local.sid), ]) self.assertDictEqual(localKeepData, validLocalKeepData) localSafeData = odict([ ('eid', stack.local.eid), ('name', stack.local.name), ('sighex', stack.local.signer.keyhex), ('prihex', stack.local.priver.keyhex), ]) self.assertDictEqual(localSafeData, validLocalSafeData) stack.removeAllRemotes() stack.loadRemotes() self.assertDictEqual(stack.remotes, {}) # round trip with non empty remote data other1Data = self.createRoadData(name='other1', base=base) stack.addRemote( estating.RemoteEstate( eid=2, name=other1Data['name'], ha=('127.0.0.1', 7531), verkey=other1Data['verhex'], pubkey=other1Data['pubhex'], )) other2Data = self.createRoadData(name='other2', base=base) stack.addRemote( estating.RemoteEstate( eid=3, name=other2Data['name'], ha=('127.0.0.1', 7532), verkey=other2Data['verhex'], pubkey=other2Data['pubhex'], )) stack.dumpRemotes() remoteKeepData = stack.keep.loadAllRemoteData() console.terse("Remote keep data = '{0}'\n".format(remoteKeepData)) validRemoteKeepData = { '2': { 'eid': 2, 'name': other1Data['name'], 'host': '127.0.0.1', 'port': 7531, 'sid': 0, 'rsid': 0 }, '3': { 'eid': 3, 'name': other2Data['name'], 'host': '127.0.0.1', 'port': 7532, 'sid': 0, 'rsid': 0 } } self.assertDictEqual(remoteKeepData, validRemoteKeepData) remoteSafeData = stack.safe.loadAllRemoteData() console.terse("Remote safe data = '{0}'\n".format(remoteSafeData)) validRemoteSafeData = { '2': { 'eid': 2, 'name': other1Data['name'], 'acceptance': None, 'verhex': other1Data['verhex'], 'pubhex': other1Data['pubhex'] }, '3': { 'eid': 3, 'name': other2Data['name'], 'acceptance': None, 'verhex': other2Data['verhex'], 'pubhex': other2Data['pubhex'] } } self.assertDictEqual(remoteSafeData, validRemoteSafeData) # stack method #convert string uid keys into int uid keys temp = validRemoteKeepData validRemoteKeepData = odict() for uid in temp: validRemoteKeepData[int(uid)] = temp[uid] temp = validRemoteSafeData validRemoteSafeData = odict() for uid in temp: validRemoteSafeData[int(uid)] = temp[uid] stack.removeAllRemotes() stack.loadRemotes() remoteKeepData = odict() for remote in stack.remotes.values(): remoteKeepData[remote.uid] = odict([ ('eid', remote.eid), ('name', remote.name), ('host', remote.host), ('port', remote.port), ('sid', remote.sid), ('rsid', remote.rsid), ]) self.assertDictEqual(remoteKeepData, validRemoteKeepData) remoteSafeData = odict() for remote in stack.remotes.values(): remoteSafeData[remote.uid] = odict([ ('eid', remote.eid), ('name', remote.name), ('acceptance', remote.acceptance), ('verhex', remote.verfer.keyhex), ('pubhex', remote.pubber.keyhex), ]) self.assertDictEqual(remoteSafeData, validRemoteSafeData) stack.server.close() # bootstrap new stack from stored keep and safe data stack = stacking.RoadStack(name=data['name'], auto=auto, dirpath=data['dirpath'], store=self.store) localKeepData = odict([ ('eid', stack.local.eid), ('name', stack.local.name), ('main', stack.local.main), ('host', stack.local.host), ('port', stack.local.port), ('sid', stack.local.sid), ]) console.terse("Local keep data = '{0}'\n".format(localKeepData)) self.assertDictEqual(localKeepData, validLocalKeepData) localSafeData = odict([ ('eid', stack.local.eid), ('name', stack.local.name), ('sighex', stack.local.signer.keyhex), ('prihex', stack.local.priver.keyhex), ]) console.terse("Local safe data = '{0}'\n".format(localSafeData)) self.assertDictEqual(localSafeData, validLocalSafeData) remoteKeepData = odict() for remote in stack.remotes.values(): remoteKeepData[remote.uid] = odict([ ('eid', remote.eid), ('name', remote.name), ('host', remote.host), ('port', remote.port), ('sid', remote.sid), ('rsid', remote.rsid), ]) self.assertDictEqual(remoteKeepData, validRemoteKeepData) remoteSafeData = odict() for remote in stack.remotes.values(): remoteSafeData[remote.uid] = odict([ ('eid', remote.eid), ('name', remote.name), ('acceptance', remote.acceptance), ('verhex', remote.verfer.keyhex), ('pubhex', remote.pubber.keyhex), ]) self.assertDictEqual(remoteSafeData, validRemoteSafeData) stack.server.close() stack.clearLocal() stack.clearRemoteKeeps()
def testJoinForever(self): ''' Test other joining with timeout set to 0.0 and default ''' console.terse("{0}\n".format(self.testJoinForever.__doc__)) self.other.addRemote( estating.RemoteEstate( stack=self.other, fuid=0, # vacuous join sid=0, # always 0 for join ha=self.main.local.ha)) self.other.join(timeout=0.0) #attempt to join forever with timeout 0.0 self.serviceOther(duration=20.0, real=False) # only service other so no response console.terse("\nStack '{0}' uid= {1}\n".format( self.main.name, self.main.local.uid)) self.assertEqual(self.main.local.uid, 1) self.assertEqual(self.main.name, 'main') self.assertEqual(len(self.main.transactions), 0) self.assertEqual(len(self.main.remotes), 0) console.terse("\nStack '{0}' uid= {1}\n".format( self.other.name, self.other.local.uid)) self.assertEqual(self.other.local.uid, 1) self.assertEqual(self.other.name, 'other') self.assertEqual(len(self.other.transactions), 1) remote = self.other.remotes.values()[0] self.assertIs(remote.joined, None) self.assertEqual(remote.uid, 2) console.terse( "Stack '{0}' estate name '{1}' joined with '{2}' = {3}\n".format( self.other.name, self.other.local.name, remote.name, remote.joined)) console.terse("{0} Stats\n".format(self.main.name)) for key, val in self.main.stats.items(): console.terse(" {0}={1}\n".format(key, val)) self.assertEqual(len(self.main.stats), 0) console.terse("{0} Stats\n".format(self.other.name)) for key, val in self.other.stats.items(): console.terse(" {0}={1}\n".format(key, val)) self.assertEqual(self.other.stats.get('joiner_tx_join_redo'), 6) # Now allow join to complete self.service() console.terse("\nStack '{0}' uid= {1}\n".format( self.main.name, self.main.local.uid)) self.assertEqual(self.main.local.uid, 1) self.assertEqual(self.main.name, 'main') self.assertEqual(len(self.main.transactions), 0) remote = self.main.remotes.values()[0] self.assertTrue(remote.joined) self.assertEqual(remote.uid, 2) self.assertTrue(2 in self.main.remotes) self.assertTrue(len(self.main.remotes), 1) self.assertTrue(len(self.main.nameRemotes), 1) self.assertEqual(remote.name, 'other') self.assertTrue('other' in self.main.nameRemotes) console.terse( "Stack '{0}' estate name '{1}' joined with '{2}' = {3}\n".format( self.main.name, self.main.local.name, remote.name, remote.joined)) console.terse("\nStack '{0}' uid= {1}\n".format( self.other.name, self.other.local.uid)) self.assertEqual(self.other.local.uid, 1) self.assertEqual(self.other.name, 'other') self.assertEqual(len(self.other.transactions), 0) remote = self.other.remotes.values()[0] self.assertTrue(remote.joined) self.assertEqual(remote.uid, 2) self.assertTrue(2 in self.other.remotes) self.assertTrue(len(self.other.remotes), 1) self.assertTrue(len(self.other.nameRemotes), 1) self.assertEqual(remote.name, 'main') self.assertTrue('main' in self.other.nameRemotes) console.terse( "Stack '{0}' estate name '{1}' joined with '{2}' = {3}\n".format( self.other.name, self.other.local.name, remote.name, remote.joined)) # Now try again with existing remote data self.other.join(timeout=0.0) #attempt to join forever with timeout 0.0 self.serviceOther(duration=20.0, real=False) # only service other so no response # main will still have join results from previous join transaction console.terse("\nStack '{0}' uid= {1}\n".format( self.main.name, self.main.local.uid)) self.assertEqual(self.main.local.uid, 1) self.assertEqual(self.main.name, 'main') self.assertEqual(len(self.main.transactions), 0) remote = self.main.remotes.values()[0] self.assertTrue(remote.joined) self.assertEqual(remote.uid, 2) self.assertTrue(2 in self.main.remotes) self.assertTrue(len(self.main.remotes), 1) self.assertTrue(len(self.main.nameRemotes), 1) self.assertEqual(remote.name, 'other') self.assertTrue('other' in self.main.nameRemotes) console.terse( "Stack '{0}' estate name '{1}' joined with '{2}' = {3}\n".format( self.main.name, self.main.local.name, remote.name, remote.joined)) # Other will have outstanding join transaction console.terse("\nStack '{0}' uid= {1}\n".format( self.other.name, self.other.local.uid)) self.assertEqual(self.other.local.uid, 1) self.assertEqual(self.other.name, 'other') self.assertEqual(len(self.other.transactions), 1) remote = self.other.remotes.values()[0] self.assertIs(remote.joined, None) self.assertEqual(remote.uid, 2) self.assertTrue(2 in self.other.remotes) self.assertTrue(len(self.other.remotes), 1) self.assertTrue(len(self.other.nameRemotes), 1) self.assertEqual(remote.name, 'main') self.assertTrue('main' in self.other.nameRemotes) console.terse( "Stack '{0}' estate name '{1}' joined with '{2}' = {3}\n".format( self.other.name, self.other.local.name, remote.name, remote.joined)) console.terse("{0} Stats\n".format(self.main.name)) for key, val in self.main.stats.items(): console.terse(" {0}={1}\n".format(key, val)) self.assertEqual(self.main.stats.get('join_correspond_complete'), 1) console.terse("{0} Stats\n".format(self.other.name)) for key, val in self.other.stats.items(): console.terse(" {0}={1}\n".format(key, val)) self.assertEqual(self.other.stats.get('joiner_tx_join_redo'), 12) self.assertEqual(self.other.stats.get('join_initiate_complete'), 1) # Now allow join to complete self.service() console.terse("\nStack '{0}' uid= {1}\n".format( self.main.name, self.main.local.uid)) self.assertEqual(self.main.local.uid, 1) self.assertEqual(self.main.name, 'main') self.assertEqual(len(self.main.transactions), 0) remote = self.main.remotes.values()[0] self.assertTrue(remote.joined) self.assertEqual(remote.uid, 2) self.assertTrue(2 in self.main.remotes) self.assertTrue(len(self.main.remotes), 1) self.assertTrue(len(self.main.nameRemotes), 1) self.assertEqual(remote.name, 'other') self.assertTrue('other' in self.main.nameRemotes) console.terse( "Stack '{0}' estate name '{1}' joined with '{2}' = {3}\n".format( self.main.name, self.main.local.name, remote.name, remote.joined)) console.terse("\nStack '{0}' uid= {1}\n".format( self.other.name, self.other.local.uid)) self.assertEqual(self.other.local.uid, 1) self.assertEqual(self.other.name, 'other') self.assertEqual(len(self.other.transactions), 0) remote = self.other.remotes.values()[0] self.assertTrue(remote.joined) self.assertEqual(remote.uid, 2) self.assertTrue(2 in self.other.remotes) self.assertTrue(len(self.other.remotes), 1) self.assertTrue(len(self.other.nameRemotes), 1) self.assertEqual(remote.name, 'main') self.assertTrue('main' in self.other.nameRemotes) console.terse( "Stack '{0}' estate name '{1}' joined with '{2}' = {3}\n".format( self.other.name, self.other.local.name, remote.name, remote.joined))
def testAliveUnjoinedFromMain(self): ''' Test alive transaction for unjoined main to other ''' console.terse("{0}\n".format(self.testAliveUnjoinedOther.__doc__)) mainData = self.createRoadData(name='main', base=self.base, auto=True) keeping.clearAllKeepSafe(mainData['dirpath']) main = self.createRoadStack(data=mainData, eid=1, main=True, auto=mainData['auto'], ha=None) otherData = self.createRoadData(name='other', base=self.base) keeping.clearAllKeepSafe(otherData['dirpath']) other = self.createRoadStack(data=otherData, eid=0, main=None, auto=None, ha=("", raeting.RAET_TEST_PORT)) console.terse("\nBoth unjoined Alive Main to Other *********\n") self.assertEqual(len(main.remotes), 0) self.assertEqual(len(other.remotes), 0) #now create remote for other and add to main main.addRemote(estating.RemoteEstate(stack=main, eid=2, name=otherData['name'], ha=('127.0.0.1', other.local.port), verkey=otherData['verhex'], pubkey=otherData['pubhex'], period=main.period, offset=main.offset)) self.alive(main, other, mha=('127.0.0.1', other.local.port)) self.assertEqual(len(main.transactions), 0) self.assertEqual(len(other.transactions), 0) otherRemote = main.remotes[other.local.uid] mainRemote = other.remotes[main.local.uid] self.assertIs(otherRemote.joined, True) self.assertIs(mainRemote.joined, True) self.assertIs(otherRemote.alived, None) self.assertIs(mainRemote.alived, None) self.alive(main, other, deid=other.local.uid) self.assertEqual(len(main.transactions), 0) self.assertEqual(len(other.transactions), 0) self.assertIs(otherRemote.allowed, True) self.assertIs(mainRemote.allowed, True) self.assertIs(otherRemote.alived, None) self.assertIs(mainRemote.alived, None) self.alive(main, other, deid=other.local.uid) self.assertEqual(len(main.transactions), 0) self.assertEqual(len(other.transactions), 0) self.assertIs(otherRemote.alived, True) self.assertIs(mainRemote.alived, True) console.terse("\nAlive Other to Main *********\n") otherRemote.alived = None mainRemote.alived = None self.assertIs(otherRemote.alived, None) self.assertIs(mainRemote.alived, None) self.alive(other, main, deid=main.local.uid) self.assertEqual(len(main.transactions), 0) self.assertEqual(len(other.transactions), 0) self.assertTrue(otherRemote.alived) self.assertTrue(mainRemote.alived) main.server.close() main.clearLocal() main.clearRemoteKeeps() other.server.close() other.clearLocal() other.clearRemoteKeeps()
def testBasic(self): ''' Basic keep setup for stack keep and safe persistence load and dump ''' console.terse("{0}\n".format(self.testBasic.__doc__)) self.assertEqual(self.mainSafe.loadLocalData(), None) self.assertEqual(self.mainSafe.loadAllRemoteData(), {}) dataMain = self.createRoadData(name='main', base=self.baseDirpath) main = self.createRoadStack(data=dataMain, eid=1, main=True, ha=None, #default ha is ("", raeting.RAET_PORT) safe=self.mainSafe) console.terse("{0}\nkeep dirpath = {1}\nsafe dirpath = {2}\n".format( main.name, main.keep.dirpath, main.safe.dirpath)) self.assertTrue(main.keep.dirpath.endswith('road/keep/main')) self.assertTrue(main.safe.dirpath.endswith('pki')) self.assertTrue(main.local.ha, ("0.0.0.0", raeting.RAET_PORT)) self.assertTrue(main.safe.auto) self.assertDictEqual(main.keep.loadLocalData(), {'uid': 1, 'name': 'main', 'ha': ['0.0.0.0', 7530], 'main': True, 'sid': 0}) self.assertDictEqual(main.safe.loadLocalData(), {'prihex': dataMain['prihex'], 'sighex': dataMain['sighex'], 'auto': True}) data1 = self.createRoadData(name='remote1', base=self.baseDirpath) main.addRemote(estating.RemoteEstate(stack=main, eid=3, name=data1['name'], ha=('127.0.0.1', 7532), verkey=data1['verhex'], pubkey=data1['pubhex'], period=main.period, offset=main.offset, )) data2 = self.createRoadData(name='remote2', base=self.baseDirpath) main.addRemote(estating.RemoteEstate(stack=main, eid=4, name=data2['name'], ha=('127.0.0.1', 7533), verkey=data2['verhex'], pubkey=data2['pubhex'], period=main.period, offset=main.offset,)) main.dumpRemotes() self.assertDictEqual(main.safe.loadAllRemoteData(), {'3': {'uid': 3, 'name': data1['name'], 'acceptance': 1, 'verhex': data1['verhex'], 'pubhex': data1['pubhex']}, '4': {'uid': 4, 'name': data2['name'], 'acceptance': 1, 'verhex': data2['verhex'], 'pubhex': data2['pubhex']}}) self.assertDictEqual(main.keep.loadAllRemoteData(), {'3': {'uid': 3, 'name': 'remote1', 'ha': ['127.0.0.1', 7532], 'sid': 0, 'rsid': 0}, '4': {'uid': 4, 'name': 'remote2', 'ha': ['127.0.0.1', 7533], 'sid': 0, 'rsid': 0}}) # now recreate with saved data main.server.close() main = stacking.RoadStack(name='main', dirpath=dataMain['dirpath'], store=self.store, safe=self.mainSafe) self.assertEqual(main.local.priver.keyhex, dataMain['prihex']) self.assertEqual(main.local.signer.keyhex, dataMain['sighex']) self.assertEqual(len(main.remotes.values()), 2) self.assertEqual(self.otherSafe.loadLocalData(), None) self.assertEqual(self.otherSafe.loadAllRemoteData(), {}) # other stack dataOther = self.createRoadData(name='other', base=self.baseDirpath) other = self.createRoadStack(data=dataOther, eid=0, main=None, ha=("", raeting.RAET_TEST_PORT), safe=self.otherSafe) console.terse("{0} keep dirpath = {1} safe dirpath = {2}\n".format( other.name, other.keep.dirpath, other.safe.dirpath)) self.assertTrue(other.keep.dirpath.endswith('road/keep/other')) self.assertTrue(other.safe.dirpath.endswith('pki')) self.assertEqual(other.local.ha, ("0.0.0.0", raeting.RAET_TEST_PORT)) self.assertDictEqual(other.safe.loadLocalData(), {'prihex': dataOther['prihex'], 'sighex': dataOther['sighex'], 'auto': True,}) data3 = self.createRoadData(name='remote3', base=self.baseDirpath) other.addRemote(estating.RemoteEstate(stack=other, eid=3, name=data3['name'], ha=('127.0.0.1', 7534), verkey=data3['verhex'], pubkey=data3['pubhex'], period=main.period, offset=main.offset,)) data4 = self.createRoadData(name='remote4', base=self.baseDirpath) other.addRemote(estating.RemoteEstate(stack=other, eid=4, name=data4['name'], ha=('127.0.0.1', 7535), verkey=data4['verhex'], pubkey=data4['pubhex'], period=main.period, offset=main.offset,)) other.dumpRemotes() self.assertDictEqual(other.safe.loadAllRemoteData(), {'3': {'uid': 3, 'name': data3['name'], 'acceptance': 1, 'verhex': data3['verhex'], 'pubhex': data3['pubhex']}, '4': {'uid': 4, 'name': data4['name'], 'acceptance': 1, 'verhex': data4['verhex'], 'pubhex': data4['pubhex']}}) other.server.close() self.assertDictEqual(other.keep.loadAllRemoteData(), {'3': {'uid': 3, 'name': 'remote3', 'ha': ['127.0.0.1', 7534], 'sid': 0, 'rsid': 0}, '4': {'uid': 4, 'name': 'remote4', 'ha': ['127.0.0.1', 7535], 'sid': 0, 'rsid': 0}}) main.server.close() other.server.close()
def testJoinentVacuousAcceptNewRole(self): ''' Test joinent accept vacuous join with an updated role (D6) ''' console.terse("{0}\n".format( self.testJoinentVacuousAcceptNewRole.__doc__)) # Status: Accepted (auto accept keys) # Mode: Never, Once, Always alpha, beta = self.bootstrapJoinedRemotes() # Mutable: Yes alpha.mutable = True # Vacuous: Yes betaRemote = beta.addRemote( estating.RemoteEstate( stack=beta, fuid=0, # vacuous join sid=0, # always 0 for join ha=alpha.local.ha)) # Ephemeral: No Name (the name is known) alphaRemote = estating.RemoteEstate(stack=alpha, fuid=betaRemote.nuid, ha=beta.local.ha, name=beta.name, verkey=beta.local.signer.verhex, pubkey=beta.local.priver.pubhex) alpha.addRemote(alphaRemote) oldRole = 'beta' newRole = 'beta_new' # Name: Old # Main: Either # Appl: Either # RHA: Either # Nuid: Computed # Fuid: Either # Leid: 0 # Reid: Either # Role: New self.assertIs(beta.local.role, oldRole) beta.local.role = newRole # Keys: Either # Sameness: Not sameall keep = self.copyData(alphaRemote) # Test self.join(beta, alpha, deid=betaRemote.nuid) # Action: Accept, Dump for stack in [alpha, beta]: self.assertEqual(len(stack.transactions), 0) self.assertEqual(len(stack.remotes), 1) self.assertEqual(len(stack.nameRemotes), 1) for remote in stack.remotes.values(): self.assertIs(remote.joined, True) self.assertIs(remote.allowed, None) self.assertIs(remote.alived, None) self.assertIs(beta.mutable, None) self.assertIs(alpha.mutable, True) self.assertIs(self.sameAll(alphaRemote, keep), False) self.assertEqual(alphaRemote.role, newRole) self.assertEqual(alphaRemote.acceptance, raeting.acceptances.accepted) self.assertEqual(betaRemote.acceptance, raeting.acceptances.accepted) for stack in [alpha, beta]: stack.server.close() stack.clearAllKeeps()
def testBasic(self): ''' Basic keep setup for stack keep and safe persistence load and dump ''' console.terse("{0}\n".format(self.testBasic.__doc__)) opts = self.createOpts(name='main', dirpath=self.tempDirpath, openMode=True, autoAccept=True) mainData = self.createRoadData(name='main', cachedirpath=opts['cachedir'] ) mainKeep = salting.SaltKeep(opts=opts, basedirpath=mainData['basedirpath'], stackname=mainData['name']) self.assertEqual(mainKeep.loadLocalData(), None) self.assertEqual(mainKeep.loadAllRemoteData(), {}) main = self.createRoadStack(data=mainData, eid=1, main=True, ha=None, #default ha is ("", raeting.RAET_PORT) keep=mainKeep) console.terse("{0}\nkeep dirpath = {1}\n".format( main.name, main.keep.dirpath)) self.assertTrue(main.keep.dirpath.endswith('main/raet/main')) self.assertTrue(main.local.ha, ("0.0.0.0", raeting.RAET_PORT)) self.assertTrue(main.keep.auto) self.assertDictEqual(main.keep.loadLocalData(), {'uid': 1, 'name': 'main', 'ha': ['0.0.0.0', 7530], 'main': True, 'sid': 0, 'neid': 1, 'sighex': mainData['sighex'], 'prihex': mainData['prihex'], 'auto': True, }) data1 = self.createRoadData(name='remote1', cachedirpath=opts['cachedir']) main.addRemote(estating.RemoteEstate(stack=main, eid=3, name=data1['name'], ha=('127.0.0.1', 7532), verkey=data1['verhex'], pubkey=data1['pubhex'], period=main.period, offset=main.offset, )) data2 = self.createRoadData(name='remote2', cachedirpath=opts['cachedir']) main.addRemote(estating.RemoteEstate(stack=main, eid=4, name=data2['name'], ha=('127.0.0.1', 7533), verkey=data2['verhex'], pubkey=data2['pubhex'], period=main.period, offset=main.offset,)) main.dumpRemotes() self.assertDictEqual(main.keep.loadAllRemoteData(), { 'remote1': {'uid': 3, 'name': data1['name'], 'ha': ['127.0.0.1', 7532], 'sid': 0, 'joined': None, 'acceptance': 1, 'verhex': data1['verhex'], 'pubhex': data1['pubhex']}, 'remote2': {'uid': 4, 'name': data2['name'], 'ha': ['127.0.0.1', 7533], 'sid': 0, 'joined': None, 'acceptance': 1, 'verhex': data2['verhex'], 'pubhex': data2['pubhex']}}) # now recreate with saved data main.server.close() mainKeep = salting.SaltKeep(opts=opts, basedirpath=mainData['basedirpath'], stackname=mainData['name']) main = stacking.RoadStack(name=mainData['name'], store=self.store, keep=mainKeep) self.assertEqual(main.local.priver.keyhex, mainData['prihex']) self.assertEqual(main.local.signer.keyhex, mainData['sighex']) self.assertEqual(len(main.remotes.values()), 2) # other stack opts = self.createOpts(name='other', dirpath=self.tempDirpath, openMode=True, autoAccept=True) otherData = self.createRoadData(name='other', cachedirpath=opts['cachedir'] ) otherKeep = salting.SaltKeep(opts=opts, basedirpath=otherData['basedirpath'], stackname=otherData['name']) self.assertEqual(otherKeep.loadLocalData(), None) self.assertEqual(otherKeep.loadAllRemoteData(), {}) other = self.createRoadStack(data=otherData, eid=0, main=None, ha=("", raeting.RAET_TEST_PORT), keep=otherKeep) console.terse("{0} keep dirpath = {1}\n".format( other.name, other.keep.dirpath)) self.assertTrue(other.keep.dirpath.endswith('other/raet/other')) self.assertEqual(other.local.ha, ("0.0.0.0", raeting.RAET_TEST_PORT)) self.assertDictEqual(other.keep.loadLocalData(), { 'uid': 0, 'name': 'other', 'ha': ['0.0.0.0', 7531], 'main': None, 'sid': 0, 'neid': 1, 'sighex': otherData['sighex'], 'prihex': otherData['prihex'], 'auto': True, }) data3 = self.createRoadData(name='remote3', cachedirpath=opts['cachedir']) other.addRemote(estating.RemoteEstate(stack=other, eid=3, name=data3['name'], ha=('127.0.0.1', 7534), verkey=data3['verhex'], pubkey=data3['pubhex'], period=main.period, offset=main.offset,)) data4 = self.createRoadData(name='remote4', cachedirpath=opts['cachedir']) other.addRemote(estating.RemoteEstate(stack=other, eid=4, name=data4['name'], ha=('127.0.0.1', 7535), verkey=data4['verhex'], pubkey=data4['pubhex'], period=main.period, offset=main.offset,)) other.dumpRemotes() self.assertDictEqual(other.keep.loadAllRemoteData(), { 'remote3': { 'uid': 3, 'name': data3['name'], 'ha': ['127.0.0.1', 7534], 'sid': 0, 'joined': None, 'acceptance': 1, 'verhex': data3['verhex'], 'pubhex': data3['pubhex'] }, 'remote4': { 'uid': 4, 'name': data4['name'], 'ha': ['127.0.0.1', 7535], 'sid': 0, 'joined': None, 'acceptance': 1, 'verhex': data4['verhex'], 'pubhex': data4['pubhex'] } }) main.server.close() other.server.close()