testrail-lock 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env perl
  2. # ABSTRACT: Lock a test in a TestRail, and return the test name if successful.
  3. # PODNAME: testrail-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. =head1 DESCRIPTION
  8. testrail-lock - pick an untested/retest test in TestRail, lock it, and return the test name if successful.
  9. 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.
  10. 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.
  11. 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.
  12. Will respect test priority when making the choice of what test to lock.
  13. 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.
  14. =head1 PARAMETERS:
  15. =head2 MANDATORY PARAMETERS
  16. =over 4
  17. --apiurl : full URL to get to TestRail index document
  18. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  19. --user : Your TestRail User Name.
  20. -j --project : desired project name.
  21. -r --run : desired run name.
  22. -l --lockname : internal name of lock status.
  23. =back
  24. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  25. =head2 SEMI-OPTIONAL PARAMETERS
  26. =over 4
  27. -p --plan : desired plan name. Required if the run passed is a child of a plan.
  28. -m --match : attempt to find filenames matching the test names in the provided directory.
  29. --no-match : attempt to find filenames that do not match test names in the provided directory.
  30. -n --no-recurse : if match (or no-match) passed, do not recurse subdirectories.
  31. -t --case-type : Only attempt to lock cases of the specified type. May be passed multiple times.
  32. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  33. =back
  34. =head2 OPTIONAL PARAMETERS
  35. =over 4
  36. -c --config : configuration name to filter plans in run. Can be passed multiple times.
  37. =back
  38. =head1 CONFIGURATION FILE
  39. 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.
  40. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  41. All options specified thereby are overridden by passing the command-line switches above.
  42. =head1 MISCELLANEOUS OPTIONS:
  43. =over 4
  44. --mock : don't do any real HTTP requests. Used only by tests.
  45. --help : show this output
  46. =back
  47. =cut
  48. use strict;
  49. use warnings;
  50. use utf8;
  51. use TestRail::Utils;
  52. use Getopt::Long;
  53. use File::HomeDir qw{my_home};
  54. use Sys::Hostname qw{hostname};
  55. my $opts = {};
  56. #Parse config file if we are missing api url/key or user
  57. my $homedir = my_home() || '.';
  58. if (-e $homedir . '/.testrailrc') {
  59. $opts = TestRail::Utils::parseConfig($homedir);
  60. }
  61. GetOptions(
  62. 'apiurl=s' => \$opts->{'apiurl'},
  63. 'password=s' => \$opts->{'password'},
  64. 'user=s' => \$opts->{'user'},
  65. 'l|lockname=s' => \$opts->{'lockname'},
  66. 'j|project=s' => \$opts->{'project'},
  67. 'p|plan=s' => \$opts->{'plan'},
  68. 'r|run=s' => \$opts->{'run'},
  69. 'c|config=s@' => \$opts->{'configs'},
  70. 'm|match=s' => \$opts->{'match'},
  71. 'no-match=s' => \$opts->{'no-match'},
  72. 'n|no-recurse' => \$opts->{'no-recurse'},
  73. 't|case-type=s@' => \$opts->{'case-types'},
  74. 'mock' => \$opts->{'mock'},
  75. 'e|encoding=s' => \$opts->{'encoding'},
  76. 'h|help' => \$opts->{'help'}
  77. );
  78. if ($opts->{help}) { TestRail::Utils::help(); }
  79. $opts->{'hostname'} = hostname;
  80. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project run lockname});
  81. my $ret = TestRail::Utils::pickAndLockTest($opts);
  82. exit 255 if !$ret;
  83. print $ret->{'path'}."\n";
  84. exit 0;
  85. __END__
  86. L<TestRail::API>
  87. L<File::HomeDir> for the finding of .testrailrc
  88. =head1 SPECIAL THANKS
  89. Thanks to cPanel Inc, for graciously funding the creation of this distribution.