Skip to content

Commit 0a3d391

Browse files
committed
Merge branch 'master' into feature/runtime_fields
2 parents 6d8170c + 462e25f commit 0a3d391

File tree

372 files changed

+13587
-5085
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

372 files changed

+13587
-5085
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.gradle
21+
22+
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
23+
import org.gradle.testkit.runner.GradleRunner
24+
import spock.lang.IgnoreIf
25+
import spock.lang.Requires
26+
import spock.util.environment.OperatingSystem
27+
28+
import static org.elasticsearch.gradle.fixtures.DistributionDownloadFixture.withMockedDistributionDownload
29+
30+
/**
31+
* We do not have coverage for the test cluster startup on windows yet.
32+
* One step at a time...
33+
* */
34+
@IgnoreIf({ os.isWindows() })
35+
class TestClustersPluginFuncTest extends AbstractGradleFuncTest {
36+
37+
def setup() {
38+
buildFile << """
39+
import org.elasticsearch.gradle.testclusters.DefaultTestClustersTask
40+
plugins {
41+
id 'elasticsearch.testclusters'
42+
}
43+
44+
class SomeClusterAwareTask extends DefaultTestClustersTask {
45+
@TaskAction void doSomething() {
46+
println 'SomeClusterAwareTask executed'
47+
}
48+
}
49+
"""
50+
}
51+
52+
def "test cluster distribution is configured and started"() {
53+
given:
54+
buildFile << """
55+
testClusters {
56+
myCluster {
57+
testDistribution = 'default'
58+
}
59+
}
60+
61+
tasks.register('myTask', SomeClusterAwareTask) {
62+
useCluster testClusters.myCluster
63+
}
64+
"""
65+
66+
when:
67+
def result = withMockedDistributionDownload(gradleRunner("myTask", '-i')) { GradleRunner runner ->
68+
return runner.build()
69+
}
70+
71+
then:
72+
result.output.contains("elasticsearch-keystore script executed!")
73+
assertEsStdoutContains("myCluster", "Starting Elasticsearch process")
74+
assertEsStdoutContains("myCluster", "Stopping node")
75+
assertNoCustomDistro('myCluster')
76+
}
77+
78+
def "custom distro folder created for tweaked cluster distribution"() {
79+
given:
80+
buildFile << """
81+
testClusters {
82+
myCluster {
83+
testDistribution = 'default'
84+
extraJarFile(file('${someJar().absolutePath}'))
85+
}
86+
}
87+
88+
tasks.register('myTask', SomeClusterAwareTask) {
89+
useCluster testClusters.myCluster
90+
}
91+
"""
92+
93+
when:
94+
def result = withMockedDistributionDownload(gradleRunner("myTask", '-i')) { GradleRunner runner ->
95+
return runner.build()
96+
}
97+
98+
then:
99+
result.output.contains("elasticsearch-keystore script executed!")
100+
assertEsStdoutContains("myCluster", "Starting Elasticsearch process")
101+
assertEsStdoutContains("myCluster", "Stopping node")
102+
assertCustomDistro('myCluster')
103+
}
104+
105+
boolean assertEsStdoutContains(String testCluster, String expectedOutput) {
106+
assert new File(testProjectDir.root, "build/testclusters/${testCluster}-0/logs/es.stdout.log").text.contains(expectedOutput)
107+
true
108+
}
109+
110+
boolean assertCustomDistro(String clusterName) {
111+
assert customDistroFolder(clusterName).exists()
112+
true
113+
}
114+
115+
boolean assertNoCustomDistro(String clusterName) {
116+
assert !customDistroFolder(clusterName).exists()
117+
true
118+
}
119+
120+
private File customDistroFolder(String clusterName) {
121+
new File(testProjectDir.root, "build/testclusters/${clusterName}-0/distro")
122+
}
123+
}

buildSrc/src/integTest/groovy/org/elasticsearch/gradle/fixtures/AbstractGradleFuncTest.groovy

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import org.junit.rules.TemporaryFolder
2525
import spock.lang.Specification
2626

2727
import java.lang.management.ManagementFactory
28+
import java.util.jar.JarEntry
29+
import java.util.jar.JarOutputStream
2830

