Debian Codegraph @

test/nfstest_sparse

raw
1 #!/usr/bin/env python3
2 #===============================================================================
3 # Copyright 2015 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 errno
17 import struct
18 import traceback
19 import nfstest_config as c
20 from nfstest.utils import *
21 from packet.nfs.nfs4_const import *
22 from nfstest.test_util import TestUtil
23 from fcntl import fcntl,F_RDLCK,F_SETLK
24
25 # Module constants
26 __author__ = "Jorge Mora (%s)" % c.NFSTEST_AUTHOR_EMAIL
27 __copyright__ = "Copyright (C) 2015 NetApp, Inc."
28 __license__ = "GPL v2"
29 __version__ = "1.0"
30
31 USAGE = """%prog --server <server> [options]
32
33 Sparse file tests
34 =================
35 Verify correct functionality of sparse files. These are files which
36 have unallocated or uninitialized data blocks as holes. The new NFSv4.2
37 operation SEEK is used to search for the next hole or data segment
38 in a file.
39
40 Basic tests verify the SEEK operation returns the correct offset of the
41 next hole or data with respect to the starting offset given to the seek
42 system call. Verify the SEEK operation is sent to the server with the
43 correct stateid as a READ call. All files have a virtual hole at the
44 end of the file so when searching for the next hole, even if the file
45 does not have a hole, it returns the size of the file.
46
47 Some tests include testing at the protocol level by taking a packet
48 trace and inspecting the actual packets sent to the server.
49
50 Negative tests include trying to SEEK starting from an offset beyond
51 the end of the file.
52
53 Examples:
54 The only required option is --server
55 $ %prog --server 192.168.0.11
56
57 Notes:
58 The user id in the local host must have access to run commands as root
59 using the 'sudo' command without the need for a password.
60
61 Valid only for NFS version 4.2 and above."""
62
63 # Test script ID
64 SCRIPT_ID = "SPARSE"
65
66 SEEK_TESTS = [
67 "seek01",
68 "seek02",
69 "seek03",
70 "seek04",
71 ]
72
73 # Include the test groups in the list of test names
74 # so they are displayed in the help
75 TESTNAMES = ["seek"] + SEEK_TESTS
76
77 TESTGROUPS = {
78 "seek": {
79 "tests": SEEK_TESTS,
80 "desc": "Run all SEEK tests: ",
81 },
82 }
83
84 def getlock(fd, lock_type, offset=0, length=0):
85 """Get byte range lock on file given by file descriptor"""
86 lockdata = struct.pack('hhllhh', lock_type, 0, offset, length, 0, 0)
87 out = fcntl(fd, F_SETLK, lockdata)
88 return struct.unpack('hhllhh', out)
89
90 class SparseTest(TestUtil):
91 """SparseTest object
92
93 SparseTest() -> New test object
94
95 Usage:
96 x = SparseTest(testnames=['seek01', 'seek02', ...])
97
98 # Run all the tests
99 x.run_tests()
100 x.exit()
101 """
102 def __init__(self, **kwargs):
103 """Constructor
104
105 Initialize object's private data.
106 """
107 TestUtil.__init__(self, **kwargs)
108 self.opts.version = "%prog " + __version__
109 # Tests are valid for NFSv4.2 and beyond
110 self.opts.set_defaults(nfsversion=4.2)
111 self.scan_options()
112
113 # Disable createtraces option
114 self.createtraces = False
115
116 def setup(self, **kwargs):
117 """Setup test environment"""
118 self.umount()
119 self.trace_start()
120 self.mount()
121 # Get block size for mounted volume
122 self.statvfs = os.statvfs(self.mtdir)
123 super(SparseTest, self).setup(**kwargs)
124
125 # Sparse file definition
126 self.sparsesize = 5 * self.filesize
127 uargs = {
128 "size" : self.sparsesize,
129 "hole_list" : [self.filesize, 3*self.filesize],
130 "hole_size" : self.filesize,
131 "ftype" : FTYPE_SP_DEALLOC,
132 }
133
134 # Create sparse file where it starts and ends with data
135 self.create_file(**uargs)
136 # Create sparse file where it starts and ends with a hole
137 uargs["hole_list"] = [0, 2*self.filesize, 4*self.filesize]
138 self.create_file(**uargs)
139 self.umount()
140 self.trace_stop()
141
142 def test_seek(self, fd, whence, msg=""):
143 """Verify SEEK succeeds searching for the next data or hole
144
145 fd:
146 File descriptor for opened file
147 whence:
148 Search for data when using SEEK_DATA or a hole using SEEK_HOLE
149 msg:
150 String to identify the specific test running and it is appended
151 to the main assertion message [default: ""]
152 """
153 file_size = self.sfile.filesize
154
155 if whence == SEEK_DATA:
156 segstr = "data"
157 what = NFS4_CONTENT_DATA
158 # Append offset for last byte on file to test limit condition
159 offset_list = self.sfile.data_offsets + [file_size-1]
160 else:
161 segstr = "hole"
162 what = NFS4_CONTENT_HOLE
163 offset_list = self.sfile.hole_offsets
164 if self.sfile.endhole:
165 # Append offset so the last byte offset is used
166 offset_list += [file_size-1]
167 else:
168 # Append offset so the last byte offset is used
169 # but expecting the implicit hole at the end of the file
170 offset_list += [file_size]
171
172 eof = 0
173 offset = 0
174 for doffset in offset_list:
175 try:
176 self.test_info("==== %s test %02d %s%s" % (self.testname, self.testidx, SEEKmap[whence], msg))
177 self.testidx += 1
178 self.trace_start()
179 fmsg = ""
180 werrno = 0
181 seek_offset = offset
182 self.dprint('DBG3', "SEEK using %s on file %s starting at offset %d" % (SEEKmap[whence], self.absfile, offset))
183 offset = os.lseek(fd, offset, whence)
184 self.dprint('INFO', "SEEK returned offset %d" % offset)
185 if offset == file_size:
186 # Hole was not found
187 eof = 1
188 except OSError as werror:
189 werrno = werror.errno
190 fmsg = ", got error [%s] %s" % (errno.errorcode.get(werrno, werrno), os.strerror(werrno))
191 finally:
192 self.trace_stop()
193
194 if whence == SEEK_DATA and self.sfile.endhole and doffset == offset_list[-1]:
195 # Looking for data starting on a hole which is the end of the file
196 fmsg = ", expecting ENXIO but it succeeded"
197 expr = werrno == errno.ENXIO
198 tmsg = "SEEK should fail with ENXIO searching for the next %s when file ends in a hole" % segstr
199 self.test(expr, tmsg+msg, failmsg=fmsg)
200 self.set_nfserr_list(nfs4list=[NFS4ERR_NOENT, NFS4ERR_NXIO])
201 else:
202 tmsg = "SEEK should succeed searching for the next %s" % segstr
203 self.test(werrno == 0, tmsg+msg, failmsg=fmsg)
204
205 if werrno == 0:
206 fmsg = ", expecting offset %d but got %d" % (doffset, offset)
207 if whence == SEEK_HOLE and offset == file_size:
208 # Found the implicit hole at the end of the file
209 tmsg = "SEEK should return the size of the file when the next hole is not found"
210 else:
211 tmsg = "SEEK should return correct offset when the next %s is found" % segstr
212 self.test(offset == doffset, tmsg+msg, failmsg=fmsg)
213 offset += self.filesize
214 if offset >= file_size:
215 # Use the offset exactly on the last byte of the file
216 offset = file_size - 1
217
218 self.trace_open()
219 (pktcall, pktreply) = self.find_nfs_op(OP_SEEK, status=None, last_call=True)
220 self.dprint('DBG7', str(pktcall))
221 self.dprint('DBG7', str(pktreply))
222 self.test(pktcall, "SEEK should be sent to the server")
223 if pktcall is None:
224 return
225 seekobj = pktcall.NFSop
226 fmsg = ", expecting %s but got %s" % (self.stid_str(self.stateid), self.stid_str(seekobj.stateid.other))
227 self.test(seekobj.stateid == self.stateid, "SEEK should be sent with correct stateid", failmsg=fmsg)
228 fmsg = ", expecting %d but got %d" % (seek_offset, seekobj.offset)
229 self.test(seekobj.offset == seek_offset, "SEEK should be sent with correct offset", failmsg=fmsg)
230 fmsg = ", expecting %s but got %s" % (data_content4.get(what,what), seekobj.offset)
231 self.test(seekobj.what == what, "SEEK should be sent with %s" % seekobj.what, failmsg=fmsg)
232
233 self.test(pktreply, "SEEK should be sent to the client")
234 if pktreply is None:
235 return
236
237 if whence == SEEK_DATA and self.sfile.endhole and doffset == offset_list[-1]:
238 fmsg = ", expecting NFS4ERR_NXIO but got %s" % pktreply.nfs.status
239 self.test(pktreply and pktreply.nfs.status == NFS4ERR_NXIO, "SEEK should return NFS4ERR_NXIO", failmsg=fmsg)
240 else:
241 fmsg = ", got %s" % pktreply.nfs.status
242 self.test(pktreply and pktreply.nfs.status == 0, "SEEK should return NFS4_OK", failmsg=fmsg)
243 if pktreply and pktreply.nfs.status == 0:
244 idx = pktcall.NFSidx
245 rseekobj = pktreply.nfs.array[idx]
246 fmsg = ", expecting %d but got %d" % (doffset, rseekobj.offset)
247 self.test(rseekobj.offset == doffset, "SEEK should return the correct offset", failmsg=fmsg)
248 fmsg = ", but got %s" % rseekobj.eof
249 self.test(rseekobj.eof == eof, "SEEK should return eof as %s" % nfs_bool[eof], failmsg=fmsg)
250
251 def seek01(self, whence, lock=False):
252 """Verify SEEK succeeds searching for the next data or hole
253
254 whence:
255 Search for data when using SEEK_DATA or a hole using SEEK_HOLE
256 lock:
257 Lock file before seeking for the data or hole [default: False]
258 """
259 for sparseidx in [0,1]:
260 try:
261 fd = None
262 msg = ""
263 if lock:
264 msg = " (locking file)"
265 if sparseidx == 0:
266 self.test_info("<<<<<<<<<< Using sparse file starting and ending with data%s >>>>>>>>>>" % msg)
267 else:
268 self.test_info("<<<<<<<<<< Using sparse file starting and ending with hole%s >>>>>>>>>>" % msg)
269
270 self.umount()
271 self.trace_start()
272 self.mount()
273
274 self.sfile = self.sparse_files[sparseidx]
275 self.absfile = self.sfile.absfile
276 self.filename = self.sfile.filename
277 self.dprint('DBG3', "Open file %s for reading" % self.absfile)
278 fd = os.open(self.absfile, os.O_RDONLY)
279
280 if lock:
281 self.dprint('DBG3', "Lock file %s" % self.absfile)
282 out = getlock(fd, F_RDLCK)
283
284 self.trace_stop()
285 self.trace_open()
286 self.get_stateid(self.filename)
287 if self.deleg_stateid is not None:
288 self._deleg_granted = True
289
290 # Search for the data/hole segments
291 self.test_seek(fd, whence, msg)
292 except Exception:
293 self.test(False, traceback.format_exc())
294 finally:
295 if fd:
296 os.close(fd)
297 self.umount()
298
299 def seek01_test(self):
300 """Verify SEEK succeeds searching for the next data"""
301 self.test_group("Verify SEEK succeeds searching for the next data")
302 self.testidx = 1
303 self._deleg_granted = False
304 self.seek01(SEEK_DATA)
305
306 if not self._deleg_granted:
307 # Run tests with byte range locking
308 self.seek01(SEEK_DATA, lock=True)
309
310 def seek02_test(self):
311 """Verify SEEK succeeds searching for the next hole"""
312 self.test_group("Verify SEEK succeeds searching for the next hole")
313 self.testidx = 1
314 self._deleg_granted = False
315 self.seek01(SEEK_HOLE)
316
317 if not self._deleg_granted:
318 # Run tests with byte range locking
319 self.seek01(SEEK_HOLE, lock=True)
320
321 def seek03(self, whence, offset, sparseidx=0, lock=False, msg=""):
322 """Verify SEEK fails with ENXIO when offset is beyond the end of the file
323
324 whence:
325 Search for data when using SEEK_DATA or a hole using SEEK_HOLE
326 offset:
327 Search for data or hole starting from this offset
328 sparseidx:
329 Index of the sparse file to use for the testing [default: 0]
330 lock:
331 Lock file before seeking for the data or hole [default: False]
332 msg:
333 String to identify the specific test running and it is appended
334 to the main assertion message [default: ""]
335 """
336 try:
337 fd = None
338 self.test_info("==== %s test %02d %s%s" % (self.testname, self.testidx, SEEKmap[whence], msg))
339 self.testidx += 1
340
341 self.umount()
342 self.trace_start()
343 self.mount()
344
345 # Use sparse file given by the index
346 sfile = self.sparse_files[sparseidx]
347 absfile = sfile.absfile
348 filesize = sfile.filesize
349
350 if whence == SEEK_DATA:
351 segstr = "data segment"
352 smsg = "using SEEK_DATA"
353 what = NFS4_CONTENT_DATA
354 else:
355 segstr = "hole"
356 smsg = "using SEEK_HOLE"
357 what = NFS4_CONTENT_HOLE
358
359 if offset < filesize:
360 smsg += " when offset is in the middle of last hole"
361 elif offset == filesize:
362 smsg += " when offset equals to the file size"
363 else:
364 smsg += " when offset is beyond the end of the file"
365
366 self.dprint('DBG3', "Open file %s for reading" % absfile)
367 fd = os.open(absfile, os.O_RDONLY)
368
369 if lock:
370 self.dprint('DBG3', "Lock file %s" % self.absfile)
371 out = getlock(fd, F_RDLCK)
372
373 self.dprint('DBG3', "Search for the next %s on file %s starting at offset %d" % (segstr, absfile, offset))
374 try:
375 werrno = 0
376 fmsg = ", expecting ENXIO but it succeeded"
377 o_offset = os.lseek(fd, offset, whence)
378 self.dprint('DBG5', "SEEK returned offset %d" % o_offset)
379 except OSError as werror:
380 werrno = werror.errno
381 fmsg = ", expecting ENXIO but got %s" % errno.errorcode.get(werrno, werrno)
382 expr = werrno == errno.ENXIO
383 tmsg = "SEEK system call should fail with ENXIO %s" % smsg
384 self.test(expr, tmsg+msg, failmsg=fmsg)
385 except Exception:
386 self.test(False, traceback.format_exc())
387 finally:
388 if fd:
389 os.close(fd)
390 self.umount()
391 self.trace_stop()
392
393 try:
394 self.set_nfserr_list(nfs4list=[NFS4ERR_NOENT, NFS4ERR_NXIO])
395 self.trace_open()
396 self.get_stateid(sfile.filename)
397 (pktcall, pktreply) = self.find_nfs_op(OP_SEEK, status=None)
398 self.dprint('DBG7', str(pktcall))
399 self.dprint('DBG7', str(pktreply))
400 if pktcall:
401 seekobj = pktcall.NFSop
402 fmsg = ", expecting %s but got %s" % (self.stid_str(self.stateid), self.stid_str(seekobj.stateid.other))
403 self.test(seekobj.stateid == self.stateid, "SEEK should be sent with correct stateid", failmsg=fmsg)
404 fmsg = ", expecting %d but got %d" % (offset, seekobj.offset)
405 self.test(seekobj.offset == offset, "SEEK should be sent with correct offset", failmsg=fmsg)
406 fmsg = ", expecting %s but got %s" % (data_content4.get(what,what), seekobj.offset)
407 self.test(seekobj.what == what, "SEEK should be sent with %s" % seekobj.what, failmsg=fmsg)
408 else:
409 self.test(False, "SEEK packet call was not found")
410 if not pktreply:
411 self.test(False, "SEEK packet reply was not found")
412 if pktcall and pktreply:
413 idx = pktcall.NFSidx
414 status = pktreply.nfs.array[idx].status
415 expr = status == NFS4ERR_NXIO
416 fmsg = ", expecting NFS4ERR_NXIO but got %s" % nfsstat4.get(status, status)
417 tmsg = "SEEK should fail with NFS4ERR_NXIO %s" % smsg
418 self.test(expr, tmsg+msg, failmsg=fmsg)
419 except Exception:
420 self.test(False, traceback.format_exc())
421
422 def seek03_test(self):
423 """Verify SEEK searching for next data fails with ENXIO when offset is beyond the end of the file"""
424 self.test_group("Verify SEEK searching for next data fails with ENXIO when offset is beyond the end of the file")
425 self.testidx = 1
426 self.seek03(SEEK_DATA, self.sparsesize-2, 1)
427 self.seek03(SEEK_DATA, self.sparsesize, 1)
428 self.seek03(SEEK_DATA, 2*self.sparsesize, 1)
429
430 if self.deleg_stateid is None:
431 # Run tests with byte range locking
432 msg = " (locking file)"
433 self.seek03(SEEK_DATA, self.sparsesize-2, 1, lock=True, msg=msg)
434 self.seek03(SEEK_DATA, self.sparsesize, 1, lock=True, msg=msg)
435 self.seek03(SEEK_DATA, 2*self.sparsesize, 1, lock=True, msg=msg)
436
437 def seek04_test(self):
438 """Verify SEEK searching for next hole fails with ENXIO when offset is beyond the end of the file"""
439 self.test_group("Verify SEEK searching for next hole fails with ENXIO when offset is beyond the end of the file")
440 self.testidx = 1
441 self.seek03(SEEK_HOLE, self.sparsesize, 1)
442 self.seek03(SEEK_HOLE, 2*self.sparsesize, 1)
443
444 if self.deleg_stateid is None:
445 # Run tests with byte range locking
446 msg = " (locking file)"
447 self.seek03(SEEK_HOLE, self.sparsesize, 1, lock=True, msg=msg)
448 self.seek03(SEEK_HOLE, 2*self.sparsesize, 1, lock=True, msg=msg)
449
450 ################################################################################
451 # Entry point
452 x = SparseTest(usage=USAGE, testnames=TESTNAMES, testgroups=TESTGROUPS, sid=SCRIPT_ID)
453
454 try:
455 x.setup(nfiles=1)
456
457 # Run all the tests
458 x.run_tests()
459 except Exception:
460 x.test(False, traceback.format_exc())
461 finally:
462 x.cleanup()
463 x.exit()