summaryrefslogtreecommitdiffstats
path: root/glustolibs-io/shared_files/scripts/file_lock.py
diff options
context:
space:
mode:
authorPranav <prprakas@redhat.com>2020-11-04 13:40:32 +0530
committerArthy Loganathan <aloganat@redhat.com>2021-01-04 06:15:02 +0000
commit7936283847db39070d760f79f0af1804be7f6744 (patch)
tree80c3c92b9eeaf15a9b9061b492850fcf17b6c50e /glustolibs-io/shared_files/scripts/file_lock.py
parentd38294674c4f2a1ea0b3bc8a65fa717db7364478 (diff)
[Test] Add test to verify lock behaviour from 2 diff clients
Test to verify whether the lock is being granted to two diff clients at the same time. - Take lock from client 1 => Lock is acquired - Try taking lock from client 2 - Release lock from client1 - Take lock from client2 - Again try taking lock from client 1 Also, verifying the behaviour with eagerlock and other eagerlock set of on and off. Change-Id: Ie839f893f7a4f9b2c6fc9375cdf9ee8a27fad13b Signed-off-by: Pranav <prprakas@redhat.com>
Diffstat (limited to 'glustolibs-io/shared_files/scripts/file_lock.py')
-rw-r--r--glustolibs-io/shared_files/scripts/file_lock.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/glustolibs-io/shared_files/scripts/file_lock.py b/glustolibs-io/shared_files/scripts/file_lock.py
new file mode 100644
index 000000000..e29fd1b1d
--- /dev/null
+++ b/glustolibs-io/shared_files/scripts/file_lock.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+# Copyright (C) 2020 Red Hat, Inc. <http://www.redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+from fcntl import flock, LOCK_EX, LOCK_NB, LOCK_UN
+from time import sleep
+from argparse import ArgumentParser
+
+
+def get_file_lock(args):
+ """
+ Gets the lock to a file and releases it after timeout
+ """
+ file_name = args.f
+ timeout = args.t
+ f = open(file_name, 'w')
+ flock(f.fileno(), LOCK_EX | LOCK_NB)
+ sleep(int(timeout))
+ flock(f.fileno(), LOCK_UN)
+
+
+if __name__ == "__main__":
+ file_lock_parser = ArgumentParser(
+ prog="file_lock.py", description="Program to validate file lock ops")
+
+ file_lock_req_args = file_lock_parser.add_argument_group(
+ 'required named arguments')
+ file_lock_req_args.add_argument(
+ '-f', type=str, required=True,
+ help="File on which lock has to be applied")
+ file_lock_req_args.add_argument(
+ '-t', help="time for which lock has to be retained", type=int,
+ required=True)
+
+ file_lock_parser.set_defaults(func=get_file_lock)
+
+ args = file_lock_parser.parse_args()
+ rc = args.func(args)