def test_access_log_keepalive(self): self.load('mirror') assert self.get()['status'] == 200, 'init' (resp, sock) = self.post( headers={ 'Host': 'localhost', 'Connection': 'keep-alive', 'Content-Type': 'text/html', }, start=True, body='01234', read_timeout=1, ) assert ( self.wait_for_record(r'"POST / HTTP/1.1" 200 5') is not None ), 'keepalive 1' resp = self.post( headers={ 'Host': 'localhost', 'Connection': 'close', 'Content-Type': 'text/html', }, sock=sock, body='0123456789', ) unit_stop() assert ( self.wait_for_record(r'"POST / HTTP/1.1" 200 10') is not None ), 'keepalive 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_access_log_pipeline(self): self.load('empty') self.http( b"""GET / HTTP/1.1 Host: localhost Referer: Referer-1 GET / HTTP/1.1 Host: localhost Referer: Referer-2 GET / HTTP/1.1 Host: localhost Referer: Referer-3 Connection: close """, raw_resp=True, raw=True, ) unit_stop() assert (self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "Referer-1" "-"') is not None), 'pipeline 1' assert (self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "Referer-2" "-"') is not None), 'pipeline 2' assert (self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "Referer-3" "-"') is not None), 'pipeline 3'
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 unit_stop() 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 test_python_application_close(self): self.load('close') self.get() unit_stop() assert self.wait_for_record(r'Close called\.') is not None, 'close'
def test_asgi_lifespan_targets(self): assert 'success' in self.conf({ "listeners": { "*:7080": { "pass": "******" } }, "routes": [ { "match": { "uri": "/1" }, "action": { "pass": "******" }, }, { "match": { "uri": "/2" }, "action": { "pass": "******" }, }, ], "applications": { "targets": { "type": "python", "processes": { "spare": 0 }, "working_directory": option.test_dir + "/python/lifespan/empty", "path": option.test_dir + '/python/lifespan/empty', "targets": { "1": { "module": "asgi", "callable": "application" }, "2": { "module": "asgi", "callable": "application2", }, }, } }, }) self.setup_cookies('') self.setup_cookies('app2_') assert self.get(url="/1")['status'] == 204 assert self.get(url="/2")['status'] == 204 unit_stop() self.assert_cookies('') self.assert_cookies('app2_')
def test_perl_application_errors_print(self): self.load('errors_print') assert self.get()['body'] == '1', 'errors result' unit_stop() assert (self.wait_for_record(r'\[error\].+Error in application') is not None), 'errors print'
def test_ruby_application_errors_puts(self): self.load('errors_puts') self.get() unit_stop() assert (self.wait_for_record(r'\[error\].+Error in application') is not None), 'errors puts'
def test_ruby_application_errors_write_int(self): self.load('errors_write_int') self.get() unit_stop() assert (self.wait_for_record(r'\[error\].+1234567890') is not None), 'errors write int'
def test_ruby_application_body_each_error(self): self.load('body_each_error') assert self.get()['status'] == 500, 'body each error status' unit_stop() assert (self.wait_for_record(r'\[error\].+Failed to run ruby script') is not None), 'body each error'
def test_python_application_errors_write(self): self.load('errors_write') self.get() unit_stop() assert (self.wait_for_record(r'\[error\].+Error in application\.') is not None), 'errors write'
def test_access_log_http10(self): self.load('empty') self.get(http_10=True) unit_stop() assert (self.wait_for_record(r'"GET / HTTP/1.0" 200 0 "-" "-"') is not None), 'http 1.0'
def test_access_log_delete(self): self.load('empty') self.conf_delete('access_log') self.get(url='/delete') unit_stop() assert self.search_in_log(r'/delete', 'access.log') is None, 'delete'
def test_access_log_get_parameters(self): self.load('empty') self.get(url='/?blah&var=val') unit_stop() assert (self.wait_for_record( r'"GET /\?blah&var=val HTTP/1.1" 200 0 "-" "-"') is not None), 'get parameters'
def test_asgi_lifespan(self): self.load('lifespan/empty') self.setup_cookies('') assert self.get()['status'] == 204 unit_stop() self.assert_cookies('')
def test_python_application_not_iterable(self): self.load('not_iterable') self.get() unit_stop() assert (self.wait_for_record( r'\[error\].+the application returned not an iterable object') is not None), 'not iterable'
def test_python_atexit(self): self.load('atexit') self.get() self.conf({"listeners": {}, "applications": {}}) unit_stop() assert self.wait_for_record(r'At exit called\.') is not None, 'atexit'
def test_access_log_partial_2(self): self.load('empty') assert self.post()['status'] == 200, 'init' self.http(b"""GET /\n""", raw=True) unit_stop() assert (self.wait_for_record(r'"GET /" 400 \d+ "-" "-"') is not None), 'partial 2'
def test_ruby_application_at_exit(self): self.load('at_exit') self.get() self.conf({"listeners": {}, "applications": {}}) unit_stop() assert (self.wait_for_record(r'\[error\].+At exit called\.') is not None), 'at exit'
def test_access_log_partial_5(self): self.load('empty') assert self.post()['status'] == 200, 'init' self.get(headers={'Connection': 'close'}) unit_stop() assert (self.wait_for_record(r'"GET / HTTP/1.1" 400 \d+ "-" "-"') is not None), 'partial 5'
def test_php_application_fastcgi_finish_request(self, temp_dir): self.load('fastcgi_finish_request') assert self.get()['body'] == '0123' unit_stop() with open(temp_dir + '/unit.log', 'r', errors='ignore') as f: errs = re.findall(r'Error in fastcgi_finish_request', f.read()) assert len(errs) == 0, 'no error'
def test_access_log_ipv6(self): self.load('empty') self.conf({"[::1]:7080": {"pass": "******"}}, 'listeners') self.get(sock_type='ipv6') unit_stop() assert (self.wait_for_record( r'::1 - - \[.+\] "GET / HTTP/1.1" 200 0 "-" "-"') is not None), 'ipv6'
def test_access_log_partial_4(self): self.load('empty') assert self.post()['status'] == 200, 'init' resp = self.http(b"""GET / HTTP/1.1\n""", raw=True, read_timeout=1) time.sleep(1) unit_stop() assert (self.wait_for_record(r'"GET / HTTP/1.1" 400 0 "-" "-"') is not None), 'partial 4'
def test_access_log_change(self, temp_dir): self.load('empty') self.get() self.conf('"' + option.temp_dir + '/new.log"', 'access_log') self.get() unit_stop() assert (self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', 'new.log') is not None), 'change'
def test_php_application_fastcgi_finish_request_2(self, temp_dir): self.load('fastcgi_finish_request') resp = self.get(url='/?skip') assert resp['status'] == 200 assert resp['body'] == '' unit_stop() with open(temp_dir + '/unit.log', 'r', errors='ignore') as f: errs = re.findall(r'Error in fastcgi_finish_request', f.read()) assert len(errs) == 0, 'no error'
def test_access_log_referer(self): self.load('empty') self.get( headers={ 'Host': 'localhost', 'Referer': 'referer-value', 'Connection': 'close', }) unit_stop() assert ( self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "referer-value" "-"') is not None), 'referer'
def test_access_log_user_agent(self): self.load('empty') self.get( headers={ 'Host': 'localhost', 'User-Agent': 'user-agent-value', 'Connection': 'close', }) unit_stop() assert (self.wait_for_record( r'"GET / HTTP/1.1" 200 0 "-" "user-agent-value"') is not None), 'user agent'
def test_access_log_unix(self): self.load('empty') addr = option.temp_dir + '/sock' self.conf({"unix:" + addr: { "pass": "******" }}, 'listeners') self.get(sock_type='unix', addr=addr) unit_stop() assert (self.wait_for_record( r'unix: - - \[.+\] "GET / HTTP/1.1" 200 0 "-" "-"') is not None), 'unix'
def test_python_application_ctx_iter_atexit(self): self.load('ctx_iter_atexit') resp = self.post( headers={ 'Host': 'localhost', 'Connection': 'close', 'Content-Type': 'text/html', }, body='0123456789', ) assert resp['status'] == 200, 'ctx iter status' assert resp['body'] == '0123456789', 'ctx iter body' self.conf({"listeners": {}, "applications": {}}) unit_stop() assert (self.wait_for_record(r'RuntimeError') is not None), 'ctx iter atexit'
def setup_class(cls, complete_check=True): check = super().setup_class(complete_check=False) unit = unit_run() option.temp_dir = unit['temp_dir'] TestFeatureIsolation().check(option.available, unit['temp_dir']) assert unit_stop() is None shutil.rmtree(unit['temp_dir']) return check if not complete_check else check()