Completed
Pull Request — master (#100)
by Marc-Alexandre
57s
created

smartdispatch.tests._test_open_with_lock()   B

Complexity

Conditions 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 33
rs 8.8571
1
import os
2
import time
3
import tempfile
4
import shutil
5
6
from subprocess import Popen, PIPE
7
from nose.tools import assert_equal, assert_true
8
9
from smartdispatch.filelock import open_with_lock, open_with_dirlock, open_with_flock
10
from smartdispatch.filelock import find_mount_point, get_fs
11
12
13
def _test_open_with_lock(lock_func):
14
    """ Test a particular file lock.
15
16
    Notes
17
    -----
18
    This test only checks if the locking mechanism works on a single
19
    computer/compute node. There is *no* check for multi-node lock.
20
    """
21
    temp_dir = tempfile.mkdtemp()
22
    filename = os.path.join(temp_dir, "testing.lck")
23
24
    python_script = os.path.join(temp_dir, "test_lock.py")
25
26
    script = ["import logging",
27
              "from smartdispatch.filelock import {}".format(lock_func.__name__),
28
              "logging.root.setLevel(logging.INFO)",
29
              "with {}('{}', 'r+'): pass".format(lock_func.__name__, filename)]
30
31
    open(os.path.join(temp_dir, "test_lock.py"), 'w').write("\n".join(script))
32
33
    command = "python " + python_script
34
35
    # Lock the commands file before running python command
36
    with lock_func(filename, 'w'):
37
        process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
38
        time.sleep(1)
39
40
    stdout, stderr = process.communicate()
41
    assert_equal(stdout, "")
42
    assert_true("Traceback" not in stderr, msg="Unexpected error: '{}'".format(stderr))
43
    assert_true("write-lock" in stderr, msg="Forcing a race condition, try increasing sleeping time above.")
44
45
    shutil.rmtree(temp_dir)  # Cleaning up.
46
47
48
def test_open_with_default_lock():
49
    _test_open_with_lock(open_with_lock)
50
51
52
def test_open_with_dirlock():
53
    _test_open_with_lock(open_with_dirlock)
54
55
56
def test_open_with_flock():
57
    _test_open_with_lock(open_with_flock)
58
59
60
def test_find_mount_point():
61
    assert_equal(find_mount_point('/'), '/')
62
63
    for d in os.listdir('/mnt'):
64
        path = os.path.join('/mnt', d)
65
        if os.path.ismount(path):
66
            assert_equal(find_mount_point(path), path)
67
        else:
68
            assert_equal(find_mount_point(path), '/')
69
70
71
def test_get_fs():
72
    fs = get_fs('/')
73
    assert_true(fs is not None)
74