def run_without_tunnel(self): port = get_open_port() # run the side specified by self.run_first cmd = ['python', self.cc_src, self.run_first, port] sys.stderr.write('Running %s %s...\n' % (self.cc, self.run_first)) self.proc_first = Popen(cmd, preexec_fn=os.setsid) # sleep just in case the process isn't quite listening yet # the cleaner approach might be to try to verify the socket is open time.sleep(self.run_first_setup_time) self.test_start_time = utc_time() # run the other side specified by self.run_second sh_cmd = 'python %s %s $MAHIMAHI_BASE %s' % ( self.cc_src, self.run_second, port) sh_cmd = ' '.join(self.mm_cmd) + " -- sh -c '%s'" % sh_cmd sys.stderr.write('Running %s %s...\n' % (self.cc, self.run_second)) self.proc_second = Popen(sh_cmd, shell=True, preexec_fn=os.setsid) signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(self.runtime) try: self.proc_first.wait() self.proc_second.wait() except TimeoutError: pass else: signal.alarm(0) sys.stderr.write('Warning: test exited before time limit\n') finally: self.test_end_time = utc_time() return True
def run_without_tunnel(self): port = get_open_port() # run the side specified by self.run_first cmd = ['python', self.cc_src, self.run_first, port] sys.stderr.write('Running %s %s...\n' % (self.cc, self.run_first)) self.proc_first = Popen(cmd, preexec_fn=os.setsid) # sleep just in case the process isn't quite listening yet # the cleaner approach might be to try to verify the socket is open time.sleep(self.run_first_setup_time) self.test_start_time = utc_time() # run the other side specified by self.run_second sh_cmd = 'python %s %s $MAHIMAHI_BASE %s' % (self.cc_src, self.run_second, port) sh_cmd = ' '.join(self.mm_cmd) + " -- sh -c '%s'" % sh_cmd sys.stderr.write('Running %s %s...\n' % (self.cc, self.run_second)) self.proc_second = Popen(sh_cmd, shell=True, preexec_fn=os.setsid) signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(self.runtime) try: self.proc_first.wait() self.proc_second.wait() except TimeoutError: pass else: signal.alarm(0) sys.stderr.write('Warning: test exited before time limit\n') finally: self.test_end_time = utc_time() return True
def main(): h.call(['echo', '1']) h.check_call('echo 2', shell=True) ret = h.check_output(['echo', '3']).strip() print ret assert ret == '3' proc = h.Popen(['echo', '4'], stdout=h.PIPE) ret = proc.communicate()[0].strip() print ret assert ret == '4' print h.get_open_port() h.make_sure_path_exists(h.TMPDIR) print h.parse_config()
def run_first_side(self, tun_id, send_manager, recv_manager, send_pri_ip, recv_pri_ip): first_src = self.cc_src second_src = self.cc_src if self.run_first == 'receiver': if self.mode == 'remote': if self.sender_side == 'local': first_src = self.r['cc_src'] else: second_src = self.r['cc_src'] port = get_open_port() first_cmd = 'tunnel %s python %s receiver %s\n' % (tun_id, first_src, port) second_cmd = 'tunnel %s python %s sender %s %s\n' % ( tun_id, second_src, recv_pri_ip, port) recv_manager.stdin.write(first_cmd) recv_manager.stdin.flush() else: # self.run_first == 'sender' if self.mode == 'remote': if self.sender_side == 'local': second_src = self.r['cc_src'] else: first_src = self.r['cc_src'] port = get_open_port() first_cmd = 'tunnel %s python %s sender %s\n' % (tun_id, first_src, port) second_cmd = 'tunnel %s python %s receiver %s %s\n' % ( tun_id, second_src, send_pri_ip, port) send_manager.stdin.write(first_cmd) send_manager.stdin.flush() return second_cmd
def run_first_side(self, tun_id, send_manager, recv_manager, send_pri_ip, recv_pri_ip): first_src = self.cc_src second_src = self.cc_src if self.run_first == 'receiver': if self.mode == 'remote': if self.sender_side == 'local': first_src = self.r['cc_src'] else: second_src = self.r['cc_src'] port = get_open_port() first_cmd = 'tunnel %s python %s receiver %s\n' % ( tun_id, first_src, port) second_cmd = 'tunnel %s python %s sender %s %s\n' % ( tun_id, second_src, recv_pri_ip, port) recv_manager.stdin.write(first_cmd) recv_manager.stdin.flush() else: # self.run_first == 'sender' if self.mode == 'remote': if self.sender_side == 'local': second_src = self.r['cc_src'] else: first_src = self.r['cc_src'] port = get_open_port() first_cmd = 'tunnel %s python %s sender %s\n' % ( tun_id, first_src, port) second_cmd = 'tunnel %s python %s receiver %s %s\n' % ( tun_id, second_src, send_pri_ip, port) send_manager.stdin.write(first_cmd) send_manager.stdin.flush() return second_cmd
def test_schemes(args): src_dir = path.join(project_root.DIR, 'src') if args.all: schemes = parse_config()['schemes'].keys() elif args.schemes is not None: schemes = args.schemes.split() for scheme in schemes: sys.stderr.write('Testing %s...\n' % scheme) src = path.join(src_dir, scheme + '.py') run_first = check_output([src, 'run_first']).strip() run_second = 'receiver' if run_first == 'sender' else 'sender' port = get_open_port() # run first to run cmd = [src, run_first, port] first_proc = Popen(cmd, preexec_fn=os.setsid) # wait for 'run_first' to be ready time.sleep(3) # run second to run cmd = [src, run_second, '127.0.0.1', port] second_proc = Popen(cmd, preexec_fn=os.setsid) # test lasts for 3 seconds signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(3) try: for proc in [first_proc, second_proc]: proc.wait() if proc.returncode != 0: sys.exit('%s failed in tests' % scheme) except TimeoutError: pass except Exception as exception: sys.exit('test_schemes.py: %s\n' % exception) else: signal.alarm(0) sys.exit('test exited before time limit') finally: # cleanup kill_proc_group(first_proc) kill_proc_group(second_proc)
def run_first_side(self, tun_id, send_manager, recv_manager, send_pri_ip, recv_pri_ip): first_src = self.cc_src second_src = self.cc_src if self.run_first == 'receiver': if self.mode == 'remote': if self.sender_side == 'local': first_src = self.r['cc_src'] else: second_src = self.r['cc_src'] port = get_open_port() first_cmd = 'tunnel %s python %s receiver %s\n' % (tun_id, first_src, port) second_cmd = 'tunnel %s python %s sender %s %s\n' % ( tun_id, second_src, recv_pri_ip, port) recv_manager.stdin.write(first_cmd) recv_manager.stdin.flush() elif self.run_first == 'sender': # self.run_first == 'sender' if self.mode == 'remote': if self.sender_side == 'local': second_src = self.r['cc_src'] else: first_src = self.r['cc_src'] port = get_open_port() first_cmd = 'tunnel %s python %s sender %s\n' % (tun_id, first_src, port) second_cmd = 'tunnel %s python %s receiver %s %s\n' % ( tun_id, second_src, send_pri_ip, port) send_manager.stdin.write(first_cmd) send_manager.stdin.flush() # get run_first and run_second from the flow object else: assert (hasattr(self, 'flow_objs')) flow = self.flow_objs[tun_id - 1] # tunnel id starts with 1 first_src = flow.cc_src_local second_src = flow.cc_src_local if flow.run_first == 'receiver': if self.mode == 'remote': if self.sender_side == 'local': first_src = flow.cc_src_remote else: second_src = flow.cc_src_remote port = get_open_port() first_cmd = 'tunnel %s python %s receiver %s\n' % ( tun_id, first_src, port) second_cmd = 'tunnel %s python %s sender %s %s\n' % ( tun_id, second_src, recv_pri_ip, port) recv_manager.stdin.write(first_cmd) recv_manager.stdin.flush() else: # flow.run_first == 'sender' if self.mode == 'remote': if self.sender_side == 'local': second_src = flow.cc_src_remote else: first_src = flow.cc_src_remote port = get_open_port() first_cmd = 'tunnel %s python %s sender %s\n' % ( tun_id, first_src, port) second_cmd = 'tunnel %s python %s receiver %s %s\n' % ( tun_id, second_src, send_pri_ip, port) send_manager.stdin.write(first_cmd) send_manager.stdin.flush() return second_cmd