test-patch.sh 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. #!/usr/bin/env bash
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. #set -x
  14. ulimit -n 20480
  15. ### Setup some variables.
  16. ### SVN_REVISION and BUILD_URL are set by Hudson if it is run by patch process
  17. ### Read variables from properties file
  18. bindir=$(dirname $0)
  19. # Defaults
  20. if [ -z "$MAVEN_HOME" ]; then
  21. MVN=mvn
  22. else
  23. MVN=$MAVEN_HOME/bin/mvn
  24. fi
  25. PROJECT_NAME=Ambari
  26. JENKINS=false
  27. PATCH_DIR=/tmp
  28. SUPPORT_DIR=/tmp
  29. BASEDIR=$(pwd)
  30. PS=${PS:-ps}
  31. AWK=${AWK:-awk}
  32. WGET=${WGET:-wget}
  33. GIT=${GIT:-git}
  34. GREP=${GREP:-grep}
  35. PATCH=${PATCH:-patch}
  36. DIFF=${DIFF:-diff}
  37. JIRACLI=${JIRA:-jira}
  38. FINDBUGS_HOME=${FINDBUGS_HOME}
  39. FORREST_HOME=${FORREST_HOME}
  40. ECLIPSE_HOME=${ECLIPSE_HOME}
  41. ###############################################################################
  42. printUsage() {
  43. echo "Usage: $0 [options] patch-file | defect-number"
  44. echo
  45. echo "Where:"
  46. echo " patch-file is a local patch file containing the changes to test"
  47. echo " defect-number is a JIRA defect number (e.g. 'AMBARI-1234') to test (Jenkins only)"
  48. echo
  49. echo "Options:"
  50. echo "--patch-dir=<dir> The directory for working and output files (default '/tmp')"
  51. echo "--basedir=<dir> The directory to apply the patch to (default current directory)"
  52. echo "--mvn-cmd=<cmd> The 'mvn' command to use (default \$MAVEN_HOME/bin/mvn, or 'mvn')"
  53. echo "--ps-cmd=<cmd> The 'ps' command to use (default 'ps')"
  54. echo "--awk-cmd=<cmd> The 'awk' command to use (default 'awk')"
  55. echo "--git-cmd=<cmd> The 'git' command to use (default 'git')"
  56. echo "--grep-cmd=<cmd> The 'grep' command to use (default 'grep')"
  57. echo "--patch-cmd=<cmd> The 'patch' command to use (default 'patch')"
  58. echo "--diff-cmd=<cmd> The 'diff' command to use (default 'diff')"
  59. echo "--findbugs-home=<path> Findbugs home directory (default FINDBUGS_HOME environment variable)"
  60. echo "--forrest-home=<path> Forrest home directory (default FORREST_HOME environment variable)"
  61. echo "--dirty-workspace Allow the local SVN workspace to have uncommitted changes"
  62. echo "--run-tests Run all tests below the base directory"
  63. echo
  64. echo "Jenkins-only options:"
  65. echo "--jenkins Run by Jenkins (runs tests and posts results to JIRA)"
  66. echo "--support-dir=<dir> The directory to find support files in"
  67. echo "--wget-cmd=<cmd> The 'wget' command to use (default 'wget')"
  68. echo "--jira-cmd=<cmd> The 'jira' command to use (default 'jira')"
  69. echo "--jira-password=<pw> The password for the 'jira' command"
  70. echo "--eclipse-home=<path> Eclipse home directory (default ECLIPSE_HOME environment variable)"
  71. }
  72. ###############################################################################
  73. parseArgs() {
  74. for i in $*
  75. do
  76. case $i in
  77. --jenkins)
  78. JENKINS=true
  79. ;;
  80. --patch-dir=*)
  81. PATCH_DIR=${i#*=}
  82. ;;
  83. --support-dir=*)
  84. SUPPORT_DIR=${i#*=}
  85. ;;
  86. --basedir=*)
  87. BASEDIR=${i#*=}
  88. ;;
  89. --mvn-cmd=*)
  90. MVN=${i#*=}
  91. ;;
  92. --ps-cmd=*)
  93. PS=${i#*=}
  94. ;;
  95. --awk-cmd=*)
  96. AWK=${i#*=}
  97. ;;
  98. --wget-cmd=*)
  99. WGET=${i#*=}
  100. ;;
  101. --git-cmd=*)
  102. GIT=${i#*=}
  103. ;;
  104. --grep-cmd=*)
  105. GREP=${i#*=}
  106. ;;
  107. --patch-cmd=*)
  108. PATCH=${i#*=}
  109. ;;
  110. --diff-cmd=*)
  111. DIFF=${i#*=}
  112. ;;
  113. --jira-cmd=*)
  114. JIRACLI=${i#*=}
  115. ;;
  116. --jira-password=*)
  117. JIRA_PASSWD=${i#*=}
  118. ;;
  119. --findbugs-home=*)
  120. FINDBUGS_HOME=${i#*=}
  121. ;;
  122. --forrest-home=*)
  123. FORREST_HOME=${i#*=}
  124. ;;
  125. --eclipse-home=*)
  126. ECLIPSE_HOME=${i#*=}
  127. ;;
  128. --dirty-workspace)
  129. DIRTY_WORKSPACE=true
  130. ;;
  131. --run-tests)
  132. RUN_TESTS=true
  133. ;;
  134. *)
  135. PATCH_OR_DEFECT=$i
  136. ;;
  137. esac
  138. done
  139. if [ -z "$PATCH_OR_DEFECT" ]; then
  140. printUsage
  141. exit 1
  142. fi
  143. if [[ $JENKINS == "true" ]] ; then
  144. echo "Running in Jenkins mode"
  145. defect=$PATCH_OR_DEFECT
  146. ECLIPSE_PROPERTY="-Declipse.home=$ECLIPSE_HOME"
  147. else
  148. echo "Running in developer mode"
  149. JENKINS=false
  150. ### PATCH_FILE contains the location of the patchfile
  151. PATCH_FILE=$PATCH_OR_DEFECT
  152. if [[ ! -e "$PATCH_FILE" ]] ; then
  153. echo "Unable to locate the patch file $PATCH_FILE"
  154. cleanupAndExit 0
  155. fi
  156. ### Obtain the patch filename to append it to the version number
  157. defect=`basename $PATCH_FILE`
  158. fi
  159. }
  160. ###############################################################################
  161. checkout () {
  162. echo ""
  163. echo ""
  164. echo "======================================================================"
  165. echo "======================================================================"
  166. echo " Testing patch for ${defect}."
  167. echo "======================================================================"
  168. echo "======================================================================"
  169. echo ""
  170. echo ""
  171. ### When run by a developer, if the workspace contains modifications, do not continue
  172. ### unless the --dirty-workspace option was set
  173. # status=`$GIT status -s | sed -e '/^X[ ]*/D'`
  174. status=`$GIT status -s `
  175. if [[ $JENKINS == "false" ]] ; then
  176. if [[ "$status" != "" && -z $DIRTY_WORKSPACE ]] ; then
  177. echo "ERROR: can't run in a workspace that contains the following modifications"
  178. echo "$status"
  179. cleanupAndExit 1
  180. fi
  181. echo
  182. else
  183. cd $BASEDIR
  184. $GIT checkout -- .
  185. $GIT clean -x -f -d
  186. $GIT pull
  187. fi
  188. return $?
  189. }
  190. ###############################################################################
  191. downloadPatch () {
  192. ### Download latest patch file (ignoring .htm and .html) when run from patch process
  193. if [[ $JENKINS == "true" ]] ; then
  194. $WGET -q -O $PATCH_DIR/jira http://issues.apache.org/jira/browse/$defect
  195. if [[ `$GREP -c 'Patch Available' $PATCH_DIR/jira` == 0 ]] ; then
  196. echo "$defect is not \"Patch Available\". Exiting."
  197. cleanupAndExit 0
  198. fi
  199. relativePatchURL=`$GREP -o '"/jira/secure/attachment/[0-9]*/[^"]*' $PATCH_DIR/jira | $GREP -v -e 'htm[l]*$' | sort | tail -1 | $GREP -o '/jira/secure/attachment/[0-9]*/[^"]*'`
  200. patchURL="http://issues.apache.org${relativePatchURL}"
  201. patchNum=`echo $patchURL | $GREP -o '[0-9]*/' | $GREP -o '[0-9]*'`
  202. echo "$defect patch is being downloaded at `date` from"
  203. echo "$patchURL"
  204. $WGET -q -O $PATCH_DIR/patch $patchURL
  205. VERSION=${SVN_REVISION}_${defect}_PATCH-${patchNum}
  206. JIRA_COMMENT="Here are the results of testing the latest attachment
  207. $patchURL
  208. against trunk revision ${SVN_REVISION}."
  209. ### Copy in any supporting files needed by this process
  210. # cp -r $SUPPORT_DIR/lib/* ./lib
  211. #PENDING: cp -f $SUPPORT_DIR/etc/checkstyle* ./src/test
  212. ### Copy the patch file to $PATCH_DIR
  213. else
  214. VERSION=PATCH-${defect}
  215. cp $PATCH_FILE $PATCH_DIR/patch
  216. if [[ $? == 0 ]] ; then
  217. echo "Patch file $PATCH_FILE copied to $PATCH_DIR"
  218. else
  219. echo "Could not copy $PATCH_FILE to $PATCH_DIR"
  220. cleanupAndExit 0
  221. fi
  222. fi
  223. }
  224. ###############################################################################
  225. verifyPatch () {
  226. # Before building, check to make sure that the patch is valid
  227. # $bindir/smart-apply-patch.sh $PATCH_DIR/patch dryrun
  228. $GIT apply --check $PATCH_DIR/patch
  229. if [[ $? != 0 ]] ; then
  230. echo "PATCH APPLICATION FAILED"
  231. JIRA_COMMENT="$JIRA_COMMENT
  232. {color:red}-1 patch{color}. The patch command could not apply the patch."
  233. return 1
  234. else
  235. return 0
  236. fi
  237. }
  238. ###############################################################################
  239. prebuildWithoutPatch () {
  240. echo ""
  241. echo ""
  242. echo "======================================================================"
  243. echo "======================================================================"
  244. echo " Pre-build trunk to verify trunk stability and javac warnings"
  245. echo "======================================================================"
  246. echo "======================================================================"
  247. echo ""
  248. echo ""
  249. echo "Compiling $(pwd)"
  250. echo "$MVN clean test -DskipTests -Dxlint=all -D${PROJECT_NAME}PatchProcess > $PATCH_DIR/trunkJavacWarnings.txt 2>&1"
  251. $MVN clean test -DskipTests -Dxlint=all -D${PROJECT_NAME}PatchProcess > $PATCH_DIR/trunkJavacWarnings.txt 2>&1
  252. if [[ $? != 0 ]] ; then
  253. echo "Top-level trunk compilation is broken?"
  254. JIRA_COMMENT="$JIRA_COMMENT
  255. {color:red}-1 patch{color}. Top-level [trunk compilation|$BUILD_URL/artifact/patch-work/trunkJavacWarnings.txt] may be broken."
  256. return 1
  257. fi
  258. return 0
  259. }
  260. ###############################################################################
  261. ### Check for @author tags in the patch
  262. checkAuthor () {
  263. echo ""
  264. echo ""
  265. echo "======================================================================"
  266. echo "======================================================================"
  267. echo " Checking there are no @author tags in the patch."
  268. echo "======================================================================"
  269. echo "======================================================================"
  270. echo ""
  271. echo ""
  272. authorTags=`$GREP -c -i '@author' $PATCH_DIR/patch`
  273. echo "There appear to be $authorTags @author tags in the patch."
  274. if [[ $authorTags != 0 ]] ; then
  275. JIRA_COMMENT="$JIRA_COMMENT
  276. {color:red}-1 @author{color}. The patch appears to contain $authorTags @author tags which the Hadoop community has agreed to not allow in code contributions."
  277. return 1
  278. fi
  279. JIRA_COMMENT="$JIRA_COMMENT
  280. {color:green}+1 @author{color}. The patch does not contain any @author tags."
  281. return 0
  282. }
  283. ###############################################################################
  284. ### Check for tests in the patch
  285. checkTests () {
  286. echo ""
  287. echo ""
  288. echo "======================================================================"
  289. echo "======================================================================"
  290. echo " Checking there are new or changed tests in the patch."
  291. echo "======================================================================"
  292. echo "======================================================================"
  293. echo ""
  294. echo ""
  295. testReferences=`$GREP -c -i -e '^+++.*/test' $PATCH_DIR/patch`
  296. echo "There appear to be $testReferences test files referenced in the patch."
  297. if [[ $testReferences == 0 ]] ; then
  298. if [[ $JENKINS == "true" ]] ; then
  299. patchIsDoc=`$GREP -c -i 'title="documentation' $PATCH_DIR/jira`
  300. if [[ $patchIsDoc != 0 ]] ; then
  301. echo "The patch appears to be a documentation patch that doesn't require tests."
  302. JIRA_COMMENT="$JIRA_COMMENT
  303. {color:green}+0 tests included{color}. The patch appears to be a documentation patch that doesn't require tests."
  304. return 0
  305. fi
  306. fi
  307. JIRA_COMMENT="$JIRA_COMMENT
  308. {color:red}-1 tests included{color}. The patch doesn't appear to include any new or modified tests.
  309. Please justify why no new tests are needed for this patch.
  310. Also please list what manual steps were performed to verify this patch."
  311. return 1
  312. fi
  313. JIRA_COMMENT="$JIRA_COMMENT
  314. {color:green}+1 tests included{color}. The patch appears to include $testReferences new or modified test files."
  315. return 0
  316. }
  317. cleanUpXml () {
  318. cd $BASEDIR/conf
  319. for file in `ls *.xml.template`
  320. do
  321. rm -f `basename $file .template`
  322. done
  323. cd $BASEDIR
  324. }
  325. ###############################################################################
  326. ### Attempt to apply the patch
  327. applyPatch () {
  328. echo ""
  329. echo ""
  330. echo "======================================================================"
  331. echo "======================================================================"
  332. echo " Applying patch."
  333. echo "======================================================================"
  334. echo "======================================================================"
  335. echo ""
  336. echo ""
  337. export PATCH
  338. # $bindir/smart-apply-patch.sh $PATCH_DIR/patch
  339. $GIT apply $PATCH_DIR/patch
  340. if [[ $? != 0 ]] ; then
  341. echo "PATCH APPLICATION FAILED"
  342. JIRA_COMMENT="$JIRA_COMMENT
  343. {color:red}-1 patch{color}. The patch command could not apply the patch."
  344. return 1
  345. fi
  346. return 0
  347. }
  348. ###############################################################################
  349. ### Check there are no changes in the number of Javac warnings
  350. checkJavacWarnings () {
  351. echo ""
  352. echo ""
  353. echo "======================================================================"
  354. echo "======================================================================"
  355. echo " Determining number of patched javac warnings."
  356. echo "======================================================================"
  357. echo "======================================================================"
  358. echo ""
  359. echo ""
  360. echo "$MVN clean test -DskipTests -Dxlint=all -D${PROJECT_NAME}PatchProcess > $PATCH_DIR/patchJavacWarnings.txt 2>&1"
  361. $MVN clean test -DskipTests -Dxlint=all -D${PROJECT_NAME}PatchProcess > $PATCH_DIR/patchJavacWarnings.txt 2>&1
  362. if [[ $? != 0 ]] ; then
  363. JIRA_COMMENT="$JIRA_COMMENT
  364. {color:red}-1 javac{color}. The patch appears to cause the [build to fail|$BUILD_URL/artifact/patch-work/patchJavacWarnings.txt]."
  365. return 2
  366. fi
  367. ### Compare trunk and patch javac warning numbers
  368. if [[ -f $PATCH_DIR/patchJavacWarnings.txt ]] ; then
  369. $GREP '\[WARNING\]' $PATCH_DIR/trunkJavacWarnings.txt > $PATCH_DIR/filteredTrunkJavacWarnings.txt
  370. $GREP '\[WARNING\]' $PATCH_DIR/patchJavacWarnings.txt > $PATCH_DIR/filteredPatchJavacWarnings.txt
  371. trunkJavacWarnings=`cat $PATCH_DIR/filteredTrunkJavacWarnings.txt | $AWK 'BEGIN {total = 0} {total += 1} END {print total}'`
  372. patchJavacWarnings=`cat $PATCH_DIR/filteredPatchJavacWarnings.txt | $AWK 'BEGIN {total = 0} {total += 1} END {print total}'`
  373. echo "There appear to be $trunkJavacWarnings javac compiler warnings before the patch and $patchJavacWarnings javac compiler warnings after applying the patch."
  374. if [[ $patchJavacWarnings != "" && $trunkJavacWarnings != "" ]] ; then
  375. if [[ $patchJavacWarnings -gt $trunkJavacWarnings ]] ; then
  376. JIRA_COMMENT="$JIRA_COMMENT
  377. {color:red}-1 javac{color}. The applied patch generated $patchJavacWarnings javac compiler warnings (more than the trunk's current $trunkJavacWarnings warnings)."
  378. $DIFF $PATCH_DIR/filteredTrunkJavacWarnings.txt $PATCH_DIR/filteredPatchJavacWarnings.txt > $PATCH_DIR/diffJavacWarnings.txt
  379. JIRA_COMMENT_FOOTER="Javac warnings: $BUILD_URL/artifact/patch-work/diffJavacWarnings.txt
  380. $JIRA_COMMENT_FOOTER"
  381. return 1
  382. fi
  383. fi
  384. fi
  385. JIRA_COMMENT="$JIRA_COMMENT
  386. {color:green}+1 javac{color}. The applied patch does not increase the total number of javac compiler warnings."
  387. return 0
  388. }
  389. ###############################################################################
  390. ### Check there are no changes in the number of release audit (RAT) warnings
  391. checkReleaseAuditWarnings () {
  392. echo ""
  393. echo ""
  394. echo "======================================================================"
  395. echo "======================================================================"
  396. echo " Determining number of patched release audit warnings."
  397. echo "======================================================================"
  398. echo "======================================================================"
  399. echo ""
  400. echo ""
  401. echo "$MVN apache-rat:check -D${PROJECT_NAME}PatchProcess > $PATCH_DIR/patchReleaseAuditOutput.txt 2>&1"
  402. $MVN apache-rat:check -D${PROJECT_NAME}PatchProcess > $PATCH_DIR/patchReleaseAuditOutput.txt 2>&1
  403. find $BASEDIR -name rat.txt | xargs cat > $PATCH_DIR/patchReleaseAuditWarnings.txt
  404. ### Compare trunk and patch release audit warning numbers
  405. if [[ -f $PATCH_DIR/patchReleaseAuditWarnings.txt ]] ; then
  406. patchReleaseAuditWarnings=`$GREP -c '\!?????' $PATCH_DIR/patchReleaseAuditWarnings.txt`
  407. echo ""
  408. echo ""
  409. echo "There appear to be $patchReleaseAuditWarnings release audit warnings after applying the patch."
  410. if [[ $patchReleaseAuditWarnings != "" ]] ; then
  411. if [[ $patchReleaseAuditWarnings -gt 0 ]] ; then
  412. JIRA_COMMENT="$JIRA_COMMENT
  413. {color:red}-1 release audit{color}. The applied patch generated $patchReleaseAuditWarnings release audit warnings."
  414. $GREP '\!?????' $PATCH_DIR/patchReleaseAuditWarnings.txt > $PATCH_DIR/patchReleaseAuditProblems.txt
  415. echo "Lines that start with ????? in the release audit report indicate files that do not have an Apache license header." >> $PATCH_DIR/patchReleaseAuditProblems.txt
  416. JIRA_COMMENT_FOOTER="Release audit warnings: $BUILD_URL/artifact/patch-work/patchReleaseAuditProblems.txt
  417. $JIRA_COMMENT_FOOTER"
  418. return 1
  419. fi
  420. fi
  421. fi
  422. JIRA_COMMENT="$JIRA_COMMENT
  423. {color:green}+1 release audit{color}. The applied patch does not increase the total number of release audit warnings."
  424. return 0
  425. }
  426. ###############################################################################
  427. ### Check there are no changes in the number of Checkstyle warnings
  428. checkStyle () {
  429. echo ""
  430. echo ""
  431. echo "======================================================================"
  432. echo "======================================================================"
  433. echo " Determining number of patched checkstyle warnings."
  434. echo "======================================================================"
  435. echo "======================================================================"
  436. echo ""
  437. echo ""
  438. echo "THIS IS NOT IMPLEMENTED YET"
  439. echo ""
  440. echo ""
  441. echo "$MVN test checkstyle:checkstyle -DskipTests -D${PROJECT_NAME}PatchProcess"
  442. $MVN test checkstyle:checkstyle -DskipTests -D${PROJECT_NAME}PatchProcess
  443. JIRA_COMMENT_FOOTER="Checkstyle results: $BUILD_URL/artifact/trunk/build/test/checkstyle-errors.html
  444. $JIRA_COMMENT_FOOTER"
  445. ### TODO: calculate actual patchStyleErrors
  446. # patchStyleErrors=0
  447. # if [[ $patchStyleErrors != 0 ]] ; then
  448. # JIRA_COMMENT="$JIRA_COMMENT
  449. #
  450. # {color:red}-1 checkstyle{color}. The patch generated $patchStyleErrors code style errors."
  451. # return 1
  452. # fi
  453. # JIRA_COMMENT="$JIRA_COMMENT
  454. #
  455. # {color:green}+1 checkstyle{color}. The patch generated 0 code style errors."
  456. return 0
  457. }
  458. ###############################################################################
  459. ### Install the new jars so tests and findbugs can find all of the updated jars
  460. buildAndInstall () {
  461. echo ""
  462. echo ""
  463. echo "======================================================================"
  464. echo "======================================================================"
  465. echo " Installing all of the jars"
  466. echo "======================================================================"
  467. echo "======================================================================"
  468. echo ""
  469. echo ""
  470. echo "$MVN install -Dmaven.javadoc.skip=true -DskipTests -D${PROJECT_NAME}PatchProcess"
  471. $MVN install -Dmaven.javadoc.skip=true -DskipTests -D${PROJECT_NAME}PatchProcess
  472. return $?
  473. }
  474. ###############################################################################
  475. ### Check there are no changes in the number of Findbugs warnings
  476. checkFindbugsWarnings () {
  477. findbugs_version=`${FINDBUGS_HOME}/bin/findbugs -version`
  478. echo ""
  479. echo ""
  480. echo "======================================================================"
  481. echo "======================================================================"
  482. echo " Determining number of patched Findbugs warnings."
  483. echo "======================================================================"
  484. echo "======================================================================"
  485. echo ""
  486. echo ""
  487. modules=$(findModules)
  488. rc=0
  489. for module in $modules;
  490. do
  491. cd $module
  492. echo " Running findbugs in $module"
  493. module_suffix=`basename ${module}`
  494. echo "$MVN clean test findbugs:findbugs -DskipTests -D${PROJECT_NAME}PatchProcess < /dev/null > $PATCH_DIR/patchFindBugsOutput${module_suffix}.txt 2>&1"
  495. $MVN clean test findbugs:findbugs -DskipTests -D${PROJECT_NAME}PatchProcess < /dev/null > $PATCH_DIR/patchFindBugsOutput${module_suffix}.txt 2>&1
  496. (( rc = rc + $? ))
  497. cd -
  498. done
  499. if [ $rc != 0 ] ; then
  500. JIRA_COMMENT="$JIRA_COMMENT
  501. {color:red}-1 findbugs{color}. The patch appears to cause Findbugs (version ${findbugs_version}) to fail."
  502. return 1
  503. fi
  504. findbugsWarnings=0
  505. for file in $(find $BASEDIR -name findbugsXml.xml)
  506. do
  507. relative_file=${file#$BASEDIR/} # strip leading $BASEDIR prefix
  508. if [ ! $relative_file == "target/findbugsXml.xml" ]; then
  509. module_suffix=${relative_file%/target/findbugsXml.xml} # strip trailing path
  510. module_suffix=`basename ${module_suffix}`
  511. fi
  512. cp $file $PATCH_DIR/patchFindbugsWarnings${module_suffix}.xml
  513. $FINDBUGS_HOME/bin/setBugDatabaseInfo -timestamp "01/01/2000" \
  514. $PATCH_DIR/patchFindbugsWarnings${module_suffix}.xml \
  515. $PATCH_DIR/patchFindbugsWarnings${module_suffix}.xml
  516. newFindbugsWarnings=`$FINDBUGS_HOME/bin/filterBugs -first "01/01/2000" $PATCH_DIR/patchFindbugsWarnings${module_suffix}.xml \
  517. $PATCH_DIR/newPatchFindbugsWarnings${module_suffix}.xml | $AWK '{print $1}'`
  518. echo "Found $newFindbugsWarnings Findbugs warnings ($file)"
  519. findbugsWarnings=$((findbugsWarnings+newFindbugsWarnings))
  520. $FINDBUGS_HOME/bin/convertXmlToText -html \
  521. $PATCH_DIR/newPatchFindbugsWarnings${module_suffix}.xml \
  522. $PATCH_DIR/newPatchFindbugsWarnings${module_suffix}.html
  523. if [[ $newFindbugsWarnings > 0 ]] ; then
  524. JIRA_COMMENT_FOOTER="Findbugs warnings: $BUILD_URL/artifact/patch-work/newPatchFindbugsWarnings${module_suffix}.html
  525. $JIRA_COMMENT_FOOTER"
  526. fi
  527. done
  528. if [[ $findbugsWarnings -gt 0 ]] ; then
  529. JIRA_COMMENT="$JIRA_COMMENT
  530. {color:red}-1 findbugs{color}. The patch appears to introduce $findbugsWarnings new Findbugs (version ${findbugs_version}) warnings."
  531. return 1
  532. fi
  533. JIRA_COMMENT="$JIRA_COMMENT
  534. {color:green}+1 findbugs{color}. The patch does not introduce any new Findbugs (version ${findbugs_version}) warnings."
  535. return 0
  536. }
  537. ###############################################################################
  538. ### Verify eclipse:eclipse works
  539. checkEclipseGeneration () {
  540. echo ""
  541. echo ""
  542. echo "======================================================================"
  543. echo "======================================================================"
  544. echo " Running mvn eclipse:eclipse."
  545. echo "======================================================================"
  546. echo "======================================================================"
  547. echo ""
  548. echo ""
  549. echo "$MVN eclipse:eclipse -D${PROJECT_NAME}PatchProcess > $PATCH_DIR/patchEclipseOutput.txt 2>&1"
  550. $MVN eclipse:eclipse -D${PROJECT_NAME}PatchProcess > $PATCH_DIR/patchEclipseOutput.txt 2>&1
  551. if [[ $? != 0 ]] ; then
  552. JIRA_COMMENT="$JIRA_COMMENT
  553. {color:red}-1 eclipse:eclipse{color}. The patch failed to build with eclipse:eclipse."
  554. return 1
  555. fi
  556. JIRA_COMMENT="$JIRA_COMMENT
  557. {color:green}+1 eclipse:eclipse{color}. The patch built with eclipse:eclipse."
  558. return 0
  559. }
  560. ###############################################################################
  561. ### Run the tests
  562. runTests () {
  563. echo ""
  564. echo ""
  565. echo "======================================================================"
  566. echo "======================================================================"
  567. echo " Running tests."
  568. echo "======================================================================"
  569. echo "======================================================================"
  570. echo ""
  571. echo ""
  572. failed_tests=""
  573. modules=$(findModules)
  574. failed_test_builds=""
  575. test_timeouts=""
  576. # Get the list of modified files
  577. TMP=/tmp/tmp.paths.$$
  578. $GREP '^+++ \|^--- ' $PATCH_DIR/patch | cut -c '5-' | $GREP -v /dev/null | sort > $TMP
  579. # if all of the lines start with a/ or b/, then this is a git patch that
  580. # was generated without --no-prefix
  581. if ! $GREP -qv '^a/\|^b/' $TMP ; then
  582. sed -i -e 's,^[ab]/,,' $TMP
  583. fi
  584. for module in $modules; do
  585. cd $module
  586. skip_surefiretests=1
  587. module_suffix=`basename ${module}`
  588. echo "Get the list of files modified in module $module"
  589. # Come up with a list of changed files for $module into $MODULE_FILES
  590. MODULE_FILES=/tmp/${module_suffix}_files.txt
  591. $GREP $module $TMP > $MODULE_FILES
  592. module_files=$(cat $MODULE_FILES)
  593. for module_filename in $module_files; do
  594. file_ext=$(echo $module_filename | awk -F . '{if (NF>1) {print $NF}}')
  595. # Look for java extension
  596. if [[ "$file_ext" == "java" ]]; then
  597. skip_surefiretests=0
  598. break
  599. fi
  600. done
  601. test_logfile=$PATCH_DIR/testrun_${module_suffix}.txt
  602. test_logfile_url=$BUILD_URL/artifact/patch-work/testrun_${module_suffix}.txt
  603. echo " Running tests in $module"
  604. # Skip java tests if this module did not have changes to java files
  605. if [[ $skip_surefiretests -eq 0 ]];
  606. then
  607. echo " $MVN clean install -fae -D${PROJECT_NAME}PatchProcess"
  608. $MVN clean install -fae -D${PROJECT_NAME}PatchProcess > $test_logfile 2>&1
  609. else
  610. echo " $MVN clean install -fae -D${PROJECT_NAME}PatchProcess -DskipSurefireTests"
  611. $MVN clean install -fae -D${PROJECT_NAME}PatchProcess -DskipSurefireTests > $test_logfile 2>&1
  612. fi
  613. test_build_result=$?
  614. cat $test_logfile
  615. # module_test_timeouts=`$AWK '/^Running / { if (last) { print last } last=$2 } /^Tests run: / { last="" }' $test_logfile`
  616. module_test_timeouts=""
  617. if [[ -n "$module_test_timeouts" ]] ; then
  618. test_timeouts="$test_timeouts
  619. $module_test_timeouts"
  620. fi
  621. module_failed_tests=`find . -name 'TEST*.xml' | xargs $GREP -l -E "<failure|<error" | sed -e "s|.*target/surefire-reports/TEST-| |g" | sed -e "s|\.xml||g"`
  622. if [[ -n "$module_failed_tests" ]] ; then
  623. failed_tests="${failed_tests}
  624. ${module_failed_tests}"
  625. fi
  626. if [[ $test_build_result != 0 && -z "$module_failed_tests" && -z "$module_test_timeouts" ]] ; then
  627. failed_test_builds="[$module|$test_logfile_url] $failed_test_builds"
  628. fi
  629. cd -
  630. done
  631. result=0
  632. comment_prefix=" {color:red}-1 core tests{color}."
  633. if [[ -n "$failed_tests" ]] ; then
  634. JIRA_COMMENT="$JIRA_COMMENT
  635. $comment_prefix The patch failed these unit tests in $modules:
  636. $failed_tests"
  637. comment_prefix=" "
  638. result=1
  639. fi
  640. if [[ -n "$test_timeouts" ]] ; then
  641. JIRA_COMMENT="$JIRA_COMMENT
  642. $comment_prefix The following test timeouts occurred in $modules:
  643. $test_timeouts"
  644. comment_prefix=" "
  645. result=1
  646. fi
  647. if [[ -n "$failed_test_builds" ]] ; then
  648. JIRA_COMMENT="$JIRA_COMMENT
  649. $comment_prefix The test build failed in $failed_test_builds"
  650. result=1
  651. fi
  652. if [[ $result == 0 ]] ; then
  653. JIRA_COMMENT="$JIRA_COMMENT
  654. {color:green}+1 core tests{color}. The patch passed unit tests in $modules."
  655. fi
  656. return $result
  657. }
  658. ###############################################################################
  659. # Find the maven module containing the given file.
  660. findModule (){
  661. dir=`dirname $1`
  662. while [ 1 ]
  663. do
  664. if [ -f "$dir/pom.xml" ]
  665. then
  666. echo $dir
  667. return
  668. else
  669. dir=`dirname $dir`
  670. fi
  671. done
  672. }
  673. findModules () {
  674. # Come up with a list of changed files into $TMP
  675. TMP=/tmp/tmp.paths.$$
  676. $GREP '^+++ \|^--- ' $PATCH_DIR/patch | cut -c '5-' | $GREP -v /dev/null | sort | uniq > $TMP
  677. # if all of the lines start with a/ or b/, then this is a git patch that
  678. # was generated without --no-prefix
  679. if ! $GREP -qv '^a/\|^b/' $TMP ; then
  680. sed -i -e 's,^[ab]/,,' $TMP
  681. fi
  682. # Now find all the modules that were changed
  683. TMP_MODULES=/tmp/tmp.modules.$$
  684. for file in $(cut -f 1 $TMP | sort | uniq); do
  685. echo $(findModule $file) >> $TMP_MODULES
  686. done
  687. rm $TMP
  688. # Filter out modules without code
  689. CHANGED_MODULES=""
  690. for module in $(cat $TMP_MODULES | sort | uniq); do
  691. $GREP "<packaging>pom</packaging>" $module/pom.xml > /dev/null
  692. if [ "$?" != 0 ] || [ "$module" == "ambari-web" ]; then # ambari-web is packing = pom but has unit tests
  693. CHANGED_MODULES="$CHANGED_MODULES $module"
  694. fi
  695. done
  696. rm $TMP_MODULES
  697. echo $CHANGED_MODULES
  698. }
  699. ###############################################################################
  700. ### Run the test-contrib target
  701. runContribTests () {
  702. echo ""
  703. echo ""
  704. echo "======================================================================"
  705. echo "======================================================================"
  706. echo " Running contrib tests."
  707. echo "======================================================================"
  708. echo "======================================================================"
  709. echo ""
  710. echo ""
  711. if [[ `$GREP -c 'test-contrib' build.xml` == 0 ]] ; then
  712. echo "No contrib tests in this project."
  713. return 0
  714. fi
  715. ### Kill any rogue build processes from the last attempt
  716. $PS auxwww | $GREP ${PROJECT_NAME}PatchProcess | $AWK '{print $2}' | /usr/bin/xargs -t -I {} /bin/kill -9 {} > /dev/null
  717. echo "NOP"
  718. if [[ $? != 0 ]] ; then
  719. JIRA_COMMENT="$JIRA_COMMENT
  720. {color:red}-1 contrib tests{color}. The patch failed contrib unit tests."
  721. return 1
  722. fi
  723. JIRA_COMMENT="$JIRA_COMMENT
  724. {color:green}+1 contrib tests{color}. The patch passed contrib unit tests."
  725. return 0
  726. }
  727. ###############################################################################
  728. ### Run the inject-system-faults target
  729. checkInjectSystemFaults () {
  730. echo ""
  731. echo ""
  732. echo "======================================================================"
  733. echo "======================================================================"
  734. echo " Checking the integrity of system test framework code."
  735. echo "======================================================================"
  736. echo "======================================================================"
  737. echo ""
  738. echo ""
  739. ### Kill any rogue build processes from the last attempt
  740. $PS auxwww | $GREP ${PROJECT_NAME}PatchProcess | $AWK '{print $2}' | /usr/bin/xargs -t -I {} /bin/kill -9 {} > /dev/null
  741. echo "NOP"
  742. return 0
  743. if [[ $? != 0 ]] ; then
  744. JIRA_COMMENT="$JIRA_COMMENT
  745. {color:red}-1 system test framework{color}. The patch failed system test framework compile."
  746. return 1
  747. fi
  748. JIRA_COMMENT="$JIRA_COMMENT
  749. {color:green}+1 system test framework{color}. The patch passed system test framework compile."
  750. return 0
  751. }
  752. ###############################################################################
  753. ### Submit a comment to the defect's Jira
  754. submitJiraComment () {
  755. local result=$1
  756. ### Do not output the value of JIRA_COMMENT_FOOTER when run by a developer
  757. if [[ $JENKINS == "false" ]] ; then
  758. JIRA_COMMENT_FOOTER=""
  759. fi
  760. if [[ $result == 0 ]] ; then
  761. comment="{color:green}+1 overall{color}. $JIRA_COMMENT
  762. $JIRA_COMMENT_FOOTER"
  763. else
  764. comment="{color:red}-1 overall{color}. $JIRA_COMMENT
  765. $JIRA_COMMENT_FOOTER"
  766. fi
  767. ### Output the test result to the console
  768. echo "
  769. $comment"
  770. if [[ $JENKINS == "true" ]] ; then
  771. echo ""
  772. echo ""
  773. echo "======================================================================"
  774. echo "======================================================================"
  775. echo " Adding comment to Jira."
  776. echo "======================================================================"
  777. echo "======================================================================"
  778. echo ""
  779. echo ""
  780. ### Update Jira with a comment
  781. export USER=hudson
  782. $JIRACLI -s https://issues.apache.org/jira -a addcomment -u hadoopqa -p $JIRA_PASSWD --comment "$comment" --issue $defect
  783. $JIRACLI -s https://issues.apache.org/jira -a logout -u hadoopqa -p $JIRA_PASSWD
  784. fi
  785. }
  786. ###############################################################################
  787. ### Cleanup files
  788. cleanupAndExit () {
  789. local result=$1
  790. echo ""
  791. echo ""
  792. echo "======================================================================"
  793. echo "======================================================================"
  794. echo " Finished build."
  795. echo "======================================================================"
  796. echo "======================================================================"
  797. echo ""
  798. echo ""
  799. exit $result
  800. }
  801. ###############################################################################
  802. ###############################################################################
  803. ###############################################################################
  804. JIRA_COMMENT=""
  805. JIRA_COMMENT_FOOTER="Console output: $BUILD_URL/console
  806. This message is automatically generated."
  807. ### Check if arguments to the script have been specified properly or not
  808. parseArgs $@
  809. cd $BASEDIR
  810. checkout
  811. RESULT=$?
  812. if [[ $JENKINS == "true" ]] ; then
  813. if [[ $RESULT != 0 ]] ; then
  814. exit 100
  815. fi
  816. fi
  817. ### Check if $PATCH_DIR exists. If it does not exist, create a new directory
  818. if [[ ! -e "$PATCH_DIR" ]] ; then
  819. mkdir "$PATCH_DIR"
  820. if [[ $? == 0 ]] ; then
  821. echo "$PATCH_DIR has been created"
  822. else
  823. echo "Unable to create $PATCH_DIR"
  824. cleanupAndExit 0
  825. fi
  826. fi
  827. downloadPatch
  828. verifyPatch
  829. (( RESULT = RESULT + $? ))
  830. if [[ $RESULT != 0 ]] ; then
  831. submitJiraComment 1
  832. cleanupAndExit 1
  833. fi
  834. prebuildWithoutPatch
  835. (( RESULT = RESULT + $? ))
  836. if [[ $RESULT != 0 ]] ; then
  837. submitJiraComment 1
  838. cleanupAndExit 1
  839. fi
  840. checkAuthor
  841. (( RESULT = RESULT + $? ))
  842. # if [[ $JENKINS == "true" ]] ; then
  843. # cleanUpXml
  844. # fi
  845. echo "before checkTests $RESULT"
  846. checkTests
  847. (( RESULT = RESULT + $? ))
  848. applyPatch
  849. APPLY_PATCH_RET=$?
  850. (( RESULT = RESULT + $APPLY_PATCH_RET ))
  851. echo "after applyPatch $RESULT"
  852. if [[ $APPLY_PATCH_RET != 0 ]] ; then
  853. submitJiraComment 1
  854. cleanupAndExit 1
  855. fi
  856. echo "before checkReleaseAuditWarnings $RESULT"
  857. checkReleaseAuditWarnings
  858. (( RESULT = RESULT + $? ))
  859. checkJavacWarnings
  860. JAVAC_RET=$?
  861. #2 is returned if the code could not compile
  862. if [[ $JAVAC_RET == 2 ]] ; then
  863. submitJiraComment 1
  864. cleanupAndExit 1
  865. fi
  866. (( RESULT = RESULT + $JAVAC_RET ))
  867. ### Checkstyle not implemented yet
  868. #checkStyle
  869. #(( RESULT = RESULT + $? ))
  870. echo "before buildAndInstall $RESULT"
  871. buildAndInstall
  872. # checkEclipseGeneration
  873. # (( RESULT = RESULT + $? ))
  874. # checkFindbugsWarnings
  875. # (( RESULT = RESULT + $? ))
  876. echo "before runTests $RESULT"
  877. ### Run tests for Jenkins or if explictly asked for by a developer
  878. if [[ $JENKINS == "true" || $RUN_TESTS == "true" ]] ; then
  879. runTests
  880. (( RESULT = RESULT + $? ))
  881. # runContribTests
  882. # (( RESULT = RESULT + $? ))
  883. fi
  884. checkInjectSystemFaults
  885. (( RESULT = RESULT + $? ))
  886. submitJiraComment $RESULT
  887. cleanupAndExit $RESULT