Initial ABSDEV deployment framework development preview
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,281 @@
|
||||
import os
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from core.locking import DeploymentLock, LockError
|
||||
|
||||
|
||||
class LockingTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
|
||||
self.root = Path(self.temp.name)
|
||||
self.path = self.root / ".deployment.lock"
|
||||
|
||||
def test_lock_acquisition(self):
|
||||
|
||||
with DeploymentLock(self.path) as lock:
|
||||
|
||||
self.assertTrue(lock.acquired)
|
||||
|
||||
def test_lock_file_created(self):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
|
||||
self.assertTrue(self.path.is_file())
|
||||
|
||||
def test_lock_file_permissions(self):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
|
||||
self.assertEqual(
|
||||
stat.S_IMODE(self.path.stat().st_mode),
|
||||
0o600,
|
||||
)
|
||||
|
||||
def test_lock_release(self):
|
||||
|
||||
lock = DeploymentLock(self.path)
|
||||
|
||||
with lock:
|
||||
|
||||
self.assertTrue(lock.acquired)
|
||||
|
||||
self.assertFalse(lock.acquired)
|
||||
|
||||
def test_lock_file_preserved(self):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
|
||||
original = self.path.stat().st_ino
|
||||
|
||||
self.assertTrue(self.path.exists())
|
||||
|
||||
self.assertEqual(
|
||||
self.path.stat().st_ino,
|
||||
original,
|
||||
)
|
||||
|
||||
def test_reacquisition(self):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
pass
|
||||
|
||||
with DeploymentLock(self.path) as lock:
|
||||
|
||||
self.assertTrue(lock.acquired)
|
||||
|
||||
def test_concurrent_lock_rejected(self):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
pass
|
||||
|
||||
def test_independent_locks(self):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
|
||||
with DeploymentLock(
|
||||
self.root / ".other.lock"
|
||||
) as lock:
|
||||
|
||||
self.assertTrue(lock.acquired)
|
||||
|
||||
def test_release_after_exception(self):
|
||||
|
||||
with self.assertRaises(RuntimeError):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
|
||||
raise RuntimeError("Simulated failure")
|
||||
|
||||
with DeploymentLock(self.path) as lock:
|
||||
|
||||
self.assertTrue(lock.acquired)
|
||||
|
||||
def test_symlink_rejected(self):
|
||||
|
||||
target = self.root / "target"
|
||||
target.touch()
|
||||
|
||||
self.path.symlink_to(target)
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
pass
|
||||
|
||||
def test_dangling_symlink_rejected(self):
|
||||
|
||||
self.path.symlink_to(
|
||||
self.root / "missing"
|
||||
)
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
pass
|
||||
|
||||
def test_hardlink_rejected(self):
|
||||
|
||||
target = self.root / "target"
|
||||
target.touch(mode=0o600)
|
||||
|
||||
os.link(target, self.path)
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
pass
|
||||
|
||||
def test_directory_rejected(self):
|
||||
|
||||
self.path.mkdir()
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
pass
|
||||
|
||||
def test_fifo_rejected(self):
|
||||
|
||||
os.mkfifo(self.path, 0o600)
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
pass
|
||||
|
||||
def test_insecure_permissions_rejected(self):
|
||||
|
||||
self.path.touch()
|
||||
self.path.chmod(0o666)
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
pass
|
||||
|
||||
def test_missing_parent_rejected(self):
|
||||
|
||||
path = self.root / "missing" / ".lock"
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(path):
|
||||
pass
|
||||
|
||||
def test_symlink_parent_rejected(self):
|
||||
|
||||
actual = self.root / "actual"
|
||||
actual.mkdir()
|
||||
|
||||
link = self.root / "linked"
|
||||
link.symlink_to(actual)
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
with DeploymentLock(link / ".lock"):
|
||||
pass
|
||||
|
||||
def test_relative_path_rejected(self):
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
DeploymentLock("relative.lock")
|
||||
|
||||
def test_duplicate_acquire_rejected(self):
|
||||
|
||||
lock = DeploymentLock(self.path)
|
||||
|
||||
with lock:
|
||||
|
||||
with self.assertRaises(LockError):
|
||||
|
||||
lock.acquire()
|
||||
|
||||
def test_release_is_idempotent(self):
|
||||
|
||||
lock = DeploymentLock(self.path)
|
||||
|
||||
lock.acquire()
|
||||
lock.release()
|
||||
lock.release()
|
||||
|
||||
self.assertFalse(lock.acquired)
|
||||
|
||||
def test_cross_process_contention(self):
|
||||
|
||||
code = """
|
||||
import sys
|
||||
from core.locking import DeploymentLock, LockError
|
||||
|
||||
try:
|
||||
with DeploymentLock(sys.argv[1]):
|
||||
sys.exit(0)
|
||||
except LockError:
|
||||
sys.exit(7)
|
||||
"""
|
||||
|
||||
with DeploymentLock(self.path):
|
||||
|
||||
result = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
"-B",
|
||||
"-c",
|
||||
code,
|
||||
str(self.path),
|
||||
],
|
||||
stdin=subprocess.DEVNULL,
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
self.assertEqual(result.returncode, 7)
|
||||
|
||||
def test_process_exit_releases_lock(self):
|
||||
|
||||
code = """
|
||||
import os
|
||||
import sys
|
||||
from core.locking import DeploymentLock
|
||||
|
||||
lock = DeploymentLock(sys.argv[1])
|
||||
lock.acquire()
|
||||
os._exit(0)
|
||||
"""
|
||||
|
||||
result = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
"-B",
|
||||
"-c",
|
||||
code,
|
||||
str(self.path),
|
||||
],
|
||||
stdin=subprocess.DEVNULL,
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
self.assertEqual(result.returncode, 0)
|
||||
|
||||
with DeploymentLock(self.path) as lock:
|
||||
|
||||
self.assertTrue(lock.acquired)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user