Skip to content

Instantly share code, notes, and snippets.

@athornton
Last active July 31, 2019 22:49
Show Gist options
  • Save athornton/daa7ba4d1a740a5d0800e8567caa6e53 to your computer and use it in GitHub Desktop.
Save athornton/daa7ba4d1a740a5d0800e8567caa6e53 to your computer and use it in GitHub Desktop.
Test lock types on various filesystems
Display the source blob
Display the rendered blob
Raw
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