|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * OpenSearch Plugin build tools don't work with the Gradle Jacoco Plugin to report coverage out of the box. |
| 8 | + * https://github.com/elastic/elasticsearch/issues/28867. |
| 9 | + * |
| 10 | + * This code sets up coverage reporting manually for OpenSearch plugin tests. This is complicated because: |
| 11 | + * 1. The OpenSearch integTest Task doesn't implement Gradle's JavaForkOptions so we have to manually start the jacoco agent with the test JVM |
| 12 | + * 2. The cluster nodes are stopped using 'kill -9' which means jacoco can't dump it's execution output to a file on VM shutdown |
| 13 | + * 3. The Java Security Manager prevents JMX from writing execution output to the file. |
| 14 | + * |
| 15 | + * To workaround these we start the cluster with jmx enabled and then use Jacoco's JMX MBean to get the execution data before the |
| 16 | + * cluster is stopped and dump it to a file. Luckily our current security policy seems to allow this. This will also probably |
| 17 | + * break if there are multiple nodes in the integTestCluster. But for now... it sorta works. |
| 18 | + */ |
| 19 | +apply plugin: 'jacoco' |
| 20 | + |
| 21 | +// Get gradle to generate the required jvm agent arg for us using a dummy tasks of type Test. Unfortunately Elastic's |
| 22 | +// testing tasks don't derive from Test so the jacoco plugin can't do this automatically. |
| 23 | +def jacocoDir = "${buildDir}/jacoco" |
| 24 | +task dummyTest(type: Test) { |
| 25 | + enabled = false |
| 26 | + workingDir = file("/") // Force absolute path to jacoco agent jar |
| 27 | + jacoco { |
| 28 | + destinationFile = file("${jacocoDir}/test.exec") |
| 29 | + destinationFile.parentFile.mkdirs() |
| 30 | + jmx = true |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +jacocoTestReport { |
| 35 | + dependsOn test |
| 36 | + executionData dummyTest.jacoco.destinationFile |
| 37 | + getSourceDirectories().from(sourceSets.main.allSource) |
| 38 | + getClassDirectories().from(sourceSets.main.output) |
| 39 | + reports { |
| 40 | + html.enabled = true // human readable |
| 41 | + xml.enabled = true // for coverlay |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +project.gradle.projectsEvaluated { |
| 46 | + jacocoTestReport.dependsOn test |
| 47 | +} |
| 48 | + |
| 49 | +check.dependsOn jacocoTestReport |
0 commit comments