| 1 |
#!/usr/bin/env python3 |
| 2 |
#=============================================================================== |
| 3 |
# Copyright 2013 NetApp, Inc. All Rights Reserved, |
| 4 |
# contribution by Jorge Mora <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify it under |
| 7 |
# the terms of the GNU General Public License as published by the Free Software |
| 8 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 9 |
# version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 |
#=============================================================================== |
| 15 |
import os |
| 16 |
import time |
| 17 |
import errno |
| 18 |
import signal |
| 19 |
import struct |
| 20 |
import traceback |
| 21 |
from formatstr import * |
| 22 |
import nfstest_config as c |
| 23 |
from baseobj import BaseObj |
| 24 |
from nfstest.test_util import TestUtil |
| 25 |
import packet.nfs.nfs3_const as nfs3_const |
| 26 |
import packet.nfs.nfs4_const as nfs4_const |
| 27 |
import packet.nfs.nlm4_const as nlm4_const |
| 28 |
from fcntl import fcntl,F_RDLCK,F_WRLCK,F_UNLCK,F_SETLK,F_SETLKW,F_GETLK |
| 29 |
|
| 30 |
# Module constants |
| 31 |
__author__ = "Jorge Mora (%s)" % c.NFSTEST_AUTHOR_EMAIL |
| 32 |
__copyright__ = "Copyright (C) 2013 NetApp, Inc." |
| 33 |
__license__ = "GPL v2" |
| 34 |
__version__ = "1.3" |
| 35 |
|
| 36 |
USAGE = """%prog --server <server> [--client <client>] [options] |
| 37 |
|
| 38 |
Locking tests |
| 39 |
============= |
| 40 |
Basic locking tests verify that a lock is granted using various arguments |
| 41 |
to fcntl. These include blocking and non-blocking locks, read or write locks, |
| 42 |
where the file is opened either for reading, writing or both. It also checks |
| 43 |
different ranges including limit conditions. |
| 44 |
|
| 45 |
Non-overlapping tests verity that locks are granted on both the client under |
| 46 |
test and a second process or a remote client when locking the same file. |
| 47 |
|
| 48 |
Overlapping tests verity that a lock is granted on the client under test |
| 49 |
and a second process or a remote client trying to lock the same file will |
| 50 |
be denied if a non-blocking lock is issue or will be blocked if a blocking |
| 51 |
lock is issue on the second process or remote client. |
| 52 |
|
| 53 |
Examples: |
| 54 |
Run the tests which use only the main client (no client option): |
| 55 |
%prog --server 192.168.0.2 --export /exports |
| 56 |
|
| 57 |
Use short options instead: |
| 58 |
%prog -s 192.168.0.2 -e /exports |
| 59 |
|
| 60 |
Use positional arguments with nfsversion=3 for extra client: |
| 61 |
%prog -s 192.168.0.2 -e /exports --client 192.168.0.10:::3 |
| 62 |
|
| 63 |
Use named arguments instead: |
| 64 |
%prog -s 192.168.0.2 -e /exports --client 192.168.0.10:nfsversion=3 |
| 65 |
|
| 66 |
Use positional arguments with nfsversion=3 for second process: |
| 67 |
%prog -s 192.168.0.2 -e /exports --nfsopts :::3 |
| 68 |
|
| 69 |
Use named arguments instead: |
| 70 |
%prog -s 192.168.0.2 -e /exports --nfsopts nfsversion=3 |
| 71 |
|
| 72 |
Notes: |
| 73 |
The user id in the local host and the host specified by --client must |
| 74 |
have access to run commands as root using the 'sudo' command without |
| 75 |
the need for a password. |
| 76 |
|
| 77 |
The user id must be able to 'ssh' to remote host without the need for |
| 78 |
a password.""" |
| 79 |
|
| 80 |
# Test script ID |
| 81 |
SCRIPT_ID = "LOCK" |
| 82 |
|
| 83 |
# Basic tests |
| 84 |
BTESTS = ['btest01'] |
| 85 |
|
| 86 |
# Non-overlapping lock tests using a second process |
| 87 |
NPTESTS = [ |
| 88 |
'nptest01', |
| 89 |
'nptest02', |
| 90 |
'nptest03', |
| 91 |
'nptest04', |
| 92 |
] |
| 93 |
# Non-overlapping lock tests using a second client |
| 94 |
NCTESTS = [ |
| 95 |
'nctest01', |
| 96 |
'nctest02', |
| 97 |
'nctest03', |
| 98 |
'nctest04', |
| 99 |
] |
| 100 |
# Overlapping lock tests using a second process |
| 101 |
OPTESTS = [ |
| 102 |
'optest01', |
| 103 |
'optest02', |
| 104 |
'optest03', |
| 105 |
'optest04', |
| 106 |
'optest05', |
| 107 |
'optest06', |
| 108 |
'optest07', |
| 109 |
'optest08', |
| 110 |
'optest09', |
| 111 |
] |
| 112 |
# Overlapping lock tests using a second client |
| 113 |
OCTESTS = [ |
| 114 |
'octest01', |
| 115 |
'octest02', |
| 116 |
'octest03', |
| 117 |
'octest04', |
| 118 |
'octest05', |
| 119 |
'octest06', |
| 120 |
'octest07', |
| 121 |
'octest08', |
| 122 |
'octest09', |
| 123 |
] |
| 124 |
|
| 125 |
# Dictionary having the number of processes required by each test |
| 126 |
TEST_PROCESS_DICT = {x:1 for x in NPTESTS + OPTESTS} |
| 127 |
TEST_PROCESS_DICT.update({x:2 for x in ["optest09"]}) |
| 128 |
|
| 129 |
# Dictionary having the number of clients required by each test |
| 130 |
TEST_CLIENT_DICT = {x:1 for x in NCTESTS + OCTESTS} |
| 131 |
TEST_CLIENT_DICT.update({x:2 for x in ["octest09"]}) |
| 132 |
|
| 133 |
# All tests, include the test groups in the list of test names |
| 134 |
# so they are displayed in the help |
| 135 |
TESTNAMES = BTESTS + ["noverlap", "nptest"] + NPTESTS + ["nctest"] + \ |
| 136 |
NCTESTS + ["overlap", "optest"] + OPTESTS + ["octest"] + OCTESTS |
| 137 |
|
| 138 |
TESTGROUPS = { |
| 139 |
"noverlap": { |
| 140 |
"tests": NPTESTS + NCTESTS, |
| 141 |
"desc": "Run all non-overlapping locking tests: ", |
| 142 |
}, |
| 143 |
"nptest": { |
| 144 |
"tests": NPTESTS, |
| 145 |
"desc": "Run all non-overlapping locking tests using a second process: ", |
| 146 |
}, |
| 147 |
"nctest": { |
| 148 |
"tests": NCTESTS, |
| 149 |
"desc": "Run all non-overlapping locking tests using a second client: ", |
| 150 |
}, |
| 151 |
"overlap": { |
| 152 |
"tests": OPTESTS + OCTESTS, |
| 153 |
"desc": "Run all overlapping locking tests: ", |
| 154 |
}, |
| 155 |
"optest": { |
| 156 |
"tests": OPTESTS, |
| 157 |
"desc": "Run all overlapping locking tests using a second process: ", |
| 158 |
}, |
| 159 |
"octest": { |
| 160 |
"tests": OCTESTS, |
| 161 |
"desc": "Run all overlapping locking tests using a second client: ", |
| 162 |
}, |
| 163 |
} |
| 164 |
|
| 165 |
# Mapping dictionaries |
| 166 |
LOCKMAP = {F_RDLCK:'F_RDLCK', F_WRLCK:'F_WRLCK', F_UNLCK:'F_UNLCK'} |
| 167 |
LOCKMAP_R = {'read':F_RDLCK, 'write':F_WRLCK, 'unlock':F_UNLCK} |
| 168 |
SLOCKMAP = {F_SETLK:'F_SETLK', F_SETLKW:'F_SETLKW'} |
| 169 |
SLOCKMAP_R = {'immediate':F_SETLK, 'block':F_SETLKW} |
| 170 |
OPENMAP = {os.O_RDONLY:'O_RDONLY', os.O_WRONLY:'O_WRONLY', os.O_RDWR:'O_RDWR'} |
| 171 |
OPENMAP_R = {'read':os.O_RDONLY, 'write':os.O_WRONLY, 'rdwr':os.O_RDWR} |
| 172 |
|
| 173 |
# Locking and helper functions |
| 174 |
def getlock(fd, lock_type, offset=0, length=0, stype=F_SETLK, timeout=30): |
| 175 |
"""Get byte range lock on file given by file descriptor""" |
| 176 |
lockdata = struct.pack('hhllhh', lock_type, 0, offset, length, 0, 0) |
| 177 |
if stype == F_SETLK: |
| 178 |
out = fcntl(fd, stype, lockdata) |
| 179 |
else: |
| 180 |
# Set alarm so the blocking lock could be interrupted |
| 181 |
signal.alarm(timeout) |
| 182 |
try: |
| 183 |
out = fcntl(fd, stype, lockdata) |
| 184 |
finally: |
| 185 |
# Reset alarm |
| 186 |
signal.alarm(0) |
| 187 |
return struct.unpack('hhllhh', out) |
| 188 |
|
| 189 |
def testlock(fd, lock_type, offset=0, length=0): |
| 190 |
"""Test byte range lock on file given by file descriptor""" |
| 191 |
lockdata = struct.pack('hhllhh', lock_type, 0, offset, length, 0, 0) |
| 192 |
out = fcntl(fd, F_GETLK, lockdata) |
| 193 |
return struct.unpack('hhllhh', out) |
| 194 |
|
| 195 |
def get_ioerror(ioerrno, err): |
| 196 |
"""Return fail message when expecting an error""" |
| 197 |
fmsg = "" |
| 198 |
# Test expression to return |
| 199 |
expr = ioerrno == err |
| 200 |
if not ioerrno: |
| 201 |
fmsg = ": no error was returned" |
| 202 |
elif ioerrno != err: |
| 203 |
# Got the wrong error |
| 204 |
expected = errno.errorcode[err] |
| 205 |
error = errno.errorcode[ioerrno] |
| 206 |
fmsg = ": expecting %s, got %s" % (expected, error) |
| 207 |
return (expr, fmsg) |
| 208 |
|
| 209 |
def get_range(offset, length): |
| 210 |
"""Return byte range (start, end) given by the offset and length""" |
| 211 |
if length == 0: |
| 212 |
# Lock until the end of file |
| 213 |
end = 0xffffffffffffffff |
| 214 |
else: |
| 215 |
end = offset + length - 1 |
| 216 |
return (offset, end) |
| 217 |
|
| 218 |
class ProcInfo(BaseObj): |
| 219 |
"""ProcInfo object""" |
| 220 |
# Class attributes: |
| 221 |
# ProcInfo object could be used as a direct replacement for Rexec object |
| 222 |
_fattrs = ("execobj",) |
| 223 |
# ProcInfo local and remote ordinal number |
| 224 |
_procidx = [2, 2] |
| 225 |
|
| 226 |
def __init__(self, clientobj, execobj): |
| 227 |
self.clientobj = clientobj # Host object associated with this process |
| 228 |
self.execobj = execobj # Rexec object for this process |
| 229 |
self.offset = 0 # Lock offset for this process |
| 230 |
self.length = 0 # Lock length for this process |
| 231 |
self.fd = None # File descriptor associated with this process |
| 232 |
self.result = None # Output of locking operation |
| 233 |
self.need_unlock = None # Lock was granted and it needs to be unlocked |
| 234 |
self.isoverlap = None # Lock overlaps with main lock |
| 235 |
# Ordinal number for this object |
| 236 |
self.proc_ordnum = ordinal_number(self._procidx[self.remote]) |
| 237 |
# Increment local or remote ordinal number |
| 238 |
self._procidx[self.remote] += 1 |
| 239 |
|
| 240 |
def close_fd(self): |
| 241 |
"""Close opened file""" |
| 242 |
if self.fd is not None: |
| 243 |
self.run(os.close, self.fd) |
| 244 |
self.fd = None |
| 245 |
|
| 246 |
def reset(self): |
| 247 |
"""Reset lock info""" |
| 248 |
self.result = None |
| 249 |
self.need_unlock = None |
| 250 |
|
| 251 |
def lock_range(self, offset, length): |
| 252 |
"""Set lock range and return this object""" |
| 253 |
self.offset = offset |
| 254 |
self.length = length |
| 255 |
self.isoverlap = None |
| 256 |
self.reset() |
| 257 |
return self |
| 258 |
|
| 259 |
# Main test object definition |
| 260 |
class LockTest(TestUtil): |
| 261 |
"""LockTest object |
| 262 |
|
| 263 |
LockTest() -> New test object |
| 264 |
|
| 265 |
Usage: |
| 266 |
x = LockTest(testnames=['test1', ...]) |
| 267 |
|
| 268 |
# Run all the tests |
| 269 |
x.run_tests() |
| 270 |
x.exit() |
| 271 |
""" |
| 272 |
def __init__(self, **kwargs): |
| 273 |
"""Constructor |
| 274 |
|
| 275 |
Initialize object's private data. |
| 276 |
""" |
| 277 |
TestUtil.__init__(self, **kwargs) |
| 278 |
self.opts.version = "%prog " + __version__ |
| 279 |
|
| 280 |
# Set default script options |
| 281 |
# Display all test messages |
| 282 |
self.opts.set_defaults(tverbose='2') |
| 283 |
|
| 284 |
# Options specific for this test script |
| 285 |
hmsg = "Remote NFS client and options used for conflicting lock tests. " \ |
| 286 |
"Clients are separated by a ',' and each client definition is " \ |
| 287 |
"a list of arguments separated by a ':' given in the following " \ |
| 288 |
"order if positional arguments is used (see examples): " \ |
| 289 |
"clientname:server:export:nfsversion:port:proto:sec:mtpoint" |
| 290 |
self.test_opgroup.add_option("--client", default=None, help=hmsg) |
| 291 |
hmsg = "Local process NFS options used for conflicting lock tests. " \ |
| 292 |
"Processes are separated by a ',' and each process definition " \ |
| 293 |
"is a list of arguments separated by a ':' given in the " \ |
| 294 |
"following order if positional arguments is used (see examples): " \ |
| 295 |
":server:export:nfsversion:port:proto:sec:mtpoint" |
| 296 |
self.test_opgroup.add_option("--nfsopts", default=None, help=hmsg) |
| 297 |
hmsg = "Offset of first lock granted [default: %default]" |
| 298 |
self.test_opgroup.add_option("--offset", default="4k", help=hmsg) |
| 299 |
hmsg = "Length of first lock granted [default: %default]" |
| 300 |
self.test_opgroup.add_option("--length", default="4k", help=hmsg) |
| 301 |
# Object attribute: self.unlock_delay |
| 302 |
hmsg = "Time in seconds to unlock first lock [default: %default]" |
| 303 |
self.test_opgroup.add_option("--unlock-delay", type="float", default=2.0, help=hmsg) |
| 304 |
# Object attribute: self.lockw_timeout |
| 305 |
hmsg = "Time in seconds to wait for blocked lock after " \ |
| 306 |
"conflicting lock has been released [default: %default]" |
| 307 |
self.test_opgroup.add_option("--lockw-timeout", type="int", default=30, help=hmsg) |
| 308 |
hmsg = "List of open types to test [default: %default]" |
| 309 |
self.test_opgroup.add_option("--opentype", default="read,write,rdwr", help=hmsg) |
| 310 |
hmsg = "List of lock types to test [default: %default]" |
| 311 |
self.test_opgroup.add_option("--locktype", default="read,write", help=hmsg) |
| 312 |
hmsg = "List of open types to test on remote client [default: %default]" |
| 313 |
self.test_opgroup.add_option("--opentype2", default="read,write,rdwr", help=hmsg) |
| 314 |
hmsg = "List of lock types to test on remote client [default: %default]" |
| 315 |
self.test_opgroup.add_option("--locktype2", default="read,write", help=hmsg) |
| 316 |
hmsg = "List of set lock types to test [default: %default]" |
| 317 |
self.test_opgroup.add_option("--setlock", default="immediate,block", help=hmsg) |
| 318 |
hmsg = "Create a packet trace for each sub-test. Use it with " + \ |
| 319 |
"caution since it will create a lot of packet traces. " + \ |
| 320 |
"Use --createtraces instead unless trying to get a packet " + \ |
| 321 |
"trace for a specific sub-test. Best if it is used in " + \ |
| 322 |
"combination with the --runtest option." |
| 323 |
self.cap_opgroup.add_option("--subtraces", action="store_true", default=False, help=hmsg) |
| 324 |
self.scan_options() |
| 325 |
self.st_time = time.time() |
| 326 |
|
| 327 |
# Convert units |
| 328 |
self.offset = int_units(self.offset) |
| 329 |
self.length = int_units(self.length) |
| 330 |
|
| 331 |
if self.subtraces: |
| 332 |
# Disable createtraces option since a packet trace will be created |
| 333 |
# here for every sub-test when the --subtraces option is set and |
| 334 |
# a packet trace should not be started by test_util.py |
| 335 |
self.createtraces = False |
| 336 |
|
| 337 |
# Sanity checks |
| 338 |
if self.offset < 2: |
| 339 |
self.opts.error("invalid value given in --offset [%d], must be > 1" % self.offset) |
| 340 |
if self.length < 2: |
| 341 |
self.opts.error("invalid value given in --length [%d], must be > 1" % self.length) |
| 342 |
|
| 343 |
if float(self.lockw_timeout) < (1.2*self.unlock_delay): |
| 344 |
self.opts.error("invalid value given in --lockw-timeout, must be greater than 1.2(--unlock-delay)") |
| 345 |
|
| 346 |
# Process options |
| 347 |
self.open_list = self.get_list(self.opentype, OPENMAP_R) |
| 348 |
self.lock_list = self.get_list(self.locktype, LOCKMAP_R) |
| 349 |
self.open2_list = self.get_list(self.opentype2, OPENMAP_R) |
| 350 |
self.lock2_list = self.get_list(self.locktype2, LOCKMAP_R) |
| 351 |
self.setl_list = self.get_list(self.setlock, SLOCKMAP_R) |
| 352 |
if self.open_list is None: |
| 353 |
self.opts.error("invalid type given in --opentype [%s]" % self.opentype) |
| 354 |
if self.lock_list is None: |
| 355 |
self.opts.error("invalid type given in --locktype [%s]" % self.locktype) |
| 356 |
if self.open2_list is None: |
| 357 |
self.opts.error("invalid type given in --opentype2 [%s]" % self.opentype2) |
| 358 |
if self.lock2_list is None: |
| 359 |
self.opts.error("invalid type given in --locktype2 [%s]" % self.locktype2) |
| 360 |
if self.setl_list is None: |
| 361 |
self.opts.error("invalid type given in --setlock [%s]" % self.setlock) |
| 362 |
|
| 363 |
def lock_setup(self, **kwargs): |
| 364 |
"""Setup for locking tests: |
| 365 |
- start different processes if needed |
| 366 |
- start the remote procedure server on other clients if needed |
| 367 |
- mount the exported file system on remote clients |
| 368 |
- mount the exported file system locally for each extra |
| 369 |
process if different NFS options are specified |
| 370 |
- mount the exported file system locally for the main process |
| 371 |
- call setup() |
| 372 |
|
| 373 |
Arguments are passed to the main setup() method |
| 374 |
""" |
| 375 |
# List of local(index=0) and remote(index=1) ProcInfo items |
| 376 |
self.proc_info_list = ([], []) |
| 377 |
|
| 378 |
# Find how many extra processes and clients should be started |
| 379 |
nclients = 0 |
| 380 |
nprocesses = 0 |
| 381 |
for tname in self.testlist: |
| 382 |
ncount = TEST_CLIENT_DICT.get(tname, 0) |
| 383 |
nclients = max(nclients, ncount) |
| 384 |
ncount = TEST_PROCESS_DICT.get(tname, 0) |
| 385 |
nprocesses = max(nprocesses, ncount) |
| 386 |
|
| 387 |
client_list = self.process_client_option(count=nclients) |
| 388 |
self.verify_client_option(TEST_CLIENT_DICT) |
| 389 |
|
| 390 |
nfsopts_list = self.process_client_option("nfsopts", remote=False, count=nprocesses) |
| 391 |
|
| 392 |
# Flush log file before starting child processes |
| 393 |
self.flush_log() |
| 394 |
|
| 395 |
# Start remote procedure server(s) locally |
| 396 |
for nfsopt_item in nfsopts_list: |
| 397 |
self.create_proc_info(nfsopt_item) |
| 398 |
|
| 399 |
# Start remote procedure server(s) remotely |
| 400 |
for client_item in client_list: |
| 401 |
self.create_proc_info(client_item) |
| 402 |
|
| 403 |
# Unmount server on local host |
| 404 |
self.umount() |
| 405 |
# Mount server on local host |
| 406 |
self.mount() |
| 407 |
|
| 408 |
# Call base object setup method |
| 409 |
self.setup(**kwargs) |
| 410 |
|
| 411 |
def start_rexec(self, clientobj): |
| 412 |
"""Start remote procedure server locally or on the host given by |
| 413 |
the client object. |
| 414 |
Set up the remote server with helper functions to lock and |
| 415 |
unlock a file. |
| 416 |
|
| 417 |
clientobj: |
| 418 |
Client object where the remote procedure server will be started |
| 419 |
""" |
| 420 |
# Start remote procedure server on given client |
| 421 |
execobj = self.create_rexec(clientobj.host) |
| 422 |
|
| 423 |
# Setup function to lock and unlock a file |
| 424 |
execobj.rimport("fcntl", ["fcntl", "F_SETLK"]) |
| 425 |
execobj.rimport("struct") |
| 426 |
execobj.rimport("signal") |
| 427 |
execobj.rcode(getlock) |
| 428 |
# Set SIGALRM handler to do nothing but not ignoring the signal |
| 429 |
# just to interrupt a blocked lock |
| 430 |
execobj.reval("signal.signal(signal.SIGALRM, lambda signum,frame:None)") |
| 431 |
return ProcInfo(clientobj, execobj) |
| 432 |
|
| 433 |
def create_proc_info(self, proc_item): |
| 434 |
"""Create a ProcInfo object and mount server if necessary""" |
| 435 |
# Create a copy of the process item |
| 436 |
client_args = dict(proc_item) |
| 437 |
# Create a Host object for the given client |
| 438 |
client_name = client_args.pop("client", "") |
| 439 |
clientobj = self.create_host(client_name, **client_args) |
| 440 |
if proc_item.get("mount"): |
| 441 |
# Mount only if necessary |
| 442 |
clientobj.umount() |
| 443 |
clientobj.mount() |
| 444 |
# Start a remote procedure server locally or remotely |
| 445 |
pinfo = self.start_rexec(clientobj) |
| 446 |
self.proc_info_list[pinfo.remote].append(pinfo) |
| 447 |
return pinfo |
| 448 |
|
| 449 |
def get_info_list(self, lock_list, remote=0): |
| 450 |
"""Return a list of ProcInfo objects representing the locks in the |
| 451 |
given list. |
| 452 |
|
| 453 |
lock_list: |
| 454 |
List of lock definitions where each definition is given by a |
| 455 |
tuple (offset, length) |
| 456 |
remote: |
| 457 |
Using a remote client or a local process [default: 0] |
| 458 |
""" |
| 459 |
idx = 0 |
| 460 |
info_list = [] |
| 461 |
x_info_list = self.proc_info_list[remote] |
| 462 |
for offset, length in lock_list: |
| 463 |
# Set the lock range in the ProcInfo object and add it to the list |
| 464 |
info_list.append(x_info_list[idx].lock_range(offset, length)) |
| 465 |
idx += 1 |
| 466 |
return info_list |
| 467 |
|
| 468 |
def get_time(self): |
| 469 |
"""Return the number of seconds since the object was instantiated""" |
| 470 |
return time.time() - self.st_time |
| 471 |
|
| 472 |
def get_opts_list(self, sflag=True, oflag=True, lflag=True, coflag=True, clflag=True): |
| 473 |
"""Return a list of all the permutations given by all option lists |
| 474 |
|
| 475 |
sflag: |
| 476 |
If true, use blocking and non-blocking locks |
| 477 |
oflag: |
| 478 |
If true, use open type of read, write and rdwr |
| 479 |
lflag: |
| 480 |
If true, use lock type of read, write and unlock |
| 481 |
coflag: |
| 482 |
If true, use open type of read, write and rdwr on second |
| 483 |
process or client |
| 484 |
clflag: |
| 485 |
If true, use lock type of read, write and unlock on second |
| 486 |
process or client |
| 487 |
""" |
| 488 |
ret = [] |
| 489 |
setl_list = self.setl_list if sflag else [None] |
| 490 |
open_list = self.open_list if oflag else [None] |
| 491 |
lock_list = self.lock_list if lflag else [None] |
| 492 |
open2_list = self.open2_list if coflag else [None] |
| 493 |
lock2_list = self.lock2_list if clflag else [None] |
| 494 |
for stype in setl_list: |
| 495 |
for oltype in open_list: |
| 496 |
for ltype in lock_list: |
| 497 |
for octype in open2_list: |
| 498 |
for ctype in lock2_list: |
| 499 |
ret.append({ |
| 500 |
'stype' : stype, |
| 501 |
'oltype' : oltype, |
| 502 |
'ltype' : ltype, |
| 503 |
'octype' : octype, |
| 504 |
'ctype' : ctype, |
| 505 |
}) |
| 506 |
return ret |
| 507 |
|
| 508 |
def open_file(self, otype, pinfo=None): |
| 509 |
"""Open file with given open type on either local or remote client |
| 510 |
|
| 511 |
otype: |
| 512 |
Open type, either O_RDONLY, O_WRONLY or O_RDWR |
| 513 |
pinfo: |
| 514 |
ProcInfo object to open file on a different process or client |
| 515 |
[default: None] |
| 516 |
""" |
| 517 |
filename = self.files[0] |
| 518 |
# Use the correct mount point in the full path of file |
| 519 |
if pinfo is None: |
| 520 |
absfile = self.abspath(filename) |
| 521 |
else: |
| 522 |
absfile = pinfo.clientobj.abspath(filename) |
| 523 |
|
| 524 |
if otype & os.O_WRONLY == os.O_WRONLY: |
| 525 |
ostr = "writing" |
| 526 |
elif otype & os.O_RDWR == os.O_RDWR: |
| 527 |
ostr = "reading and writing" |
| 528 |
else: |
| 529 |
ostr = "reading" |
| 530 |
|
| 531 |
if pinfo: |
| 532 |
# OPEN on different process or remote client |
| 533 |
pstr = "client" if pinfo.remote else "process" |
| 534 |
self.dprint('DBG2', "Open file for %s [%s] on %s %s" % (ostr, filename, pinfo.proc_ordnum, pstr)) |
| 535 |
fd = pinfo.run(os.open, absfile, otype) |
| 536 |
else: |
| 537 |
# OPEN locally |
| 538 |
self.dprint('DBG2', "Open file for %s [%s]" % (ostr, filename)) |
| 539 |
fd = os.open(absfile, otype) |
| 540 |
return fd |
| 541 |
|
| 542 |
def do_lock_test(self, fd, ltype, offset=None, length=None, msg="", submsg=None, stype=F_SETLK, block=False, error=0, pinfo=None): |
| 543 |
"""Do the actual lock on a file given by the file descriptor |
| 544 |
|
| 545 |
fd: |
| 546 |
File descriptor of file to lock |
| 547 |
ltype: |
| 548 |
Lock type: F_RDLCK, F_WRLCK or F_UNLCK |
| 549 |
offset: |
| 550 |
Starting offset of byte range lock [default: None] |
| 551 |
If offset is None then use the offset from the ProcInfo |
| 552 |
object given by pinfo |
| 553 |
length: |
| 554 |
Length of byte range lock [default: None] |
| 555 |
If length is None then use the length from the ProcInfo |
| 556 |
object given by pinfo |
| 557 |
msg: |
| 558 |
Test message to display [default: ""] |
| 559 |
submsg: |
| 560 |
Subtest message to display [default: None] |
| 561 |
stype: |
| 562 |
Blocking lock type: F_SETLK or F_SETLKW [default: F_SETLK] |
| 563 |
block: |
| 564 |
Expect lock to block [default: False] |
| 565 |
error: |
| 566 |
Expected locking error [default: 0] |
| 567 |
pinfo: |
| 568 |
ProcInfo object to lock file on a different process or client |
| 569 |
[default: None] |
| 570 |
""" |
| 571 |
try: |
| 572 |
fmsg = "" |
| 573 |
pexpr = False |
| 574 |
ioerrno = 0 |
| 575 |
if pinfo: |
| 576 |
# Use the lock range given in the ProcInfo object |
| 577 |
offset = pinfo.offset if offset is None else offset |
| 578 |
length = pinfo.length if length is None else length |
| 579 |
|
| 580 |
# Set up debugging message options |
| 581 |
lmsg = "Unlock" if ltype == F_UNLCK else "Lock" |
| 582 |
smsg = "F_SETLK" if stype == F_SETLK else "F_SETLKW" |
| 583 |
(start, end) = get_range(offset, length) |
| 584 |
info = "%s file (%s, %s) off=%d len=%d range(%d, %d)" % (lmsg, LOCKMAP[ltype], smsg, offset, length, start, end) |
| 585 |
if pinfo: |
| 586 |
# Lock file in different process or remote client |
| 587 |
pstr = "client" if pinfo.remote else "process" |
| 588 |
self.dprint('DBG1', info + " on %s %s @%.2f" % (pinfo.proc_ordnum, pstr, self.get_time())) |
| 589 |
nowait = True if stype == F_SETLKW else False |
| 590 |
out = pinfo.run("getlock", fd, ltype, offset, length, stype, self.lockw_timeout, NOWAIT=nowait) |
| 591 |
if nowait: |
| 592 |
# It is a blocking lock, it could block if overlapping range |
| 593 |
# Poll Rexec object to make sure the lock is blocked if it |
| 594 |
# is expected to block |
| 595 |
pexpr = pinfo.poll(0.1) |
| 596 |
if pexpr: |
| 597 |
if block: |
| 598 |
# Expected to block, but did not |
| 599 |
fmsg = ": lock did not block" |
| 600 |
elif error != errno.EAGAIN: |
| 601 |
# Get lock results |
| 602 |
out = pinfo.results() |
| 603 |
else: |
| 604 |
# Lock file locally |
| 605 |
self.dprint('DBG1', info) |
| 606 |
getlock(fd, ltype, offset, length, stype, self.lockw_timeout) |
| 607 |
except IOError as ioerr: |
| 608 |
# Set up fail message if expecting no errors |
| 609 |
ioerrno = ioerr.errno |
| 610 |
errstr = errno.errorcode[ioerr.errno] |
| 611 |
self.dprint("DBG7", "Got error %s" % errstr) |
| 612 |
fmsg = ": got error %s" % errstr |
| 613 |
if error: |
| 614 |
# Expecting an error |
| 615 |
(expr, fmsg) = get_ioerror(ioerrno, error) |
| 616 |
else: |
| 617 |
# Not expecting an error |
| 618 |
expr = not ioerrno and (not block or not pexpr) |
| 619 |
self.test(expr, msg, subtest=submsg, failmsg=fmsg) |
| 620 |
return ioerrno is None |
| 621 |
|
| 622 |
def wait_for_lock(self, fd, info_list, lockmsg, submsg_list): |
| 623 |
"""Wait for blocked lock |
| 624 |
|
| 625 |
fd: |
| 626 |
File descriptor of file holding the current lock, so it will |
| 627 |
be unlocked to let the blocked lock be granted |
| 628 |
info_list: |
| 629 |
List of ProcInfo objects with info for each blocked lock |
| 630 |
on a different process or client |
| 631 |
lockmsg: |
| 632 |
Description of current lock |
| 633 |
submsg_list: |
| 634 |
List of subtest messages to display for each lock given in |
| 635 |
info_list |
| 636 |
""" |
| 637 |
fmsg = "" |
| 638 |
out = None |
| 639 |
expr = False |
| 640 |
# Flag used for keeping track of the time since the first client |
| 641 |
# unlocked the file and timed out if blocked lock is not granted |
| 642 |
sl_time = 0 |
| 643 |
# Flag used for waiting to unlock file on the first client |
| 644 |
stime = time.time() |
| 645 |
# Flag used to verify if the unlock of the first client was done |
| 646 |
need_unlock = True |
| 647 |
# Polling granularity |
| 648 |
delta = self.unlock_delay/5.0 |
| 649 |
self.dprint('DBG3', "Wait %.2f secs to unlock conflicting lock @%.2f" % (self.unlock_delay, self.get_time())) |
| 650 |
dbgmsg = "Check if blocked lock is still waiting" |
| 651 |
while True: |
| 652 |
self.dprint('DBG3', "%s @%.2f" % (dbgmsg, self.get_time())) |
| 653 |
if need_unlock and (expr or time.time() - stime >= self.unlock_delay): |
| 654 |
# Unlock current file lock so the blocked lock could be granted |
| 655 |
msg = "Unlocking full file after delay should be granted" |
| 656 |
self.do_lock_test(fd, F_UNLCK, 0, 0, msg, lockmsg) |
| 657 |
if expr: |
| 658 |
# Blocked lock has already been granted |
| 659 |
break |
| 660 |
need_unlock = False |
| 661 |
sl_time = time.time() |
| 662 |
self.dprint('DBG3', "Wait up to %d secs to check if blocked lock has been granted @%.2f" % (self.lockw_timeout, self.get_time())) |
| 663 |
dbgmsg = "Check if blocked lock has been granted" |
| 664 |
delta = self.lockw_timeout/30.0 |
| 665 |
if delta < 0.2: |
| 666 |
delta = 0.2 |
| 667 |
delta_time = delta |
| 668 |
idx = 0 |
| 669 |
ordnum = "" |
| 670 |
for pinfo in info_list: |
| 671 |
submsg = submsg_list[idx] |
| 672 |
idx += 1 |
| 673 |
if len(info_list) > 1: |
| 674 |
ordnum = "%s " % ordinal_number(idx) |
| 675 |
if pinfo.need_unlock is not None: |
| 676 |
# This lock has already been granted |
| 677 |
continue |
| 678 |
if pinfo.poll(delta_time): |
| 679 |
out = None |
| 680 |
try: |
| 681 |
# Blocking lock just returned |
| 682 |
self.dprint('DBG3', "Getting results from %sblocked lock @%.2f" % (ordnum, self.get_time())) |
| 683 |
out = pinfo.results() |
| 684 |
expr = True |
| 685 |
except Exception as e: |
| 686 |
# Unable to get results from blocked lock |
| 687 |
if getattr(e, "errno", None) == errno.EINTR: |
| 688 |
self.test(False, "Timeout waiting for %sblocked lock to be granted" % ordnum, subtest=submsg) |
| 689 |
else: |
| 690 |
self.test(False, "Error while getting results from %sblocked lock" % ordnum, subtest=submsg, failmsg=": %s" % e) |
| 691 |
pinfo.result = out |
| 692 |
pinfo.need_unlock = need_unlock |
| 693 |
delta_time = min(delta/10.0, 0.01) |
| 694 |
if not need_unlock: |
| 695 |
if [x.need_unlock for x in info_list].count(None) == 0: |
| 696 |
# All locks have been granted or timed out |
| 697 |
break |
| 698 |
idx = 0 |
| 699 |
for pinfo in info_list: |
| 700 |
submsg = submsg_list[idx] |
| 701 |
idx += 1 |
| 702 |
if pinfo.need_unlock: |
| 703 |
self.test(not pinfo.result, "Blocked lock is granted before conflicting lock was unlocked", subtest=submsg) |
| 704 |
else: |
| 705 |
self.test(pinfo.result, "Blocked lock is granted after conflicting lock is released", subtest=submsg, failmsg=fmsg) |
| 706 |
|
| 707 |
def basic_lock(self, info_list, oltype, ltype, octype, ctype, stype): |
| 708 |
"""This is the main locking method for overlapping and non-overlapping |
| 709 |
tests. This method does the following: |
| 710 |
1. Open file locally |
| 711 |
2. Lock file locally -- this is the conflicting lock |
| 712 |
3. Open file on a different process or remote client |
| 713 |
4. Lock file on a different process or remote client |
| 714 |
5. If locks do not overlap, verify both locks are granted |
| 715 |
6. If using a blocking lock on an overlapping range, then wait |
| 716 |
for the number of seconds given by option unlock-delay and |
| 717 |
verify the blocking lock is not granted until the conflicting |
| 718 |
lock has been unlocked at the end of the wait period. Once the |
| 719 |
conflicting lock has been unlocked verify the blocked lock |
| 720 |
is granted |
| 721 |
7. If using a non-blocking lock on an overlapping range, verify |
| 722 |
the correct error is returned |
| 723 |
8. Unlock both the local and remote locks |
| 724 |
|
| 725 |
info_list: |
| 726 |
List of ProcInfo objects with info for each extra lock to take |
| 727 |
on a different process or client |
| 728 |
oltype: |
| 729 |
Open type to use on local file |
| 730 |
ltype: |
| 731 |
Lock type to use on local file |
| 732 |
octype: |
| 733 |
Open type to use on second file (other process or remote) |
| 734 |
ctype: |
| 735 |
Lock type to use on second file (other process or remote) |
| 736 |
stype: |
| 737 |
Either a blocking or non-blocking lock |
| 738 |
""" |
| 739 |
try: |
| 740 |
err = 0 |
| 741 |
fdl = None |
| 742 |
|
| 743 |
if self.subtraces: |
| 744 |
self.trace_start() |
| 745 |
|
| 746 |
if self.createtraces or self.subtraces: |
| 747 |
# Have a marker on the packet trace for the running test, |
| 748 |
# this will make the client send a LOOKUP with the test |
| 749 |
# info as the file name |
| 750 |
self.insert_trace_marker(self.sub_testname) |
| 751 |
|
| 752 |
# Find if ranges overlap |
| 753 |
isoverlap = info_list[0].isoverlap |
| 754 |
|
| 755 |
pstr = "client" if info_list[0].remote else "process" |
| 756 |
smsg1 = ", lock1(%s, %s, %s)" % (OPENMAP[oltype], LOCKMAP[ltype], SLOCKMAP[stype]) |
| 757 |
smsg_list = [] |
| 758 |
for i in range(len(info_list)): |
| 759 |
smsgx = ", lock%d(%s, %s, %s)" % (i+2, OPENMAP[octype], LOCKMAP[ctype], SLOCKMAP[stype]) |
| 760 |
smsg_list.append(smsgx) |
| 761 |
|
| 762 |
# Set up main test message |
| 763 |
error = 0 |
| 764 |
blocking = False |
| 765 |
ostr = "" if isoverlap else "non-" |
| 766 |
if ctype == F_RDLCK and octype == os.O_WRONLY or \ |
| 767 |
ctype == F_WRLCK and octype == os.O_RDONLY: |
| 768 |
error = errno.EBADF |
| 769 |
imsg = "return %s" % errno.errorcode[error] |
| 770 |
elif not isoverlap or (ltype == F_RDLCK and ctype == F_RDLCK): |
| 771 |
error = 0 |
| 772 |
imsg = "be granted" |
| 773 |
if isoverlap: |
| 774 |
imsg += " since both locks are %s" % LOCKMAP[ltype] |
| 775 |
elif stype == F_SETLKW: |
| 776 |
error = 0 |
| 777 |
imsg = "block" |
| 778 |
blocking = True |
| 779 |
elif ltype != ctype or (ltype == F_WRLCK and ctype == ltype): |
| 780 |
error = errno.EAGAIN |
| 781 |
imsg = "return %s" % errno.errorcode[error] |
| 782 |
else: |
| 783 |
error = 0 |
| 784 |
imsg = "be granted" |
| 785 |
|
| 786 |
lmsg = "be granted" |
| 787 |
if ltype == F_RDLCK and oltype == os.O_WRONLY or \ |
| 788 |
ltype == F_WRLCK and oltype == os.O_RDONLY: |
| 789 |
err = errno.EBADF |
| 790 |
lmsg = "return %s" % errno.errorcode[err] |
| 791 |
|
| 792 |
# Open file on main process and lock it, this will become the |
| 793 |
# conflicting lock |
| 794 |
fdl = self.open_file(oltype) |
| 795 |
submsg = " should %s%s" % (lmsg, smsg1) |
| 796 |
msg = "Locking byte range" |
| 797 |
self.do_lock_test(fdl, ltype, self.offset, self.length, msg, submsg, stype=stype, error=err) |
| 798 |
|
| 799 |
if not err: |
| 800 |
idx = 0 |
| 801 |
for pinfo in info_list: |
| 802 |
pinfo.fd = self.open_file(octype, pinfo) |
| 803 |
submsg = " should %s%s" % (imsg, smsg_list[idx]) |
| 804 |
msg = "Locking with %soverlapping range on %s %s" % (ostr, pinfo.proc_ordnum, pstr) |
| 805 |
locked = self.do_lock_test(pinfo.fd, ctype, msg=msg, submsg=submsg, stype=stype, error=error, pinfo=pinfo, block=blocking) |
| 806 |
idx += 1 |
| 807 |
|
| 808 |
if blocking: |
| 809 |
# Wait for blocked lock to be granted by unlocking the |
| 810 |
# conflicting lock |
| 811 |
self.wait_for_lock(fdl, info_list, smsg1, smsg_list) |
| 812 |
else: |
| 813 |
# No locking conflict so unlock local file |
| 814 |
msg = "Unlocking full file should be granted" |
| 815 |
self.do_lock_test(fdl, F_UNLCK, 0, 0, msg, smsg1) |
| 816 |
|
| 817 |
if not locked and error != errno.EBADF: |
| 818 |
xmsg = "be granted" |
| 819 |
if ctype == F_RDLCK and octype == os.O_WRONLY or \ |
| 820 |
ctype == F_WRLCK and octype == os.O_RDONLY: |
| 821 |
err = errno.EBADF |
| 822 |
xmsg = "return %s" % errno.errorcode[err] |
| 823 |
idx = 0 |
| 824 |
for pinfo in info_list: |
| 825 |
submsg = " should %s%s" % (xmsg, smsg_list[idx]) |
| 826 |
msg = "Locking byte range on %s %s" % (pinfo.proc_ordnum, pstr) |
| 827 |
self.do_lock_test(pinfo.fd, ctype, msg=msg, submsg=submsg, stype=stype, error=err, pinfo=pinfo) |
| 828 |
idx += 1 |
| 829 |
if error != errno.EBADF: |
| 830 |
idx = 0 |
| 831 |
for pinfo in info_list: |
| 832 |
msg = "Unlocking full file on %s %s should be granted" % (pinfo.proc_ordnum, pstr) |
| 833 |
self.do_lock_test(pinfo.fd, F_UNLCK, 0, 0, msg, smsg_list[idx], pinfo=pinfo) |
| 834 |
idx += 1 |
| 835 |
except: |
| 836 |
self.test(False, traceback.format_exc()) |
| 837 |
finally: |
| 838 |
# Close open files |
| 839 |
if fdl is not None: |
| 840 |
os.close(fdl) |
| 841 |
for pinfo in info_list: |
| 842 |
pinfo.close_fd() |
| 843 |
if self.subtraces: |
| 844 |
self.trace_stop() |
| 845 |
self.trace_open() |
| 846 |
self.pktt.close() |
| 847 |
|
| 848 |
def do_basic_lock(self, info_list, overlap=False): |
| 849 |
"""This is the main locking method for testing all different |
| 850 |
permutations of the same test by varying the open type of both |
| 851 |
files, the locking type and for blocking and non-blocking locks. |
| 852 |
|
| 853 |
info_list: |
| 854 |
List of ProcInfo objects with info for each extra lock to take |
| 855 |
on a different process or client |
| 856 |
overlap: |
| 857 |
True if range is expected to overlap |
| 858 |
""" |
| 859 |
# Find if ranges overlap |
| 860 |
(start1, end1) = get_range(self.offset, self.length) |
| 861 |
|
| 862 |
ridx = 2 |
| 863 |
for pinfo in info_list: |
| 864 |
(start2, end2) = get_range(pinfo.offset, pinfo.length) |
| 865 |
isoverlap = (start1 <= end2 and start2 <= end1) |
| 866 |
pinfo.isoverlap = isoverlap |
| 867 |
|
| 868 |
# Check if ranges overlap when expected |
| 869 |
fmsg = ": range1(%d, %d), range%d(%d, %d)" % (start1, end1, ridx, start2, end2) |
| 870 |
if overlap and not isoverlap: |
| 871 |
self.test(False, "Range does not overlap", failmsg=fmsg) |
| 872 |
return |
| 873 |
elif not overlap and isoverlap: |
| 874 |
self.test(False, "Range overlaps", failmsg=fmsg) |
| 875 |
return |
| 876 |
ridx += 1 |
| 877 |
|
| 878 |
testidx = 1 |
| 879 |
for item in self.get_opts_list(): |
| 880 |
stype = item['stype'] |
| 881 |
oltype = item['oltype'] |
| 882 |
ltype = item['ltype'] |
| 883 |
octype = item['octype'] |
| 884 |
ctype = item['ctype'] |
| 885 |
|
| 886 |
self.sub_testname = "%s_%03d" % (self.testname, testidx) |
| 887 |
|
| 888 |
if self.tverbose == 2: |
| 889 |
self.dprint("INFO", "Running %s" % self.sub_testname) |
| 890 |
|
| 891 |
# Reset ProcInfo objects for next test |
| 892 |
expr = self.nfs_version < 4 |
| 893 |
for pinfo in info_list: |
| 894 |
expr = expr or pinfo.clientobj.nfs_version < 4 |
| 895 |
pinfo.reset() |
| 896 |
|
| 897 |
if expr or info_list[0].remote: |
| 898 |
# Expect NFS errors only if NFS version < 4 or a remote client |
| 899 |
self.set_nfserr_list( |
| 900 |
nfs3list=[nfs3_const.NFS3ERR_NOENT, nfs3_const.NFS3ERR_NOTEMPTY], |
| 901 |
nfs4list=[nfs4_const.NFS4ERR_NOENT, nfs4_const.NFS4ERR_DENIED], |
| 902 |
nlm4list=[nlm4_const.NLM4_BLOCKED, nlm4_const.NLM4_DENIED], |
| 903 |
) |
| 904 |
|
| 905 |
# Do actual test |
| 906 |
self.basic_lock(info_list, oltype, ltype, octype, ctype, stype) |
| 907 |
testidx += 1 |
| 908 |
|
| 909 |
def btest01_test(self): |
| 910 |
"""Basic locking tests |
| 911 |
These tests verify that a lock is granted using various arguments |
| 912 |
to fcntl. These include blocking and non-blocking locks, read or |
| 913 |
write locks, where the file is opened either for reading, writing |
| 914 |
or both. It also checks different ranges including limit conditions. |
| 915 |
""" |
| 916 |
self.test_group("Basic locking tests") |
| 917 |
nmax = 0x7fffffff if self.nfs_version == 2 else 0x7fffffffffffffff |
| 918 |
tlist = [ (0, 0), (0, 1), (1, 0), (0, self.length), |
| 919 |
(self.offset, self.length), (self.offset, 0), |
| 920 |
(self.offset, 1), (0, nmax), (1, nmax), (nmax, 1), |
| 921 |
(nmax, 0), (0, -1), (-1, 0) ] |
| 922 |
|
| 923 |
testidx = 1 |
| 924 |
for offset, length in tlist: |
| 925 |
offstr = "NMAX" if offset == nmax else str(offset) |
| 926 |
lenstr = "NMAX" if length == nmax else str(length) |
| 927 |
|
| 928 |
for item in self.get_opts_list(coflag=False, clflag=False): |
| 929 |
try: |
| 930 |
fd = None |
| 931 |
self.sub_testname = "%s_%03d" % (self.testname, testidx) |
| 932 |
|
| 933 |
if self.tverbose > 1: |
| 934 |
self.dprint("INFO", "Running %s" % self.sub_testname) |
| 935 |
|
| 936 |
if self.subtraces: |
| 937 |
self.trace_start() |
| 938 |
|
| 939 |
if self.createtraces or self.subtraces: |
| 940 |
# Have a marker on the packet trace for the running |
| 941 |
# test, this will make the client send a LOOKUP with |
| 942 |
# the test info as the file name |
| 943 |
self.insert_trace_marker(self.sub_testname) |
| 944 |
testidx += 1 |
| 945 |
|
| 946 |
stype = item['stype'] |
| 947 |
oltype = item['oltype'] |
| 948 |
ltype = item['ltype'] |
| 949 |
lerr = 0 |
| 950 |
uerr = 0 |
| 951 |
if offset < 0 or length < 0: |
| 952 |
lerr = errno.EINVAL |
| 953 |
uerr = errno.EINVAL |
| 954 |
elif ltype == F_RDLCK and oltype == os.O_WRONLY or \ |
| 955 |
ltype == F_WRLCK and oltype == os.O_RDONLY: |
| 956 |
lerr = errno.EBADF |
| 957 |
lmsg = "return %s" % errno.errorcode[lerr] if lerr else "be granted" |
| 958 |
umsg = "return %s" % errno.errorcode[uerr] if uerr else "be granted" |
| 959 |
submsg = "open(%s) lock(%s, %s)" % (OPENMAP[oltype], LOCKMAP[ltype], SLOCKMAP[stype]) |
| 960 |
lsubmsg = " should %s, %s" % (lmsg, submsg) |
| 961 |
usubmsg = " should %s, %s" % (umsg, submsg) |
| 962 |
|
| 963 |
# Open file |
| 964 |
fd = self.open_file(oltype) |
| 965 |
|
| 966 |
msg = "Unlocking byte range (off:%s, len:%s) while file is not locked" % (offstr, lenstr) |
| 967 |
self.do_lock_test(fd, F_UNLCK, offset, length, msg, usubmsg, stype=stype, error=uerr) |
| 968 |
|
| 969 |
msg = "Locking byte range (off:%s, len:%s)" % (offstr, lenstr) |
| 970 |
locked = self.do_lock_test(fd, ltype, offset, length, msg, lsubmsg, stype=stype, error=lerr) |
| 971 |
if locked: |
| 972 |
msg = "Unlocking byte range (off:%s, len:%s)" % (offstr, lenstr) |
| 973 |
self.do_lock_test(fd, F_UNLCK, offset, length, msg, usubmsg, stype=stype, error=uerr) |
| 974 |
except: |
| 975 |
self.test(False, traceback.format_exc()) |
| 976 |
finally: |
| 977 |
# Close open file |
| 978 |
if fd is not None: |
| 979 |
os.close(fd) |
| 980 |
if self.subtraces: |
| 981 |
self.trace_stop() |
| 982 |
self.trace_open() |
| 983 |
self.pktt.close() |
| 984 |
|
| 985 |
def ntest01(self, remote=0): |
| 986 |
"""Locking non-overlapping range from a second process where end2 < start1 |
| 987 |
process1: |------------------| |
| 988 |
process2: |--------| |
| 989 |
""" |
| 990 |
pstr = "client" if remote else "process" |
| 991 |
self.test_group("Locking non-overlapping range from a second %s where end2 < start1" % pstr) |
| 992 |
self.do_basic_lock(self.get_info_list([(0, int(self.offset/2))], remote)) |
| 993 |
|
| 994 |
def ntest02(self, remote=0): |
| 995 |
"""Locking non-overlapping range from a second process where end2 == start1 - 1 |
| 996 |
process1: |------------------| |
| 997 |
process2: |------------------| |
| 998 |
""" |
| 999 |
pstr = "client" if remote else "process" |
| 1000 |
self.test_group("Locking non-overlapping range from a second %s where end2 == start1 - 1" % pstr) |
| 1001 |
self.do_basic_lock(self.get_info_list([(0, self.offset)], remote)) |
| 1002 |
|
| 1003 |
def ntest03(self, remote=0): |
| 1004 |
"""Locking non-overlapping range from a second process where start2 > end1 |
| 1005 |
process1: |------------------| |
| 1006 |
process2: |--------| |
| 1007 |
""" |
| 1008 |
pstr = "client" if remote else "process" |
| 1009 |
offset2 = self.offset + self.length + int(self.length/2) |
| 1010 |
length2 = int(self.length/2) |
| 1011 |
self.test_group("Locking non-overlapping range from a second %s where start2 > end1" % pstr) |
| 1012 |
self.do_basic_lock(self.get_info_list([(offset2, length2)], remote)) |
| 1013 |
|
| 1014 |
self.test_group("Locking non-overlapping range from a second %s where start2 > end1 and end2 == EOF" % pstr) |
| 1015 |
self.do_basic_lock(self.get_info_list([(offset2, 0)], remote)) |
| 1016 |
|
| 1017 |
def ntest04(self, remote=0): |
| 1018 |
"""Locking non-overlapping range from a second process where start2 == end1 + 1 |
| 1019 |
process1: |------------------| |
| 1020 |
process2: |------------------| |
| 1021 |
""" |
| 1022 |
pstr = "client" if remote else "process" |
| 1023 |
offset2 = self.offset + self.length |
| 1024 |
self.test_group("Locking non-overlapping range from a second %s where start2 == end1 + 1" % pstr) |
| 1025 |
self.do_basic_lock(self.get_info_list([(offset2, self.length)], remote)) |
| 1026 |
|
| 1027 |
self.test_group("Locking non-overlapping range from a second %s where start2 == end1 + 1 and end2 == EOF" % pstr) |
| 1028 |
self.do_basic_lock(self.get_info_list([(offset2, 0)], remote)) |
| 1029 |
|
| 1030 |
def otest01(self, remote=0): |
| 1031 |
"""Locking same range from a second process |
| 1032 |
process1: |------------------| |
| 1033 |
process2: |------------------| |
| 1034 |
""" |
| 1035 |
pstr = "client" if remote else "process" |
| 1036 |
self.test_group("Locking same range from a second %s" % pstr) |
| 1037 |
self.do_basic_lock(self.get_info_list([(self.offset, self.length)], remote), overlap=True) |
| 1038 |
|
| 1039 |
def otest02(self, remote=0): |
| 1040 |
"""Locking overlapping range from a second process where start2 < start1 |
| 1041 |
process1: |------------------| |
| 1042 |
process2: |------------------| |
| 1043 |
""" |
| 1044 |
pstr = "client" if remote else "process" |
| 1045 |
offset2 = self.offset - int(self.length/2) |
| 1046 |
if offset2 < 0: |
| 1047 |
offset2 = 0 |
| 1048 |
length2 = self.offset + int(self.length/2) - offset2 |
| 1049 |
self.test_group("Locking overlapping range from a second %s where start2 < start1" % pstr) |
| 1050 |
self.do_basic_lock(self.get_info_list([(offset2, length2)], remote), overlap=True) |
| 1051 |
|
| 1052 |
if offset2 > 0: |
| 1053 |
length2 = self.offset + int(self.length/2) |
| 1054 |
self.test_group("Locking overlapping range from a second %s where start2 < start1 and start2 == 0" % pstr) |
| 1055 |
self.do_basic_lock(self.get_info_list([(0, length2)], remote), overlap=True) |
| 1056 |
|
| 1057 |
def otest03(self, remote=0): |
| 1058 |
"""Locking overlapping range from a second process where end2 > end1 |
| 1059 |
process1: |------------------| |
| 1060 |
process2: |------------------| |
| 1061 |
""" |
| 1062 |
pstr = "client" if remote else "process" |
| 1063 |
offset2 = self.offset + int(self.length/2) |
| 1064 |
self.test_group("Locking overlapping range from a second %s where end2 > end1" % pstr) |
| 1065 |
self.do_basic_lock(self.get_info_list([(offset2, self.length)], remote), overlap=True) |
| 1066 |
|
| 1067 |
self.test_group("Locking overlapping range from a second %s where end2 > end1 and end2 == EOF" % pstr) |
| 1068 |
self.do_basic_lock(self.get_info_list([(offset2, 0)], remote), overlap=True) |
| 1069 |
|
| 1070 |
def otest04(self, remote=0): |
| 1071 |
"""Locking overlapping range from a second process where range2 is entirely within range1 |
| 1072 |
process1: |------------------| |
| 1073 |
process2: |--------| |
| 1074 |
""" |
| 1075 |
pstr = "client" if remote else "process" |
| 1076 |
offset2 = self.offset + int(self.length/4) |
| 1077 |
length2 = int(self.length/2) |
| 1078 |
self.test_group("Locking overlapping range from a second %s where range2 is entirely within range1" % pstr) |
| 1079 |
self.do_basic_lock(self.get_info_list([(offset2, length2)], remote), overlap=True) |
| 1080 |
|
| 1081 |
def otest05(self, remote=0): |
| 1082 |
"""Locking overlapping range from a second process where range1 is entirely within range2 |
| 1083 |
process1: |------------------| |
| 1084 |
process2: |----------------------------| |
| 1085 |
""" |
| 1086 |
pstr = "client" if remote else "process" |
| 1087 |
offset2 = self.offset - int(self.length/4) |
| 1088 |
if offset2 < 0: |
| 1089 |
offset2 = 0 |
| 1090 |
length2 = self.length + int(self.length/2) |
| 1091 |
self.test_group("Locking overlapping range from a second %s where range1 is entirely within range2" % pstr) |
| 1092 |
self.do_basic_lock(self.get_info_list([(offset2, length2)], remote), overlap=True) |
| 1093 |
|
| 1094 |
if offset2 > 0: |
| 1095 |
length2 = self.offset + self.length + int(self.length/4) |
| 1096 |
self.test_group("Locking overlapping range from a second %s where range1 is entirely within range2 and start2 == 0" % pstr) |
| 1097 |
self.do_basic_lock(self.get_info_list([(0, length2)], remote), overlap=True) |
| 1098 |
|
| 1099 |
self.test_group("Locking overlapping range from a second %s where range1 is entirely within range2 and end2 == EOF" % pstr) |
| 1100 |
self.do_basic_lock(self.get_info_list([(offset2, 0)], remote), overlap=True) |
| 1101 |
|
| 1102 |
def otest06(self, remote=0): |
| 1103 |
"""Locking full file range from a second process""" |
| 1104 |
pstr = "client" if remote else "process" |
| 1105 |
self.test_group("Locking full file range from a second %s" % pstr) |
| 1106 |
self.do_basic_lock(self.get_info_list([(0, 0)], remote), overlap=True) |
| 1107 |
|
| 1108 |
def otest07(self, remote=0): |
| 1109 |
"""Locking overlapping range from a second process where end2 == start1 |
| 1110 |
process1: |------------------| |
| 1111 |
process2: |------------------| |
| 1112 |
""" |
| 1113 |
pstr = "client" if remote else "process" |
| 1114 |
offset2 = self.offset - self.length + 1 |
| 1115 |
if offset2 < 0: |
| 1116 |
offset2 = 0 |
| 1117 |
length2 = self.offset - offset2 + 1 |
| 1118 |
self.test_group("Locking overlapping range from a second %s where end2 == start1" % pstr) |
| 1119 |
self.do_basic_lock(self.get_info_list([(offset2, length2)], remote), overlap=True) |
| 1120 |
|
| 1121 |
if offset2 > 0: |
| 1122 |
length2 = self.offset + 1 |
| 1123 |
self.test_group("Locking overlapping range from a second %s where end2 == start1 and start2 == 0" % pstr) |
| 1124 |
self.do_basic_lock(self.get_info_list([(0, length2)], remote), overlap=True) |
| 1125 |
|
| 1126 |
def otest08(self, remote=0): |
| 1127 |
"""Locking overlapping range from a second process where start2 == end1 |
| 1128 |
process1: |------------------| |
| 1129 |
process2: |------------------| |
| 1130 |
""" |
| 1131 |
pstr = "client" if remote else "process" |
| 1132 |
offset2 = self.offset + self.length - 1 |
| 1133 |
self.test_group("Locking overlapping range from a second %s where start2 == end1" % pstr) |
| 1134 |
self.do_basic_lock(self.get_info_list([(offset2, self.length)], remote), overlap=True) |
| 1135 |
|
| 1136 |
self.test_group("Locking overlapping range from a second %s where start2 == end1 and end2 == EOF" % pstr) |
| 1137 |
self.do_basic_lock(self.get_info_list([(offset2, 0)], remote), overlap=True) |
| 1138 |
|
| 1139 |
def otest09(self, remote=0): |
| 1140 |
"""Locking overlapping range from multiple processes where range2 and |
| 1141 |
range3 are entirely within range1 |
| 1142 |
process1: |-----------------------------| |
| 1143 |
process2: |--------| |
| 1144 |
process3: |--------| |
| 1145 |
""" |
| 1146 |
pstr = "clients" if remote else "processes" |
| 1147 |
offset2 = self.offset + int(self.length/4) |
| 1148 |
length2 = int(self.length/4) |
| 1149 |
lock_list = [ |
| 1150 |
(offset2, length2), |
| 1151 |
(offset2+length2, length2), |
| 1152 |
] |
| 1153 |
self.test_group("Locking overlapping range from multiple %s where " \ |
| 1154 |
"range2 and range3 are entirely within range1" % pstr) |
| 1155 |
info_list = self.get_info_list(lock_list, remote) |
| 1156 |
self.do_basic_lock(info_list, overlap=True) |
| 1157 |
|
| 1158 |
def nptest01_test(self): |
| 1159 |
"""Locking non-overlapping range from a second process where end2 < start1 |
| 1160 |
process1: |------------------| |
| 1161 |
process2: |--------| |
| 1162 |
""" |
| 1163 |
self.ntest01(remote=0) |
| 1164 |
|
| 1165 |
def nptest02_test(self): |
| 1166 |
"""Locking non-overlapping range from a second process where end2 == start1 - 1 |
| 1167 |
process1: |------------------| |
| 1168 |
process2: |------------------| |
| 1169 |
""" |
| 1170 |
self.ntest02(remote=0) |
| 1171 |
|
| 1172 |
def nptest03_test(self): |
| 1173 |
"""Locking non-overlapping range from a second process where start2 > end1 |
| 1174 |
process1: |------------------| |
| 1175 |
process2: |--------| |
| 1176 |
""" |
| 1177 |
self.ntest03(remote=0) |
| 1178 |
|
| 1179 |
def nptest04_test(self): |
| 1180 |
"""Locking non-overlapping range from a second process where start2 == end1 + 1 |
| 1181 |
process1: |------------------| |
| 1182 |
process2: |------------------| |
| 1183 |
""" |
| 1184 |
self.ntest04(remote=0) |
| 1185 |
|
| 1186 |
def nctest01_test(self): |
| 1187 |
"""Locking non-overlapping range from a second client where end2 < start1 |
| 1188 |
client1: |------------------| |
| 1189 |
client2: |--------| |
| 1190 |
""" |
| 1191 |
self.ntest01(remote=1) |
| 1192 |
|
| 1193 |
def nctest02_test(self): |
| 1194 |
"""Locking non-overlapping range from a second client where end2 == start1 - 1 |
| 1195 |
client1: |------------------| |
| 1196 |
client2: |------------------| |
| 1197 |
""" |
| 1198 |
self.ntest02(remote=1) |
| 1199 |
|
| 1200 |
def nctest03_test(self): |
| 1201 |
"""Locking non-overlapping range from a second client where start2 > end1 |
| 1202 |
client1: |------------------| |
| 1203 |
client2: |--------| |
| 1204 |
""" |
| 1205 |
self.ntest03(remote=1) |
| 1206 |
|
| 1207 |
def nctest04_test(self): |
| 1208 |
"""Locking non-overlapping range from a second client where start2 == end1 + 1 |
| 1209 |
client1: |------------------| |
| 1210 |
client2: |------------------| |
| 1211 |
""" |
| 1212 |
self.ntest04(remote=1) |
| 1213 |
|
| 1214 |
def optest01_test(self): |
| 1215 |
"""Locking same range from a second process |
| 1216 |
process1: |------------------| |
| 1217 |
process2: |------------------| |
| 1218 |
""" |
| 1219 |
self.otest01(remote=0) |
| 1220 |
|
| 1221 |
def optest02_test(self): |
| 1222 |
"""Locking overlapping range from a second process where start2 < start1 |
| 1223 |
process1: |------------------| |
| 1224 |
process2: |------------------| |
| 1225 |
""" |
| 1226 |
self.otest02(remote=0) |
| 1227 |
|
| 1228 |
def optest03_test(self): |
| 1229 |
"""Locking overlapping range from a second process where end2 > end1 |
| 1230 |
process1: |------------------| |
| 1231 |
process2: |------------------| |
| 1232 |
""" |
| 1233 |
self.otest03(remote=0) |
| 1234 |
|
| 1235 |
def optest04_test(self): |
| 1236 |
"""Locking overlapping range from a second process where range2 is entirely within range1 |
| 1237 |
process1: |------------------| |
| 1238 |
process2: |--------| |
| 1239 |
""" |
| 1240 |
self.otest04(remote=0) |
| 1241 |
|
| 1242 |
def optest05_test(self): |
| 1243 |
"""Locking overlapping range from a second process where range1 is entirely within range2 |
| 1244 |
process1: |------------------| |
| 1245 |
process2: |----------------------------| |
| 1246 |
""" |
| 1247 |
self.otest05(remote=0) |
| 1248 |
|
| 1249 |
def optest06_test(self): |
| 1250 |
"""Locking full file range from a second process""" |
| 1251 |
self.otest06(remote=0) |
| 1252 |
|
| 1253 |
def optest07_test(self): |
| 1254 |
"""Locking overlapping range from a second process where end2 == start1 |
| 1255 |
process1: |------------------| |
| 1256 |
process2: |------------------| |
| 1257 |
""" |
| 1258 |
self.otest07(remote=0) |
| 1259 |
|
| 1260 |
def optest08_test(self): |
| 1261 |
"""Locking overlapping range from a second process where start2 == end1 |
| 1262 |
process1: |------------------| |
| 1263 |
process2: |------------------| |
| 1264 |
""" |
| 1265 |
self.otest08(remote=0) |
| 1266 |
|
| 1267 |
def optest09_test(self): |
| 1268 |
"""Locking overlapping range from multiple processes where range2 and |
| 1269 |
range3 are entirely within range1 |
| 1270 |
process1: |-----------------------------| |
| 1271 |
process2: |--------| |
| 1272 |
process3: |--------| |
| 1273 |
""" |
| 1274 |
self.otest09(remote=0) |
| 1275 |
|
| 1276 |
def octest01_test(self): |
| 1277 |
"""Locking same range from a second client |
| 1278 |
client1: |------------------| |
| 1279 |
client2: |------------------| |
| 1280 |
""" |
| 1281 |
self.otest01(remote=1) |
| 1282 |
|
| 1283 |
def octest02_test(self): |
| 1284 |
"""Locking overlapping range from a second client where start2 < start1 |
| 1285 |
client1: |------------------| |
| 1286 |
client2: |------------------| |
| 1287 |
""" |
| 1288 |
self.otest02(remote=1) |
| 1289 |
|
| 1290 |
def octest03_test(self): |
| 1291 |
"""Locking overlapping range from a second client where end2 > end1 |
| 1292 |
client1: |------------------| |
| 1293 |
client2: |------------------| |
| 1294 |
""" |
| 1295 |
self.otest03(remote=1) |
| 1296 |
|
| 1297 |
def octest04_test(self): |
| 1298 |
"""Locking overlapping range from a second client where range2 is entirely within range1 |
| 1299 |
client1: |------------------| |
| 1300 |
client2: |--------| |
| 1301 |
""" |
| 1302 |
self.otest04(remote=1) |
| 1303 |
|
| 1304 |
def octest05_test(self): |
| 1305 |
"""Locking overlapping range from a second client where range1 is entirely within range2 |
| 1306 |
client1: |------------------| |
| 1307 |
client2: |----------------------------| |
| 1308 |
""" |
| 1309 |
self.otest05(remote=1) |
| 1310 |
|
| 1311 |
def octest06_test(self): |
| 1312 |
"""Locking full file range from a second client""" |
| 1313 |
self.otest06(remote=1) |
| 1314 |
|
| 1315 |
def octest07_test(self): |
| 1316 |
"""Locking overlapping range from a second client where end2 == start1 |
| 1317 |
client1: |------------------| |
| 1318 |
client2: |------------------| |
| 1319 |
""" |
| 1320 |
self.otest07(remote=1) |
| 1321 |
|
| 1322 |
def octest08_test(self): |
| 1323 |
"""Locking overlapping range from a second client where start2 == end1 |
| 1324 |
client1: |------------------| |
| 1325 |
client2: |------------------| |
| 1326 |
""" |
| 1327 |
self.otest08(remote=1) |
| 1328 |
|
| 1329 |
def octest09_test(self): |
| 1330 |
"""Locking overlapping range from multiple clients where range2 and |
| 1331 |
range3 are entirely within range1 |
| 1332 |
client1: |-----------------------------| |
| 1333 |
client2: |--------| |
| 1334 |
client3: |--------| |
| 1335 |
""" |
| 1336 |
self.otest09(remote=1) |
| 1337 |
|
| 1338 |
################################################################################ |
| 1339 |
# Entry point |
| 1340 |
x = LockTest(usage=USAGE, testnames=TESTNAMES, testgroups=TESTGROUPS, sid=SCRIPT_ID) |
| 1341 |
|
| 1342 |
try: |
| 1343 |
# Call setup |
| 1344 |
x.lock_setup(nfiles=1) |
| 1345 |
|
| 1346 |
# Run all the tests |
| 1347 |
x.run_tests() |
| 1348 |
except Exception: |
| 1349 |
x.test(False, traceback.format_exc()) |
| 1350 |
finally: |
| 1351 |
x.cleanup() |
| 1352 |
x.exit() |