From 8f916285639d7f9aac9ef03cace5e4dcbcca70cd Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Tue, 8 Dec 2020 14:37:25 +0000 Subject: Tests: utils module introduced. --- test/unit/utils.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/unit/utils.py (limited to 'test/unit/utils.py') diff --git a/test/unit/utils.py b/test/unit/utils.py new file mode 100644 index 00000000..f24e9728 --- /dev/null +++ b/test/unit/utils.py @@ -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) + -- cgit From 4c846ae69308983050a55f6467c2d53e78120e0b Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Wed, 9 Dec 2020 16:15:50 +0000 Subject: Tests: isolation check moved to the pytest_sessionstart(). This change eliminates the need for some classes to run Unit one more time before running tests. --- test/unit/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'test/unit/utils.py') diff --git a/test/unit/utils.py b/test/unit/utils.py index f24e9728..1307a4f6 100644 --- a/test/unit/utils.py +++ b/test/unit/utils.py @@ -48,3 +48,15 @@ def waitforsocket(port): pytest.fail('Can\'t connect to the 127.0.0.1:' + port) + +def getns(nstype): + # read namespace id from symlink file: + # it points to: ':[]' + # # eg.: 'pid:[4026531836]' + nspath = '/proc/self/ns/' + nstype + data = None + + if os.path.exists(nspath): + data = int(os.readlink(nspath)[len(nstype) + 2 : -1]) + + return data -- cgit From db9b70932b2e3e4df8b4bcdb24fedce042da15aa Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Wed, 13 Jan 2021 06:24:26 +0000 Subject: Tests: waitformount() and waitforunmount() introduced. --- test/unit/utils.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'test/unit/utils.py') diff --git a/test/unit/utils.py b/test/unit/utils.py index 1307a4f6..7a0a3fe5 100644 --- a/test/unit/utils.py +++ b/test/unit/utils.py @@ -1,5 +1,6 @@ import os import socket +import subprocess import time import pytest @@ -49,6 +50,37 @@ def waitforsocket(port): pytest.fail('Can\'t connect to the 127.0.0.1:' + port) +def findmnt(): + try: + out = subprocess.check_output( + ['findmnt', '--raw'], stderr=subprocess.STDOUT + ).decode() + except FileNotFoundError: + pytest.skip('requires findmnt') + + return out + + +def waitformount(template, wait=50): + for i in range(wait): + if findmnt().find(template) != -1: + return True + + time.sleep(0.1) + + return False + + +def waitforunmount(template, wait=50): + for i in range(wait): + if findmnt().find(template) == -1: + return True + + time.sleep(0.1) + + return False + + def getns(nstype): # read namespace id from symlink file: # it points to: ':[]' -- cgit