Skip to content

Commit 5e3ea6e

Browse files
committed
Merge branch 'master' into feature/runtime_fields
2 parents e98dfcc + 04ca191 commit 5e3ea6e

File tree

126 files changed

+2719
-2599
lines changed

Some content is hidden

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

126 files changed

+2719
-2599
lines changed
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
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 com.github.tomakehurst.wiremock.WireMockServer
23+
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
24+
import org.elasticsearch.gradle.fixtures.WiremockFixture
25+
import org.elasticsearch.gradle.transform.SymbolicLinkPreservingUntarTransform
26+
import org.elasticsearch.gradle.transform.UnzipTransform
27+
import spock.lang.Unroll
28+
29+
import java.nio.file.Files
30+
import java.nio.file.Path
31+
import java.nio.file.Paths
32+
import java.util.regex.Matcher
33+
import java.util.regex.Pattern
34+
35+
import static org.elasticsearch.gradle.JdkDownloadPlugin.VENDOR_ADOPTOPENJDK
36+
import static org.elasticsearch.gradle.JdkDownloadPlugin.VENDOR_OPENJDK
37+
38+
class JdkDownloadPluginFuncTest extends AbstractGradleFuncTest {
39+
40+
private static final String OPENJDK_VERSION_OLD = "1+99"
41+
private static final String ADOPT_JDK_VERSION = "12.0.2+10"
42+
private static final String OPEN_JDK_VERSION = "12.0.1+99@123456789123456789123456789abcde"
43+
private static final Pattern JDK_HOME_LOGLINE = Pattern.compile("JDK HOME: (.*)");
44+
45+
@Unroll
46+
def "jdk #jdkVendor for #platform#suffix are downloaded and extracted"() {
47+
given:
48+
def mockRepoUrl = urlPath(jdkVendor, jdkVersion, platform);
49+
def mockedContent = filebytes(jdkVendor, platform)
50+
buildFile.text = """
51+
plugins {
52+
id 'elasticsearch.jdk-download'
53+
}
54+
55+
jdks {
56+
myJdk {
57+
vendor = '$jdkVendor'
58+
version = '$jdkVersion'
59+
platform = "$platform"
60+
architecture = "x64"
61+
}
62+
}
63+
64+
tasks.register("getJdk") {
65+
dependsOn jdks.myJdk
66+
doLast {
67+
println "JDK HOME: " + jdks.myJdk
68+
}
69+
}
70+
"""
71+
72+
when:
73+
def result = WiremockFixture.withWireMock(mockRepoUrl, mockedContent) { server ->
74+
buildFile << repositoryMockSetup(server, jdkVendor, jdkVersion)
75+
gradleRunner("getJdk").build()
76+
}
77+
78+
then:
79+
assertExtraction(result.output, expectedJavaBin);
80+
81+
where:
82+
platform | jdkVendor | jdkVersion | expectedJavaBin | suffix
83+
"linux" | VENDOR_ADOPTOPENJDK | ADOPT_JDK_VERSION | "bin/java" | ""
84+
"linux" | VENDOR_OPENJDK | OPEN_JDK_VERSION | "bin/java" | ""
85+
"linux" | VENDOR_OPENJDK | OPENJDK_VERSION_OLD | "bin/java" | "(old version)"
86+
"windows" | VENDOR_ADOPTOPENJDK | ADOPT_JDK_VERSION | "bin/java" | ""
87+
"windows" | VENDOR_OPENJDK | OPEN_JDK_VERSION | "bin/java" | ""
88+
"windows" | VENDOR_OPENJDK | OPENJDK_VERSION_OLD | "bin/java" | "(old version)"
89+
"darwin" | VENDOR_ADOPTOPENJDK | ADOPT_JDK_VERSION | "Contents/Home/bin/java" | ""
90+
"darwin" | VENDOR_OPENJDK | OPEN_JDK_VERSION | "Contents/Home/bin/java" | ""
91+
"darwin" | VENDOR_OPENJDK | OPENJDK_VERSION_OLD | "Contents/Home/bin/java" | "(old version)"
92+
}
93+
94+
def "transforms are reused across projects"() {
95+
given:
96+
def mockRepoUrl = urlPath(jdkVendor, jdkVersion, platform)
97+
def mockedContent = filebytes(jdkVendor, platform)
98+
10.times {
99+
settingsFile << """
100+
include ':sub-$it'
101+
"""
102+
}
103+
buildFile.text = """
104+
plugins {
105+
id 'elasticsearch.jdk-download' apply false
106+
}
107+
108+
subprojects {
109+
apply plugin: 'elasticsearch.jdk-download'
110+
111+
jdks {
112+
myJdk {
113+
vendor = '$jdkVendor'
114+
version = '$jdkVersion'
115+
platform = "$platform"
116+
architecture = "x64"
117+
}
118+
}
119+
tasks.register("getJdk") {
120+
dependsOn jdks.myJdk
121+
doLast {
122+
println "JDK HOME: " + jdks.myJdk
123+
}
124+
}
125+
}
126+
"""
127+
128+
when:
129+
def result = WiremockFixture.withWireMock(mockRepoUrl, mockedContent) { server ->
130+
buildFile << repositoryMockSetup(server, jdkVendor, jdkVersion)
131+
gradleRunner('getJdk', '-i', '-g', testProjectDir.newFolder().toString()).build()
132+
}
133+
134+
then:
135+
result.tasks.size() == 10
136+
result.output.count("Unpacking linux-12.0.2-x64.tar.gz using SymbolicLinkPreservingUntarTransform.") == 1
137+
138+
where:
139+
platform | jdkVendor | jdkVersion | expectedJavaBin
140+
"linux" | VENDOR_ADOPTOPENJDK | ADOPT_JDK_VERSION | "bin/java"
141+
}
142+
143+
@Unroll
144+
def "transforms of type #transformType are kept across builds"() {
145+
given:
146+
def mockRepoUrl = urlPath(VENDOR_ADOPTOPENJDK, ADOPT_JDK_VERSION, platform)
147+
def mockedContent = filebytes(VENDOR_ADOPTOPENJDK, platform)
148+
buildFile.text = """
149+
plugins {
150+
id 'elasticsearch.jdk-download'
151+
}
152+
153+
apply plugin: 'elasticsearch.jdk-download'
154+
155+
jdks {
156+
myJdk {
157+
vendor = '$VENDOR_ADOPTOPENJDK'
158+
version = '$ADOPT_JDK_VERSION'
159+
platform = "$platform"
160+
architecture = "x64"
161+
}
162+
}
163+
164+
tasks.register("getJdk") {
165+
dependsOn jdks.myJdk
166+
doLast {
167+
println "JDK HOME: " + jdks.myJdk
168+
}
169+
}
170+
"""
171+
172+
when:
173+
def result = WiremockFixture.withWireMock(mockRepoUrl, mockedContent) { server ->
174+
buildFile << repositoryMockSetup(server, VENDOR_ADOPTOPENJDK, ADOPT_JDK_VERSION)
175+
176+
def commonGradleUserHome = testProjectDir.newFolder().toString()
177+
// initial run
178+
gradleRunner('getJdk', '-g', commonGradleUserHome).build()
179+
// run against up-to-date transformations
180+
gradleRunner('getJdk', '-i', '-g', commonGradleUserHome).build()
181+
}
182+
183+
then:
184+
assertOutputContains(result.output, "Skipping $transformType")
185+
186+
where:
187+
platform | transformType
188+
"linux" | SymbolicLinkPreservingUntarTransform.class.simpleName
189+
"windows" | UnzipTransform.class.simpleName
190+
}
191+
192+
static boolean assertExtraction(String output, String javaBin) {
193+
Matcher matcher = JDK_HOME_LOGLINE.matcher(output);
194+
assert matcher.find() == true;
195+
String jdkHome = matcher.group(1);
196+
Path javaPath = Paths.get(jdkHome, javaBin);
197+
assert Files.exists(javaPath) == true;
198+
true
199+
}
200+
201+
private static String urlPath(final String vendor, final String version, final String platform) {
202+
if (vendor.equals(VENDOR_ADOPTOPENJDK)) {
203+
final String module = platform.equals("darwin") ? "mac" : platform;
204+
return "/jdk-12.0.2+10/" + module + "/x64/jdk/hotspot/normal/adoptopenjdk";
205+
} else if (vendor.equals(VENDOR_OPENJDK)) {
206+
final String effectivePlatform = platform.equals("darwin") ? "osx" : platform;
207+
final boolean isOld = version.equals(OPENJDK_VERSION_OLD);
208+
final String versionPath = isOld ? "jdk1/99" : "jdk12.0.1/123456789123456789123456789abcde/99";
209+
final String filename = "openjdk-" + (isOld ? "1" : "12.0.1") + "_" + effectivePlatform + "-x64_bin." + extension(platform);
210+
return "/java/GA/" + versionPath + "/GPL/" + filename;
211+
}
212+
}
213+
214+
private static byte[] filebytes(final String vendor, final String platform) throws IOException {
215+
final String effectivePlatform = platform.equals("darwin") ? "osx" : platform;
216+
if (vendor.equals(VENDOR_ADOPTOPENJDK)) {
217+
return JdkDownloadPluginFuncTest.class.getResourceAsStream("fake_adoptopenjdk_" + effectivePlatform + "." + extension(platform)).getBytes()
218+
} else if (vendor.equals(VENDOR_OPENJDK)) {
219+
JdkDownloadPluginFuncTest.class.getResourceAsStream("fake_openjdk_" + effectivePlatform + "." + extension(platform)).getBytes()
220+
}
221+
}
222+
223+
private static String extension(String platform) {
224+
platform.equals("windows") ? "zip" : "tar.gz";
225+
}
226+
227+
private static String repositoryMockSetup(WireMockServer server, String jdkVendor, String jdkVersion) {
228+
"""allprojects{ p ->
229+
// wire the jdk repo to wiremock
230+
p.repositories.all { repo ->
231+
if(repo.name == "jdk_repo_${jdkVendor}_${jdkVersion}") {
232+
repo.setUrl('${server.baseUrl()}')
233+
}
234+
}
235+
}"""
236+
}
237+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class AbstractGradleFuncTest extends Specification{
3636

3737
def setup() {
3838
settingsFile = testProjectDir.newFile('settings.gradle')
39-
settingsFile << "rootProject.name = 'hello-world'"
39+
settingsFile << "rootProject.name = 'hello-world'\n"
4040
buildFile = testProjectDir.newFile('build.gradle')
4141
}
4242

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 com.github.tomakehurst.wiremock.WireMockServer
23+
import org.gradle.testkit.runner.BuildResult
24+
25+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse
26+
import static com.github.tomakehurst.wiremock.client.WireMock.get
27+
import static com.github.tomakehurst.wiremock.client.WireMock.head
28+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
29+
30+
/**
31+
* A test fixture that allows running testkit builds with wiremock
32+
* */
33+
class WiremockFixture {
34+
35+
/**
36+
* the buildRunClosure has passed an instance of WireMockServer that can be used to access e.g. the baseUrl of
37+
* the configured server:
38+
*
39+
* <pre>
40+
* WiremockFixture.withWireMock(mockRepoUrl, mockedContent) { server ->
41+
* buildFile << """
42+
* // wire a gradle repository with wiremock
43+
* repositories {
44+
* maven {
45+
* url = '${server.baseUrl()}'
46+
* }
47+
* }
48+
* }
49+
* gadleRunner('myTask').build()
50+
* </pre>
51+
* */
52+
static BuildResult withWireMock(String expectedUrl, byte[] expectedContent, Closure<BuildResult> buildRunClosure) {
53+
WireMockServer wireMock = new WireMockServer(0);
54+
try {
55+
wireMock.stubFor(head(urlEqualTo(expectedUrl)).willReturn(aResponse().withStatus(200)));
56+
wireMock.stubFor(
57+
get(urlEqualTo(expectedUrl)).willReturn(aResponse().withStatus(200).withBody(expectedContent))
58+
)
59+
wireMock.start();
60+
return buildRunClosure.call(wireMock);
61+
} catch (Exception e) {
62+
// for debugging
63+
System.err.println("missed requests: " + wireMock.findUnmatchedRequests().getRequests());
64+
throw e;
65+
} finally {
66+
wireMock.stop();
67+
}
68+
}
69+
70+
}

buildSrc/src/integTest/java/org/elasticsearch/gradle/AdoptOpenJdkDownloadPluginIT.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)