async def test_server_mirror(self): with self.getTestDir() as dirn: path00 = s_common.gendir(dirn, 'core00') path01 = s_common.gendir(dirn, 'core01') async with self.getTestCore(dirn=path00) as core00: await core00.nodes('[ inet:ipv4=1.2.3.4 ]') s_tools_backup.backup(path00, path01) async with self.getTestCore(dirn=path00) as core00: outp = self.getTestOutp() argv = ['--telepath', 'tcp://127.0.0.1:0/', '--https', '0', '--mirror', core00.getLocalUrl(), path01] async with await s_s_cortex.main(argv, outp=outp) as core01: evnt = await core01._getWaitFor('inet:ipv4', '5.5.5.5') await core00.nodes('[ inet:ipv4=5.5.5.5 ]') await evnt.wait() self.len(1, await core01.nodes('inet:ipv4=5.5.5.5'))
async def test_server_mirror_restart(self): with self.getTestDir() as dirn, self.withSetLoggingMock(): path00 = s_common.gendir(dirn, 'core00') path01 = s_common.gendir(dirn, 'core01') conf00 = { 'layer:lmdb:map_async': True, 'provenance:en': True, 'nexslog:en': True, 'layers:logedits': True, 'auth:passwd': 'secret', } async with await s_cortex.Cortex.anit(dirn=path00, conf=conf00) as core00: await core00.nodes('[ inet:asn=0 ]') s_tools_backup.backup(path00, path01) async with await s_cortex.Cortex.anit(dirn=path00, conf=conf00) as core00: # add a node for core01 to sync before window await core00.nodes('[ inet:asn=1 ]') argv = [ '--telepath', 'tcp://127.0.0.1:0/', '--https', '0', '--auth-passwd', 'secret', '--mirror', core00.getLocalUrl(), path01 ] async with await s_cortex.Cortex.initFromArgv(argv) as core01: await core01.sync() self.len(1, await core01.nodes('inet:asn=0')) self.len(1, await core01.nodes('inet:asn=1')) # get the nexus index nexusind = core01.nexsroot.nexslog.index() await core00.nodes('[ inet:asn=2 ]') async with await s_cortex.Cortex.initFromArgv(argv) as core01: # check that startup does not create any events self.eq(nexusind, core01.nexsroot.nexslog.index()) await core01.sync() self.len(1, await core01.nodes('inet:asn=0')) self.len(1, await core01.nodes('inet:asn=1')) self.len(1, await core01.nodes('inet:asn=2')) await core00.nodes('[ inet:asn=3 ]') await core01.sync() self.len(4, await core01.nodes('inet:asn'))
async def test_lib_aha_mirrors(self): with self.getTestDir() as dirn: dir0 = s_common.gendir(dirn, 'aha0') dir1 = s_common.gendir(dirn, 'aha1') conf = {'nexslog:en': True} async with self.getTestAha(conf={'nexslog:en': True}, dirn=dir0) as aha0: user = await aha0.auth.addUser('reguser', passwd='secret') await user.setAdmin(True) s_tools_backup.backup(dir0, dir1) async with self.getTestAha(conf=conf, dirn=dir0) as aha0: upstream_url = aha0.getLocalUrl() mirrorconf = { 'nexslog:en': True, 'mirror': upstream_url, } async with self.getTestAha(conf=mirrorconf, dirn=dir1) as aha1: # CA is nexus-fied cabyts = await aha0.genCaCert('mirrorca') await aha1.sync() mirbyts = await aha1.genCaCert('mirrorca') self.eq(cabyts, mirbyts) iden = s_common.guid() # Adding, downing, and removing service is also nexusified info = { 'urlinfo': { 'host': '127.0.0.1', 'port': 8080, 'scheme': 'tcp' }, 'online': iden } await aha0.addAhaSvc('test', info, network='example.net') await aha1.sync() mnfo = await aha1.getAhaSvc('test.example.net') self.eq(mnfo.get('name'), 'test.example.net') wait00 = aha0.waiter(1, 'aha:svcdown') await aha0.setAhaSvcDown('test', iden, network='example.net') self.len(1, await wait00.wait(timeout=6)) await aha1.sync() mnfo = await aha1.getAhaSvc('test.example.net') self.notin('online', mnfo) await aha0.delAhaSvc('test', network='example.net') await aha1.sync() mnfo = await aha1.getAhaSvc('test.example.net') self.none(mnfo)
async def test_layer_upstream_with_mirror(self): with self.getTestDir() as dirn: path00 = s_common.gendir(dirn, 'core00') # layer upstream path01 = s_common.gendir( dirn, 'core01') # layer downstream, mirror leader path02 = s_common.gendir( dirn, 'core02') # layer downstream, mirror follower async with self.getTestCore(dirn=path00) as core00: layriden = core00.view.layers[0].iden await core00.nodes('[test:str=foobar +#hehe.haha]') await core00.nodes('[ inet:ipv4=1.2.3.4 ]') await core00.addTagProp('score', ('int', {}), {}) async with self.getTestCore(dirn=path01) as core01: url = core00.getLocalUrl('*/layer') conf = {'upstream': url} ldef = await core01.addLayer(ldef=conf) layr = core01.getLayer(ldef.get('iden')) await core01.view.addLayer(layr.iden) s_tools_backup.backup(path01, path02) async with self.getTestCore(dirn=path01) as core01: layr = core01.getLayer(ldef.get('iden')) # Sync core01 with core00 offs = await core00.getView().layers[0].getNodeEditOffset() evnt = await layr.waitUpstreamOffs(layriden, offs) await asyncio.wait_for(evnt.wait(), timeout=8.0) self.len(1, await core01.nodes('inet:ipv4=1.2.3.4')) url = core01.getLocalUrl() async with self.getTestCore(dirn=path02, conf={'mirror': url}) as core02: await core02.sync() layr = core01.getLayer(ldef.get('iden')) self.true(layr.allow_upstream) layr = core02.getLayer(ldef.get('iden')) self.false(layr.allow_upstream) self.len(1, await core02.nodes('inet:ipv4=1.2.3.4'))
async def test_trigger_async_mirror(self): with self.getTestDir() as dirn: path00 = s_common.gendir(dirn, 'core00') path01 = s_common.gendir(dirn, 'core01') async with self.getTestCore(dirn=path00) as core00: await core00.stormlist( 'trigger.add node:add --async --form inet:ipv4 --query { [+#foo] $lib.queue.gen(foo).put($node.iden()) }' ) await core00.view.finiTrigTask() await core00.nodes('[ inet:ipv4=1.2.3.4 ]') s_tools_backup.backup(path00, path01) async with self.getTestCore(dirn=path00) as core00: url = core00.getLocalUrl() core01conf = {'mirror': url} async with await s_cortex.Cortex.anit( dirn=path01, conf=core01conf) as core01: # ensure sync by forcing node construction await core01.nodes('[ou:org=*]') self.nn(await core00.callStorm( 'return($lib.queue.gen(foo).pop(wait=$lib.true))')) self.none( await core00.callStorm('return($lib.queue.gen(foo).pop())')) await core01.nodes('[inet:ipv4=8.8.8.8]') self.nn(await core01.callStorm( 'return($lib.queue.gen(foo).pop(wait=$lib.true))')) self.none( await core00.callStorm('return($lib.queue.gen(foo).pop())')) self.none( await core01.callStorm('return($lib.queue.gen(foo).pop())')) self.nn(core00.view.trigtask) self.none(core01.view.trigtask)
async def test_server_mirror(self): with self.getTestDir() as dirn, self.withSetLoggingMock(): path00 = s_common.gendir(dirn, 'core00') path01 = s_common.gendir(dirn, 'core01') async with self.getTestCore(dirn=path00) as core00: await core00.nodes('[ inet:ipv4=1.2.3.4 ]') s_tools_backup.backup(path00, path01) async with self.getTestCore(dirn=path00) as core00: outp = self.getTestOutp() argv = ['--telepath', 'tcp://127.0.0.1:0/', '--https', '0', '--mirror', core00.getLocalUrl(), path01] # add a node for core01 to sync before window await core00.nodes('[ inet:ipv4=5.5.5.5 ]') offs = core00.nexsroot.getOffset() s_common.yamlsave({'logchanges': True}, path01, 'cell.yaml') async with await s_cortex.Cortex.initFromArgv(argv, outp=outp) as core01: # TODO functionalize this API on the cortex (cell?) await core01.nexsroot.waitForOffset(offs - 1) self.len(1, await core01.nodes('inet:ipv4=5.5.5.5')) # add a node for core01 to sync via window self.len(1, await core00.nodes('[ inet:ipv4=6.6.6.6 ]')) offs = core00.nexsroot.getOffset() await core01.nexsroot.waitForOffset(offs - 1) self.len(1, await core01.nodes('inet:ipv4=6.6.6.6'))
async def test_server_mirror(self): with self.getTestDir() as dirn: async with self.withSetLoggingMock() as mock: path00 = s_common.gendir(dirn, 'core00') path01 = s_common.gendir(dirn, 'core01') async with self.getTestCore(dirn=path00) as core00: await core00.nodes('[ inet:ipv4=1.2.3.4 ]') s_tools_backup.backup(path00, path01) async with self.getTestCore(dirn=path00) as core00: argv = [ '--telepath', 'tcp://127.0.0.1:0/', '--https', '0', '--mirror', core00.getLocalUrl(), path01 ] # add a node for core01 to sync before window await core00.nodes('[ inet:ipv4=5.5.5.5 ]') s_common.yamlsave({'nexslog:en': True}, path01, 'cell.yaml') async with await s_cortex.Cortex.initFromArgv(argv ) as core01: await core01.sync() self.len(1, await core01.nodes('inet:ipv4=5.5.5.5')) # add a node for core01 to sync via window self.len(1, await core00.nodes('[ inet:ipv4=6.6.6.6 ]')) await core01.sync() self.len(1, await core01.nodes('inet:ipv4=6.6.6.6'))
async def getCortexAndProxy(self) -> AsyncIterator[Tuple[Any, Any]]: with syntest.getTestDir(startdir=self.tmpdir) as dirn: s_tools_backup.backup(self.testdata.dirn, dirn, compact=False) async with await s_cortex.Cortex.anit(dirn, conf=self.coreconfig) as core: assert not core.inaugural lockmemory = self.coreconfig.get('layers:lockmemory', False) logedits = self.coreconfig.get('layers:logedits', True) await core.view.layers[0].layrinfo.set('lockmemory', lockmemory) await core.view.layers[0].layrinfo.set('logedits', logedits) async with await s_cortex.Cortex.anit(dirn, conf=self.coreconfig) as core: async with core.getLocalProxy() as prox: if lockmemory: await core.view.layers[0].layrslab.lockdoneevent.wait() await s_lmdbslab.Slab.syncLoopOnce() yield core, prox
async def test_server_mirror(self): with self.getTestDir() as dirn: path00 = s_common.gendir(dirn, 'core00') path01 = s_common.gendir(dirn, 'core01') async with self.getTestCore(dirn=path00) as core00: await core00.nodes('[ inet:ipv4=1.2.3.4 ]') s_tools_backup.backup(path00, path01) async with self.getTestCore(dirn=path00) as core00: outp = self.getTestOutp() argv = [ '--telepath', 'tcp://127.0.0.1:0/', '--https', '0', '--mirror', core00.getLocalUrl(), path01 ] # add a node for core01 to sync before window await core00.nodes('[ inet:ipv4=5.5.5.5 ]') async with await s_s_cortex.main(argv, outp=outp) as core01: evnt00 = await core01._getWaitFor('inet:ipv4', '5.5.5.5') await evnt00.wait() self.len(1, await core01.nodes('inet:ipv4=5.5.5.5')) # add a node for core01 to sync via window evnt01 = await core01._getWaitFor('inet:ipv4', '6.6.6.6') self.len(1, await core00.nodes('[ inet:ipv4=6.6.6.6 ]')) await evnt01.wait() self.len(1, await core01.nodes('inet:ipv4=6.6.6.6'))
async def test_storm_svc_mirror(self): with self.getTestDir() as dirn: path00 = s_common.gendir(dirn, 'core00') path01 = s_common.gendir(dirn, 'core01') async with self.getTestDmon() as dmon: dmon.share('real', RealService()) host, port = dmon.addr lurl = f'tcp://127.0.0.1:{port}/real' async with self.getTestCore(dirn=path00) as core00: await core00.nodes('[ inet:ipv4=1.2.3.4 ]') s_tools_backup.backup(path00, path01) async with self.getTestCore(dirn=path00) as core00: url = core00.getLocalUrl() conf = {'mirror': url} async with await s_cortex.Cortex.anit(dirn=path01, conf=conf) as core01: await core01.sync() # Add a storm service await core01.nodes(f'service.add real {lurl}') await core01.nodes('$lib.service.wait(real)') # Make sure it shows up on leader msgs = await core00.stormlist('help') self.stormIsInPrint('service: real', msgs) self.stormIsInPrint('package: foo', msgs) self.stormIsInPrint('foobar', msgs) self.isin('foo.bar', core00.stormmods) queue = core00.multiqueue.list() self.len(1, queue) self.eq('vertex', queue[0]['name']) self.nn(core00.getStormCmd('ohhai')) # Make sure it shows up on mirror msgs = await core01.stormlist('help') self.stormIsInPrint('service: real', msgs) self.stormIsInPrint('package: foo', msgs) self.stormIsInPrint('foobar', msgs) self.isin('foo.bar', core01.stormmods) queue = core01.multiqueue.list() self.len(1, queue) self.eq('vertex', queue[0]['name']) self.nn(core01.getStormCmd('ohhai')) # Delete storm service iden = core01.getStormSvcs()[0].iden await core01.delStormSvc(iden) await core01.sync() # Make sure it got removed from both self.none(core00.getStormCmd('ohhai')) q = 'for ($o, $m) in $lib.queue.get(vertex).gets(wait=10) {return (($o, $m))}' retn = await core00.callStorm(q) self.eq(retn, (0, 'done')) self.none(core01.getStormCmd('ohhai')) q = 'for ($o, $m) in $lib.queue.get(vertex).gets(wait=10) {return (($o, $m))}' retn = await core01.callStorm(q) self.eq(retn, (0, 'done'))