|
24 | 24 | import com.avast.gradle.dockercompose.tasks.ComposeUp;
|
25 | 25 | import org.elasticsearch.gradle.OS;
|
26 | 26 | import org.elasticsearch.gradle.SystemPropertyCommandLineArgumentProvider;
|
| 27 | +import org.elasticsearch.gradle.info.BuildParams; |
27 | 28 | import org.elasticsearch.gradle.precommit.TestingConventionsTasks;
|
28 | 29 | import org.gradle.api.Action;
|
29 | 30 | import org.gradle.api.DefaultTask;
|
|
32 | 33 | import org.gradle.api.Task;
|
33 | 34 | import org.gradle.api.plugins.BasePlugin;
|
34 | 35 | import org.gradle.api.plugins.ExtraPropertiesExtension;
|
| 36 | +import org.gradle.api.provider.Provider; |
35 | 37 | import org.gradle.api.tasks.TaskContainer;
|
36 | 38 | import org.gradle.api.tasks.testing.Test;
|
37 | 39 |
|
|
44 | 46 |
|
45 | 47 | public class TestFixturesPlugin implements Plugin<Project> {
|
46 | 48 |
|
| 49 | + private static final String DOCKER_COMPOSE_THROTTLE = "dockerComposeThrottle"; |
47 | 50 | static final String DOCKER_COMPOSE_YML = "docker-compose.yml";
|
48 | 51 |
|
49 | 52 | @Override
|
50 | 53 | public void apply(Project project) {
|
51 | 54 | TaskContainer tasks = project.getTasks();
|
52 | 55 |
|
53 | 56 | TestFixtureExtension extension = project.getExtensions().create("testFixtures", TestFixtureExtension.class, project);
|
| 57 | + Provider<DockerComposeThrottle> dockerComposeThrottle = project.getGradle() |
| 58 | + .getSharedServices() |
| 59 | + .registerIfAbsent(DOCKER_COMPOSE_THROTTLE, DockerComposeThrottle.class, spec -> spec.getMaxParallelUsages().set(1)); |
54 | 60 |
|
55 | 61 | ExtraPropertiesExtension ext = project.getExtensions().getByType(ExtraPropertiesExtension.class);
|
56 | 62 | File testfixturesDir = project.file("testfixtures_shared");
|
@@ -81,20 +87,26 @@ public void apply(Project project) {
|
81 | 87 | buildFixture.setEnabled(false);
|
82 | 88 | pullFixture.setEnabled(false);
|
83 | 89 | } else {
|
84 |
| - project.apply(spec -> spec.plugin(BasePlugin.class)); |
85 |
| - project.apply(spec -> spec.plugin(DockerComposePlugin.class)); |
| 90 | + project.getPluginManager().apply(BasePlugin.class); |
| 91 | + project.getPluginManager().apply(DockerComposePlugin.class); |
86 | 92 | ComposeExtension composeExtension = project.getExtensions().getByType(ComposeExtension.class);
|
87 | 93 | composeExtension.setUseComposeFiles(Collections.singletonList(DOCKER_COMPOSE_YML));
|
88 | 94 | composeExtension.setRemoveContainers(true);
|
89 | 95 | composeExtension.setExecutable(
|
90 | 96 | project.file("/usr/local/bin/docker-compose").exists() ? "/usr/local/bin/docker-compose" : "/usr/bin/docker-compose"
|
91 | 97 | );
|
92 | 98 |
|
93 |
| - buildFixture.dependsOn(tasks.getByName("composeUp")); |
94 |
| - pullFixture.dependsOn(tasks.getByName("composePull")); |
95 |
| - tasks.getByName("composeUp").mustRunAfter(preProcessFixture); |
96 |
| - tasks.getByName("composePull").mustRunAfter(preProcessFixture); |
97 |
| - tasks.getByName("composeDown").doLast((task) -> { project.delete(testfixturesDir); }); |
| 99 | + buildFixture.dependsOn(tasks.named("composeUp")); |
| 100 | + pullFixture.dependsOn(tasks.named("composePull")); |
| 101 | + tasks.named("composeUp").configure(t -> { |
| 102 | + // Avoid running docker-compose tasks in parallel in CI due to some issues on certain Linux distributions |
| 103 | + if (BuildParams.isCi()) { |
| 104 | + t.usesService(dockerComposeThrottle); |
| 105 | + } |
| 106 | + t.mustRunAfter(preProcessFixture); |
| 107 | + }); |
| 108 | + tasks.named("composePull").configure(t -> t.mustRunAfter(preProcessFixture)); |
| 109 | + tasks.named("composeDown").configure(t -> t.doLast(t2 -> project.delete(testfixturesDir))); |
98 | 110 |
|
99 | 111 | configureServiceInfoForTask(
|
100 | 112 | postProcessFixture,
|
@@ -147,13 +159,13 @@ public void apply(Project project) {
|
147 | 159 | }
|
148 | 160 |
|
149 | 161 | private void conditionTaskByType(TaskContainer tasks, TestFixtureExtension extension, Class<? extends DefaultTask> taskClass) {
|
150 |
| - tasks.withType( |
151 |
| - taskClass, |
152 |
| - task -> task.onlyIf( |
153 |
| - spec -> extension.fixtures.stream() |
154 |
| - .anyMatch(fixtureProject -> fixtureProject.getTasks().getByName("buildFixture").getEnabled() == false) == false |
155 |
| - ) |
156 |
| - ); |
| 162 | + tasks.withType(taskClass) |
| 163 | + .configureEach( |
| 164 | + task -> task.onlyIf( |
| 165 | + spec -> extension.fixtures.stream() |
| 166 | + .anyMatch(fixtureProject -> fixtureProject.getTasks().getByName("buildFixture").getEnabled() == false) == false |
| 167 | + ) |
| 168 | + ); |
157 | 169 | }
|
158 | 170 |
|
159 | 171 | private void configureServiceInfoForTask(
|
|
0 commit comments