Skip to content

Commit 3adc627

Browse files
committed
add forgotten files
1 parent 24842f8 commit 3adc627

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.transport
11+
12+
import org.gradle.testkit.runner.BuildResult
13+
import org.gradle.testkit.runner.TaskOutcome
14+
15+
class UpdateTransportVersionsFuncTest extends AbstractTransportVersionFuncTest {
16+
def runUpdateTask(String... additionalArgs) {
17+
List<String> args = new ArrayList<>()
18+
args.add(":myserver:updateTransportVersions")
19+
args.addAll(additionalArgs);
20+
return gradleRunner(args.toArray())
21+
}
22+
23+
void assertUpdateSuccess(BuildResult result) {
24+
assert result.task(":myserver:updateTransportVersions").outcome == TaskOutcome.SUCCESS
25+
}
26+
27+
void assertUpdateFailure(BuildResult result, String expectedOutput) {
28+
assert result.task(":myserver:updateTransportVersions").outcome == TaskOutcome.FAILED
29+
assertOutputContains(result.output, expectedOutput)
30+
}
31+
32+
void assertTransportVersionsCsv(String expectedContent) {
33+
File csvFile = file("myserver/src/main/resources/org/elasticsearch/TransportVersions.csv")
34+
assert csvFile.exists()
35+
// Normalize both sides: strip leading/trailing whitespace from each line and the whole string
36+
def actualLines = csvFile.text.readLines().collect { it.strip() }.findAll { it != "" }
37+
def expectedLines = expectedContent.readLines().collect { it.strip() }.findAll { it != "" }
38+
assert actualLines == expectedLines
39+
}
40+
41+
def "updates csv with minor version"() {
42+
when:
43+
def result = runUpdateTask("--stack-version", "9.2.0").build()
44+
45+
then:
46+
assertUpdateSuccess(result)
47+
assertTransportVersionsCsv("""
48+
9.0.0,8000000
49+
9.1.0,8012001
50+
9.2.0,8123000
51+
""")
52+
}
53+
54+
def "updates csv with patch version"() {
55+
given:
56+
execute("git checkout main")
57+
unreferableTransportVersion("initial_9.1.1", "8012002")
58+
transportVersionUpperBound("9.1", "initial_9.1.1", "8012002")
59+
execute('git commit -a -m "update"')
60+
61+
when:
62+
def result = runUpdateTask("--stack-version", "9.1.1").build()
63+
64+
then:
65+
assertUpdateSuccess(result)
66+
assertTransportVersionsCsv("""
67+
9.0.0,8000000
68+
9.1.0,8012001
69+
9.1.1,8012002
70+
""")
71+
}
72+
73+
def "fails when upper bound does not exist"() {
74+
when:
75+
def result = runUpdateTask("--stack-version", "9.3.0").buildAndFail()
76+
77+
then:
78+
assertUpdateFailure(result, "Missing upper bound 9.3 for stack version 9.3.0")
79+
}
80+
81+
def "is idempotent when version already exists"() {
82+
when:
83+
// Run the task twice with the same version
84+
def result1 = runUpdateTask("--stack-version", "9.2.0").build()
85+
def result2 = runUpdateTask("--stack-version", "9.2.0").build()
86+
87+
then:
88+
assertUpdateSuccess(result1)
89+
assertUpdateSuccess(result2)
90+
assertOutputContains(result2.output, "Version 9.2.0 already exists in TransportVersions.csv, skipping")
91+
// Should only have one entry for 9.2.0
92+
assertTransportVersionsCsv("""
93+
9.0.0,8000000
94+
9.1.0,8012001
95+
9.2.0,8123000
96+
""")
97+
}
98+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.gradle.internal.transport;
11+
12+
import org.elasticsearch.gradle.Version;
13+
import org.gradle.api.DefaultTask;
14+
import org.gradle.api.file.RegularFileProperty;
15+
import org.gradle.api.provider.Property;
16+
import org.gradle.api.services.ServiceReference;
17+
import org.gradle.api.tasks.Input;
18+
import org.gradle.api.tasks.InputFile;
19+
import org.gradle.api.tasks.TaskAction;
20+
import org.gradle.api.tasks.options.Option;
21+
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.StandardOpenOption;
25+
26+
public abstract class UpdateTransportVersionsTask extends DefaultTask {
27+
28+
@ServiceReference("transportVersionResources")
29+
abstract Property<TransportVersionResourcesService> getResourceService();
30+
31+
@InputFile
32+
public abstract RegularFileProperty getTransportVersionsFile();
33+
34+
@Input
35+
@Option(option = "stack-version", description = "The Elasticsearch version to record in TransportVersions.csv")
36+
public abstract Property<String> getStackVersion();
37+
38+
@TaskAction
39+
public void run() throws IOException {
40+
Version stackVersion = Version.fromString(getStackVersion().get());
41+
42+
// Check if this version is already in the CSV file (idempotency check)
43+
if (isVersionAlreadyRecorded(stackVersion)) {
44+
getLogger().lifecycle("Version {} already exists in TransportVersions.csv, skipping", stackVersion);
45+
return;
46+
}
47+
48+
String upperBoundName = getUpperBoundName(stackVersion);
49+
TransportVersionResourcesService resources = getResourceService().get();
50+
TransportVersionUpperBound upperBound = resources.getUpperBoundFromGitBase(upperBoundName);
51+
52+
if (upperBound == null) {
53+
throw new RuntimeException("Missing upper bound " + upperBoundName + " for stack version " + stackVersion);
54+
}
55+
56+
int transportVersionId = upperBound.definitionId().complete();
57+
addTransportVersionRecord(stackVersion, transportVersionId);
58+
}
59+
60+
private boolean isVersionAlreadyRecorded(Version stackVersion) throws IOException {
61+
var csvFile = getTransportVersionsFile().getAsFile().get().toPath();
62+
63+
String versionPrefix = stackVersion.toString() + ",";
64+
return Files.readAllLines(csvFile).stream()
65+
.anyMatch(line -> line.trim().startsWith(versionPrefix));
66+
}
67+
68+
private void addTransportVersionRecord(Version stackVersion, int transportVersionId) throws IOException {
69+
String newEntry = stackVersion + "," + transportVersionId + "\n";
70+
Files.writeString(getTransportVersionsFile().getAsFile().get().toPath(), newEntry, StandardOpenOption.APPEND);
71+
}
72+
73+
private String getUpperBoundName(Version version) {
74+
return version.getMajor() + "." + version.getMinor();
75+
}
76+
}

0 commit comments

Comments
 (0)