Monday, April 8, 2019

Distributing Test Cases to a set of Jenkins slave for execution

This groovy script will distribute a set of testcases to a set of slaves under a certain label.

Prerequisites:
  1. The slaves are part of a label
  2. The Jenkins job which executes the test cases should have the following capabilities:
    1. It has "test case list" as a parameter. Therefore this job should be able to execute 1 or more test cases upon trigger by looping its execution through that list.
    2. It should be able to identify already running (or already passed) test cases as a first step of the execution loop and skip these cases already.
What this script does:
  1. Create a pool of online slaves. While creating this, the free slaves will have priority over the busy slaves once test-case distribution happens. 
  2. If the number of slaves in the pool is greater than the number of test cases, trim the pool to create a matching number of slaves but taking only from the priority side.   
  3. Pair the test-case list (list as a string, because this will be passed later on as a job parameter for the test execution job) to each of the slaves in the pool. But for every next slave, the test case list will rotate its list. Ex:
    • slaveA:TC001,TC002,TC003
    • slaveB:TC002,TC003,TC001 
    • slaveC:TC003,TC001,TC002
  4. Trigger parallel builds of the "test execution job", passing the pairs as Node and testcase list parameters of that target job. Because this step will trigger parallel execution of test cases, the rotating test case list will prevent a scenario where multiple slaves are executing the same test case at the initial run.