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.
This commit is contained in:
Andrei Zeliankou
2023-02-21 17:21:29 +00:00
parent fcabbf09d8
commit 7934dcabbc
74 changed files with 695 additions and 778 deletions

View File

@@ -14,23 +14,21 @@ class TestApplicationGo(TestApplicationProto):
except subprocess.CalledProcessError:
return None
temp_dir = option.temp_dir + '/go/'
temp_dir = f'{option.temp_dir}/go/'
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
cache_dir = option.cache_dir + '/go-build'
cache_dir = f'{option.cache_dir}/go-build'
if not os.path.exists(cache_dir):
os.mkdir(cache_dir)
env = os.environ.copy()
env['GOPATH'] = option.current_dir + '/build/go'
env['GOPATH'] = f'{option.current_dir}/build/go'
env['GOCACHE'] = cache_dir
shutil.copy2(
option.test_dir + '/go/' + script + '/' + name + '.go', temp_dir
)
shutil.copy2(f'{option.test_dir}/go/{script}/{name}.go', temp_dir)
if static:
args = [
@@ -41,21 +39,21 @@ class TestApplicationGo(TestApplicationProto):
'-ldflags',
'-extldflags "-static"',
'-o',
temp_dir + name,
temp_dir + name + '.go',
f'{temp_dir}{name}',
f'{temp_dir}{name}.go',
]
else:
args = [
'go',
'build',
'-o',
temp_dir + name,
temp_dir + name + '.go',
f'{temp_dir}{name}',
f'{temp_dir}{name}.go',
]
replace_path = option.current_dir + '/build/go/src/unit.nginx.org/go'
replace_path = f'{option.current_dir}/build/go/src/unit.nginx.org/go'
with open(temp_dir + 'go.mod', 'w') as f:
with open(f'{temp_dir}go.mod', 'w') as f:
f.write(
f"""module test/app
require unit.nginx.org/go v0.0.0
@@ -64,7 +62,7 @@ replace unit.nginx.org/go => {replace_path}
)
if option.detailed:
print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))
print(f'\n$ GOPATH={env["GOPATH"]} {" ".join(args)}')
try:
output = subprocess.check_output(
@@ -82,18 +80,18 @@ replace unit.nginx.org/go => {replace_path}
def load(self, script, name='app', **kwargs):
static_build = False
wdir = option.test_dir + "/go/" + script
executable = option.temp_dir + "/go/" + name
wdir = f'{option.test_dir}/go/{script}'
executable = f'{option.temp_dir}/go/{name}'
if 'isolation' in kwargs and 'rootfs' in kwargs['isolation']:
wdir = "/go/"
executable = "/go/" + name
executable = f"/go/{name}"
static_build = True
TestApplicationGo.prepare_env(script, name, static=static_build)
conf = {
"listeners": {"*:7080": {"pass": "applications/" + script}},
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
"applications": {
script: {
"type": "external",

View File

@@ -12,10 +12,10 @@ class TestApplicationJava(TestApplicationProto):
application_type = "java"
def prepare_env(self, script):
app_path = option.temp_dir + '/java'
web_inf_path = app_path + '/WEB-INF/'
classes_path = web_inf_path + 'classes/'
script_path = option.test_dir + '/java/' + script + '/'
app_path = f'{option.temp_dir}/java'
web_inf_path = f'{app_path}/WEB-INF/'
classes_path = f'{web_inf_path}classes/'
script_path = f'{option.test_dir}/java/{script}/'
if not os.path.isdir(app_path):
os.makedirs(app_path)
@@ -23,7 +23,7 @@ class TestApplicationJava(TestApplicationProto):
src = []
for f in os.listdir(script_path):
file_path = script_path + f
file_path = f'{script_path}{f}'
if f.endswith('.java'):
src.append(file_path)
@@ -36,7 +36,7 @@ class TestApplicationJava(TestApplicationProto):
if f == 'WEB-INF':
continue
shutil.copytree(file_path, app_path + '/' + f)
shutil.copytree(file_path, f'{app_path}/{f}')
continue
if f == 'web.xml':
@@ -52,11 +52,11 @@ class TestApplicationJava(TestApplicationProto):
os.makedirs(classes_path)
classpath = (
option.current_dir + '/build/tomcat-servlet-api-9.0.70.jar'
f'{option.current_dir}/build/tomcat-servlet-api-9.0.70.jar'
)
ws_jars = glob.glob(
option.current_dir + '/build/websocket-api-java-*.jar'
f'{option.current_dir}/build/websocket-api-java-*.jar'
)
if not ws_jars:
@@ -74,12 +74,12 @@ class TestApplicationJava(TestApplicationProto):
'-d',
classes_path,
'-classpath',
classpath + ':' + ws_jars[0],
f'{classpath}:{ws_jars[0]}',
]
javac.extend(src)
if option.detailed:
print("\n$ " + " ".join(javac))
print(f'\n$ {" ".join(javac)}')
try:
subprocess.check_output(javac, stderr=subprocess.STDOUT)
@@ -88,26 +88,24 @@ class TestApplicationJava(TestApplicationProto):
raise
except subprocess.CalledProcessError:
pytest.fail('Can\'t run javac process.')
pytest.fail("Can't run javac process.")
def load(self, script, **kwargs):
self.prepare_env(script)
script_path = f'{option.test_dir}/java/{script}/'
self._load_conf(
{
"listeners": {"*:7080": {"pass": "applications/" + script}},
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
"applications": {
script: {
"unit_jars": option.current_dir + '/build',
"unit_jars": f'{option.current_dir}/build',
"type": self.get_application_type(),
"processes": {"spare": 0},
"working_directory": option.test_dir
+ '/java/'
+ script
+ '/',
"webapp": option.temp_dir + '/java',
"working_directory": script_path,
"webapp": f'{option.temp_dir}/java',
}
},
},
**kwargs
**kwargs,
)

View File

@@ -13,16 +13,16 @@ class TestApplicationNode(TestApplicationProto):
def prepare_env(self, script):
# copy application
shutil.copytree(
option.test_dir + '/node/' + script, option.temp_dir + '/node'
f'{option.test_dir}/node/{script}', f'{option.temp_dir}/node'
)
# copy modules
shutil.copytree(
option.current_dir + '/node/node_modules',
option.temp_dir + '/node/node_modules',
f'{option.current_dir}/node/node_modules',
f'{option.temp_dir}/node/node_modules',
)
public_dir(option.temp_dir + '/node')
public_dir(f'{option.temp_dir}/node')
def load(self, script, name='app.js', **kwargs):
self.prepare_env(script)
@@ -43,17 +43,17 @@ class TestApplicationNode(TestApplicationProto):
self._load_conf(
{
"listeners": {
"*:7080": {"pass": "applications/" + quote(script, '')}
"*:7080": {"pass": f"applications/{quote(script, '')}"}
},
"applications": {
script: {
"type": "external",
"processes": {"spare": 0},
"working_directory": option.temp_dir + '/node',
"working_directory": f'{option.temp_dir}/node',
"executable": '/usr/bin/env',
"arguments": arguments,
}
},
},
**kwargs
**kwargs,
)

View File

@@ -6,19 +6,19 @@ class TestApplicationPerl(TestApplicationProto):
application_type = "perl"
def load(self, script, name='psgi.pl', **kwargs):
script_path = option.test_dir + '/perl/' + script
script_path = f'{option.test_dir}/perl/{script}'
self._load_conf(
{
"listeners": {"*:7080": {"pass": "applications/" + script}},
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
"applications": {
script: {
"type": self.get_application_type(),
"processes": {"spare": 0},
"working_directory": script_path,
"script": script_path + '/' + name,
"script": f'{script_path}/{name}',
}
},
},
**kwargs
**kwargs,
)

View File

@@ -9,18 +9,18 @@ class TestApplicationPHP(TestApplicationProto):
application_type = "php"
def load(self, script, index='index.php', **kwargs):
script_path = option.test_dir + '/php/' + script
script_path = f'{option.test_dir}/php/{script}'
if kwargs.get('isolation') and kwargs['isolation'].get('rootfs'):
rootfs = kwargs['isolation']['rootfs']
if not os.path.exists(rootfs + '/app/php/'):
os.makedirs(rootfs + '/app/php/')
if not os.path.exists(f'{rootfs}/app/php/'):
os.makedirs(f'{rootfs}/app/php/')
if not os.path.exists(rootfs + '/app/php/' + script):
shutil.copytree(script_path, rootfs + '/app/php/' + script)
if not os.path.exists(f'{rootfs}/app/php/{script}'):
shutil.copytree(script_path, f'{rootfs}/app/php/{script}')
script_path = '/app/php/' + script
script_path = f'/app/php/{script}'
app = {
"type": self.get_application_type(),
@@ -41,8 +41,8 @@ class TestApplicationPHP(TestApplicationProto):
self._load_conf(
{
"listeners": {"*:7080": {"pass": "applications/" + script}},
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
"applications": {script: app},
},
**kwargs
**kwargs,
)

View File

@@ -20,18 +20,18 @@ class TestApplicationPython(TestApplicationProto):
if script[0] == '/':
script_path = script
else:
script_path = option.test_dir + '/python/' + script
script_path = f'{option.test_dir}/python/{script}'
if kwargs.get('isolation') and kwargs['isolation'].get('rootfs'):
rootfs = kwargs['isolation']['rootfs']
if not os.path.exists(rootfs + '/app/python/'):
os.makedirs(rootfs + '/app/python/')
if not os.path.exists(f'{rootfs}/app/python/'):
os.makedirs(f'{rootfs}/app/python/')
if not os.path.exists(rootfs + '/app/python/' + name):
shutil.copytree(script_path, rootfs + '/app/python/' + name)
if not os.path.exists(f'{rootfs}/app/python/{name}'):
shutil.copytree(script_path, f'{rootfs}/app/python/{name}')
script_path = '/app/python/' + name
script_path = f'/app/python/{name}'
app = {
"type": self.get_application_type(),
@@ -58,9 +58,9 @@ class TestApplicationPython(TestApplicationProto):
self._load_conf(
{
"listeners": {
"*:7080": {"pass": "applications/" + quote(name, '')}
"*:7080": {"pass": f"applications/{quote(name, '')}"}
},
"applications": {name: app},
},
**kwargs
**kwargs,
)

View File

@@ -10,22 +10,22 @@ class TestApplicationRuby(TestApplicationProto):
def prepare_env(self, script):
shutil.copytree(
option.test_dir + '/ruby/' + script,
option.temp_dir + '/ruby/' + script,
f'{option.test_dir}/ruby/{script}',
f'{option.temp_dir}/ruby/{script}',
)
public_dir(option.temp_dir + '/ruby/' + script)
public_dir(f'{option.temp_dir}/ruby/{script}')
def load(self, script, name='config.ru', **kwargs):
self.prepare_env(script)
script_path = option.temp_dir + '/ruby/' + script
script_path = f'{option.temp_dir}/ruby/{script}'
app = {
"type": self.get_application_type(),
"processes": {"spare": 0},
"working_directory": script_path,
"script": script_path + '/' + name,
"script": f'{script_path}/{name}',
}
for key in [
@@ -36,8 +36,8 @@ class TestApplicationRuby(TestApplicationProto):
self._load_conf(
{
"listeners": {"*:7080": {"pass": "applications/" + script}},
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
"applications": {script: app},
},
**kwargs
**kwargs,
)

View File

@@ -22,13 +22,13 @@ class TestApplicationTLS(TestApplicationProto):
'-x509',
'-new',
'-subj',
'/CN=' + name + '/',
f'/CN={name}/',
'-config',
option.temp_dir + '/openssl.conf',
f'{option.temp_dir}/openssl.conf',
'-out',
option.temp_dir + '/' + name + '.crt',
f'{option.temp_dir}/{name}.crt',
'-keyout',
option.temp_dir + '/' + name + '.key',
f'{option.temp_dir}/{name}.key',
],
stderr=subprocess.STDOUT,
)
@@ -40,11 +40,11 @@ class TestApplicationTLS(TestApplicationProto):
if key is None:
key = crt
key_path = option.temp_dir + '/' + key + '.key'
crt_path = option.temp_dir + '/' + crt + '.crt'
key_path = f'{option.temp_dir}/{key}.key'
crt_path = f'{option.temp_dir}/{crt}.crt'
with open(key_path, 'rb') as k, open(crt_path, 'rb') as c:
return self.conf(k.read() + c.read(), '/certificates/' + crt)
return self.conf(k.read() + c.read(), f'/certificates/{crt}')
def get_ssl(self, **kwargs):
return self.get(wrapper=self.context.wrap_socket, **kwargs)
@@ -54,54 +54,48 @@ class TestApplicationTLS(TestApplicationProto):
def openssl_conf(self, rewrite=False, alt_names=None):
alt_names = alt_names or []
conf_path = option.temp_dir + '/openssl.conf'
conf_path = f'{option.temp_dir}/openssl.conf'
if not rewrite and os.path.exists(conf_path):
return
# Generates alt_names section with dns names
a_names = "[alt_names]\n"
a_names = '[alt_names]\n'
for i, k in enumerate(alt_names, 1):
k = k.split('|')
if k[0] == 'IP':
a_names += "IP.%d = %s\n" % (i, k[1])
a_names += f'IP.{i} = {k[1]}\n'
else:
a_names += "DNS.%d = %s\n" % (i, k[0])
a_names += f'DNS.{i} = {k[0]}\n'
# Generates section for sign request extension
a_sec = """req_extensions = myca_req_extensions
a_sec = f'''req_extensions = myca_req_extensions
[ myca_req_extensions ]
subjectAltName = @alt_names
{a_names}""".format(
a_names=a_names
)
{a_names}'''
with open(conf_path, 'w') as f:
f.write(
"""[ req ]
f'''[ req ]
default_bits = 2048
encrypt_key = no
distinguished_name = req_distinguished_name
{a_sec}
[ req_distinguished_name ]""".format(
a_sec=a_sec if alt_names else ""
)
{a_sec if alt_names else ""}
[ req_distinguished_name ]'''
)
def load(self, script, name=None):
if name is None:
name = script
script_path = option.test_dir + '/python/' + script
script_path = f'{option.test_dir}/python/{script}'
self._load_conf(
{
"listeners": {"*:7080": {"pass": "applications/" + name}},
"listeners": {"*:7080": {"pass": f"applications/{name}"}},
"applications": {
name: {
"type": "python",

View File

@@ -52,7 +52,7 @@ class TestApplicationWebsocket(TestApplicationProto):
while True:
rlist = select.select([sock], [], [], 60)[0]
if not rlist:
pytest.fail('Can\'t read response from server.')
pytest.fail("Can't read response from server.")
resp += sock.recv(4096).decode()
@@ -77,7 +77,7 @@ class TestApplicationWebsocket(TestApplicationProto):
# For all current cases if the "read_timeout" was changed
# than test do not expect to get a response from server.
if read_timeout == 60:
pytest.fail('Can\'t read response from server.')
pytest.fail("Can't read response from server.")
break
data += sock.recv(bytes - len(data))

View File

@@ -12,7 +12,7 @@ def check_chroot():
resp = http.put(
url='/config',
sock_type='unix',
addr=option.temp_dir + '/control.unit.sock',
addr=f'{option.temp_dir}/control.unit.sock',
body=json.dumps(
{
"listeners": {"*:7080": {"pass": "routes"}},

View File

@@ -27,8 +27,8 @@ def check_isolation():
"empty": {
"type": "external",
"processes": {"spare": 0},
"working_directory": option.test_dir + "/go/empty",
"executable": option.temp_dir + "/go/app",
"working_directory": f"{option.test_dir}/go/empty",
"executable": f"{option.test_dir}/go/app",
"isolation": {"namespaces": {"credential": True}},
},
},
@@ -41,8 +41,8 @@ def check_isolation():
"empty": {
"type": "python",
"processes": {"spare": 0},
"path": option.test_dir + "/python/empty",
"working_directory": option.test_dir + "/python/empty",
"path": f"{option.test_dir}/python/empty",
"working_directory": f"{option.test_dir}/python/empty",
"module": "wsgi",
"isolation": {"namespaces": {"credential": True}},
}
@@ -56,8 +56,8 @@ def check_isolation():
"phpinfo": {
"type": "php",
"processes": {"spare": 0},
"root": option.test_dir + "/php/phpinfo",
"working_directory": option.test_dir + "/php/phpinfo",
"root": f"{option.test_dir}/php/phpinfo",
"working_directory": f"{option.test_dir}/php/phpinfo",
"index": "index.php",
"isolation": {"namespaces": {"credential": True}},
}
@@ -73,8 +73,8 @@ def check_isolation():
"empty": {
"type": "ruby",
"processes": {"spare": 0},
"working_directory": option.temp_dir + "/ruby/empty",
"script": option.temp_dir + "/ruby/empty/config.ru",
"working_directory": f"{option.temp_dir}/ruby/empty",
"script": f"{option.temp_dir}/ruby/empty/config.ru",
"isolation": {"namespaces": {"credential": True}},
}
},
@@ -87,11 +87,11 @@ def check_isolation():
"listeners": {"*:7080": {"pass": "applications/empty"}},
"applications": {
"empty": {
"unit_jars": option.current_dir + "/build",
"unit_jars": f"{option.current_dir}/build",
"type": "java",
"processes": {"spare": 0},
"working_directory": option.test_dir + "/java/empty/",
"webapp": option.temp_dir + "/java",
"working_directory": f"{option.temp_dir}/java/empty/",
"webapp": f"{option.temp_dir}/java",
"isolation": {"namespaces": {"credential": True}},
}
},
@@ -106,7 +106,7 @@ def check_isolation():
"basic": {
"type": "external",
"processes": {"spare": 0},
"working_directory": option.temp_dir + "/node",
"working_directory": f"{option.temp_dir}/node",
"executable": "app.js",
"isolation": {"namespaces": {"credential": True}},
}
@@ -120,8 +120,8 @@ def check_isolation():
"body_empty": {
"type": "perl",
"processes": {"spare": 0},
"working_directory": option.test_dir + "/perl/body_empty",
"script": option.test_dir + "/perl/body_empty/psgi.pl",
"working_directory": f"{option.test_dir}/perl/body_empty",
"script": f"{option.test_dir}/perl/body_empty/psgi.pl",
"isolation": {"namespaces": {"credential": True}},
}
},
@@ -133,7 +133,7 @@ def check_isolation():
resp = http.put(
url='/config',
sock_type='unix',
addr=option.temp_dir + '/control.unit.sock',
addr=f'{option.temp_dir}/control.unit.sock',
body=json.dumps(conf),
)

View File

@@ -3,7 +3,7 @@ import subprocess
def check_node(current_dir):
if not os.path.exists(current_dir + '/node/node_modules'):
if not os.path.exists(f'{current_dir}/node/node_modules'):
return None
try:

View File

@@ -12,7 +12,7 @@ def check_unix_abstract():
resp = http.put(
url='/config',
sock_type='unix',
addr=option.temp_dir + '/control.unit.sock',
addr=f'{option.temp_dir}/control.unit.sock',
body=json.dumps(
{
"listeners": {"unix:@sock": {"pass": "routes"}},

View File

@@ -21,7 +21,7 @@ def args_handler(conf_func):
url = args[1] if len(args) == 2 else url_default
url = url if url.startswith('/') else url_default + '/' + url
url = url if url.startswith('/') else f'{url_default}/{url}'
arguments = (self, url) if conf is None else (self, conf, url)
return json.loads(conf_func(*arguments))
@@ -50,7 +50,7 @@ class TestControl(TestHTTP):
args = {
'url': url,
'sock_type': 'unix',
'addr': option.temp_dir + '/control.unit.sock',
'addr': f'{option.temp_dir}/control.unit.sock',
}
if conf is not None:

View File

@@ -53,13 +53,13 @@ class TestHTTP:
sock.connect(connect_args)
except (ConnectionRefusedError, FileNotFoundError):
sock.close()
pytest.fail('Client can\'t connect to the server.')
pytest.fail("Client can't connect to the server.")
else:
sock = kwargs['sock']
if 'raw' not in kwargs:
req = ' '.join([start_str, url, http]) + crlf
req = f'{start_str} {url} {http}{crlf}'
if body != b'':
if isinstance(body, str):
@@ -75,10 +75,10 @@ class TestHTTP:
for header, value in headers.items():
if isinstance(value, list):
for v in value:
req += header + ': ' + str(v) + crlf
req += f'{header}: {v}{crlf}'
else:
req += header + ': ' + str(value) + crlf
req += f'{header}: {value}{crlf}'
req = (req + crlf).encode() + body
@@ -151,12 +151,12 @@ class TestHTTP:
len_log = len(log)
if len_log > limit:
log = log[:limit]
appendix = '(...logged %s of %s bytes)' % (limit, len_log)
appendix = f'(...logged {limit} of {len_log} bytes)'
if isinstance(log, bytes):
appendix = appendix.encode()
log = log + appendix
log = f'{log}{appendix}'
return log
@@ -188,7 +188,7 @@ class TestHTTP:
# For all current cases if the "read_timeout" was changed
# than test do not expect to get a response from server.
if timeout == timeout_default:
pytest.fail('Can\'t read response from server.')
pytest.fail("Can't read response from server.")
break
try:
@@ -263,7 +263,7 @@ class TestHTTP:
size = int(chunks.pop(0), 16)
except ValueError:
pytest.fail('Invalid chunk size %s' % str(size))
pytest.fail(f'Invalid chunk size {size}')
if size == 0:
assert len(chunks) == 1, 'last zero size'
@@ -310,7 +310,7 @@ class TestHTTP:
def form_url_encode(self, fields):
data = "&".join(
"%s=%s" % (name, value) for name, value in fields.items()
f'{name}={value}' for name, value in fields.items()
).encode()
return data, 'application/x-www-form-urlencoded'
@@ -341,21 +341,21 @@ class TestHTTP:
else:
pytest.fail('multipart requires a string or stream data')
body += ("--%s\r\nContent-Disposition: form-data; name=\"%s\"") % (
boundary,
field,
body += (
f'--{boundary}\r\nContent-Disposition: form-data;'
f'name="{field}"'
)
if filename != '':
body += "; filename=\"%s\"" % filename
body += f'; filename="{filename}"'
body += "\r\n"
body += '\r\n'
if datatype != '':
body += "Content-Type: %s\r\n" % datatype
body += f'Content-Type: {datatype}\r\n'
body += "\r\n%s\r\n" % data
body += f'\r\n{data}\r\n'
body += "--%s--\r\n" % boundary
body += f'--{boundary}--\r\n'
return body.encode(), "multipart/form-data; boundary=%s" % boundary
return body.encode(), f'multipart/form-data; boundary={boundary}'

View File

@@ -20,4 +20,4 @@ class Log:
Log.pos[name] = pos
def get_path(name=UNIT_LOG):
return Log.temp_dir + '/' + name
return f'{Log.temp_dir}/{name}'

View File

@@ -69,7 +69,7 @@ def waitforsocket(port):
except KeyboardInterrupt:
raise
pytest.fail('Can\'t connect to the 127.0.0.1:' + str(port))
pytest.fail(f"Can't connect to the 127.0.0.1:{port}")
def check_findmnt():
@@ -125,7 +125,7 @@ def getns(nstype):
# read namespace id from symlink file:
# it points to: '<nstype>:[<ns id>]'
# # eg.: 'pid:[4026531836]'
nspath = '/proc/self/ns/' + nstype
nspath = f'/proc/self/ns/{nstype}'
data = None
if os.path.exists(nspath):