testrail-lock 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env 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. This obviously does not make sense with case_per_ok test upload; support for locking entire sections when in case_per_ok upload mode is not supported at this time.
  16. Can also be used as the modulino TestRail::Bin::Lock.
  17. Has a single 'run' function which accepts a hash with the 'args' parameter being the array of arguments.
  18. =head1 PARAMETERS:
  19. =head2 MANDATORY PARAMETERS
  20. =over 4
  21. --apiurl : full URL to get to TestRail index document
  22. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  23. --user : Your TestRail User Name.
  24. -j --project : desired project name.
  25. -r --run : desired run name.
  26. -l --lockname : internal name of lock status.
  27. =back
  28. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  29. =head2 SEMI-OPTIONAL PARAMETERS
  30. =over 4
  31. -p --plan : desired plan name. Required if the run passed is a child of a plan.
  32. -m --match : attempt to find filenames matching the test names in the provided directory.
  33. --no-match : attempt to find filenames that do not match test names in the provided directory.
  34. -n --no-recurse : if match (or no-match) passed, do not recurse subdirectories.
  35. -t --case-type : Only attempt to lock cases of the specified type. May be passed multiple times.
  36. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  37. =back
  38. =head2 OPTIONAL PARAMETERS
  39. =over 4
  40. -c --config : configuration name to filter plans in run. Can be passed multiple times.
  41. =back
  42. =head1 CONFIGURATION FILE
  43. 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.
  44. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  45. All options specified thereby are overridden by passing the command-line switches above.
  46. =head1 MISCELLANEOUS OPTIONS:
  47. =over 4
  48. --help : show this output
  49. =back
  50. =cut
  51. package TestRail::Bin::Lock;
  52. use strict;
  53. use warnings;
  54. use utf8;
  55. use TestRail::Utils;
  56. use TestRail::Utils::Lock;
  57. use Getopt::Long qw{GetOptionsFromArray};
  58. use File::HomeDir qw{my_home};
  59. use Sys::Hostname qw{hostname};
  60. if (!caller()) {
  61. my ($out,$code) = run('args' => \@ARGV);
  62. print $out;
  63. exit $code;
  64. }
  65. sub run {
  66. my %params = @_;
  67. my $opts = {};
  68. #Parse config file if we are missing api url/key or user
  69. my $homedir = my_home() || '.';
  70. if (-e $homedir . '/.testrailrc') {
  71. $opts = TestRail::Utils::parseConfig($homedir);
  72. }
  73. GetOptionsFromArray($params{'args'},
  74. 'apiurl=s' => \$opts->{'apiurl'},
  75. 'password=s' => \$opts->{'password'},
  76. 'user=s' => \$opts->{'user'},
  77. 'l|lockname=s' => \$opts->{'lockname'},
  78. 'j|project=s' => \$opts->{'project'},
  79. 'p|plan=s' => \$opts->{'plan'},
  80. 'r|run=s' => \$opts->{'run'},
  81. 'c|config=s@' => \$opts->{'configs'},
  82. 'm|match=s' => \$opts->{'match'},
  83. 'no-match=s' => \$opts->{'no-match'},
  84. 'n|no-recurse' => \$opts->{'no-recurse'},
  85. 't|case-type=s@' => \$opts->{'case-types'},
  86. 'e|encoding=s' => \$opts->{'encoding'},
  87. 'h|help' => \$opts->{'help'},
  88. );
  89. if ($opts->{help}) { return ('',TestRail::Utils::help()); }
  90. $opts->{'browser'} = $params{'browser'};
  91. $opts->{'hostname'} = hostname;
  92. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project run lockname});
  93. my $tr = TestRail::Utils::getHandle($opts);
  94. my $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  95. return ('Could not lock case.', 255) if !$ret;
  96. return ($ret->{'path'}."\n",0);
  97. }
  98. 1;
  99. __END__
  100. L<TestRail::API>
  101. L<File::HomeDir> for the finding of .testrailrc
  102. =head1 SPECIAL THANKS
  103. Thanks to cPanel Inc, for graciously funding the creation of this distribution.