[TASK] Run acceptance tests in parallel
[Packages/TYPO3.CMS.git] / components / testing_framework / Resources / Core / Build / Scripts / splitAcceptanceTests.sh
1 #!/bin/bash
2
3 #########################
4 #
5 # This file is typically executed by bamboo, but could be
6 # used locally, too.
7 #
8 # It expects to be run from the core root.
9 #
10 # ./components/testing_framework/core/Build/Scripts/splitAcceptanceTests.sh <numberOfConfigs>
11 #
12 # The script finds all acceptance tests and creates <numberOfConfigs> number
13 # of codeception group files, each containing a sub set of Cest files to execute.
14 #
15 # components/testing_framework/Resources/Core/Build/Configuration/Acceptance/AcceptanceTests-Job-<counter>
16 #
17 # Those sub-groups can then be executed with a command like (example for files in group 2 here)
18 # ./bin/codecept run Acceptance -d -g AcceptanceTests-Job-2 -c components/testing_framework/Resources/Core/Build/AcceptanceTests.yml
19 #
20 #########################
21
22 numberOfAcceptanceTestJobs=${1}
23
24 # Have a dir for temp files and clean up possibly existing stuff
25 if [ ! -d buildTemp ]; then
26 mkdir buildTemp || exit 1
27 fi
28 if [ -f buildTemp/testFiles.txt ]; then
29 rm buildTemp/testFiles.txt
30 fi
31 if [ -f buildTemp/testFilesWithNumberOfTestFiles.txt ]; then
32 rm buildTemp/testFilesWithNumberOfTestFiles.txt
33 fi
34 if [ -f buildTemp/testFilesWeighted.txt ]; then
35 rm buildTemp/testFilesWeighted.txt
36 fi
37
38 # A list of all acceptance test files
39 find . -name \*Cest.php -path \*typo3/sysext/*/Tests/Acceptance* > buildTemp/testFiles.txt
40
41 # File with test files of format "42 ./path/to/file"
42 while read testFile; do
43 numberOfTestsInTestFile=`grep "public function [^\_].*" ${testFile} | wc -l`
44 echo "${numberOfTestsInTestFile} ${testFile}" >> buildTemp/testFilesWithNumberOfTestFiles.txt
45 done < buildTemp/testFiles.txt
46
47 # Sort list of files numeric
48 cat buildTemp/testFilesWithNumberOfTestFiles.txt | sort -n -r > buildTemp/testFilesWeighted.txt
49
50 groupFilePath="components/testing_framework/Resources/Core/Build/Configuration/Acceptance"
51 # Config file boilerplate per job
52 for (( i=1; i<=${numberOfAcceptanceTestJobs}; i++)); do
53 if [ -f ${groupFilePath}/AcceptanceTests-Job-${i} ]; then
54 rm ${groupFilePath}/AcceptanceTests-Job-${i}
55 fi
56 touch ${groupFilePath}/AcceptanceTests-Job-${i}
57 done
58
59 counter=0
60 direction=ascending
61 while read testFileWeighted; do
62 # test file only, without leading ./
63 testFile=`echo ${testFileWeighted} | cut -f2 -d" " | cut -f2-40 -d"/"`
64
65 # Goal: with 3 jobs, have:
66 # file #0 to job #0 (asc)
67 # file #1 to job #1 (asc)
68 # file #2 to job #2 (asc)
69 # file #3 to job #2 (desc)
70 # file #4 to job #1 (desc)
71 # file #5 to job #0 (desc)
72 # file #6 to job #0 (asc)
73 # ...
74 testFileModuleNumberOfJobs=$(( counter % numberOfAcceptanceTestJobs ))
75 if [[ ${direction} == "descending" ]]; then
76 targetJobNumberForFile=$(( numberOfAcceptanceTestJobs - testFileModuleNumberOfJobs))
77 else
78 targetJobNumberForFile=${testFileModuleNumberOfJobs}
79 fi
80 if [ ${testFileModuleNumberOfJobs} -eq ${numberOfAcceptanceTestJobs} ]; then
81 if [[ ${direction} == "descending" ]]; then
82 direction=ascending
83 else
84 direction=descending
85 fi
86 fi
87 echo "../../../../../${testFile}" >> ${groupFilePath}/AcceptanceTests-Job-$(( targetJobNumberForFile + 1 ))
88 (( counter ++ ))
89 done < buildTemp/testFilesWeighted.txt
90
91 # Clean up
92 rm buildTemp/testFiles.txt
93 rm buildTemp/testFilesWeighted.txt
94 rm buildTemp/testFilesWithNumberOfTestFiles.txt
95 rmdir buildTemp