Tests: renamed share to static.

Also minor style changes.
This commit is contained in:
Andrei Zeliankou
2021-06-28 22:05:40 +01:00
parent 72420358be
commit b86891c4ef
8 changed files with 57 additions and 62 deletions

View File

@@ -22,8 +22,8 @@ from unit.check.node import check_node
from unit.check.regex import check_regex from unit.check.regex import check_regex
from unit.check.tls import check_openssl from unit.check.tls import check_openssl
from unit.http import TestHTTP from unit.http import TestHTTP
from unit.option import option
from unit.log import Log from unit.log import Log
from unit.option import option
from unit.utils import public_dir from unit.utils import public_dir
from unit.utils import waitforfiles from unit.utils import waitforfiles

View File

@@ -1,6 +1,7 @@
import socket
import pytest import pytest
import socket
from unit.control import TestControl from unit.control import TestControl

View File

@@ -1,6 +1,7 @@
from distutils.version import LooseVersion
import pytest import pytest
from distutils.version import LooseVersion
from unit.applications.lang.node import TestApplicationNode from unit.applications.lang.node import TestApplicationNode
from unit.applications.websockets import TestApplicationWebsocket from unit.applications.websockets import TestApplicationWebsocket

View File

@@ -6,17 +6,14 @@ import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
class TestShareChroot(TestApplicationProto): class TestStaticChroot(TestApplicationProto):
prerequisites = {'features': ['chroot']} prerequisites = {'features': ['chroot']}
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def setup_method_fixture(self, temp_dir): def setup_method_fixture(self, temp_dir):
os.makedirs(temp_dir + '/assets/dir') os.makedirs(temp_dir + '/assets/dir')
with open(temp_dir + '/assets/index.html', 'w') as index, open( Path(temp_dir + '/assets/index.html').write_text('0123456789')
temp_dir + '/assets/dir/file', 'w' Path(temp_dir + '/assets/dir/file').write_text('blah')
) as file:
index.write('0123456789')
file.write('blah')
test = Path(__file__) test = Path(__file__)
self.test_path = '/' + test.parent.name + '/' + test.name self.test_path = '/' + test.parent.name + '/' + test.name
@@ -28,7 +25,7 @@ class TestShareChroot(TestApplicationProto):
} }
) )
def test_share_chroot(self, temp_dir): def test_static_chroot(self, temp_dir):
assert self.get(url='/dir/file')['status'] == 200, 'default chroot' assert self.get(url='/dir/file')['status'] == 200, 'default chroot'
assert self.get(url='/index.html')['status'] == 200, 'default chroot 2' assert self.get(url='/index.html')['status'] == 200, 'default chroot 2'
@@ -44,7 +41,7 @@ class TestShareChroot(TestApplicationProto):
assert self.get(url='/index.html')['status'] == 403, 'chroot 403 2' assert self.get(url='/index.html')['status'] == 403, 'chroot 403 2'
assert self.get(url='/file')['status'] == 403, 'chroot 403' assert self.get(url='/file')['status'] == 403, 'chroot 403'
def test_share_chroot_permission(self, is_su, temp_dir): def test_static_chroot_permission(self, is_su, temp_dir):
if is_su: if is_su:
pytest.skip('does\'t work under root') pytest.skip('does\'t work under root')
@@ -60,7 +57,7 @@ class TestShareChroot(TestApplicationProto):
assert self.get(url='/dir/file')['status'] == 200, 'chroot' assert self.get(url='/dir/file')['status'] == 200, 'chroot'
def test_share_chroot_empty(self, temp_dir): def test_static_chroot_empty(self, temp_dir):
assert 'success' in self.conf( assert 'success' in self.conf(
{"share": temp_dir + "/assets", "chroot": ""}, 'routes/0/action', {"share": temp_dir + "/assets", "chroot": ""}, 'routes/0/action',
), 'configure chroot empty absolute' ), 'configure chroot empty absolute'
@@ -77,7 +74,7 @@ class TestShareChroot(TestApplicationProto):
self.get(url=self.test_path)['status'] == 200 self.get(url=self.test_path)['status'] == 200
), 'chroot empty relative' ), 'chroot empty relative'
def test_share_chroot_relative(self, is_su, temp_dir): def test_static_chroot_relative(self, is_su, temp_dir):
if is_su: if is_su:
pytest.skip('does\'t work under root') pytest.skip('does\'t work under root')
@@ -99,7 +96,7 @@ class TestShareChroot(TestApplicationProto):
assert self.get(url=self.test_path)['status'] == 200, 'relative' assert self.get(url=self.test_path)['status'] == 200, 'relative'
def test_share_chroot_invalid(self, temp_dir): def test_static_chroot_invalid(self, temp_dir):
assert 'error' in self.conf( assert 'error' in self.conf(
{"share": temp_dir, "chroot": True}, 'routes/0/action', {"share": temp_dir, "chroot": True}, 'routes/0/action',
), 'configure chroot error' ), 'configure chroot error'

