From fcabbf09d85397e244f6486d6eca25e2366b61cd Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Tue, 21 Feb 2023 15:35:38 +0000 Subject: Tests: added Python tests with encoding. --- test/test_python_application.py | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'test/test_python_application.py') diff --git a/test/test_python_application.py b/test/test_python_application.py index c9483b6a..c9065eae 100644 --- a/test/test_python_application.py +++ b/test/test_python_application.py @@ -2,9 +2,12 @@ import grp import os import pwd import re +import subprocess import time +import venv import pytest +from packaging import version from unit.applications.lang.python import TestApplicationPython @@ -526,6 +529,81 @@ last line: 987654321 assert self.get()['body'] == '0123456789', 'write' + def test_python_application_encoding(self): + self.load('encoding') + + try: + locales = ( + subprocess.check_output( + ['locale', '-a'], + stderr=subprocess.STDOUT, + ) + .decode() + .splitlines() + ) + except ( + FileNotFoundError, + UnicodeDecodeError, + subprocess.CalledProcessError, + ): + pytest.skip('require locale') + + to_check = [ + re.compile(r'.*UTF[-_]?8'), + re.compile(r'.*ISO[-_]?8859[-_]?1'), + ] + matches = [ + loc + for loc in locales + if any(pattern.match(loc.upper()) for pattern in to_check) + ] + + if not matches: + pytest.skip('no available locales') + + def unify(str): + str.upper().replace('-', '').replace('_', '') + + for loc in matches: + assert 'success' in self.conf( + {"LC_CTYPE": loc, "LC_ALL": ""}, + '/config/applications/encoding/environment', + ) + resp = self.get() + assert resp['status'] == 200, 'status' + assert unify(resp['headers']['X-Encoding']) == unify( + loc.split('.')[-1] + ) + + def test_python_application_unicode(self, temp_dir): + try: + app_type = self.get_application_type() + v = version.Version(app_type.split()[-1]) + if v.major != 3: + raise version.InvalidVersion + + except version.InvalidVersion: + pytest.skip('require python module version 3') + + venv_path = temp_dir + '/venv' + venv.create(venv_path) + + self.load('unicode') + assert 'success' in self.conf( + '"' + venv_path + '"', + '/config/applications/unicode/home', + ) + assert ( + self.get( + headers={ + 'Host': 'localhost', + 'Temp-dir': temp_dir, + 'Connection': 'close', + } + )['status'] + == 200 + ) + def test_python_application_threading(self): """wait_for_record() timeouts after 5s while every thread works at least 3s. So without releasing GIL test should fail. -- cgit From 7934dcabbc3c2b585e8d3f8fcee7020ba26f1687 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Tue, 21 Feb 2023 17:21:29 +0000 Subject: Tests: switched to using f-strings. Previously, it was necessary to support older versions of Python for compatibility. F-strings were released in Python 3.6. Python 3.5 was marked as unsupported by the end of 2020, so now it's possible to start using f-strings safely for better readability and performance. --- test/test_python_application.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'test/test_python_application.py') diff --git a/test/test_python_application.py b/test/test_python_application.py index c9065eae..d412ac68 100644 --- a/test/test_python_application.py +++ b/test/test_python_application.py @@ -20,17 +20,16 @@ class TestPythonApplication(TestApplicationPython): body = 'Test body string.' resp = self.http( - b"""POST / HTTP/1.1 + f"""POST / HTTP/1.1 Host: localhost -Content-Length: %d +Content-Length: {len(body)} Custom-Header: blah Custom-hEader: Blah Content-Type: text/html Connection: close custom-header: BLAH -%s""" - % (len(body), body.encode()), +{body}""".encode(), raw=True, ) @@ -101,7 +100,7 @@ custom-header: BLAH self.load('prefix', prefix='/api/rest') def set_prefix(prefix): - self.conf('"' + prefix + '"', 'applications/prefix/prefix') + self.conf(f'"{prefix}"', 'applications/prefix/prefix') def check_prefix(url, script_name, path_info): resp = self.get(url=url) @@ -585,12 +584,12 @@ last line: 987654321 except version.InvalidVersion: pytest.skip('require python module version 3') - venv_path = temp_dir + '/venv' + venv_path = f'{temp_dir}/venv' venv.create(venv_path) self.load('unicode') assert 'success' in self.conf( - '"' + venv_path + '"', + f'"{venv_path}"', '/config/applications/unicode/home', ) assert ( @@ -640,7 +639,7 @@ last line: 987654321 assert self.wait_for_record(r'Traceback') is not None, 'traceback' assert ( - self.wait_for_record(r'raise Exception\(\'first exception\'\)') + self.wait_for_record(r"raise Exception\('first exception'\)") is not None ), 'first exception raise' assert len(self.findall(r'Traceback')) == 1, 'traceback count 1' @@ -659,7 +658,7 @@ last line: 987654321 ), 'error 2' assert ( - self.wait_for_record(r'raise Exception\(\'second exception\'\)') + self.wait_for_record(r"raise Exception\('second exception'\)") is not None ), 'exception raise second' assert len(self.findall(r'Traceback')) == 2, 'traceback count 2' @@ -676,7 +675,7 @@ last line: 987654321 ) assert ( - self.wait_for_record(r'raise Exception\(\'third exception\'\)') + self.wait_for_record(r"raise Exception\('third exception'\)") is not None ), 'exception raise third' assert len(self.findall(r'Traceback')) == 3, 'traceback count 3' @@ -711,7 +710,7 @@ last line: 987654321 ) assert ( - self.wait_for_record(r'raise Exception\(\'next exception\'\)') + self.wait_for_record(r"raise Exception\('next exception'\)") is not None ), 'exception raise next' assert len(self.findall(r'Traceback')) == 5, 'traceback count 5' @@ -747,7 +746,7 @@ last line: 987654321 ), 'error' assert ( - self.wait_for_record(r'raise Exception\(\'close exception\'\)') + self.wait_for_record(r"raise Exception\('close exception'\)") is not None ), 'exception raise close' assert len(self.findall(r'Traceback')) == 8, 'traceback count 8' @@ -781,18 +780,14 @@ last line: 987654321 self.load('user_group', user='nobody', group=group) obj = self.getjson()['body'] - assert obj['UID'] == nobody_uid, ( - 'nobody uid user=nobody group=%s' % group - ) - - assert obj['GID'] == group_id, 'nobody gid user=nobody group=%s' % group + assert obj['UID'] == nobody_uid, f'nobody uid user=nobody group={group}' + assert obj['GID'] == group_id, f'nobody gid user=nobody group={group}' self.load('user_group', group=group) obj = self.getjson()['body'] - assert obj['UID'] == nobody_uid, 'nobody uid group=%s' % group - - assert obj['GID'] == group_id, 'nobody gid group=%s' % group + assert obj['UID'] == nobody_uid, f'nobody uid group={group}' + assert obj['GID'] == group_id, f'nobody gid group={group}' self.load('user_group', user='root') -- cgit