testrail-lock 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/perl
  2. # ABSTRACT: Lock a test in a TestRail, and return the test name if successful.
  3. # PODNAME: TestRail::Bin::Lock
  4. =head1 SYNOPSIS
  5. # Lock a group of tests and execute them
  6. testrail-tests [OPTIONS] | xargs testrail-lock [OPTIONS] | xargs prove -PTestrail=...
  7. require `which testrail-lock`;
  8. TestRail::Bin::Lock::run('args' => \@args);
  9. =head1 DESCRIPTION
  10. testrail-lock - pick an untested/retest test in TestRail, lock it, and return the test name if successful.
  11. It is useful to lock the test in situations where you have multiple disconnected test running processes trying to allocate resources toward testing outstanding cases so that effort is not duplicated.
  12. This is accomplished via setting a special locking result on a test rather than simple assignment, as detecting lock conflicts is impossible then due to a lack of assignment history.
  13. Results, however have a history of results set, so we use that fact to detect if a locking collision occurred (race condition) and fail to return a result when another process locked during our attempt to lock.
  14. Will respect test priority when making the choice of what test to lock.
  15. Can also be used as the modulino TestRail::Bin::Lock.
  16. Has a single 'run' function which accepts a hash with the 'args' parameter being the array of arguments.
  17. =head1 PARAMETERS:
  18. =head2 MANDATORY PARAMETERS
  19. =over 4
  20. --apiurl : full URL to get to TestRail index document
  21. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  22. --user : Your TestRail User Name.
  23. -j --project : desired project name.
  24. -r --run : desired run name.
  25. -l --lockname : internal name of lock status.
  26. =back
  27. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  28. =head2 SEMI-OPTIONAL PARAMETERS
  29. =over 4
  30. -p --plan : desired plan name. Required if the run passed is a child of a plan.
  31. -m --match : attempt to find filenames matching the test names in the provided directory.
  32. --no-match : attempt to find filenames that do not match test names in the provided directory.
  33. -n --no-recurse : if match (or no-match) passed, do not recurse subdirectories.
  34. -t --case-type : Only attempt to lock cases of the specified type. May be passed multiple times.
  35. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  36. =back
  37. =head2 OPTIONAL PARAMETERS
  38. =over 4
  39. -c --config : configuration name to filter plans in run. Can be passed multiple times.
  40. =back
  41. =head1 CONFIGURATION FILE
  42. In your $HOME, (or the current directory, if your system has no concept of a home directory) put a file called .testrailrc with key=value syntax separated by newlines.
  43. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  44. All options specified thereby are overridden by passing the command-line switches above.
  45. =head1 MISCELLANEOUS OPTIONS:
  46. =over 4
  47. --help : show this output
  48. =back
  49. =cut
  50. package TestRail::Bin::Lock;
  51. use strict;
  52. use warnings;
  53. use utf8;
  54. use TestRail::Utils;
  55. use TestRail::Utils::Lock;
  56. use Getopt::Long qw{GetOptionsFromArray};
  57. use File::HomeDir qw{my_home};
  58. use Sys::Hostname qw{hostname};
  59. if (!caller()) {
  60. my ($out,$code) = run('args' => \@ARGV);
  61. print $out;
  62. exit $code;
  63. }
  64. sub run {
  65. my %params = @_;
  66. my $opts = {};
  67. #Parse config file if we are missing api url/key or user
  68. my $homedir = my_home() || '.';
  69. if (-e $homedir . '/.testrailrc') {
  70. $opts = TestRail::Utils::parseConfig($homedir);
  71. }
  72. GetOptionsFromArray($params{'args'},
  73. 'apiurl=s' => \$opts->{'apiurl'},
  74. 'password=s' => \$opts->{'password'},
  75. 'user=s' => \$opts->{'user'},
  76. 'l|lockname=s' => \$opts->{'lockname'},
  77. 'j|project=s' => \$opts->{'project'},
  78. 'p|plan=s' => \$opts->{'plan'},
  79. 'r|run=s' => \$opts->{'run'},
  80. 'c|config=s@' => \$opts->{'configs'},
  81. 'm|match=s' => \$opts->{'match'},
  82. 'no-match=s' => \$opts->{'no-match'},
  83. 'n|no-recurse' => \$opts->{'no-recurse'},
  84. 't|case-type=s@' => \$opts->{'case-types'},
  85. 'e|encoding=s' => \$opts->{'encoding'},
  86. 'h|help' => \$opts->{'help'},
  87. );
  88. if ($opts->{help}) { return ('',TestRail::Utils::help()); }
  89. $opts->{'browser'} = $params{'browser'};
  90. $opts->{'hostname'} = hostname;
  91. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project run lockname});
  92. my $tr = TestRail::Utils::getHandle($opts);
  93. my $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  94. return ('Could not lock case.', 255) if !$ret;
  95. return ($ret->{'path'}."\n",0);
  96. }
  97. 1;
  98. __END__
  99. L<TestRail::API>
  100. L<File::HomeDir> for the finding of .testrailrc
  101. =head1 SPECIAL THANKS
  102. Thanks to cPanel Inc, for graciously funding the creation of this distribution.