View File

@@ -1,21 +1,21 @@
import os import os
from pathlib import Path
import pytest import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
from unit.option import option
class TestStatic(TestApplicationProto): class TestStaticFallback(TestApplicationProto):
prerequisites = {} prerequisites = {}
def setup_method(self): @pytest.fixture(autouse=True)
os.makedirs(option.temp_dir + '/assets/dir') def setup_method_fixture(self, temp_dir):
with open(option.temp_dir + '/assets/index.html', 'w') as index: os.makedirs(temp_dir + '/assets/dir')
index.write('0123456789') Path(temp_dir + '/assets/index.html').write_text('0123456789')
os.makedirs(option.temp_dir + '/assets/403') os.makedirs(temp_dir + '/assets/403')
os.chmod(option.temp_dir + '/assets/403', 0o000) os.chmod(temp_dir + '/assets/403', 0o000)
self._load_conf( self._load_conf(
{ {
@@ -23,21 +23,22 @@ class TestStatic(TestApplicationProto):
"*:7080": {"pass": "routes"}, "*:7080": {"pass": "routes"},
"*:7081": {"pass": "routes"}, "*:7081": {"pass": "routes"},
}, },
"routes": [{"action": {"share": option.temp_dir + "/assets"}}], "routes": [{"action": {"share": temp_dir + "/assets"}}],
"applications": {}, "applications": {},
} }
) )
def teardown_method(self): yield
try: try:
os.chmod(option.temp_dir + '/assets/403', 0o777) os.chmod(temp_dir + '/assets/403', 0o777)
except FileNotFoundError: except FileNotFoundError:
pass pass
def action_update(self, conf): def action_update(self, conf):
assert 'success' in self.conf(conf, 'routes/0/action') assert 'success' in self.conf(conf, 'routes/0/action')
def test_fallback(self): def test_static_fallback(self):
self.action_update({"share": "/blah"}) self.action_update({"share": "/blah"})
assert self.get()['status'] == 404, 'bad path no fallback' assert self.get()['status'] == 404, 'bad path no fallback'
@@ -47,7 +48,7 @@ class TestStatic(TestApplicationProto):
assert resp['status'] == 200, 'bad path fallback status' assert resp['status'] == 200, 'bad path fallback status'
assert resp['body'] == '', 'bad path fallback' assert resp['body'] == '', 'bad path fallback'
def test_fallback_valid_path(self, temp_dir): def test_static_fallback_valid_path(self, temp_dir):
self.action_update( self.action_update(
{"share": temp_dir + "/assets", "fallback": {"return": 200}} {"share": temp_dir + "/assets", "fallback": {"return": 200}}
) )
@@ -65,7 +66,7 @@ class TestStatic(TestApplicationProto):
assert self.get(url='/dir')['status'] == 301, 'fallback status 301' assert self.get(url='/dir')['status'] == 301, 'fallback status 301'
def test_fallback_nested(self): def test_static_fallback_nested(self):
self.action_update( self.action_update(
{ {
"share": "/blah", "share": "/blah",
@@ -80,7 +81,7 @@ class TestStatic(TestApplicationProto):
assert resp['status'] == 200, 'fallback nested status' assert resp['status'] == 200, 'fallback nested status'
assert resp['body'] == '', 'fallback nested' assert resp['body'] == '', 'fallback nested'
def test_fallback_share(self, temp_dir): def test_static_fallback_share(self, temp_dir):
self.action_update( self.action_update(
{"share": "/blah", "fallback": {"share": temp_dir + "/assets"},} {"share": "/blah", "fallback": {"share": temp_dir + "/assets"},}
) )
@@ -97,7 +98,7 @@ class TestStatic(TestApplicationProto):
self.get(url='/dir')['status'] == 301 self.get(url='/dir')['status'] == 301
), 'fallback share status 301' ), 'fallback share status 301'
def test_fallback_proxy(self): def test_static_fallback_proxy(self):
assert 'success' in self.conf( assert 'success' in self.conf(
[ [
{ {
@@ -119,7 +120,7 @@ class TestStatic(TestApplicationProto):
assert resp['body'] == '', 'fallback proxy' assert resp['body'] == '', 'fallback proxy'
@pytest.mark.skip('not yet') @pytest.mark.skip('not yet')
def test_fallback_proxy_loop(self, skip_alert): def test_static_fallback_proxy_loop(self, skip_alert):
skip_alert( skip_alert(
r'open.*/blah/index.html.*failed', r'open.*/blah/index.html.*failed',
r'accept.*failed', r'accept.*failed',
@@ -135,7 +136,7 @@ class TestStatic(TestApplicationProto):
assert 'success' in self.conf_delete('listeners/*:7081') assert 'success' in self.conf_delete('listeners/*:7081')
self.get(read_timeout=1) self.get(read_timeout=1)
def test_fallback_invalid(self): def test_static_fallback_invalid(self):
def check_error(conf): def check_error(conf):
assert 'error' in self.conf(conf, 'routes/0/action') assert 'error' in self.conf(conf, 'routes/0/action')

View File

@@ -1,12 +1,13 @@
import os import os
import subprocess import subprocess
from pathlib import Path
import pytest import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
class TestShareMount(TestApplicationProto): class TestStaticMount(TestApplicationProto):
prerequisites = {'features': ['chroot']} prerequisites = {'features': ['chroot']}
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@@ -17,12 +18,9 @@ class TestShareMount(TestApplicationProto):
os.makedirs(temp_dir + '/assets/dir/mount') os.makedirs(temp_dir + '/assets/dir/mount')
os.makedirs(temp_dir + '/assets/dir/dir') os.makedirs(temp_dir + '/assets/dir/dir')
os.makedirs(temp_dir + '/assets/mount') os.makedirs(temp_dir + '/assets/mount')
with open(temp_dir + '/assets/index.html', 'w') as index, open( Path(temp_dir + '/assets/index.html').write_text('index')
temp_dir + '/assets/dir/dir/file', 'w' Path(temp_dir + '/assets/dir/dir/file').write_text('file')
) as file, open(temp_dir + '/assets/mount/index.html', 'w') as mount: Path(temp_dir + '/assets/mount/index.html').write_text('mount')
index.write('index')
file.write('file')
mount.write('mount')
try: try:
process = subprocess.Popen( process = subprocess.Popen(
@@ -66,7 +64,7 @@ class TestShareMount(TestApplicationProto):
except: except:
pytest.fail('Can\'t run umount process.') pytest.fail('Can\'t run umount process.')
def test_share_mount(self, temp_dir, skip_alert): def test_static_mount(self, temp_dir, skip_alert):
skip_alert(r'opening.*failed') skip_alert(r'opening.*failed')
resp = self.get(url='/mount/') resp = self.get(url='/mount/')
@@ -89,7 +87,7 @@ class TestShareMount(TestApplicationProto):
assert resp['status'] == 200 assert resp['status'] == 200
assert resp['body'] == 'mount' assert resp['body'] == 'mount'
def test_share_mount_two_blocks(self, temp_dir, skip_alert): def test_static_mount_two_blocks(self, temp_dir, skip_alert):
skip_alert(r'opening.*failed') skip_alert(r'opening.*failed')
os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link') os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link')
@@ -117,7 +115,7 @@ class TestShareMount(TestApplicationProto):
assert self.get(url='/mount/')['status'] == 200, 'block enabled' assert self.get(url='/mount/')['status'] == 200, 'block enabled'
assert self.head(url='/mount/')['status'] == 403, 'block disabled' assert self.head(url='/mount/')['status'] == 403, 'block disabled'
def test_share_mount_chroot(self, temp_dir, skip_alert): def test_static_mount_chroot(self, temp_dir, skip_alert):
skip_alert(r'opening.*failed') skip_alert(r'opening.*failed')
assert 'success' in self.conf( assert 'success' in self.conf(

View File

@@ -1,21 +1,19 @@
import os import os
from pathlib import Path
import pytest import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
class TestShareSymlink(TestApplicationProto): class TestStaticSymlink(TestApplicationProto):
prerequisites = {'features': ['chroot']} prerequisites = {'features': ['chroot']}
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def setup_method_fixture(self, temp_dir): def setup_method_fixture(self, temp_dir):
os.makedirs(temp_dir + '/assets/dir/dir') os.makedirs(temp_dir + '/assets/dir/dir')
with open(temp_dir + '/assets/index.html', 'w') as index, open( Path(temp_dir + '/assets/index.html').write_text('0123456789')
temp_dir + '/assets/dir/file', 'w' Path(temp_dir + '/assets/dir/file').write_text('blah')
) as file:
index.write('0123456789')
file.write('blah')
self._load_conf( self._load_conf(
{ {
@@ -24,7 +22,7 @@ class TestShareSymlink(TestApplicationProto):
} }
) )
def test_share_symlink(self, temp_dir, skip_alert): def test_static_symlink(self, temp_dir, skip_alert):
skip_alert(r'opening.*failed') skip_alert(r'opening.*failed')
os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link') os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link')
@@ -48,7 +46,7 @@ class TestShareSymlink(TestApplicationProto):
assert self.get(url='/link/file')['status'] == 200, 'symlink enabled' assert self.get(url='/link/file')['status'] == 200, 'symlink enabled'
def test_share_symlink_two_blocks(self, temp_dir, skip_alert): def test_static_symlink_two_blocks(self, temp_dir, skip_alert):
skip_alert(r'opening.*failed') skip_alert(r'opening.*failed')
os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link') os.symlink(temp_dir + '/assets/dir', temp_dir + '/assets/link')
@@ -76,7 +74,7 @@ class TestShareSymlink(TestApplicationProto):
assert self.get(url='/link/file')['status'] == 200, 'block enabled' assert self.get(url='/link/file')['status'] == 200, 'block enabled'
assert self.head(url='/link/file')['status'] == 403, 'block disabled' assert self.head(url='/link/file')['status'] == 403, 'block disabled'
def test_share_symlink_chroot(self, temp_dir, skip_alert): def test_static_symlink_chroot(self, temp_dir, skip_alert):
skip_alert(r'opening.*failed') skip_alert(r'opening.*failed')
os.symlink( os.symlink(

View File

@@ -1,12 +1,11 @@
import os
from pathlib import Path from pathlib import Path
import pytest import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
from unit.option import option
class TestShareTypes(TestApplicationProto): class TestStaticTypes(TestApplicationProto):
prerequisites = {} prerequisites = {}
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@@ -36,7 +35,7 @@ class TestShareTypes(TestApplicationProto):
assert resp['status'] == 200, 'status' assert resp['status'] == 200, 'status'
assert resp['body'] == body, 'body' assert resp['body'] == body, 'body'
def test_share_types_basic(self, temp_dir): def test_static_types_basic(self, temp_dir):
self.action_update({"share": temp_dir + "/assets"}) self.action_update({"share": temp_dir + "/assets"})
self.check_body('/index.html', 'index') self.check_body('/index.html', 'index')
self.check_body('/file.xml', '.xml') self.check_body('/file.xml', '.xml')
@@ -54,7 +53,7 @@ class TestShareTypes(TestApplicationProto):
self.action_update({"share": temp_dir + "/assets", "types": [""]}) self.action_update({"share": temp_dir + "/assets", "types": [""]})
assert self.get(url='/file.xml')['status'] == 403, 'no mtype' assert self.get(url='/file.xml')['status'] == 403, 'no mtype'
def test_share_types_wildcard(self, temp_dir): def test_static_types_wildcard(self, temp_dir):
self.action_update( self.action_update(
{"share": temp_dir + "/assets", "types": ["application/*"]} {"share": temp_dir + "/assets", "types": ["application/*"]}
) )
@@ -67,7 +66,7 @@ class TestShareTypes(TestApplicationProto):
assert self.get(url='/file.xml')['status'] == 403, 'video * mtype xml' assert self.get(url='/file.xml')['status'] == 403, 'video * mtype xml'
self.check_body('/file.mp4', '.mp4') self.check_body('/file.mp4', '.mp4')
def test_share_types_negation(self, temp_dir): def test_static_types_negation(self, temp_dir):
self.action_update( self.action_update(
{"share": temp_dir + "/assets", "types": ["!application/xml"]} {"share": temp_dir + "/assets", "types": ["!application/xml"]}
) )
@@ -85,7 +84,7 @@ class TestShareTypes(TestApplicationProto):
self.check_body('/file.png', '.png') self.check_body('/file.png', '.png')
assert self.get(url='/file.jpg')['status'] == 403, 'negation sort jpg' assert self.get(url='/file.jpg')['status'] == 403, 'negation sort jpg'
def test_share_types_regex(self, temp_dir): def test_static_types_regex(self, temp_dir):
self.action_update( self.action_update(
{"share": temp_dir + "/assets", "types": ["~text/(html|plain)"]} {"share": temp_dir + "/assets", "types": ["~text/(html|plain)"]}
) )
@@ -93,7 +92,7 @@ class TestShareTypes(TestApplicationProto):
self.check_body('/file.html', '.html') self.check_body('/file.html', '.html')
self.check_body('/file.txt', '.txt') self.check_body('/file.txt', '.txt')
def test_share_types_case(self, temp_dir): def test_static_types_case(self, temp_dir):
self.action_update( self.action_update(
{"share": temp_dir + "/assets", "types": ["!APpliCaTiOn/xMl"]} {"share": temp_dir + "/assets", "types": ["!APpliCaTiOn/xMl"]}
) )
@@ -118,7 +117,7 @@ class TestShareTypes(TestApplicationProto):
self.get(url='/file.xml')['status'] == 403 self.get(url='/file.xml')['status'] == 403
), 'mixed case video * negation' ), 'mixed case video * negation'
def test_share_types_fallback(self, temp_dir): def test_static_types_fallback(self, temp_dir):
assert 'success' in self.conf( assert 'success' in self.conf(
[ [
{ {
@@ -139,7 +138,7 @@ class TestShareTypes(TestApplicationProto):
self.check_body('/file.php', '') self.check_body('/file.php', '')
self.check_body('/file.mp4', '.mp4') self.check_body('/file.mp4', '.mp4')
def test_share_types_index(self, temp_dir): def test_static_types_index(self, temp_dir):
self.action_update( self.action_update(
{"share": temp_dir + "/assets", "types": "application/xml"} {"share": temp_dir + "/assets", "types": "application/xml"}
) )
@@ -147,7 +146,7 @@ class TestShareTypes(TestApplicationProto):
self.check_body('/file.xml', '.xml') self.check_body('/file.xml', '.xml')
assert self.get(url='/file.mp4')['status'] == 403, 'forbidden mtype' assert self.get(url='/file.mp4')['status'] == 403, 'forbidden mtype'
def test_share_types_custom_mime(self, temp_dir): def test_static_types_custom_mime(self, temp_dir):
self._load_conf( self._load_conf(
{ {
"listeners": {"*:7080": {"pass": "routes"}}, "listeners": {"*:7080": {"pass": "routes"}},