summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/util/spack_lock_wrapper.py
blob: 9838324a03ef193e64a51516c0cfbe2f22831dde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

"""Tests for Spack's wrapper module around llnl.util.lock."""
import os

import pytest

from llnl.util.filesystem import getuid, group_ids

import spack.config
import spack.util.lock as lk


def test_disable_locking(tmpdir):
    """Ensure that locks do no real locking when disabled."""
    lock_path = str(tmpdir.join("lockfile"))
    lock = lk.Lock(lock_path, enable=False)

    lock.acquire_read()
    assert not os.path.exists(lock_path)

    lock.acquire_write()
    assert not os.path.exists(lock_path)

    lock.release_write()
    assert not os.path.exists(lock_path)

    lock.release_read()
    assert not os.path.exists(lock_path)


# "Disable" mock_stage fixture to avoid subdir permissions issues on cleanup.
@pytest.mark.nomockstage
def test_lock_checks_user(tmpdir):
    """Ensure lock checks work with a self-owned, self-group repo."""
    uid = getuid()
    if uid not in group_ids():
        pytest.skip("user has no group with gid == uid")

    # self-owned, own group
    tmpdir.chown(uid, uid)

    # safe
    path = str(tmpdir)
    tmpdir.chmod(0o744)
    lk.check_lock_safety(path)

    # safe
    tmpdir.chmod(0o774)
    lk.check_lock_safety(path)

    # unsafe
    tmpdir.chmod(0o777)
    with pytest.raises(spack.error.SpackError):
        lk.check_lock_safety(path)

    # safe
    tmpdir.chmod(0o474)
    lk.check_lock_safety(path)

    # safe
    tmpdir.chmod(0o477)
    lk.check_lock_safety(path)


# "Disable" mock_stage fixture to avoid subdir permissions issues on cleanup.
@pytest.mark.nomockstage
def test_lock_checks_group(tmpdir):
    """Ensure lock checks work with a self-owned, non-self-group repo."""
    uid = getuid()
    gid = next((g for g in group_ids() if g != uid), None)
    if not gid:
        pytest.skip("user has no group with gid != uid")

    # self-owned, another group
    tmpdir.chown(uid, gid)

    # safe
    path = str(tmpdir)
    tmpdir.chmod(0o744)
    lk.check_lock_safety(path)

    # unsafe
    tmpdir.chmod(0o774)
    with pytest.raises(spack.error.SpackError):
        lk.check_lock_safety(path)

    # unsafe
    tmpdir.chmod(0o777)
    with pytest.raises(spack.error.SpackError):
        lk.check_lock_safety(path)

    # safe
    tmpdir.chmod(0o474)
    lk.check_lock_safety(path)

    # safe
    tmpdir.chmod(0o477)
    lk.check_lock_safety(path)