xref: /unit/test/unit/utils.py (revision 1912:942e252e3f4c)
1*1912So.canty@f5.comimport glob
21735Szelenkov@nginx.comimport os
31735Szelenkov@nginx.comimport socket
41772Szelenkov@nginx.comimport subprocess
51735Szelenkov@nginx.comimport time
61735Szelenkov@nginx.com
71735Szelenkov@nginx.comimport pytest
81735Szelenkov@nginx.com
91735Szelenkov@nginx.com
101735Szelenkov@nginx.comdef public_dir(path):
111735Szelenkov@nginx.com    os.chmod(path, 0o777)
121735Szelenkov@nginx.com
131735Szelenkov@nginx.com    for root, dirs, files in os.walk(path):
141735Szelenkov@nginx.com        for d in dirs:
151735Szelenkov@nginx.com            os.chmod(os.path.join(root, d), 0o777)
161735Szelenkov@nginx.com        for f in files:
171735Szelenkov@nginx.com            os.chmod(os.path.join(root, f), 0o777)
181735Szelenkov@nginx.com
191735Szelenkov@nginx.com
20*1912So.canty@f5.comdef waitforfiles(*files, timeout=50):
21*1912So.canty@f5.com    for i in range(timeout):
221735Szelenkov@nginx.com        wait = False
231735Szelenkov@nginx.com
241735Szelenkov@nginx.com        for f in files:
251735Szelenkov@nginx.com            if not os.path.exists(f):
261735Szelenkov@nginx.com                wait = True
271735Szelenkov@nginx.com                break
281735Szelenkov@nginx.com
291735Szelenkov@nginx.com        if not wait:
301735Szelenkov@nginx.com            return True
311735Szelenkov@nginx.com
321735Szelenkov@nginx.com        time.sleep(0.1)
331735Szelenkov@nginx.com
341735Szelenkov@nginx.com    return False
351735Szelenkov@nginx.com
361735Szelenkov@nginx.com
37*1912So.canty@f5.comdef waitforglob(pattern, count=1, timeout=50):
38*1912So.canty@f5.com    for i in range(timeout):
39*1912So.canty@f5.com        n = 0
40*1912So.canty@f5.com
41*1912So.canty@f5.com        for f in glob.glob(pattern):
42*1912So.canty@f5.com            n += 1
43*1912So.canty@f5.com
44*1912So.canty@f5.com        if n == count:
45*1912So.canty@f5.com            return True
46*1912So.canty@f5.com
47*1912So.canty@f5.com        time.sleep(0.1)
48*1912So.canty@f5.com
49*1912So.canty@f5.com    return False
50*1912So.canty@f5.com
51*1912So.canty@f5.com
521735Szelenkov@nginx.comdef waitforsocket(port):
531735Szelenkov@nginx.com    for i in range(50):
541735Szelenkov@nginx.com        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
551735Szelenkov@nginx.com            try:
561735Szelenkov@nginx.com                sock.settimeout(5)
571735Szelenkov@nginx.com                sock.connect(('127.0.0.1', port))
581735Szelenkov@nginx.com                return
591735Szelenkov@nginx.com
601735Szelenkov@nginx.com            except ConnectionRefusedError:
611735Szelenkov@nginx.com                time.sleep(0.1)
621735Szelenkov@nginx.com
631735Szelenkov@nginx.com            except KeyboardInterrupt:
641735Szelenkov@nginx.com                raise
651735Szelenkov@nginx.com
661803Szelenkov@nginx.com    pytest.fail('Can\'t connect to the 127.0.0.1:' + str(port))
671735Szelenkov@nginx.com
681740Szelenkov@nginx.com
691772Szelenkov@nginx.comdef findmnt():
701772Szelenkov@nginx.com    try:
711772Szelenkov@nginx.com        out = subprocess.check_output(
721772Szelenkov@nginx.com            ['findmnt', '--raw'], stderr=subprocess.STDOUT
731772Szelenkov@nginx.com        ).decode()
741772Szelenkov@nginx.com    except FileNotFoundError:
751772Szelenkov@nginx.com        pytest.skip('requires findmnt')
761772Szelenkov@nginx.com
771772Szelenkov@nginx.com    return out
781772Szelenkov@nginx.com
791772Szelenkov@nginx.com
801877Szelenkov@nginx.comdef sysctl():
811877Szelenkov@nginx.com    try:
821877Szelenkov@nginx.com        out = subprocess.check_output(
831877Szelenkov@nginx.com            ['sysctl', '-a'], stderr=subprocess.STDOUT
841877Szelenkov@nginx.com        ).decode()
851877Szelenkov@nginx.com    except FileNotFoundError:
861877Szelenkov@nginx.com        pytest.skip('requires sysctl')
871877Szelenkov@nginx.com
881877Szelenkov@nginx.com    return out
891877Szelenkov@nginx.com
901877Szelenkov@nginx.com
91*1912So.canty@f5.comdef waitformount(template, timeout=50):
92*1912So.canty@f5.com    for i in range(timeout):
931772Szelenkov@nginx.com        if findmnt().find(template) != -1:
941772Szelenkov@nginx.com            return True
951772Szelenkov@nginx.com
961772Szelenkov@nginx.com        time.sleep(0.1)
971772Szelenkov@nginx.com
981772Szelenkov@nginx.com    return False
991772Szelenkov@nginx.com
1001772Szelenkov@nginx.com
101*1912So.canty@f5.comdef waitforunmount(template, timeout=50):
102*1912So.canty@f5.com    for i in range(timeout):
1031772Szelenkov@nginx.com        if findmnt().find(template) == -1:
1041772Szelenkov@nginx.com            return True
1051772Szelenkov@nginx.com
1061772Szelenkov@nginx.com        time.sleep(0.1)
1071772Szelenkov@nginx.com
1081772Szelenkov@nginx.com    return False
1091772Szelenkov@nginx.com
1101772Szelenkov@nginx.com
1111740Szelenkov@nginx.comdef getns(nstype):
1121740Szelenkov@nginx.com    # read namespace id from symlink file:
1131740Szelenkov@nginx.com    # it points to: '<nstype>:[<ns id>]'
1141740Szelenkov@nginx.com    # # eg.: 'pid:[4026531836]'
1151740Szelenkov@nginx.com    nspath = '/proc/self/ns/' + nstype
1161740Szelenkov@nginx.com    data = None
1171740Szelenkov@nginx.com
1181740Szelenkov@nginx.com    if os.path.exists(nspath):
1191740Szelenkov@nginx.com        data = int(os.readlink(nspath)[len(nstype) + 2 : -1])
1201740Szelenkov@nginx.com
1211740Szelenkov@nginx.com    return data
122