Lock.pm 5.4 KB

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