Lock.pm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 long names 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. Returns a HASHREF with the test, project, run and plan (if any) definition HASHREFs as keys.
  28. Also, a 'path' key will be set which has the full path to the test on disk, if match mode is passed, the case title otherwise.
  29. If the test could not be locked, 0 is returned.
  30. =back
  31. =cut
  32. sub pickAndLockTest {
  33. state $check = compile(HashRef, Optional[Maybe[Object]]);
  34. my ($opts, $tr) = $check->(@_);
  35. if ($opts->{mock} && !$tr) {
  36. require Test::LWP::UserAgent::TestRailMock; #LazyLoad
  37. $opts->{browser} = $Test::LWP::UserAgent::TestRailMock::mockObject;
  38. $opts->{debug} = 1;
  39. }
  40. $tr //= TestRail::API->new($opts->{apiurl},$opts->{user},$opts->{password},$opts->{'encoding'},$opts->{'debug'});
  41. $tr->{'browser'} = $opts->{'browser'} if $opts->{'browser'};
  42. $tr->{'debug'} = 0;
  43. my ($project,$plan,$run) = TestRail::Utils::getRunInformation($tr,$opts);
  44. my $status_ids;
  45. # Process statuses
  46. @$status_ids = $tr->statusNamesToIds($opts->{'lockname'},'untested','retest');
  47. my ($lock_status_id,$untested_id,$retest_id) = @$status_ids;
  48. my $cases = $tr->getTests($run->{'id'});
  49. #Filter by case types
  50. if ($opts->{'case-types'}) {
  51. my @case_types = map { my $cdef = $tr->getCaseTypeByName($_); $cdef->{'id'} } @{ $opts->{'case-types'} };
  52. @$cases = grep { my $case_type_id = $_->{'type_id'}; grep {$_ eq $case_type_id} @case_types } @$cases;
  53. }
  54. # Limit to only non-locked and open cases
  55. @$cases = grep { my $tstatus = $_->{'status_id'}; scalar(grep { $tstatus eq $_ } ($untested_id,$retest_id) ) } @$cases;
  56. @$cases = sort { $b->{'priority_id'} <=> $a->{'priority_id'} } @$cases; #Sort by priority DESC
  57. # Filter by match options
  58. @$cases = TestRail::Utils::findTests($opts,@$cases);
  59. my ($title,$test);
  60. while (@$cases) {
  61. $test = shift @$cases;
  62. $title = lockTest($test,$lock_status_id,$opts->{'hostname'},$tr);
  63. last if $title;
  64. }
  65. if (!$title) {
  66. warn "Failed to lock case! This probably means you don't have any cases left to lock.";
  67. return 0;
  68. }
  69. return {
  70. 'test' => $test,
  71. 'path' => $title,
  72. 'project' => $project,
  73. 'plan' => $plan,
  74. 'run' => $run
  75. };
  76. }
  77. =head2 lockTest(test,lock_status_id,handle)
  78. Lock the specified test, and return it's title (or full_title if it exists).
  79. =over 4
  80. =item HASHREF C<TEST> - Test object returned by getTests, or a similar method.
  81. =item INTEGER C<LOCK_STATUS_ID> - Status used to denote locking of test
  82. =item TestRail::API C<HANDLE> - Instance of TestRail::API
  83. =back
  84. Returns -1 in the event a lock could not occur, and warns & returns 0 on lock collisions.
  85. =cut
  86. sub lockTest {
  87. state $check = compile(HashRef, Int, Str, Object);
  88. my ($test,$lock_status_id,$hostname,$handle) = $check->(@_);
  89. my $res = $handle->createTestResults(
  90. $test->{id},
  91. $lock_status_id,
  92. "Test Locked by $hostname.\n
  93. If this result is preceded immediately by another lock statement like this, please disregard it;
  94. a lock collision occurred."
  95. );
  96. #If we've got more than 100 lock conflicts, we have big-time problems
  97. my $results = $handle->getTestResults($test->{id},100);
  98. #Remember, we're returned results from newest to oldest...
  99. my $next_one = 0;
  100. foreach my $result (@$results) {
  101. unless ($result->{'status_id'} == $lock_status_id) {
  102. #Clearly no lock conflict going on here if next_one is true
  103. last if $next_one;
  104. #Otherwise just skip it until we get to the test we locked
  105. next;
  106. }
  107. if ($result->{id} == $res->{'id'}) {
  108. $next_one = 1;
  109. next;
  110. }
  111. if ($next_one) {
  112. #If we got this far, a lock conflict occurred. Try the next one.
  113. warn "Lock conflict detected. Try again...\n";
  114. return 0;
  115. }
  116. }
  117. #Prefer full titles (match mode)
  118. return defined($test->{'full_title'}) ? $test->{'full_title'} : $test->{'title'} if $next_one;
  119. return -1;
  120. }
  121. 1;
  122. __END__
  123. =head1 SPECIAL THANKS
  124. Thanks to cPanel Inc, for graciously funding the creation of this module.