Tests: utils module introduced.
This commit is contained in:
@@ -18,6 +18,8 @@ from unit.check.go import check_go
|
|||||||
from unit.check.node import check_node
|
from unit.check.node import check_node
|
||||||
from unit.check.tls import check_openssl
|
from unit.check.tls import check_openssl
|
||||||
from unit.option import option
|
from unit.option import option
|
||||||
|
from unit.utils import public_dir
|
||||||
|
from unit.utils import waitforfiles
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
@@ -298,34 +300,6 @@ def unit_stop():
|
|||||||
p.kill()
|
p.kill()
|
||||||
return 'Could not terminate unit'
|
return 'Could not terminate unit'
|
||||||
|
|
||||||
def public_dir(path):
|
|
||||||
os.chmod(path, 0o777)
|
|
||||||
|
|
||||||
for root, dirs, files in os.walk(path):
|
|
||||||
for d in dirs:
|
|
||||||
os.chmod(os.path.join(root, d), 0o777)
|
|
||||||
for f in files:
|
|
||||||
os.chmod(os.path.join(root, f), 0o777)
|
|
||||||
|
|
||||||
def waitforfiles(*files):
|
|
||||||
for i in range(50):
|
|
||||||
wait = False
|
|
||||||
ret = False
|
|
||||||
|
|
||||||
for f in files:
|
|
||||||
if not os.path.exists(f):
|
|
||||||
wait = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if wait:
|
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
else:
|
|
||||||
ret = True
|
|
||||||
break
|
|
||||||
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _check_alerts(path=None):
|
def _check_alerts(path=None):
|
||||||
@@ -404,27 +378,6 @@ def stop_processes():
|
|||||||
return 'Fail to stop process(es)'
|
return 'Fail to stop process(es)'
|
||||||
|
|
||||||
|
|
||||||
def waitforsocket(port):
|
|
||||||
ret = False
|
|
||||||
|
|
||||||
for i in range(50):
|
|
||||||
try:
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
sock.connect(('127.0.0.1', port))
|
|
||||||
ret = True
|
|
||||||
break
|
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
raise
|
|
||||||
|
|
||||||
except:
|
|
||||||
sock.close()
|
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
sock.close()
|
|
||||||
|
|
||||||
assert ret, 'socket connected'
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def temp_dir(request):
|
def temp_dir(request):
|
||||||
return unit_instance['temp_dir']
|
return unit_instance['temp_dir']
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ from distutils.version import LooseVersion
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from conftest import public_dir
|
|
||||||
from conftest import unit_stop
|
from conftest import unit_stop
|
||||||
from unit.applications.lang.python import TestApplicationPython
|
from unit.applications.lang.python import TestApplicationPython
|
||||||
from unit.option import option
|
from unit.option import option
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ import os
|
|||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from conftest import public_dir
|
|
||||||
from conftest import skip_alert
|
from conftest import skip_alert
|
||||||
from unit.applications.lang.java import TestApplicationJava
|
from unit.applications.lang.java import TestApplicationJava
|
||||||
from unit.option import option
|
from unit.option import option
|
||||||
|
from unit.utils import public_dir
|
||||||
|
|
||||||
class TestJavaApplication(TestApplicationJava):
|
class TestJavaApplication(TestApplicationJava):
|
||||||
prerequisites = {'modules': {'java': 'all'}}
|
prerequisites = {'modules': {'java': 'all'}}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ import re
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from conftest import waitforfiles
|
|
||||||
from unit.applications.lang.node import TestApplicationNode
|
from unit.applications.lang.node import TestApplicationNode
|
||||||
|
from unit.utils import waitforfiles
|
||||||
|
|
||||||
|
|
||||||
class TestNodeApplication(TestApplicationNode):
|
class TestNodeApplication(TestApplicationNode):
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import pytest
|
|||||||
|
|
||||||
from conftest import run_process
|
from conftest import run_process
|
||||||
from conftest import skip_alert
|
from conftest import skip_alert
|
||||||
from conftest import waitforsocket
|
|
||||||
from unit.applications.lang.python import TestApplicationPython
|
from unit.applications.lang.python import TestApplicationPython
|
||||||
from unit.option import option
|
from unit.option import option
|
||||||
|
from unit.utils import waitforsocket
|
||||||
|
|
||||||
|
|
||||||
class TestProxy(TestApplicationPython):
|
class TestProxy(TestApplicationPython):
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import socket
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from conftest import run_process
|
from conftest import run_process
|
||||||
from conftest import waitforsocket
|
|
||||||
from unit.applications.lang.python import TestApplicationPython
|
from unit.applications.lang.python import TestApplicationPython
|
||||||
from unit.option import option
|
from unit.option import option
|
||||||
|
from unit.utils import waitforsocket
|
||||||
|
|
||||||
|
|
||||||
class TestProxyChunked(TestApplicationPython):
|
class TestProxyChunked(TestApplicationPython):
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ import socket
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from conftest import waitforfiles
|
|
||||||
from unit.applications.proto import TestApplicationProto
|
from unit.applications.proto import TestApplicationProto
|
||||||
from unit.option import option
|
from unit.option import option
|
||||||
|
from unit.utils import waitforfiles
|
||||||
|
|
||||||
|
|
||||||
class TestStatic(TestApplicationProto):
|
class TestStatic(TestApplicationProto):
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ import os
|
|||||||
from subprocess import call
|
from subprocess import call
|
||||||
|
|
||||||
from conftest import unit_stop
|
from conftest import unit_stop
|
||||||
from conftest import waitforfiles
|
|
||||||
from unit.applications.lang.python import TestApplicationPython
|
from unit.applications.lang.python import TestApplicationPython
|
||||||
|
from unit.utils import waitforfiles
|
||||||
|
|
||||||
|
|
||||||
class TestUSR1(TestApplicationPython):
|
class TestUSR1(TestApplicationPython):
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import shutil
|
import shutil
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
from conftest import public_dir
|
|
||||||
from unit.applications.proto import TestApplicationProto
|
from unit.applications.proto import TestApplicationProto
|
||||||
from unit.option import option
|
from unit.option import option
|
||||||
|
from unit.utils import public_dir
|
||||||
|
|
||||||
|
|
||||||
class TestApplicationNode(TestApplicationProto):
|
class TestApplicationNode(TestApplicationProto):
|
||||||
|
|||||||
50
test/unit/utils.py
Normal file
50
test/unit/utils.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import time
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def public_dir(path):
|
||||||
|
os.chmod(path, 0o777)
|
||||||
|
|
||||||
|
for root, dirs, files in os.walk(path):
|
||||||
|
for d in dirs:
|
||||||
|
os.chmod(os.path.join(root, d), 0o777)
|
||||||
|
for f in files:
|
||||||
|
os.chmod(os.path.join(root, f), 0o777)
|
||||||
|
|
||||||
|
|
||||||
|
def waitforfiles(*files):
|
||||||
|
for i in range(50):
|
||||||
|
wait = False
|
||||||
|
|
||||||
|
for f in files:
|
||||||
|
if not os.path.exists(f):
|
||||||
|
wait = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not wait:
|
||||||
|
return True
|
||||||
|
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def waitforsocket(port):
|
||||||
|
for i in range(50):
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||||
|
try:
|
||||||
|
sock.settimeout(5)
|
||||||
|
sock.connect(('127.0.0.1', port))
|
||||||
|
return
|
||||||
|
|
||||||
|
except ConnectionRefusedError:
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
raise
|
||||||
|
|
||||||
|
pytest.fail('Can\'t connect to the 127.0.0.1:' + port)
|
||||||
|
|
||||||
Reference in New Issue
Block a user