def test_usr1_access_log(self, temp_dir, unit_pid): self.load('empty') log = 'access.log' log_new = 'new.log' log_path = temp_dir + '/' + log assert 'success' in self.conf('"' + log_path + '"', 'access_log'), 'access log configure' assert waitforfiles(log_path), 'open' os.rename(log_path, temp_dir + '/' + log_new) assert self.get()['status'] == 200 assert (self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new) is not None), 'rename new' assert not os.path.isfile(log_path), 'rename old' os.kill(unit_pid, signal.SIGUSR1) assert waitforfiles(log_path), 'reopen' assert self.get(url='/usr1')['status'] == 200 assert (self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log) is not None), 'reopen 2' assert self.search_in_log(r'/usr1', log_new) is None, 'rename new 2'
def test_usr1_access_log(self, temp_dir): self.load('empty') log = 'access.log' log_new = 'new.log' log_path = temp_dir + '/' + log assert 'success' in self.conf('"' + log_path + '"', 'access_log'), 'access log configure' assert waitforfiles(log_path), 'open' os.rename(log_path, temp_dir + '/' + log_new) assert self.get()['status'] == 200 assert (self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new) is not None), 'rename new' assert not os.path.isfile(log_path), 'rename old' with open(temp_dir + '/unit.pid', 'r') as f: pid = f.read().rstrip() call(['kill', '-s', 'USR1', pid]) assert waitforfiles(log_path), 'reopen' assert self.get(url='/usr1')['status'] == 200 unit_stop() assert (self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log) is not None), 'reopen 2' assert self.search_in_log(r'/usr1', log_new) is None, 'rename new 2'
def test_usr1_unit_log(self, temp_dir): self.load('log_body') log_new = 'new.log' log_path = temp_dir + '/unit.log' log_path_new = temp_dir + '/' + log_new os.rename(log_path, log_path_new) body = 'body_for_a_log_new' assert self.post(body=body)['status'] == 200 assert self.wait_for_record(body, log_new) is not None, 'rename new' assert not os.path.isfile(log_path), 'rename old' with open(temp_dir + '/unit.pid', 'r') as f: pid = f.read().rstrip() call(['kill', '-s', 'USR1', pid]) assert waitforfiles(log_path), 'reopen' body = 'body_for_a_log_unit' assert self.post(body=body)['status'] == 200 assert self.wait_for_record(body) is not None, 'rename new' assert self.search_in_log(body, log_new) is None, 'rename new 2' # merge two log files into unit.log to check alerts with open(log_path, 'w') as unit_log, open( log_path_new, 'r' ) as unit_log_new: unit_log.write(unit_log_new.read())
def unit_run(): global unit_instance if not option.restart and 'unitd' in unit_instance: return unit_instance build_dir = option.current_dir + '/build' unitd = build_dir + '/unitd' if not os.path.isfile(unitd): exit('Could not find unit') temp_dir = tempfile.mkdtemp(prefix='unit-test-') public_dir(temp_dir) if oct(stat.S_IMODE(os.stat(build_dir).st_mode)) != '0o777': public_dir(build_dir) os.mkdir(temp_dir + '/state') unitd_args = [ unitd, '--no-daemon', '--modules', build_dir, '--state', temp_dir + '/state', '--pid', temp_dir + '/unit.pid', '--log', temp_dir + '/unit.log', '--control', 'unix:' + temp_dir + '/control.unit.sock', '--tmp', temp_dir, ] if option.user: unitd_args.extend(['--user', option.user]) with open(temp_dir + '/unit.log', 'w') as log: unit_instance['process'] = subprocess.Popen(unitd_args, stderr=log) if not waitforfiles(temp_dir + '/control.unit.sock'): _print_log() exit('Could not start unit') unit_instance['temp_dir'] = temp_dir unit_instance['log'] = temp_dir + '/unit.log' unit_instance['control_sock'] = temp_dir + '/control.unit.sock' unit_instance['unitd'] = unitd return unit_instance
def test_node_application_promise_end(self, temp_dir): self.load('promise_end') assert (self.post( headers={ 'Host': 'localhost', 'Content-Type': 'text/html', 'Connection': 'close', }, body='end', )['status'] == 200), 'promise end request' assert waitforfiles(temp_dir + '/node/callback'), 'promise end'
def test_usr1_unit_log(self, temp_dir, unit_pid): self.load('log_body') log_new = 'new.log' log_path = temp_dir + '/unit.log' log_path_new = temp_dir + '/' + log_new os.rename(log_path, log_path_new) Log.swap(log_new) try: body = 'body_for_a_log_new\n' assert self.post(body=body)['status'] == 200 assert (self.wait_for_record(body, log_new) is not None), 'rename new' assert not os.path.isfile(log_path), 'rename old' os.kill(unit_pid, signal.SIGUSR1) assert waitforfiles(log_path), 'reopen' body = 'body_for_a_log_unit\n' assert self.post(body=body)['status'] == 200 assert self.wait_for_record(body) is not None, 'rename new' assert self.search_in_log(body, log_new) is None, 'rename new 2' finally: # merge two log files into unit.log to check alerts with open(log_path, 'r', errors='ignore') as unit_log: log = unit_log.read() with open(log_path, 'w') as unit_log, open(log_path_new, 'r', errors='ignore') as unit_log_new: unit_log.write(unit_log_new.read()) unit_log.write(log) Log.swap(log_new)
def unit_run(): global unit_instance if not option.restart and 'unitd' in unit_instance: return unit_instance build_dir = option.current_dir + '/build' unitd = build_dir + '/unitd' if not os.path.isfile(unitd): exit('Could not find unit') temp_dir = tempfile.mkdtemp(prefix='unit-test-') public_dir(temp_dir) if oct(stat.S_IMODE(os.stat(build_dir).st_mode)) != '0o777': public_dir(build_dir) os.mkdir(temp_dir + '/state') unitd_args = [ unitd, '--no-daemon', '--modules', build_dir, '--state', temp_dir + '/state', '--pid', temp_dir + '/unit.pid', '--log', temp_dir + '/unit.log', '--control', 'unix:' + temp_dir + '/control.unit.sock', '--tmp', temp_dir, ] if option.user: unitd_args.extend(['--user', option.user]) with open(temp_dir + '/unit.log', 'w') as log: unit_instance['process'] = subprocess.Popen(unitd_args, stderr=log) Log.temp_dir = temp_dir if not waitforfiles(temp_dir + '/control.unit.sock'): _print_log() exit('Could not start unit') unit_instance['temp_dir'] = temp_dir unit_instance['control_sock'] = temp_dir + '/control.unit.sock' unit_instance['unitd'] = unitd option.temp_dir = temp_dir with open(temp_dir + '/unit.pid', 'r') as f: unit_instance['pid'] = f.read().rstrip() _clear_conf(unit_instance['temp_dir'] + '/control.unit.sock') _fds_info['main']['fds'] = _count_fds(unit_instance['pid']) router = _fds_info['router'] router['pid'] = pid_by_name(router['name']) router['fds'] = _count_fds(router['pid']) controller = _fds_info['controller'] controller['pid'] = pid_by_name(controller['name']) controller['fds'] = _count_fds(controller['pid']) return unit_instance
def test_static_space_in_name(self, temp_dir): os.rename( temp_dir + '/assets/dir/file', temp_dir + '/assets/dir/fi le', ) assert waitforfiles(temp_dir + '/assets/dir/fi le') assert self.get(url='/dir/fi le')['body'] == 'blah', 'file name' os.rename(temp_dir + '/assets/dir', temp_dir + '/assets/di r') assert waitforfiles(temp_dir + '/assets/di r/fi le') assert self.get(url='/di r/fi le')['body'] == 'blah', 'dir name' os.rename( temp_dir + '/assets/di r', temp_dir + '/assets/ di r ' ) assert waitforfiles(temp_dir + '/assets/ di r /fi le') assert ( self.get(url='/ di r /fi le')['body'] == 'blah' ), 'dir name enclosing' assert ( self.get(url='/%20di%20r%20/fi le')['body'] == 'blah' ), 'dir encoded' assert ( self.get(url='/ di r %2Ffi le')['body'] == 'blah' ), 'slash encoded' assert ( self.get(url='/ di r /fi%20le')['body'] == 'blah' ), 'file encoded' assert ( self.get(url='/%20di%20r%20%2Ffi%20le')['body'] == 'blah' ), 'encoded' assert ( self.get(url='/%20%64%69%20%72%20%2F%66%69%20%6C%65')['body'] == 'blah' ), 'encoded 2' os.rename( temp_dir + '/assets/ di r /fi le', temp_dir + '/assets/ di r / fi le ', ) assert waitforfiles(temp_dir + '/assets/ di r / fi le ') assert ( self.get(url='/%20di%20r%20/%20fi%20le%20')['body'] == 'blah' ), 'file name enclosing' try: open(temp_dir + '/ф а', 'a').close() utf8 = True except KeyboardInterrupt: raise except: utf8 = False if utf8: os.rename( temp_dir + '/assets/ di r / fi le ', temp_dir + '/assets/ di r /фа йл', ) assert waitforfiles(temp_dir + '/assets/ di r /фа йл') assert ( self.get(url='/ di r /фа йл')['body'] == 'blah' ), 'file name 2' os.rename( temp_dir + '/assets/ di r ', temp_dir + '/assets/ди ректория', ) assert waitforfiles(temp_dir + '/assets/ди ректория/фа йл') assert ( self.get(url='/ди ректория/фа йл')['body'] == 'blah' ), 'dir name 2'
def test_node_application_write_callback(self, temp_dir): self.load('write_callback') assert self.get()['body'] == 'helloworld', 'write callback order' assert waitforfiles(temp_dir + '/node/callback'), 'write callback'