summaryrefslogtreecommitdiffhomepage
path: root/test/unit
diff options
context:
space:
mode:
authorAndrei Belov <defan@nginx.com>2020-03-12 18:40:48 +0300
committerAndrei Belov <defan@nginx.com>2020-03-12 18:40:48 +0300
commit4b7ca39903178e20ec7381205694cb01f0dec6bc (patch)
tree51afb9c7003b5927183e7ddecd766eb19e421233 /test/unit
parent8414897527ed1616ea39a0cae4d1b8ee170d5cb8 (diff)
parentb3c8a7b33a29208e75dfe4f670cf81dac7b99ccc (diff)
downloadunit-4b7ca39903178e20ec7381205694cb01f0dec6bc.tar.gz
unit-4b7ca39903178e20ec7381205694cb01f0dec6bc.tar.bz2
Merged with the default branch.1.16.0-1
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/applications/lang/go.py5
-rw-r--r--test/unit/applications/lang/java.py4
-rw-r--r--test/unit/applications/lang/php.py4
-rw-r--r--test/unit/applications/tls.py3
-rw-r--r--test/unit/http.py45
-rw-r--r--test/unit/main.py91
6 files changed, 90 insertions, 62 deletions
diff --git a/test/unit/applications/lang/go.py b/test/unit/applications/lang/go.py
index 7212a95c..e0f83c0a 100644
--- a/test/unit/applications/lang/go.py
+++ b/test/unit/applications/lang/go.py
@@ -1,5 +1,5 @@
import os
-from subprocess import Popen
+import subprocess
from unit.applications.proto import TestApplicationProto
@@ -26,7 +26,7 @@ class TestApplicationGo(TestApplicationProto):
env['GOPATH'] = self.pardir + '/build/go'
try:
- process = Popen(
+ process = subprocess.Popen(
[
'go',
'build',
@@ -35,6 +35,7 @@ class TestApplicationGo(TestApplicationProto):
self.current_dir + '/go/' + script + '/' + name + '.go',
],
env=env,
+ stderr=subprocess.STDOUT,
)
process.communicate()
diff --git a/test/unit/applications/lang/java.py b/test/unit/applications/lang/java.py
index a370d96b..a8a09ce5 100644
--- a/test/unit/applications/lang/java.py
+++ b/test/unit/applications/lang/java.py
@@ -1,7 +1,7 @@
import os
import glob
import shutil
-from subprocess import Popen
+import subprocess
from unit.applications.proto import TestApplicationProto
@@ -64,7 +64,7 @@ class TestApplicationJava(TestApplicationProto):
javac.extend(src)
try:
- process = Popen(javac)
+ process = subprocess.Popen(javac, stderr=subprocess.STDOUT)
process.communicate()
except:
diff --git a/test/unit/applications/lang/php.py b/test/unit/applications/lang/php.py
index 6b1677e6..e8c70c62 100644
--- a/test/unit/applications/lang/php.py
+++ b/test/unit/applications/lang/php.py
@@ -4,7 +4,7 @@ from unit.applications.proto import TestApplicationProto
class TestApplicationPHP(TestApplicationProto):
application_type = "php"
- def load(self, script, name='index.php', **kwargs):
+ def load(self, script, index='index.php', **kwargs):
script_path = self.current_dir + '/php/' + script
self._load_conf(
@@ -16,7 +16,7 @@ class TestApplicationPHP(TestApplicationProto):
"processes": {"spare": 0},
"root": script_path,
"working_directory": script_path,
- "index": name,
+ "index": index,
}
},
},
diff --git a/test/unit/applications/tls.py b/test/unit/applications/tls.py
index 1290279d..9213974a 100644
--- a/test/unit/applications/tls.py
+++ b/test/unit/applications/tls.py
@@ -47,7 +47,8 @@ class TestApplicationTLS(TestApplicationProto):
'-config', self.testdir + '/openssl.conf',
'-out', self.testdir + '/' + name + '.crt',
'-keyout', self.testdir + '/' + name + '.key',
- ]
+ ],
+ stderr=subprocess.STDOUT,
)
if load:
diff --git a/test/unit/http.py b/test/unit/http.py
index c71e8f7e..47fb48f1 100644
--- a/test/unit/http.py
+++ b/test/unit/http.py
@@ -96,12 +96,7 @@ class TestHTTP(TestUnit):
encoding = 'utf-8' if 'encoding' not in kwargs else kwargs['encoding']
- if TestUnit.detailed:
- print('>>>')
- try:
- print(req.decode(encoding, 'ignore'))
- except UnicodeEncodeError:
- print(req)
+ self.log_out(req, encoding)
resp = ''
@@ -113,12 +108,7 @@ class TestHTTP(TestUnit):
sock, read_timeout=read_timeout, buff_size=read_buffer_size
).decode(encoding)
- if TestUnit.detailed:
- print('<<<')
- try:
- print(resp)
- except UnicodeEncodeError:
- print(resp.encode())
+ self.log_in(resp)
if 'raw_resp' not in kwargs:
resp = self._resp_to_dict(resp)
@@ -138,6 +128,37 @@ class TestHTTP(TestUnit):
return (resp, sock)
+ def log_out(self, log, encoding):
+ if TestUnit.detailed:
+ print('>>>')
+ log = self.log_truncate(log)
+ try:
+ print(log.decode(encoding, 'ignore'))
+ except UnicodeEncodeError:
+ print(log)
+
+ def log_in(self, log):
+ if TestUnit.detailed:
+ print('<<<')
+ log = self.log_truncate(log)
+ try:
+ print(log)
+ except UnicodeEncodeError:
+ print(log.encode())
+
+ def log_truncate(self, log, limit=1024):
+ len_log = len(log)
+ if len_log > limit:
+ log = log[:limit]
+ appendix = '(...logged %s of %s bytes)' % (limit, len_log)
+
+ if isinstance(log, bytes):
+ appendix = appendix.encode()
+
+ log = log + appendix
+
+ return log
+
def delete(self, **kwargs):
return self.http('DELETE', **kwargs)
diff --git a/test/unit/main.py b/test/unit/main.py
index 37d01d3b..69234dcc 100644
--- a/test/unit/main.py
+++ b/test/unit/main.py
@@ -5,6 +5,7 @@ import stat
import time
import fcntl
import shutil
+import signal
import argparse
import platform
import tempfile
@@ -30,6 +31,7 @@ class TestUnit(unittest.TestCase):
detailed = False
save_log = False
+ print_log = False
unsafe = False
def __init__(self, methodName='runTest'):
@@ -183,7 +185,7 @@ class TestUnit(unittest.TestCase):
shutil.rmtree(self.testdir)
else:
- self._print_path_to_log()
+ self._print_log()
def stop(self):
if self._started:
@@ -207,9 +209,9 @@ class TestUnit(unittest.TestCase):
os.mkdir(self.testdir + '/state')
- print()
-
- self._p = Process(target=subprocess.call, args=[ [
+ with open(self.testdir + '/unit.log', 'w') as log:
+ self._p = subprocess.Popen(
+ [
self.unitd,
'--no-daemon',
'--modules', self.pardir + '/build',
@@ -217,55 +219,40 @@ class TestUnit(unittest.TestCase):
'--pid', self.testdir + '/unit.pid',
'--log', self.testdir + '/unit.log',
'--control', 'unix:' + self.testdir + '/control.unit.sock',
- ] ])
- self._p.start()
-
- if not self.waitforfiles(
- self.testdir + '/unit.pid',
- self.testdir + '/unit.log',
- self.testdir + '/control.unit.sock',
- ):
+ '--tmp', self.testdir,
+ ],
+ stderr=log,
+ )
+
+ if not self.waitforfiles(self.testdir + '/control.unit.sock'):
exit("Could not start unit")
self._started = True
self.skip_alerts = [
r'read signalfd\(4\) failed',
+ r'last message send failed',
r'sendmsg.+failed',
r'recvmsg.+failed',
]
self.skip_sanitizer = False
def _stop(self):
- with open(self.testdir + '/unit.pid', 'r') as f:
- pid = f.read().rstrip()
-
- subprocess.call(['kill', '-s', 'QUIT', pid])
-
- for i in range(150):
- if not os.path.exists(self.testdir + '/unit.pid'):
- break
- time.sleep(0.1)
-
- self._p.join(timeout=5)
-
- if self._p.is_alive():
- self._p.terminate()
- self._p.join(timeout=5)
-
- if self._p.is_alive():
- self.fail("Could not terminate process " + str(self._p.pid))
-
- if os.path.exists(self.testdir + '/unit.pid'):
- self.fail("Could not terminate unit")
+ with self._p as p:
+ p.send_signal(signal.SIGQUIT)
+
+ try:
+ retcode = p.wait(15)
+ if retcode:
+ self.fail(
+ "Child process terminated with code " + str(retcode)
+ )
+ except:
+ self.fail("Could not terminate unit")
+ p.kill()
self._started = False
- if self._p.exitcode:
- self.fail(
- "Child process terminated with code " + str(self._p.exitcode)
- )
-
def _check_alerts(self, log):
found = False
@@ -281,14 +268,14 @@ class TestUnit(unittest.TestCase):
alerts = [al for al in alerts if re.search(skip, al) is None]
if alerts:
- self._print_path_to_log()
+ self._print_log(log)
self.assertFalse(alerts, 'alert(s)')
if not self.skip_sanitizer:
sanitizer_errors = re.findall('.+Sanitizer.+', log)
if sanitizer_errors:
- self._print_path_to_log()
+ self._print_log(log)
self.assertFalse(sanitizer_errors, 'sanitizer error(s)')
if found:
@@ -361,6 +348,13 @@ class TestUnit(unittest.TestCase):
help='Save unit.log after the test execution',
)
parser.add_argument(
+ '-r',
+ '--reprint_log',
+ dest='print_log',
+ action='store_true',
+ help='Print unit.log to stdout in case of errors',
+ )
+ parser.add_argument(
'-u',
'--unsafe',
dest='unsafe',
@@ -374,12 +368,23 @@ class TestUnit(unittest.TestCase):
def _set_args(args):
TestUnit.detailed = args.detailed
TestUnit.save_log = args.save_log
+ TestUnit.print_log = args.print_log
TestUnit.unsafe = args.unsafe
# set stdout to non-blocking
- if TestUnit.detailed:
+ if TestUnit.detailed or TestUnit.print_log:
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, 0)
- def _print_path_to_log(self):
- print('Path to unit.log:\n' + self.testdir + '/unit.log')
+ def _print_log(self, data=None):
+ path = self.testdir + '/unit.log'
+
+ print('Path to unit.log:\n' + path + '\n')
+
+ if TestUnit.print_log:
+ if data is None:
+ with open(path, 'r', encoding='utf-8', errors='ignore') as f:
+ data = f.read()
+
+ print(data)
+