PageRenderTime 120ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/FileSysTest/FileSysTest.pl

https://github.com/thecwlzone/one-offs
Perl | 328 lines | 236 code | 18 blank | 74 comment | 26 complexity | 1fbe85559c2e94c0de4efb383dd30b29 MD5 | raw file
  1. #!/usr/bin/perl -w
  2. use WriteTestFile;
  3. use FSTCommon;
  4. use TheTests;
  5. use Getopt::Long;
  6. use Time::Local;
  7. use lib ".";
  8. #
  9. # Change Log
  10. #
  11. # 0.20 - Added --maxsize option to limit file sizes to a user-defined maximum.
  12. # Syntax is --maxsize nG | nM | nK
  13. # Example: --maxsize 1G
  14. # Also affects the code in TheTests.pm
  15. #
  16. # 0.21 - Removed 0755 directive from the mkdir commands to make GSA happy
  17. #
  18. # 0.22 - Added --fixname option. This will create a file/directory hierarchy
  19. # with a repeatable naming convention.
  20. #
  21. # 0.23 - Added --datadir option. The directory is persistent, so it can be used
  22. # to test file system caching. To be used in conjnction with the new
  23. # Test12 code.
  24. #
  25. # 0.24 - Added general support for the --datadir option
  26. # When using --datadir, you really need to use --fixname or the benefit
  27. # of persistent data is lost, so the process_options subroutine checks
  28. # for that dependency.
  29. #
  30. # This is the main program.
  31. #
  32. # Syntax: ./FileSysTest.pl [options]
  33. #
  34. # Options:
  35. #
  36. # -h, -help, --help Print usage and exit.
  37. # --verbose Show processing details. Default behavior is silent.
  38. # --terse Not silent, and not verbose.
  39. # --seq Run tests sequentially. Default behavior.
  40. # --sim Run tests simultaneously. Overrides --seq option.
  41. # --all Run all tests. Default behavior.
  42. # --maxsize nG | nM | nK Use files no larger than n bytes
  43. # --dryrun Run the code, but don't really do anything.
  44. # Best used with the --verbose option.
  45. # --noclean Keep all created files and directories.
  46. # Default behavior is to delete files "on the fly".
  47. # --tests 1,2,n | 1-n Run designated tests. Overrides --all option.
  48. # --users n Simulate n number users. Default is 1 user.
  49. # n > 1 is only useful with the --sim option enabled.
  50. # --outdir Output directory. Default location is
  51. # /tmp/filesystest-`date`
  52. # --logfile Output file. Default name is filesystest-`date`.log
  53. # --fixname name Use "Name" when creating files and directories
  54. # --datadir DirName Use "DirName" as a persistent directory
  55. # -v, --version Version number
  56. #
  57. ###################
  58. # main
  59. #
  60. our $total_tests = 12;
  61. our $fh; # Log file handle
  62. my $datestamp = get_datestamp();
  63. my $MAIN_MSG_LABEL = "MAIN: ";
  64. my $VERSION = "0.24";
  65. # Initialize options
  66. our $help = 0;
  67. our $verbose = 0;
  68. our $terse = 0;
  69. our $seq = 1;
  70. our $sim = 0;
  71. our $all = 1;
  72. our $maxsize = "";
  73. our $dryrun = 0;
  74. our $noclean = 0;
  75. our $tests = "";
  76. our $users = 1;
  77. our $outdir = "/tmp";
  78. our $logfile = "filesystest-" . $datestamp . ".log";
  79. our $fixname = "";
  80. our $datadir = "";
  81. my $version = 0;
  82. process_options();
  83. # Initialize log file
  84. open($fh, "> $main::logfile") or die "Could not open the log file $main::logfile: $!";
  85. my $start_time = timelocal(localtime);
  86. print("\nStart FileSysTest Suite\n\n") if $verbose || $terse;
  87. write_to_logfile($MAIN_MSG_LABEL . "Start FileSysTest Suite\n\n");
  88. # Write options to logfile
  89. write_to_logfile($MAIN_MSG_LABEL . "Write files to " . $outdir . "\n");
  90. write_to_logfile($MAIN_MSG_LABEL . "Log file name is " . $logfile . "\n");
  91. if ($all) {
  92. write_to_logfile($MAIN_MSG_LABEL . "Running all tests\n");
  93. }
  94. else {
  95. write_to_logfile($MAIN_MSG_LABEL . "Running tests " . $tests . "\n");
  96. }
  97. if ($maxsize ne "") { write_to_logfile($MAIN_MSG_LABEL . "Maximum file size set to " . $maxsize . "\n"); }
  98. if ($sim) {
  99. write_to_logfile($MAIN_MSG_LABEL . "Running " . $users . " users simultaneously\n\n");
  100. }
  101. else {
  102. write_to_logfile($MAIN_MSG_LABEL . "Running " . $users . " users sequentially\n\n");
  103. }
  104. print("DRY RUN MODE - no files will actually be created.\n\n") if $dryrun && $verbose;
  105. write_to_logfile($MAIN_MSG_LABEL . "DRY RUN MODE - no files will actually be created.\n\n") if $dryrun;
  106. run_dispatcher();
  107. print "Tests complete. Performing teardown procedures...\n" if $verbose || $terse;
  108. # Tear down
  109. print "\nFinished FileSysTest Suite\n" if $verbose || $terse;
  110. write_to_logfile("\n" . $MAIN_MSG_LABEL . "Finished FileSysTest Suite\n");
  111. my $finish_time = timelocal(localtime);
  112. my $total_run_time = elapsed_time($start_time, $finish_time);
  113. write_to_logfile($MAIN_MSG_LABEL . $total_run_time);
  114. close($fh);
  115. directory_purge() unless $noclean;
  116. exit;
  117. # end main
  118. #######################
  119. ####
  120. # This is the heart of the program control
  121. #
  122. sub run_dispatcher {
  123. @tests = generate_test_list();
  124. # For n number of users
  125. for ($i = 1; $i <= $main::users; $i++) {
  126. # Run each specified test
  127. my $user_number = $i;
  128. foreach $item (@tests) {
  129. my $run_test = "FSTest". $item;
  130. # Sequential testing
  131. &$run_test($user_number) if ! $main::sim;
  132. # Simultaneous testing
  133. if ($main::sim) {
  134. my $pid = fork();
  135. if ($pid) {
  136. # parent
  137. push(@childs, $pid);
  138. }
  139. elsif ($pid == 0) {
  140. # child
  141. &$run_test($user_number);
  142. exit(0);
  143. }
  144. else {
  145. die "couldn't fork: $!\n";
  146. }
  147. } # if $sim
  148. } # foreach item
  149. } # for $i
  150. # Avoid zombie processes
  151. foreach (@childs) {
  152. waitpid($_, 0);
  153. }
  154. }
  155. ####
  156. ####
  157. # Process options
  158. sub process_options {
  159. use File::Basename;
  160. my $opt_result = GetOptions ('h' => \$main::help,
  161. 'help' => \$main::help,
  162. 'verbose' => \$main::verbose,
  163. 'terse' => \$main::terse,
  164. 'seq' => \$main::seq,
  165. 'sim' => \$main::sim,
  166. 'all' => \$main::all,
  167. 'maxsize=s' => \$main::maxsize,
  168. 'dryrun' => \$main::dryrun,
  169. 'noclean' => \$main::noclean,
  170. 'tests=s' => \$main::tests,
  171. 'users=i' => \$main::users,
  172. 'outdir=s' => \$main::outdir,
  173. 'logfile=s' => \$main::logfile,
  174. 'fixname=s' => \$main::fixname,
  175. 'datadir=s' => \$main::datadir,
  176. 'v' => \$version,
  177. 'version' => \$version
  178. );
  179. if (! $opt_result || $main::help) { goto USAGE; };
  180. if ($version) { print($VERSION . "\n"); exit; };
  181. if ($maxsize && $maxsize !~ /(\d+\.)*\d+[Gg]|[Mm]|[Kk]/) {
  182. warn "Incorrect --maxsize value: $maxsize Cannot continue\n";
  183. goto USAGE;
  184. }
  185. if ($datadir ne "" && $fixname eq "") {
  186. warn "When using the --datadir option, use the --fixname option to create reusable directory names";
  187. goto USAGE;
  188. }
  189. # Adjust options as required based upon user input
  190. # if ($main::fixname ne "") { $main::logfile = "filesystest-" . $fixname . ".log"; }
  191. if ($main::sim) { $seq = 0; };
  192. if ($main::tests ne "") { $main::all = 0; };
  193. # Create outdir if necessary
  194. # Note: We rely on the user to have umask values set to create files and
  195. # directories under the appropriate file system(s)
  196. if (! -d $main::outdir) {
  197. mkdir($main::outdir) || die "Could not create directory: $!";
  198. }
  199. # Now add a unique test directory name to outdir
  200. # Allows a cleanup of the test directory without losing the log file.
  201. if ($main::fixname ne "") {
  202. $main::outdir = $main::outdir . "/filesystest-" . $fixname;
  203. }
  204. else {
  205. $main::outdir = $main::outdir . "/filesystest-" . $datestamp;
  206. }
  207. mkdir($main::outdir) || die "Could not create directory: $!";
  208. # Tack on the logfile name to outdir
  209. $main::logfile = dirname($main::outdir) . "/" . $main::logfile;
  210. if ($verbose) {
  211. print "Command line options settings:\n";
  212. print " --verbose = $main::verbose\n";
  213. print " --terse = $main::terse\n";
  214. print " --seq = $main::seq\n";
  215. print " --sim = $main::sim\n";
  216. print " --all = $main::all\n";
  217. print " --maxsize = $main::maxsize\n";
  218. print " --dryrun = $main::dryrun\n";
  219. print " --noclean = $main::noclean\n";
  220. print " --tests = $main::tests\n";
  221. print " --users = $main::users\n";
  222. print " --outdir = $main::outdir\n";
  223. print " --logfile = $main::logfile\n";
  224. print " --fixname = $main::fixname\n";
  225. print " --datadir = $main::datadir\n";
  226. print " --version = $VERSION\n";
  227. }
  228. }
  229. ####
  230. ####
  231. sub generate_test_list {
  232. # Create an array of tests to be run
  233. my @tests = ();
  234. if ($main::all) {
  235. for ($i = 1; $i <= $main::total_tests; $i++) {
  236. $tests[$i] = $i;
  237. }
  238. shift @tests; # Don't use tests[0]
  239. }
  240. # --tests 3 | --tests 11
  241. elsif ($main::tests =~ /^\d+(\d+)?$/) {
  242. $tests[0] = $main::tests;
  243. }
  244. # --tests 1,2,3
  245. elsif ($main::tests =~ /^\d+,\d+/) {
  246. my @test_list = split /,/, $main::tests;
  247. for ($i = 0; $i <= $#test_list; $i++) {
  248. $tests[$i] = $test_list[$i];
  249. }
  250. }
  251. # --tests 1-11
  252. elsif ($main::tests =~ /^\d+-\d+/) {
  253. my @test_list = split /-/, $main::tests;
  254. my $j = 0;
  255. for ($i = $test_list[0]; $i <= $test_list[$#test_list]; $i++) {
  256. $tests[$j] = $i;
  257. $j++;
  258. }
  259. }
  260. else {
  261. warn "Unknown format associated with --tests option. Cannot continue...\n";
  262. goto USAGE;
  263. }
  264. # Check for valid test numbers
  265. foreach $item (@tests) {
  266. if ($item < 1 || $item > $main::total_tests) {
  267. warn "Error in --tests option. The maximum number of tests is $main::total_tests\n";
  268. warn " You entered $item. Cannot continue.\n";
  269. exit;
  270. }
  271. }
  272. return @tests;
  273. }
  274. ####
  275. # Print usage syntax
  276. use Text::Wrap qw($columns &wrap);
  277. $columns = 30;
  278. USAGE: {
  279. print "Legal options for FileSysTest.pl are:\n";
  280. print "FileSysTest.pl [-h | -help | --help] [--verbose] [--seq | --sim]\n";
  281. print " [--all | --tests 1,2,n | --tests 1-n] [--maxsize nG | nM | nK] [--dryrun] [--noclean]\n";
  282. print " [--users n] [--outdir DirName] [--logfile FileName] [--datadir DirName --fixname name] [-v | --version]\n\n";
  283. print "--verbose Show processing details. Default behavior is silent.\n";
  284. print "--terse Show progress, but not verbosely\n";
  285. print "--seq Run tests sequentially. Default behavior.\n";
  286. print "--sim Run tests simultaneously. Overrides --seq option.\n";
  287. print "--all Run all tests. Default behavior.\n";
  288. print "--maxsize nG | nM | nK Use files no larger than n bytes\n";
  289. print "--dryrun Run the code, but don't really do anything.\n";
  290. print " Best used with the --verbose option.\n";
  291. print "--noclean Keep all created files and directories.\n";
  292. print " Default behavior is to delete files \"on the fly\".\n";
  293. print "--tests 1,2,n | 1-n Run designated tests. Overrides --all option.\n";
  294. print "--users n Simulate n number users. Default is 1 user.\n";
  295. print " n > 1 is only useful with the --sim option enabled\n";
  296. print "--outdir Output directory. Default location is\n";
  297. print " /tmp/filesystest-`date`\n";
  298. print "--logfile Output file. Default name is filesystest-`date`.log\n";
  299. print "--fixname Name Use \"Name\" to create repeatable file/directory names\n";
  300. print "--datadir DirName Use \"DirName\" as a persistent directory\n";
  301. print "-v, --version Version number\n\n";
  302. exit;
  303. }
  304. ####