Last active
July 31, 2019 22:49
-
-
Save athornton/daa7ba4d1a740a5d0800e8567caa6e53 to your computer and use it in GitHub Desktop.
Test lock types on various filesystems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, fcntl | |
print("Lock test beginning.") | |
sep="\n---------\n" | |
print(sep) | |
home=os.getenv("HOME") | |
testfiles = [ | |
{"filepath": home + "/lockable.txt", | |
"description": "writeable NFS via PV and local_lock=all"}, | |
{"filepath": "/tmp/lockable.txt", | |
"description": "writeable local disk"}, | |
{"filepath": "/n" + home + "/lockable.txt", | |
"description": "writeable NFS via direct pod specification with default NFS options"}, | |
] | |
lock_calls = {"fcntl.flock": fcntl.flock, | |
"fcntl.lockf": fcntl.lockf} | |
lock_sequence = [ fcntl.LOCK_SH, fcntl.LOCK_UN, fcntl.LOCK_EX, fcntl.LOCK_UN] | |
lock_desc = { fcntl.LOCK_SH: "shared lock", | |
fcntl.LOCK_UN: "unlock", | |
fcntl.LOCK_EX: "exclusive lock"} | |
modes = [ "w", "a", "w+", "a+", "r" ] | |
successes = [] | |
failures = [] | |
for entry in testfiles: | |
filename = entry["filepath"] | |
print("Testing file '{}' ({})".format(filename,entry["description"])) | |
for lock in lock_calls: | |
for mode in modes: | |
for cmd in lock_sequence: | |
cn=lock_desc[cmd] | |
desc = "{} '{}' ('{}') via {}()".format(cn,filename, mode, lock) | |
print(" About to request {}".format(desc),end="") | |
try: | |
fd = open(filename,mode) | |
desc = "{} '{}' ('{}') via {}()".format(cn,filename, mode, lock) | |
lock_calls[lock](fd,cmd) | |
print("success.") | |
successes.append(desc) | |
except Exception as exc: | |
print("failure: {}".format(exc)) | |
failures.append("{}: {}".format(desc,exc)) | |
print(sep) | |
print("Successes:\n\n{}".format("\n".join(successes))) | |
print(sep) | |
print("Failures:\n\n{}".format("\n".join(failures))) | |
print(sep) | |
print("Lock test complete.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment