TestRail-Utils-Lock.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. use strict;
  2. use warnings;
  3. use FindBin;
  4. use lib "$FindBin::Bin/lib";
  5. use Test::More 'tests' => 11;
  6. use Test::Fatal;
  7. use File::Basename qw{dirname};
  8. use TestRail::Utils;
  9. use TestRail::Utils::Lock;
  10. use Test::LWP::UserAgent::TestRailMock;
  11. use Sys::Hostname qw{hostname};
  12. use File::Basename qw{basename};
  13. use Capture::Tiny qw{capture};
  14. my $opts = {
  15. 'project' => 'TestProject',
  16. 'run' => 'lockRun',
  17. 'case-types' => ['Automated'],
  18. 'lockname' => 'locked',
  19. 'match' => "t",
  20. 'no-recurse' => 1,
  21. 'hostname' => hostname(),
  22. 'mock' => 1
  23. };
  24. my ($apiurl,$login,$pw) = ('http://hokum.bogus','bogus','bogus');
  25. my $tr = new TestRail::API($apiurl,$login,$pw,undef,1);
  26. $tr->{'debug'} = 0;
  27. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep0();
  28. my $ret;
  29. capture { $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr) };
  30. is($ret,0,"Verify that no tests are locked in match mode, as they all are in a subdir, and recurse is off");
  31. delete $opts->{'no-recurse'};
  32. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  33. is(basename( $ret->{'path'} ), 'lockmealso.test' , "Verify the highest priority test is chosen first");
  34. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep1();
  35. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  36. is(basename( $ret->{'path'} ), 'lockme.test' , "Verify the highest priority test of type automated is chosen");
  37. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep2();
  38. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  39. is(basename( $ret->{'path'} ), 'lockmetoo.test' , "Verify that the highest priority test that exists in the tree is chosen");
  40. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep3();
  41. capture { $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr) };
  42. is($ret,0,"Verify that no tests are locked, as they either are of the wrong type or do not exist in the match tree");
  43. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep4();
  44. #Simulate lock collision
  45. $tr->{tests_cache} = {};
  46. my ($lockStatusID) = $tr->statusNamesToIds('locked');
  47. my ($project,$plan,$run) = TestRail::Utils::getRunInformation($tr,$opts);
  48. capture {
  49. $ret = TestRail::Utils::Lock::lockTest(
  50. $tr->getTestByName($run->{'id'},'lockme.test'),$lockStatusID,'race.bannon',$tr
  51. )
  52. };
  53. is($ret ,0,"False returned when race condition is simulated");
  54. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep5();
  55. #Test with a second set of options, verify that no-match and type filtering works
  56. delete $opts->{'match'};
  57. $opts->{'case-types'} = ['Other'];
  58. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  59. is($ret->{'path'},'sortalockme.test',"Test which is here but the other type is locked when ommitting match");
  60. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep6();
  61. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  62. is($ret->{'path'},'dontlockme_alsonothere.test',"Test which is not here but the other type is locked when omitting match");
  63. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep7();
  64. capture { $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr) };
  65. is($ret,0,"No tests are locked, as they are not the right type");
  66. #Make sure we only grab retest/untested
  67. delete $opts->{'case-types'};
  68. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  69. is($ret->{'path'},'dontlockme_nothere.test',"Wrong type test which is not here gets locked after we remove all restrictions");
  70. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep8();
  71. capture { $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr) };
  72. is($ret,0,"No tests are locked, as none are untested or retest");