TestRail-Utils-Lock.t 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. my ($lockStatusID) = $tr->statusNamesToIds('locked');
  46. my ($project,$plan,$run) = TestRail::Utils::getRunInformation($tr,$opts);
  47. capture { $ret = TestRail::Utils::Lock::lockTest($tr->getTestByName($run->{'id'},'lockme.test'),$lockStatusID,'race.bannon',$tr) };
  48. is($ret ,0,"False returned when race condition is simulated");
  49. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep5();
  50. #Test with a second set of options, verify that no-match and type filtering works
  51. delete $opts->{'match'};
  52. $opts->{'case-types'} = ['Other'];
  53. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  54. is($ret->{'path'},'sortalockme.test',"Test which is here but the other type is locked when ommitting match");
  55. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep6();
  56. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  57. is($ret->{'path'},'dontlockme_alsonothere.test',"Test which is not here but the other type is locked when omitting match");
  58. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep7();
  59. capture { $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr) };
  60. is($ret,0,"No tests are locked, as they are not the right type");
  61. #Make sure we only grab retest/untested
  62. delete $opts->{'case-types'};
  63. $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr);
  64. is($ret->{'path'},'dontlockme_nothere.test',"Wrong type test which is not here gets locked after we remove all restrictions");
  65. $tr->{'browser'} = Test::LWP::UserAgent::TestRailMock::lockMockStep8();
  66. capture { $ret = TestRail::Utils::Lock::pickAndLockTest($opts,$tr) };
  67. is($ret,0,"No tests are locked, as none are untested or retest");