|
| 1 | +package net.corda.testing |
| 2 | + |
| 3 | + |
| 4 | +import com.bmuschko.gradle.docker.tasks.image.DockerPushImage |
| 5 | +import org.gradle.api.Plugin |
| 6 | +import org.gradle.api.Project |
| 7 | +import org.gradle.api.Task |
| 8 | +import org.gradle.api.tasks.testing.Test |
| 9 | + |
| 10 | +/** |
| 11 | + This plugin is responsible for wiring together the various components of test task modification |
| 12 | + */ |
| 13 | +class DistributedTesting implements Plugin<Project> { |
| 14 | + |
| 15 | + static def getPropertyAsInt(Project proj, String property, Integer defaultValue) { |
| 16 | + return proj.hasProperty(property) ? Integer.parseInt(proj.property(property).toString()) : defaultValue |
| 17 | + } |
| 18 | + |
| 19 | + @Override |
| 20 | + void apply(Project project) { |
| 21 | + if (System.getProperty("kubenetize") != null) { |
| 22 | + ensureImagePluginIsApplied(project) |
| 23 | + ImageBuilding imagePlugin = project.plugins.getPlugin(ImageBuilding) |
| 24 | + DockerPushImage imageBuildingTask = imagePlugin.pushTask |
| 25 | + |
| 26 | + //in each subproject |
| 27 | + //1. add the task to determine all tests within the module |
| 28 | + //2. modify the underlying testing task to use the output of the listing task to include a subset of tests for each fork |
| 29 | + //3. KubesTest will invoke these test tasks in a parallel fashion on a remote k8s cluster |
| 30 | + project.subprojects { Project subProject -> |
| 31 | + subProject.tasks.withType(Test) { Test task -> |
| 32 | + ListTests testListerTask = createTestListingTasks(task, subProject) |
| 33 | + Test modifiedTestTask = modifyTestTaskForParallelExecution(subProject, task, testListerTask) |
| 34 | + KubesTest parallelTestTask = generateParallelTestingTask(subProject, task, imageBuildingTask) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + //now we are going to create "super" groupings of these KubesTest tasks, so that it is possible to invoke all submodule tests with a single command |
| 39 | + //group all kubes tests by their underlying target task (test/integrationTest/smokeTest ... etc) |
| 40 | + Map<String, List<KubesTest>> allKubesTestingTasksGroupedByType = project.subprojects.collect { prj -> prj.getAllTasks(false).values() } |
| 41 | + .flatten() |
| 42 | + .findAll { task -> task instanceof KubesTest } |
| 43 | + .groupBy { task -> task.taskToExecuteName } |
| 44 | + |
| 45 | + //first step is to create a single task which will invoke all the submodule tasks for each grouping |
| 46 | + //ie allParallelTest will invoke [node:test, core:test, client:rpc:test ... etc] |
| 47 | + //ie allIntegrationTest will invoke [node:integrationTest, core:integrationTest, client:rpc:integrationTest ... etc] |
| 48 | + createGroupedParallelTestTasks(allKubesTestingTasksGroupedByType, project, imageBuildingTask) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private List<Task> createGroupedParallelTestTasks(Map<String, List<KubesTest>> allKubesTestingTasksGroupedByType, Project project, DockerPushImage imageBuildingTask) { |
| 53 | + allKubesTestingTasksGroupedByType.entrySet().collect { entry -> |
| 54 | + def taskType = entry.key |
| 55 | + def allTasksOfType = entry.value |
| 56 | + def allParallelTask = project.rootProject.tasks.create("allParallel" + taskType.capitalize(), KubesTest) { |
| 57 | + dependsOn imageBuildingTask |
| 58 | + printOutput = true |
| 59 | + fullTaskToExecutePath = allTasksOfType.collect { task -> task.fullTaskToExecutePath }.join(" ") |
| 60 | + taskToExecuteName = taskType |
| 61 | + doFirst { |
| 62 | + dockerTag = imageBuildingTask.imageName.get() + ":" + imageBuildingTask.tag.get() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + //second step is to create a task to use the reports output by the parallel test task |
| 67 | + def reportOnAllTask = project.rootProject.tasks.create("reportAllParallel${taskType.capitalize()}", KubesReporting) { |
| 68 | + dependsOn allParallelTask |
| 69 | + destinationDir new File(project.rootProject.getBuildDir(), "allResults${taskType.capitalize()}") |
| 70 | + doFirst { |
| 71 | + destinationDir.deleteDir() |
| 72 | + podResults = allParallelTask.containerResults |
| 73 | + reportOn(allParallelTask.testOutput) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + //invoke this report task after parallel testing |
| 78 | + allParallelTask.finalizedBy(reportOnAllTask) |
| 79 | + project.logger.info "Created task: ${allParallelTask.getPath()} to enable testing on kubenetes for tasks: ${allParallelTask.fullTaskToExecutePath}" |
| 80 | + project.logger.info "Created task: ${reportOnAllTask.getPath()} to generate test html output for task ${allParallelTask.getPath()}" |
| 81 | + return allParallelTask |
| 82 | + |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private KubesTest generateParallelTestingTask(Project projectContainingTask, Test task, DockerPushImage imageBuildingTask) { |
| 87 | + def taskName = task.getName() |
| 88 | + def capitalizedTaskName = task.getName().capitalize() |
| 89 | + |
| 90 | + KubesTest createdParallelTestTask = projectContainingTask.tasks.create("parallel" + capitalizedTaskName, KubesTest) { |
| 91 | + dependsOn imageBuildingTask |
| 92 | + printOutput = true |
| 93 | + fullTaskToExecutePath = task.getPath() |
| 94 | + taskToExecuteName = taskName |
| 95 | + doFirst { |
| 96 | + dockerTag = imageBuildingTask.imageName.get() + ":" + imageBuildingTask.tag.get() |
| 97 | + } |
| 98 | + } |
| 99 | + projectContainingTask.logger.info "Created task: ${createdParallelTestTask.getPath()} to enable testing on kubenetes for task: ${task.getPath()}" |
| 100 | + return createdParallelTestTask as KubesTest |
| 101 | + } |
| 102 | + |
| 103 | + private Test modifyTestTaskForParallelExecution(Project subProject, Test task, ListTests testListerTask) { |
| 104 | + subProject.logger.info("modifying task: ${task.getPath()} to depend on task ${testListerTask.getPath()}") |
| 105 | + def reportsDir = new File(new File(subProject.rootProject.getBuildDir(), "test-reports"), subProject.name + "-" + task.name) |
| 106 | + task.configure { |
| 107 | + dependsOn testListerTask |
| 108 | + binResultsDir new File(reportsDir, "binary") |
| 109 | + reports.junitXml.destination new File(reportsDir, "xml") |
| 110 | + maxHeapSize = "6g" |
| 111 | + doFirst { |
| 112 | + filter { |
| 113 | + def fork = getPropertyAsInt(subProject, "dockerFork", 0) |
| 114 | + def forks = getPropertyAsInt(subProject, "dockerForks", 1) |
| 115 | + def shuffleSeed = 42 |
| 116 | + subProject.logger.info("requesting tests to include in testing task ${task.getPath()} (${fork}, ${forks}, ${shuffleSeed})") |
| 117 | + List<String> includes = testListerTask.getTestsForFork( |
| 118 | + fork, |
| 119 | + forks, |
| 120 | + shuffleSeed) |
| 121 | + subProject.logger.info "got ${includes.size()} tests to include into testing task ${task.getPath()}" |
| 122 | + |
| 123 | + if (includes.size() == 0) { |
| 124 | + subProject.logger.info "Disabling test execution for testing task ${task.getPath()}" |
| 125 | + excludeTestsMatching "*" |
| 126 | + } |
| 127 | + |
| 128 | + includes.forEach { include -> |
| 129 | + subProject.logger.info "including: $include for testing task ${task.getPath()}" |
| 130 | + includeTestsMatching include |
| 131 | + } |
| 132 | + failOnNoMatchingTests false |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return task |
| 138 | + } |
| 139 | + |
| 140 | + private static void ensureImagePluginIsApplied(Project project) { |
| 141 | + project.plugins.apply(ImageBuilding) |
| 142 | + } |
| 143 | + |
| 144 | + private ListTests createTestListingTasks(Test task, Project subProject) { |
| 145 | + def taskName = task.getName() |
| 146 | + def capitalizedTaskName = task.getName().capitalize() |
| 147 | + //determine all the tests which are present in this test task. |
| 148 | + //this list will then be shared between the various worker forks |
| 149 | + def createdListTask = subProject.tasks.create("listTestsFor" + capitalizedTaskName, ListTests) { |
| 150 | + //the convention is that a testing task is backed by a sourceSet with the same name |
| 151 | + dependsOn subProject.getTasks().getByName("${taskName}Classes") |
| 152 | + doFirst { |
| 153 | + //we want to set the test scanning classpath to only the output of the sourceSet - this prevents dependencies polluting the list |
| 154 | + scanClassPath = task.getTestClassesDirs() ? task.getTestClassesDirs() : Collections.emptyList() |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + //convenience task to utilize the output of the test listing task to display to local console, useful for debugging missing tests |
| 159 | + def createdPrintTask = subProject.tasks.create("printTestsFor" + capitalizedTaskName) { |
| 160 | + dependsOn createdListTask |
| 161 | + doLast { |
| 162 | + createdListTask.getTestsForFork( |
| 163 | + getPropertyAsInt(subProject, "dockerFork", 0), |
| 164 | + getPropertyAsInt(subProject, "dockerForks", 1), |
| 165 | + 42).forEach { testName -> |
| 166 | + println testName |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + subProject.logger.info("created task: " + createdListTask.getPath() + " in project: " + subProject + " it dependsOn: " + createdListTask.dependsOn) |
| 172 | + subProject.logger.info("created task: " + createdPrintTask.getPath() + " in project: " + subProject + " it dependsOn: " + createdPrintTask.dependsOn) |
| 173 | + |
| 174 | + return createdListTask as ListTests |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments