Lock.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # ABSTRACT: Pick high priority cases for execution and lock them via the test results mechanism.
  2. # PODNAME: TestRail::Utils::Lock
  3. package TestRail::Utils::Lock;
  4. use 5.010;
  5. use strict;
  6. use warnings;
  7. use Carp qw{confess cluck};
  8. use Types::Standard qw( slurpy ClassName Object Str Int Bool HashRef ArrayRef Maybe Optional);
  9. use Type::Params qw( compile );
  10. use TestRail::API;
  11. use TestRail::Utils;
  12. =head1 DESCRIPTION
  13. Lock a test case via usage of the test result field.
  14. Has a hard limit of looking for 250 results, which is the only weakness of this locking approach.
  15. If you have other test runners that result in such tremendous numbers of lock collisions,
  16. it will result in 'hard-locked' cases, where manual intervention will be required to free the case.
  17. However in that case, one would assume you could afford to write a reaper script to detect and
  18. correct this condition, or consider altering your run strategy to reduce the probability of lock collisions.
  19. =head2 pickAndLockTest(options,[handle])
  20. Pick and lock a test case in a TestRail Run, and return it if successful, confess() on failure.
  21. testrail-lock's primary routine.
  22. =over 4
  23. =item HASHREF C<OPTIONS> - valid keys/values correspond to the longnames of arguments taken by L<testrail-lock>.
  24. =item TestRail::API C<HANDLE> - Instance of TestRail::API, in the case where the caller already has a valid object.
  25. There is a special key, 'mock' in the HASHREF that is used for testing.
  26. The 'hostname' key must also be passed in the options, as it is required by lockTest, which this calls.
  27. =back
  28. =cut
  29. sub pickAndLockTest {
  30. state $check = compile(HashRef, Optional[Maybe[Object]]);
  31. my ($opts, $tr) = $check->(@_);
  32. if ($opts->{mock} && !$tr) {
  33. require Test::LWP::UserAgent::TestRailMock; #LazyLoad
  34. $opts->{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  35. $opts->{debug} = 1;
  36. }
  37. $tr //= TestRail::API->new($opts->{apiurl},$opts->{user},$opts->{password},$opts->{'encoding'},$opts->{'debug'});
  38. $tr->{'browser'} = $opts->{'browser'} if $opts->{'browser'};
  39. $tr->{'debug'} = 0;
  40. my ($project,$plan,$run) = TestRail::Utils::getRunInformation($tr,$opts);
  41. my $status_ids;
  42. # Process statuses
  43. @$status_ids = $tr->statusNamesToIds($opts->{'lockname'},'untested','retest');
  44. my ($lock_status_id,$untested_id,$retest_id) = @$status_ids;
  45. my $cases = $tr->getTests($run->{'id'});
  46. #Filter by case types
  47. if ($opts->{'case-types'}) {
  48. my @case_types = map { my $cdef = $tr->getCaseTypeByName($_); $cdef->{'id'} } @{ $opts->{'case-types'} };
  49. @$cases = grep { my $case_type_id = $_->{'type_id'}; grep {$_ eq $case_type_id} @case_types } @$cases;
  50. }
  51. # Limit to only non-locked and open cases
  52. @$cases = grep { my $tstatus = $_->{'status_id'}; scalar(grep { $tstatus eq $_ } ($untested_id,$retest_id) ) } @$cases;
  53. @$cases = sort { $b->{'priority_id'} <=> $a->{'priority_id'} } @$cases; #Sort by priority DESC
  54. # Filter by match options
  55. @$cases = TestRail::Utils::findTests($opts,@$cases);
  56. my $title;
  57. foreach my $test (@$cases) {
  58. $title = lockTest($test,$lock_status_id,$opts->{'hostname'},$tr);
  59. last if $title;
  60. }
  61. warn "Failed to lock case! This probably means you don't have any cases left to lock." if !$title;
  62. return $title;
  63. }
  64. =head2 lockTest(test,lock_status_id,handle)
  65. Lock the specified test, and return it's title (or full_title if it exists).
  66. =over 4
  67. =item HASHREF C<TEST> - Test object returned by getTest, or a similar method.
  68. =item INTEGER C<LOCK_STATUS_ID> - Status used to denote locking of test
  69. =item TestRail::API C<HANDLE> - Instance of TestRail::API
  70. =back
  71. Returns undef in the event a lock could not occur, and warns & returns 0 on lock collisions.
  72. =cut
  73. sub lockTest {
  74. state $check = compile(HashRef, Int, Str, Object);
  75. my ($test,$lock_status_id,$hostname,$handle) = $check->(@_);
  76. my $res = $handle->createTestResults(
  77. $test->{id},
  78. $lock_status_id,
  79. "Test Locked by $hostname.\n
  80. If this result is preceded immediately by another lock statement like this, please disregard it;
  81. a lock collision occurred."
  82. );
  83. #If we've got more than 100 lock conflicts, we have big-time problems
  84. my $results = $handle->getTestResults($test->{id},100);
  85. #Remember, we're returned results from newest to oldest...
  86. my $next_one = 0;
  87. foreach my $result (@$results) {
  88. unless ($result->{'status_id'} == $lock_status_id) {
  89. #Clearly no lock conflict going on here if next_one is true
  90. last if $next_one;
  91. #Otherwise just skip it until we get to the test we locked
  92. next;
  93. }
  94. if ($result->{id} == $res->{'id'}) {
  95. $next_one = 1;
  96. next;
  97. }
  98. if ($next_one) {
  99. #If we got this far, a lock conflict occurred. Try the next one.
  100. warn "Lock conflict detected. Try again...\n";
  101. return 0;
  102. }
  103. }
  104. #Prefer full titles (match mode)
  105. return defined($test->{'full_title'}) ? $test->{'full_title'} : $test->{'title'} if $next_one;
  106. return undef;
  107. }
  108. 1;