29-
abstract class AbstractGradleFuncTest extends Specification{
31+
abstract class AbstractGradleFuncTest extends Specification {
3032

3133
@Rule
3234
TemporaryFolder testProjectDir = new TemporaryFolder()
@@ -42,20 +44,22 @@ abstract class AbstractGradleFuncTest extends Specification{
4244

4345
GradleRunner gradleRunner(String... arguments) {
4446
GradleRunner.create()
45-
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0)
46-
.withProjectDir(testProjectDir.root)
47-
.withArguments(arguments)
48-
.withPluginClasspath()
49-
.forwardOutput()
47+
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0)
48+
.withProjectDir(testProjectDir.root)
49+
.withArguments(arguments)
50+
.withPluginClasspath()
51+
.forwardOutput()
5052
}
5153

5254
def assertOutputContains(String givenOutput, String expected) {
53-
assert normalizedString(givenOutput).contains(normalizedString(expected))
55+
assert normalizedOutput(givenOutput).contains(normalizedOutput(expected))
5456
true
5557
}
5658

57-
String normalizedString(String input) {
58-
return input.readLines().join("\n")
59+
String normalizedOutput(String input) {
60+
return input.readLines()
61+
.collect {it.replaceAll(testProjectDir.root.canonicalPath, ".") }
62+
.join("\n")
5963
}
6064

6165
File file(String path) {
@@ -64,4 +68,19 @@ abstract class AbstractGradleFuncTest extends Specification{
6468
newFile
6569
}
6670

71+
File someJar(String fileName = 'some.jar') {
72+
File jarFolder = new File(testProjectDir.root, "jars");
73+
jarFolder.mkdirs()
74+
File jarFile = new File(jarFolder, fileName)
75+
JarEntry entry = new JarEntry("foo.txt");
76+
77+
jarFile.withOutputStream {
78+
JarOutputStream target = new JarOutputStream(it)
79+
target.putNextEntry(entry);
80+
target.closeEntry();
81+
target.close();
82+
}
83+
84+
return jarFile;
85+
}
6786
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.gradle.fixtures
21+
22+
import org.elasticsearch.gradle.ElasticsearchDistribution
23+
import org.elasticsearch.gradle.VersionProperties
24+
import org.gradle.testkit.runner.BuildResult
25+
import org.gradle.testkit.runner.GradleRunner
26+
27+
class DistributionDownloadFixture {
28+
29+
public static final String INIT_SCRIPT = "repositories-init.gradle"
30+
31+
static BuildResult withMockedDistributionDownload(GradleRunner gradleRunner, Closure<BuildResult> buildRunClosure) {
32+
String urlPath = urlPath();
33+
return WiremockFixture.withWireMock(urlPath, filebytes(urlPath)) { server ->
34+
File initFile = new File(gradleRunner.getProjectDir(), INIT_SCRIPT)
35+
initFile.text = """allprojects { p ->
36+
p.repositories.all { repo ->
37+
repo.allowInsecureProtocol = true
38+
repo.setUrl('${server.baseUrl()}')
39+
}
40+
}"""
41+
List<String> givenArguments = gradleRunner.getArguments()
42+
GradleRunner effectiveRunner = gradleRunner.withArguments(givenArguments + ['-I', initFile.getAbsolutePath()])
43+
return buildRunClosure.call(effectiveRunner)
44+
}
45+
}
46+
47+
private static String urlPath() {
48+
String version = VersionProperties.getElasticsearch()
49+
ElasticsearchDistribution.Platform platform = ElasticsearchDistribution.CURRENT_PLATFORM
50+
String fileType = ((ElasticsearchDistribution.CURRENT_PLATFORM == ElasticsearchDistribution.Platform.LINUX ||
51+
ElasticsearchDistribution.CURRENT_PLATFORM == ElasticsearchDistribution.Platform.DARWIN)) ? "tar.gz" : "zip"
52+
"/downloads/elasticsearch/elasticsearch-${version}-${platform}-x86_64.$fileType"
53+
}
54+
55+
private static byte[] filebytes(String urlPath) throws IOException {
56+
String suffix = urlPath.endsWith("zip") ? "zip" : "tar.gz";
57+
return DistributionDownloadFixture.getResourceAsStream("/org/elasticsearch/gradle/fake_elasticsearch." + suffix).getBytes()
58+
}
59+
}

0 commit comments

Comments
 (0)