def test_experimental_from_name2(self): # This behavior used to be implemented incorrectly. The test # immediately above us did not catch the problem, because it was # only wrong in the sandbox method, not in the static module-level # function. x = sandbox.Sandbox('/sandboxes/foo.trunk.official') self.assertFalse(x.get_sandboxtype()._is_experimental())
def run(args): data = copy.copy(args.__dict__) if args.random_state is not None: random.seed(args.random_state) if not args.no_mutate: mutator = galgorithm.mutagen( frac=args.mutate_fraction, mutator=lambda x: MTS[args.mutate_func](x, args.mutate_deviation) ) brain_cls = neural.brainfactory(args.shape) sandbox_obj = sandbox.Sandbox( width=args.sandbox_width, height=args.sandbox_height, balls=args.balls, limit=args.score_limit ) ga = galgorithm.Evolution(brain_cls, sandbox_obj, size=args.population_size, childs=args.childs, mutator=mutator) for _ in range(args.generation_count): ga.fit() print(ga) s = ga.get_best_score() if s < args.score_limit: ga.selection() continue else: break savenn(ga, data)
def test_get_next_masternode(self): fp = StringIO.StringIO(masternode_json) sb = sandbox.Sandbox('/home/binaries', '/home/xyz/test', fp) next = sb.next_masternode_to_setup() self.assertEqual(next, ('mn1', 'wallet'))
def test_send_all(self): fp = StringIO.StringIO(masternode_json) sb = sandbox.Sandbox('/home/binaries', '/home/xyz/test', fp) def mock_check_balance(node): self.assertEqual(node, 'miner') return 1234 sb.check_balance = mock_check_balance def mock_gen_address(node): self.assertEqual(node, 'wallet') return 'b0gUS' sb.gen_address = mock_gen_address def mock_send(node, addr, amount): self.assertEqual(node, 'miner') self.assertEqual(addr, 'b0gUS') self.assertEqual(amount, 1234) mock_send.ok = True sb.send = mock_send sb.send.ok = False def mock_client_command(*args): pass sb.client_command = mock_client_command sb.send_all('miner', 'wallet') self.assertTrue(sb.send.ok)
def test_gen_or_reuse_address(self): fp = StringIO.StringIO(masternode_json) sb = sandbox.Sandbox('/home/binaries', '/home/xyz/test', fp) def mock(*args): if not hasattr(mock, 'gen_address_args'): mock.gen_address_args = args[0] return 'mtnFAwMMCqyRh8ohPmv1EDbGZa98uHDvT9', '' else: mock.get_address_args = [] self.fail('Second call to check_call') sb.check_call = mock addr1 = sb.gen_or_reuse_address('miner') addr2 = sb.gen_or_reuse_address('miner') self.assertEqual(sb.network['nodes']['miner']['receive_with'], 'mtnFAwMMCqyRh8ohPmv1EDbGZa98uHDvT9') self.assertEqual(sb.check_call.gen_address_args, [ '/home/binaries/crown-cli', '-datadir=/home/xyz/test/miner', 'getnewaddress' ]) self.assertEqual(addr1, 'mtnFAwMMCqyRh8ohPmv1EDbGZa98uHDvT9') self.assertEqual(addr2, 'mtnFAwMMCqyRh8ohPmv1EDbGZa98uHDvT9')
def test_send_to(self): fp = StringIO.StringIO(masternode_json) sb = sandbox.Sandbox('/home/binaries', '/home/xyz/test', fp) def mock(*args): if not hasattr(mock, 'gen_address_args'): mock.gen_address_args = args[0] return 'mtnFAwMMCqyRh8ohPmv1EDbGZa98uHDvT9', '' else: mock.send_args = args[0] return '2085bdef731ade8d0b0ce79ae0397a75aacf57b5028bd34724de3ae3faf48830', '' sb.check_call = mock addr = sb.gen_address('miner') txhash = sb.send('wallet', addr, 4.04) self.assertEqual(sb.check_call.gen_address_args, [ '/home/binaries/crown-cli', '-datadir=/home/xyz/test/miner', 'getnewaddress' ]) self.assertEqual(sb.check_call.send_args, [ '/home/binaries/crown-cli', '-datadir=/home/xyz/test/wallet', 'sendtoaddress', 'mtnFAwMMCqyRh8ohPmv1EDbGZa98uHDvT9', '4.04' ]) self.assertEqual( txhash, '2085bdef731ade8d0b0ce79ae0397a75aacf57b5028bd34724de3ae3faf48830')
def test_get_tx(self): fp = StringIO.StringIO(masternode_json) sb = sandbox.Sandbox('/home/binaries', '/home/xyz/test', fp) def mock(*args): if not hasattr(mock, 'getrawtx_args'): mock.getrawtx_args = args[0] return 'BAADC0DE', '' else: mock.decoderawtx_args = args[0] return tx_json, '' sb.check_call = mock tx = sb.get_transaction( 'wallet', '2085bdef731ade8d0b0ce79ae0397a75aacf57b5028bd34724de3ae3faf48830') self.assertEqual(sb.check_call.getrawtx_args, [ '/home/binaries/crown-cli', '-datadir=/home/xyz/test/wallet', 'getrawtransaction', '2085bdef731ade8d0b0ce79ae0397a75aacf57b5028bd34724de3ae3faf48830' ]) self.assertEqual(sb.check_call.decoderawtx_args, [ '/home/binaries/crown-cli', '-datadir=/home/xyz/test/wallet', 'decoderawtransaction', 'BAADC0DE' ]) self.assertEqual(tx['vout'][1]['value'], 4.04)
def run(args): """Запускаем процесс оптимизации нейронной сети средствами ГА.""" data = copy.copy(args.__dict__) if args.random_state is not None: random.seed(args.random_state) if not args.no_mutate: mutator = gena.mutagen(frac=args.mutate_fraction, mutator=lambda x: MTS[args.mutate_func] (x, args.mutate_deviation)) brain_cls = brain.brainfactory(args.shape) sandbox_obj = sandbox.Sandbox(width=args.sandbox_width, height=args.sandbox_height, balls=args.balls, limit=args.score_limit) ga = gena.Darwin(brain_cls, sandbox_obj, size=args.population_size, childs=args.childs, mutator=mutator) for _ in range(args.generation_count): ga.fit() print(ga) s = ga.get_best_score() if s < args.score_limit: ga.selection() continue else: break savenn(ga, data)
def sandy_func(exec_file, in_file_fd, user_out_fd, time, mem): cookbook = { 'args': exec_file, # targeted program 'stdin': in_file_fd, # input to targeted program 'stdout': user_out_fd, # output from targeted program 'stderr': sys.stderr, # error from targeted program 'quota': dict( wallclock=int(time), # 30 sec cpu=2000, # 2 sec memory=int(mem), # 256 MB disk=1048576) } # 1 MB # create a sandbox instance and execute till end msb = sandbox.Sandbox(**cookbook) msb.run() # print "\n" d = Sandbox.probe(msb) d['cpu'] = d['cpu_info'][0] d['mem'] = d['mem_info'][1] d['result'] = msb.result return msb.result
def main(args): ''' This main is for running tests and debugging only. Normally this module is imported. ''' handle_arguments(args) set_up_logging(OPTIONS) if OPTIONS.branch: if OPTIONS.component: return branch_component(vcs.get_working_repository(), OPTIONS.component, OPTIONS.b, OPTIONS.to) else: branch, component = OPTIONS.to.split('/') return branch_sandbox(vcs.get_working_repository(), component, OPTIONS.b, branch) sb = sandbox.Sandbox(OPTIONS.sandbox_root) if OPTIONS.revno: if OPTIONS.component: show_component_revno(OPTIONS.component, sb, OPTIONS.tree) else: show_sandbox_revno(sb, OPTIONS.tree) if OPTIONS.tags: if OPTIONS.component: show_component_tags(OPTIONS.component, sb) else: show_sandbox_tags(sb) if OPTIONS.info: if OPTIONS.component: show_component_info(OPTIONS.component, sb) else: show_sandbox_info(sb) if OPTIONS.status: if OPTIONS.component: show_component_status(OPTIONS.component, sb) else: show_sandbox_status(sb) if OPTIONS.tag_info: show_sandbox_tag_info(OPTIONS.t, sb) if OPTIONS.update: if OPTIONS.component: update_component(OPTIONS.component, sb) else: update_sandbox(sb) if OPTIONS.tag: if OPTIONS.component: tag_component(OPTIONS.component, sb) else: tag_sandbox(sb) if OPTIONS.pull: if OPTIONS.component: pull_component(OPTIONS.component, sb) else: pull_sandbox(sb, OPTIONS.source) if OPTIONS.push: if OPTIONS.component: push_component(OPTIONS.component, sb) else: push_sandbox(sb, OPTIONS.source)
def test_seed_of_two_nodes(self): fp = StringIO.StringIO(test_json) sb = sandbox.Sandbox('', '', fp) config = sb.config_for_node('seed') expected = {('port', 42001), ('rpcport', 43001), ('devnet', 'o_O'), ('rpcpassword', 'bogus'), ('addnode', '127.0.0.1:42002')} self.assertEqual(config, expected)
def test_experimental_from_name(self): x = sandbox.Sandbox('/sandboxes/foo.trunk.quick') self.assertTrue(x.get_sandboxtype()._is_experimental()) self.assertTrue(sandboxtype.SandboxType(None, path='a.b.dev')._is_experimental()) self.assertTrue(sandboxtype.SandboxType(None, path='a.b.experiment')._is_experimental()) self.assertTrue(sandboxtype.SandboxType(None, path='a.b.bugX')._is_experimental()) self.assertFalse(sandboxtype.SandboxType(None, path='a.b.official')._is_experimental()) self.assertFalse(sandboxtype.SandboxType(None, path='a.b.32bit continuous')._is_experimental())
def sandbox_test(self): try: sandbox.Sandbox(TestParameters( self.testable, self.testable.parent_suite)) except sandbox.SubprocessException: self.testable.result = Result(Result.Failed, traceback.format_exc()) else: self.testable.result = Result(Result.Passed)
def test_continuous_from_name(self): # It should be possible to call the is_continuous() method statically # or on an instance. We should return True any time the name of the # folder contains "continuous". x = sandbox.Sandbox('/sandboxes/foo.trunk.continuous') self.assertTrue(x.get_sandboxtype()._is_continuous()) self.assertTrue(sandboxtype.SandboxType(None, path='a.b.Continuous')._is_continuous()) self.assertTrue(sandboxtype.SandboxType(None, path='a.b.VeryCoNtiNuous32')._is_continuous()) self.assertFalse(sandboxtype.SandboxType(None, path='a.b.official')._is_continuous()) self.assertFalse(sandboxtype.SandboxType(None, path='a.b.32bit_bug')._is_continuous())
def test_init_wont_accept_existing_file(self): with ioutil.TempDir() as td: tempFile = os.path.join(td.path, 'foo.trunk.dev') open(tempFile, 'w').close() try: x = sandbox.Sandbox(tempFile) ok = False except AssertionError: ok = True self.assertTrue(ok)
def evalPython(line, locals=None): sandbox = S.Sandbox(config=createSandboxConfig()) if locals is not None: locals = dict(locals) else: locals = dict() try: sandbox.call(_evalPython, line, locals) except BaseException, e: print 'Error: [%s] %s' % (e.__class__.__name__, str(e))
def test_inherit_lock(self): with ioutil.TempDir() as td: sb = sandbox.layout(td.path, 'foo', 'trunk', 'x') lock = sb.lock('test') sb2 = sandbox.Sandbox(sb.get_root()) lock2 = sb2.try_to_inherit_lock() self.assertTrue(bool(lock2)) self.assertTrue(lock2.inherited) self.assertFalse(lock.inherited) lock2.release() self.assertTrue(os.path.isfile(lock.path))
def test_official_from_name(self): # It should be possible to call the is_official() method statically # or on an instance. We should return True any time the name of the # folder contains "official", "daily", or "nightly". x = sandbox.Sandbox('/sandboxes/foo.trunk.official') self.assertTrue(x.get_sandboxtype()._is_official()) self.assertTrue(sandboxtype.SandboxType(None, path='a.b.Official64')._is_official()) self.assertFalse(sandboxtype.SandboxType(None, path='a.b.Daily')._is_official()) self.assertFalse(sandboxtype.SandboxType(None, path='a.b.nIGhTlY')._is_official()) self.assertFalse(sandboxtype.SandboxType(None, path='a.b.bogus')._is_official()) self.assertFalse(sandboxtype.SandboxType(None, path='a.b.32bit continuous')._is_official())
def main(): args = get_args() data = read_config(args.brain) b = neural.BrainBase() b.set_shape(data["shape"]) b.set_synapse(data["best_nn"]) balls = args.balls if args.balls is not None else data["balls"] width = args.width if args.width is not None else data["sandbox_width"] height = args.height if args.height is not None else data["sandbox_height"] r = sandbox.Sandbox(balls=balls, width=width, height=height) window = Window(r, b) window.run()
def check_select_builder(self, fil, typ): with ioutil.TempDir() as td: sb = sandbox.Sandbox(td.path + '/foo.trunk.dev') sb.layout() os.makedirs(sb.get_code_root() + 'foo') aux_folder = sb.get_iftop_folder_path() os.makedirs(aux_folder) f = os.path.join(aux_folder, fil) #print('writing %s' % f) open(f, 'w').close() bld = build.select_builder(sb) self.assertEqual(typ, bld.get_name())
def get_sandbox(): sandbox_config = pysandbox.SandboxConfig() sandbox_config.enable("stdout") sandbox_config.enable("time") sandbox_config.enable("math") sandbox_config.enable("exit") sandbox_config.enable("stderr") sandbox_config.timeout = None sandbox = pysandbox.Sandbox(sandbox_config) return sandbox
def test_get_component_path(self): sb = sandbox.Sandbox('foo.trunk.dev') path = sb.get_component_path('foo', 'code') self.assertTrue(path.find('/foo.trunk.dev/code/foo/') > -1) path = sb.get_component_path('foo', 'test') self.assertTrue(path.find('/foo.trunk.dev/test/foo/') > -1) path = sb.get_component_path('foo', 'run') self.assertTrue(path.find('/foo.trunk.dev/run/') > -1) self.assertFalse(sb.get_component_path('bar', 'run')) path = sb.get_component_path('foo', 'report') self.assertTrue(path.find('/foo.trunk.dev/report/') > -1) self.assertFalse(sb.get_component_path('bar', 'report'))
def handle(self): options = json.loads(self.rfile.readline()) tardata = self.rfile.read() def setup_fds_fn(): os.dup2(dev_null.fileno(), 0) os.dup2(self.connection.fileno(), 1) os.dup2(self.connection.fileno(), 2) inst = sandbox.Sandbox(tardata, timeout=options.get('timeout'), setup_fds_fn=setup_fds_fn) inst.run()
def test_write_mninfo_to_config(self): fp = StringIO.StringIO(masternode_json) sb = sandbox.Sandbox('/home/binaries', '/home/xyz/test', fp) sb.network['nodes']['mn1']['mnprivkey'] = 'MNPRIVKEY' config = sb.config_for_node('mn1') expected = {('port', sb.node_port('mn1')), ('rpcport', sb.node_rpc_port('mn1')), ('devnet', 'rose'), ('rpcpassword', 'bogus'), ('masternode', 1), ('masternodeprivkey', 'MNPRIVKEY'), ('externalip', sb.node_ip('mn1')), ('addnode', sb.node_ip('seed'))} self.assertEqual(config, expected)
def test_get_next_masternode_second(self): fp = StringIO.StringIO(masternode_json) sb = sandbox.Sandbox('/home/binaries', '/home/xyz/test', fp) sb.network['nodes']['wallet']['mnconfig'] = { 'alias1': { 'ip': sb.node_ip('mn1'), 'key': '%privatekey%', 'tx': ('%txhash%', 24) } } next = sb.next_masternode_to_setup() self.assertEqual(next, ('mn2', 'wallet'))
def evalPython(line, locals=None): try: config = createSandboxConfig() sandbox = S.Sandbox(config=config) if locals is None: locals = {} sandbox.execute(evalPythonInSandbox, locals={ 'namespace': locals, 'line': line }) except BaseException, e: print 'Error: [%s] %s' % (e.__class__.__name__, str(e))
def test_bad_policy(self): # If the policy does not yield valid actions, the sandbox should # terminate with BP import sandbox class BadPolicy(sandbox.SandboxPolicy): def __call__(self, e, a): return None pass s = sandbox.Sandbox(self.task, policy=BadPolicy()) s.run() self.assertEqual(s.result, sandbox.S_RESULT_BP) pass
def test_non_policy(self): # If the base class of the policy is not sandbox.SandboxPolicy, # the sandbox should not pass self-test during initialization import sandbox class NonPolicy: def __call__(self, e, a): return a pass self.assertRaises(TypeError, sandbox.Sandbox, self.task, policy=NonPolicy()) s = sandbox.Sandbox(self.task) self.assertRaises(TypeError, setattr, s, "policy", NonPolicy()) pass
def test_bindir_is_none(self): fp = StringIO.StringIO(test_json) sb = sandbox.Sandbox(None, '/home/some/dir', fp) def mock(*args): mock.args = args[0] return '1234.321', '' sb.check_call = mock balance = sb.check_balance('miner') self.assertEqual( sb.check_call.args, ['crown-cli', '-datadir=/home/some/dir/miner', 'getbalance']) self.assertEqual(balance, 1234.321)
def test_check_balance(self): fp = StringIO.StringIO(masternode_json) sb = sandbox.Sandbox('/home/binaries', '/home/xyz/test', fp) def mock(*args): mock.args = args[0] return '1234.321', '' sb.check_call = mock balance = sb.check_balance('wallet') self.assertEqual(sb.check_call.args, [ '/home/binaries/crown-cli', '-datadir=/home/xyz/test/wallet', 'getbalance' ]) self.assertEqual(balance, 1234.321)