Debian Codegraph @

test/nfstest_cache

raw
1 #!/usr/bin/env python3
2 #===============================================================================
3 # Copyright 2012 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 traceback
17 import nfstest_config as c
18 from time import time, sleep
19 from nfstest.test_util import TestUtil
20
21 # Module constants
22 __author__ = "Jorge Mora (%s)" % c.NFSTEST_AUTHOR_EMAIL
23 __copyright__ = "Copyright (C) 2012 NetApp, Inc."
24 __license__ = "GPL v2"
25 __version__ = "1.0.1"
26
27 USAGE = """%prog --server <server> --client <client> [options]
28
29 NFS client side caching tests
30 =============================
31 Verify consistency of attribute caching by varying acregmin, acregmax,
32 acdirmin, acdirmax and actimo. Verify consistency of data caching by varying
33 acregmin, acregmax, acdirmin, acdirmax and actimo.
34
35 Valid for any version of NFS.
36
37 Examples:
38 Required options are --server and --client
39 $ %prog --server 192.168.0.11 --client 192.168.0.20
40
41 Testing with different values of --acmin and --acmax (this takes a long time)
42 $ %prog --server 192.168.0.11 --client 192.168.0.20 --acmin 10,20 --acmax 20,30,60,80
43
44 Notes:
45 The user id in the local host and the host specified by --client must
46 have access to run commands as root using the 'sudo' command without
47 the need for a password.
48
49 The user id must be able to 'ssh' to remote host without the need for
50 a password."""
51
52 # Test script ID
53 SCRIPT_ID = "CACHE"
54
55 TESTNAMES = [
56 'acregmin_attr',
57 'acregmax_attr',
58 'acdirmin_attr',
59 'acdirmax_attr',
60 'actimeo_attr',
61 'acregmin_data',
62 'acregmax_data',
63 'acdirmin_data',
64 'acdirmax_data',
65 'actimeo_data',
66 ]
67
68 class CacheTest(TestUtil):
69 """CacheTest object
70
71 CacheTest() -> New test object
72
73 Usage:
74 x = CacheTest(testnames=['acregmin_attr', 'acregmax_attr', ...])
75
76 # Run all the tests
77 x.run_tests()
78 x.exit()
79 """
80 def __init__(self, **kwargs):
81 """Constructor
82
83 Initialize object's private data.
84 """
85 TestUtil.__init__(self, **kwargs)
86 self.opts.version = "%prog " + __version__
87 self.opts.set_defaults(filesize=32)
88 hmsg = "Remote NFS client"
89 self.test_opgroup.add_option("--client", help=hmsg)
90 hmsg = "Time in seconds to test a condition just before it is " + \
91 "expected to come true [default: %default]"
92 self.test_opgroup.add_option("--justbefore", type="int", default=2, help=hmsg)
93 hmsg = "Comma separated values to use for " + \
94 "acregmin/acdirmin/actimeo [default: %default]"
95 self.test_opgroup.add_option("--acmin", default='10', help=hmsg)
96 hmsg = "Comma separated values to use for acregmax/acdirmax, " + \
97 "first value of acmin will be used as acregmin/acdirmin " + \
98 "[default: %default]"
99 self.test_opgroup.add_option("--acmax", default='20', help=hmsg)
100 self.scan_options()
101
102 if not self.client:
103 self.opts.error("client option is required")
104
105 self.create_host(self.client)
106
107 # Process --acmin option
108 self.acminlist = self.str_list(self.acmin, int)
109 if self.acminlist is None:
110 self.opts.error("invalid value given --acmin=%s" % self.acmin)
111
112 # Process --acmax option
113 self.acmaxlist = self.str_list(self.acmax, int)
114 if self.acmaxlist is None:
115 self.opts.error("invalid value given --acmax=%s" % self.acmax)
116
117 def file_test(self, data_cache, fd, data, size, atime=0, inwire=False):
118 """Evaluate attribute/data caching test on a file.
119
120 data_cache:
121 Test data caching if true, otherwise test attribute caching
122 fd:
123 Opened file descriptor of file
124 data:
125 Expected data
126 size:
127 Expected file size
128 atime:
129 Time in seconds after the file is changed [default: 0]
130 inwire:
131 The data/attribute should go to the server if true.
132 [default: False(from cache)]
133
134 Return True if test passed.
135 """
136 if data_cache:
137 rdata = fd.read()
138 fd.seek(0)
139 cache_str = "Read file"
140 test_expr = rdata == data
141 else:
142 fstat = os.stat(self.absfile)
143 rdata = "size %d" % fstat.st_size
144 cache_str = "Get file size"
145 test_expr = fstat.st_size == size
146
147 inwire_str = "should go to server" if inwire else "still from cache"
148 self.dprint('DBG3', "%s at %f secs after change -- %s [%s]" % (cache_str, atime, inwire_str, rdata))
149 return test_expr
150
151 def do_file_test(self, acregmin=None, acregmax=None, actimeo=None, data_cache=False):
152 """Test attribute or data caching on a file.
153 Option actimeo and (acregmin[,acregmax]) are mutually exclusive.
154
155 acregmin:
156 Value of acregmin in seconds
157 acregmax:
158 Value of acregmax in seconds
159 actimeo:
160 Value of actimeo in seconds
161 data_cache:
162 Test data caching if true, otherwise test attribute caching
163 """
164 try:
165 fd = None
166 attr = 'data' if data_cache else 'attribute'
167 header = "Verify consistency of %s caching with %s on a file" % (attr, self.nfsstr())
168 # Mount options
169 mtopts = "hard,intr,rsize=4096,wsize=4096"
170 if actimeo:
171 header += " actimeo = %d" % actimeo
172 mtopts += ",actimeo=%d" % actimeo
173 acstr = "actimeo"
174 else:
175 if acregmin:
176 header += " acregmin = %d" % acregmin
177 mtopts += ",acregmin=%d" % acregmin
178 acstr = "acregmin"
179 actimeo = acregmin
180 if acregmax:
181 header += " acregmax = %d" % acregmax
182 mtopts += ",acregmax=%d" % acregmax
183 acstr = "acregmax"
184 actimeo = acregmax
185
186 self.test_group(header)
187
188 # Unmount server on local client
189 self.umount()
190
191 # Mount server on local client
192 self.mount(mtopts=mtopts)
193
194 # Create test file
195 self.get_filename()
196 data = 'ABCDE'
197 dlen = len(data)
198 self.dprint('DBG3', "Creating file [%s] %d@0" % (self.absfile, dlen))
199 fdw = open(self.absfile, "w")
200 fdw.write(data)
201 fdw.close()
202
203 if data_cache:
204 # Open file for reading
205 self.dprint('DBG3', "Open file - read into cache")
206 fd = open(self.absfile, "r")
207 # Read into cache
208 fd.read()
209 fd.seek(0)
210 cache_str = 'data'
211 else:
212 cache_str = 'size'
213
214 # Verify size of test file is 0
215 fstat = os.stat(self.absfile)
216 if fstat.st_size != dlen:
217 raise Exception("Size of newly created file is %d, should have been %d" %(fstat.st_size, dlen))
218
219 if acregmax:
220 # Stat the unchanging file until acregmax is hit
221 # each stat doubles the valid cache time
222 # start with acregmin
223 sleeptime = acregmin
224 while sleeptime <= acregmax:
225 self.dprint('DBG3', "Sleeping for %f secs" % sleeptime)
226 sleep(sleeptime)
227 fstat = os.stat(self.absfile)
228 if fstat.st_size != dlen:
229 raise Exception("Size of file is %d, should have been %d" %(fstat.st_size, dlen))
230 sleeptime = sleeptime + sleeptime
231
232 data1 = 'abcde'
233 dlen1 = len(data1)
234 stime = time()
235 self.dprint('DBG3', "Change file %s from remote client" % cache_str)
236 self.clientobj.run_cmd('echo -n %s >> %s' % (data1, self.absfile))
237
238 # Still from cache so no change
239 test_expr = self.file_test(data_cache, fd, data, dlen)
240 self.test(test_expr, "File %s should have not changed at t=0" % cache_str)
241
242 # Compensate for time delays to make sure file stat is done
243 # at (acregmin|acregmax|actimeo - justbefore)
244 dtime = time() - stime
245 sleeptime = actimeo - self.justbefore - dtime
246 # XXX FIXME check sleeptime for negative values
247
248 # Sleep to just before acregmin/acregmax/actimeo
249 self.dprint('DBG3', "Sleeping for %f secs, just before %s" % (sleeptime, acstr))
250 sleep(sleeptime)
251
252 # Still from cache so no change
253 test_expr = self.file_test(data_cache, fd, data, dlen, atime=(time()-stime))
254 self.test(test_expr, "File %s should have not changed just before %s" % (cache_str, acstr))
255
256 # Sleep just past acregmin/acregmax/actimeo
257 self.dprint('DBG3', "Sleeping for %f secs, just past %s" % (self.justbefore, acstr))
258 sleep(self.justbefore)
259
260 # Should go to server
261 test_expr = self.file_test(data_cache, fd, data+data1, dlen+dlen1, atime=(time()-stime), inwire=True)
262 self.test(test_expr, "File %s should have changed just after %s" % (cache_str, acstr))
263
264 if acregmax:
265 stime = time()
266 self.dprint('DBG3', "Change file %s again from remote client -- cache timeout should be back to acregmin" % cache_str)
267 self.clientobj.run_cmd('echo -n %s >> %s' % (data, self.absfile))
268
269 # Cache timeout should be back to acregmin
270 # Wait until just before acregmin
271 dtime = time() - stime
272 sleeptime = acregmin - self.justbefore - dtime
273 self.dprint('DBG3', "Sleeping for %f secs, just before acregmin" % int(sleeptime))
274 sleep(sleeptime)
275
276 # Still from cache so no change
277 test_expr = self.file_test(data_cache, fd, data+data1, dlen+dlen1, atime=(time()-stime))
278 self.test(test_expr, "File %s should have not changed just before acregmin" % cache_str)
279
280 # Go just past acregmin
281 self.dprint('DBG3', "Sleeping for %f secs, just past acregmin" % self.justbefore)
282 sleep(self.justbefore)
283
284 # Should go to server
285 test_expr = self.file_test(data_cache, fd, data+data1+data, 2*dlen+dlen1, atime=(time()-stime), inwire=True)
286 self.test(test_expr, "File %s should have changed just after acregmin" % cache_str)
287 except Exception:
288 self.test(False, traceback.format_exc())
289 finally:
290 if fd:
291 fd.close()
292
293 def dir_test(self, data_cache, dirlist, nlink, atime=0, inwire=False):
294 """Evaluate attribute/data caching test on a directory.
295
296 data_cache:
297 Test data caching if true, otherwise test attribute caching
298 dirlist:
299 Expected directory data
300 nlink:
301 Expected number of hard links in directory
302 atime:
303 Time in seconds after the directory is changed [default: 0]
304 inwire:
305 The data/attribute should go to the server if true.
306 [default: False(from cache)]
307
308 Return True if test passed.
309 """
310 if data_cache:
311 rdata = os.listdir(self.testdir)
312 cache_str = "directory listing"
313 test_expr = set(rdata) == set(dirlist)
314 else:
315 fstat = os.stat(self.testdir)
316 rdata = "[nlink %d]" % fstat.st_nlink
317 cache_str = "hard link count"
318 test_expr = fstat.st_nlink == nlink
319
320 inwire_str = "should go to server" if inwire else "still from cache"
321 self.dprint('DBG3', "Get %s at %f secs after change -- %s %s" % (cache_str, atime, inwire_str, rdata))
322 return test_expr
323
324 def do_dir_test(self, acdirmin=None, acdirmax=None, actimeo=None, data_cache=False):
325 """Test attribute or data caching on a directory.
326 Option actimeo and (acdirmin[,acdirmax]) are mutually exclusive.
327
328 acdirmin:
329 Value of acdirmin in seconds
330 acdirmax:
331 Value of acdirmax in seconds
332 actimeo:
333 Value of actimeo in seconds
334 data_cache:
335 Test data caching if true, otherwise test attribute caching
336 """
337 try:
338 attr = 'data' if data_cache else 'attribute'
339 header = "Verify consistency of %s caching with %s on a directory" % (attr, self.nfsstr())
340 # Mount options
341 mtopts = "hard,intr,rsize=4096,wsize=4096"
342 if actimeo:
343 header += " actimeo = %d" % actimeo
344 mtopts += ",actimeo=%d" % actimeo
345 acstr = "actimeo"
346 else:
347 if acdirmin:
348 header += " acdirmin = %d" % acdirmin
349 mtopts += ",acdirmin=%d" % acdirmin
350 acstr = "acdirmin"
351 actimeo = acdirmin
352 if acdirmax:
353 header += " acdirmax = %d" % acdirmax
354 mtopts += ",acdirmax=%d" % acdirmax
355 acstr = "acdirmax"
356 actimeo = acdirmax
357
358 self.test_group(header)
359
360 # Unmount server on local client
361 self.umount()
362
363 # Mount server on local client
364 self.mount(mtopts=mtopts)
365
366 # Get a unique directory name
367 dirname = self.get_dirname()
368 self.testdir = self.absdir
369 self.dprint('DBG3', "Creating directory [%s]" % self.testdir)
370 os.mkdir(self.testdir, 0o777)
371
372 self.get_dirname(dir=dirname)
373 self.dprint('DBG3', "Creating directory [%s]" % self.absdir)
374 os.mkdir(self.absdir, 0o777)
375
376 if data_cache:
377 cache_str = "directory listing"
378 else:
379 cache_str = "hard link count"
380
381 # Get number of hard links on newly created directory
382 fstat = os.stat(self.testdir)
383 nlink = fstat.st_nlink
384 # Get list of directories on newly created directory
385 dirlist = os.listdir(self.testdir)
386
387 if acdirmax:
388 # Stat the unchanging directory
389 # each ls doubles the valid cache time
390 # start with acdirmin
391 sleeptime = acdirmin
392 while sleeptime <= acdirmax:
393 self.dprint('DBG3', "Sleeping for %f secs" % sleeptime)
394 sleep(sleeptime)
395 fstat = os.stat(self.testdir)
396 if fstat.st_nlink != nlink:
397 raise Exception("Hard link count of directory is %d, should have been %d" %(fstat.st_nlink, nlink))
398 sleeptime = sleeptime + sleeptime
399
400 # Increase the hard link count by creating a sub-directory
401 stime = time()
402 dirname2 = self.get_dirname(dir=dirname)
403 self.dprint('DBG3', "Creating directory [%s] from remote client" % self.absdir)
404 self.clientobj.run_cmd('mkdir ' + self.absdir)
405
406 # Still from cache so no change
407 test_expr = self.dir_test(data_cache, dirlist, nlink)
408 self.test(test_expr, "%s should have not changed at t=0" % cache_str.capitalize())
409
410 # Compensate for time delays to make sure directory stat is done
411 # at (acdirmin|acdirmax|actimeo - justbefore)
412 dtime = time() - stime
413 sleeptime = actimeo - self.justbefore - dtime
414
415 # Sleep to just before acdirmin/acdirmax/actimeo
416 self.dprint('DBG3', "Sleeping for %f secs, just before %s" % (int(sleeptime), acstr))
417 sleep(sleeptime)
418
419 # Still from cache so no change
420 test_expr = self.dir_test(data_cache, dirlist, nlink, atime=(time()-stime))
421 self.test(test_expr, "%s should have not changed just before %s" % (cache_str.capitalize(), acstr))
422
423 # Sleep just past acdirmin/acdirmax/actimeo
424 self.dprint('DBG3', "Sleeping for %f secs, just past %s" % (self.justbefore, acstr))
425 sleep(self.justbefore)
426
427 # Should go to server
428 dirlist.append(dirname2)
429 test_expr = self.dir_test(data_cache, dirlist, nlink+1, atime=(time()-stime), inwire=True)
430 self.test(test_expr, "%s should have changed just after %s" % (cache_str.capitalize(), acstr))
431 nlink += 1
432
433 if acdirmax:
434 # Increase the hard link count by creating another sub-directory
435 stime = time()
436 dirname3 = self.get_dirname(dir=dirname)
437 self.dprint('DBG3', "Creating directory [%s] from remote client" % self.absdir)
438 self.clientobj.run_cmd('mkdir ' + self.absdir)
439
440 # Cache timeout should be back to acdirmin
441 # Wait until just before acdirmin
442 dtime = time() - stime
443 sleeptime = acdirmin - self.justbefore - dtime
444 self.dprint('DBG3', "Sleeping for %f secs, just before acdirmin" % int(sleeptime))
445 sleep(sleeptime)
446
447 # Still from cache so no change
448 test_expr = self.dir_test(data_cache, dirlist, nlink, atime=(time()-stime))
449 self.test(test_expr, "%s should have not changed just before acdirmin" % cache_str.capitalize())
450
451 # Go just past acdirmin
452 self.dprint('DBG3', "Sleeping for %f secs, just past acdirmin" % self.justbefore)
453 sleep(self.justbefore)
454
455 # Should go to server
456 dirlist.append(dirname3)
457 test_expr = self.dir_test(data_cache, dirlist, nlink+1, atime=(time()-stime), inwire=True)
458 self.test(test_expr, "%s should have changed just after acdirmin" % cache_str.capitalize())
459 except Exception:
460 self.test(False, traceback.format_exc())
461
462 def acregmin_attr_test(self):
463 """Verify consistency of attribute caching by varying the
464 acregmin NFS option.
465
466 The cached information is assumed to be valid for attrtimeo
467 which starts at acregmin.
468 """
469 for acregmin in self.acminlist:
470 self.do_file_test(acregmin=acregmin)
471
472 def acregmax_attr_test(self):
473 """Verify consistency of attribute caching by varying the
474 acregmax NFS option.
475
476 The cached information is assumed to be valid for attrtimeo which
477 starts at acregmin. An attribute revalidation to the server
478 that shows no attribute change doubles attrtimeo up to acregmax.
479 An attribute revalidation to the server that shows a change has
480 occurred resets it to acregmin.
481 """
482 acregmin = self.acminlist[0]
483 for acregmax in self.acmaxlist:
484 self.do_file_test(acregmin=acregmin, acregmax=acregmax)
485
486 def acdirmin_attr_test(self):
487 """Verify consistency of attribute caching by varying the
488 acdirmin NFS option.
489
490 The cached information is assumed to be valid for attrtimeo
491 which starts at acdirmin. Test that this is so.
492 """
493 for acdirmin in self.acminlist:
494 self.do_dir_test(acdirmin=acdirmin)
495
496 def acdirmax_attr_test(self):
497 """Verify consistency of attribute caching by varying the
498 acdirmax NFS option.
499
500 The cached information is assumed to be valid for attrtimeo which
501 starts at acdirmin. An attribute revalidation to the server
502 that shows no attribute change doubles attrtimeo up to acdirmax.
503 An attribute revalidation to the server that shows a change has
504 occurred resets it to acdirmin.
505 """
506 acdirmin = self.acminlist[0]
507 for acdirmax in self.acmaxlist:
508 self.do_dir_test(acdirmin=acdirmin, acdirmax=acdirmax)
509
510 def actimeo_attr_test(self):
511 """Verify consistency of attribute caching by varying the
512 actimeo NFS option.
513
514 The cached information is assumed to be valid for attrtimeo
515 which starts and ends at actimeo.
516 """
517 for actimeo in self.acminlist:
518 # File test
519 self.do_file_test(actimeo=actimeo)
520
521 # Directory test
522 self.do_dir_test(actimeo=actimeo)
523
524 def acregmin_data_test(self):
525 """Verify consistency of data caching by varying the
526 acregmin NFS option.
527 """
528 for acregmin in self.acminlist:
529 self.do_file_test(acregmin=acregmin, data_cache=True)
530
531 def acregmax_data_test(self):
532 """Verify consistency of data caching by varying the
533 acregmax NFS option.
534
535 The cached information is assumed to be valid for attrtimeo which
536 starts at acregmin. An attribute revalidation to the server
537 that shows no attribute change doubles attrtimeo up to acregmax.
538 An attribute revalidation to the server that shows a change has
539 occurred resets it to acregmin.
540 """
541 acregmin = self.acminlist[0]
542 for acregmax in self.acmaxlist:
543 self.do_file_test(acregmin=acregmin, acregmax=acregmax, data_cache=True)
544
545 def acdirmin_data_test(self):
546 """Verify consistency of data caching by varying the
547 acdirmin NFS option.
548
549 The cached information is assumed to be valid for attrtimeo
550 which starts at acdirmin. Test that this is so.
551 """
552 for acdirmin in self.acminlist:
553 self.do_dir_test(acdirmin=acdirmin, data_cache=True)
554
555 def acdirmax_data_test(self):
556 """Verify consistency of data caching by varying the
557 acdirmax NFS option.
558
559 The cached information is assumed to be valid for attrtimeo which
560 starts at acdirmin. An attribute revalidation to the server
561 that shows no attribute change doubles attrtimeo up to acdirmax.
562 An attribute revalidation to the server that shows a change has
563 occurred resets it to acdirmin.
564 """
565 acdirmin = self.acminlist[0]
566 for acdirmax in self.acmaxlist:
567 self.do_dir_test(acdirmin=acdirmin, acdirmax=acdirmax, data_cache=True)
568
569 def actimeo_data_test(self):
570 """Verify consistency of data caching by varying the
571 actimeo NFS option.
572
573 The cached information is assumed to be valid for attrtimeo
574 which starts and ends at actimeo.
575 """
576 for actimeo in self.acminlist:
577 # File test
578 self.do_file_test(actimeo=actimeo, data_cache=True)
579
580 # Directory test
581 self.do_dir_test(actimeo=actimeo, data_cache=True)
582
583 ################################################################################
584 # Entry point
585 x = CacheTest(usage=USAGE, testnames=TESTNAMES, sid=SCRIPT_ID)
586
587 try:
588 # Unmount server on remote client
589 x.clientobj.umount()
590
591 # Mount server on remote client
592 x.clientobj.mount()
593
594 # Run all the tests
595 x.run_tests()
596 except Exception:
597 x.test(False, traceback.format_exc())
598 finally:
599 # Unmount server on remote client
600 x.clientobj.umount()
601 x.cleanup()
602 x.exit()