Skip to content

Commit ca78c83

Browse files
committed
tests complete
1 parent e5b8d15 commit ca78c83

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

build-tools-internal/src/integTest/groovy/org/elasticsearch/gradle/internal/transport/TransportVersionGenerationFuncTest.groovy

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99

1010
package org.elasticsearch.gradle.internal.transport
1111

12-
import org.elasticsearch.gradle.Version
13-
import org.elasticsearch.gradle.VersionProperties
14-
import org.elasticsearch.gradle.fixtures.WiremockFixture
12+
1513
import org.gradle.testkit.runner.BuildResult
1614
import org.gradle.testkit.runner.TaskOutcome
1715

18-
import java.nio.charset.StandardCharsets
19-
2016
class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTest {
2117

2218
def runGenerateAndValidateTask(String... additionalArgs) {
@@ -94,7 +90,7 @@ class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTes
9490
assertUpperBound("9.2", "new_tv,8124000")
9591
}
9692

97-
def "invalid changes to a latest file should be reverted"() {
93+
def "invalid changes to a upper bounds should be reverted"() {
9894
given:
9995
transportVersionUpperBound("9.2", "modification", "9000000")
10096

@@ -106,7 +102,7 @@ class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTes
106102
assertUpperBound("9.2", "existing_92,8123000")
107103
}
108104

109-
def "invalid changes to multiple latest files should be reverted"() {
105+
def "invalid changes to multiple upper bounds should be reverted"() {
110106
given:
111107
transportVersionUpperBound("9.2", "modification", "9000000")
112108
transportVersionUpperBound("9.1", "modification", "9000000")
@@ -198,8 +194,9 @@ class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTes
198194
assertUpperBound("9.2", "test_tv,8124000")
199195
}
200196

201-
def "a latest file can be regenerated"() {
197+
def "an upper bound can be regenerated"() {
202198
given:
199+
file("myserver/src/main/resources/transport/upper_bounds/9.2.csv").delete()
203200
referableAndReferencedTransportVersion("test_tv", "8124000")
204201

205202
when:
@@ -310,7 +307,7 @@ class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTes
310307
assertUpperBound("9.2", "test_tv,8124000")
311308
}
312309

313-
def "can add backport to an existing definition"() {
310+
def "can add backport to latest upper bound"() {
314311
when:
315312
def result = runGenerateAndValidateTask("--name=existing_92", "--backport-branches=9.1,9.0").build()
316313

@@ -322,17 +319,24 @@ class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTes
322319
assertUpperBound("9.0", "existing_92,8000001")
323320
}
324321

325-
def "increment must be positive"() {
322+
def "can add backport to older definition"() {
326323
given:
327-
referencedTransportVersion("new_tv")
324+
execute("git checkout main")
325+
referableAndReferencedTransportVersion("latest_tv", "8124000,8012002")
326+
transportVersionUpperBound("9.2", "latest_tv", "8124000")
327+
transportVersionUpperBound("9.1", "latest_tv", "8012002")
328+
execute("git commit -a -m added")
329+
execute("git checkout -b mybranch")
328330

329331
when:
330-
def result = runGenerateTask("--increment=0").buildAndFail()
332+
def result = runGenerateAndValidateTask("--name=existing_92", "--backport-branches=9.1,9.0").build()
331333

332334
then:
333-
assertGenerateFailure(result, "Invalid increment [0], must be a positive integer")
334-
assertReferableDefinitionDoesNotExist("new_tv")
335-
assertUpperBound("9.2", "existing_92,8123000")
335+
assertGenerateAndValidateSuccess(result)
336+
assertReferableDefinition("existing_92", "8123000,8012001,8000001")
337+
assertUpperBound("9.2", "latest_tv,8124000")
338+
assertUpperBound("9.1", "latest_tv,8012002")
339+
assertUpperBound("9.0", "existing_92,8000001")
336340
}
337341

338342
def "a different increment can be specified"() {
@@ -361,7 +365,7 @@ class TransportVersionGenerationFuncTest extends AbstractTransportVersionFuncTes
361365
def result = runGenerateTask("--increment=0").buildAndFail()
362366

363367
then:
364-
assertOutputContains(result.output, "Invalid increment: must be a positive integer > 0")
368+
assertOutputContains(result.output, "Invalid increment 0, must be a positive integer")
365369
}
366370

367371
def "a new definition exists and is in the latest file, but the version id is wrong and needs to be updated"(){

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/GenerateTransportVersionDefinitionTask.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -120,51 +120,36 @@ private List<TransportVersionId> updateUpperBoundsFiles(
120120
String mainReleaseBranch = getMainReleaseBranch().get();
121121
int primaryIncrement = getPrimaryIncrement().get();
122122
if (primaryIncrement <= 0) {
123-
throw new IllegalArgumentException("Invalid increment [" + primaryIncrement + "], must be a positive integer");
123+
throw new IllegalArgumentException("Invalid increment " + primaryIncrement + ", must be a positive integer");
124124
}
125125
List<TransportVersionId> ids = new ArrayList<>();
126126

127127
TransportVersionDefinition existingDefinition = resources.getReferableDefinitionFromMain(name);
128128
for (TransportVersionUpperBound existingUpperBound : existingUpperBounds) {
129129

130-
TransportVersionUpperBound upperBoundToWrite = existingUpperBound;
130+
String releaseBranch = existingUpperBound.releaseBranch();
131+
boolean targetThisBranch = targetReleaseBranches.contains(existingUpperBound.releaseBranch());
131132

132-
// LOGIC
133-
/*
134-
- name may or may not already be set in the existing upper bound
135-
- target release branches may or may not target the upper bound file
136-
*/
137-
138-
resources.writeUpperBound(upperBoundToWrite);
133+
// Case: Targeting this upper bound file and it already points at the name we're generating
134+
if (targetThisBranch && name.equals(existingUpperBound.name())) {
135+
ids.add(existingUpperBound.id());
136+
continue;
137+
}
139138

140-
if (name.equals(existingUpperBound.name())) {
141-
if (targetReleaseBranches.contains(existingUpperBound.releaseBranch()) == false) {
142-
// Here, we aren't targeting this latest file, but need to undo prior updates if the list of minor
143-
// versions has changed. We must regenerate this latest file to make this operation idempotent.
144-
resources.writeUpperBound(existingUpperBound);
145-
} else {
146-
ids.add(existingUpperBound.id());
139+
if (targetThisBranch) {
140+
// Case: targeting this branch, find an existing id for this branch if it exists
141+
TransportVersionId targetId = maybeGetExistingId(existingUpperBound, existingDefinition);
142+
if (targetId == null) {
143+
// Case: an id doesn't yet exist for this branch, so create one
144+
int increment = releaseBranch.equals(mainReleaseBranch) ? primaryIncrement : 1;
145+
targetId = TransportVersionId.fromInt(existingUpperBound.id().complete() + increment);
146+
var newUpperBound = new TransportVersionUpperBound(releaseBranch, name, targetId);
147+
resources.writeUpperBound(newUpperBound);
147148
}
149+
ids.add(targetId);
148150
} else {
149-
if (targetReleaseBranches.contains(existingUpperBound.releaseBranch())) {
150-
TransportVersionId targetId = null;
151-
if (existingDefinition != null) {
152-
for (TransportVersionId id : existingDefinition.ids()) {
153-
if (id.base() == existingUpperBound.id().base()) {
154-
targetId = id;
155-
break;
156-
}
157-
}
158-
}
159-
if (targetId == null) {
160-
int increment = existingUpperBound.releaseBranch().equals(mainReleaseBranch) ? primaryIncrement : 1;
161-
TransportVersionId id = TransportVersionId.fromInt(existingUpperBound.id().complete() + increment);
162-
ids.add(id);
163-
resources.writeUpperBound(new TransportVersionUpperBound(existingUpperBound.releaseBranch(), name, id));
164-
}
165-
} else {
166-
resources.writeUpperBound(existingUpperBound);
167-
}
151+
// Default case: we're not targeting this branch so reset it
152+
resources.writeUpperBound(existingUpperBound);
168153
}
169154
}
170155

@@ -230,4 +215,20 @@ private String findAddedTransportVersionName(
230215
}
231216
}
232217
}
218+
219+
private TransportVersionId maybeGetExistingId(TransportVersionUpperBound upperBound, TransportVersionDefinition existingDefinition) {
220+
if (existingDefinition == null) {
221+
return null;
222+
}
223+
if (upperBound.releaseBranch().equals(getMainReleaseBranch().get())) {
224+
return existingDefinition.ids().get(0); // main is always the primary id
225+
}
226+
for (TransportVersionId id : existingDefinition.ids()) {
227+
if (id.base() == upperBound.id().base()) {
228+
return id;
229+
}
230+
}
231+
return null; // no id for this release branch
232+
}
233+
233234
}

0 commit comments

Comments
 (0)