Skip to content

Commit 90c59a1

Browse files
authored
Allow skipping ranges of versions (#50014)
Multiple version ranges are allowed to be used in section skip in yml tests. This is useful when a bugfix was backported to latest versions and all previous releases contain a wire breaking bug. examples: 6.1.0 - 6.3.0, 6.6.0 - 6.7.9, 7.0 - - 7.2, 8.0.0 -
1 parent fcae55a commit 90c59a1

File tree

5 files changed

+104
-34
lines changed

5 files changed

+104
-34
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/search/180_locale_dependent_mapping.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
---
22
"Test Index and Search locale dependent mappings / dates":
3-
- skip:
4-
version: " - 6.1.99"
5-
reason: JDK9 only supports this with a special sysproperty added in 6.2.0
63
- do:
74
indices.create:
85
index: test_index

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/DoSection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ private static NodeSelector parseVersionSelector(XContentParser parser) throws I
422422
if (false == parser.currentToken().isValue()) {
423423
throw new XContentParseException(parser.getTokenLocation(), "expected [version] to be a value");
424424
}
425-
Version[] range = SkipSection.parseVersionRange(parser.text());
425+
List<VersionRange> skipVersionRanges = SkipSection.parseVersionRanges(parser.text());
426426
return new NodeSelector() {
427427
@Override
428428
public void select(Iterable<Node> nodes) {
@@ -433,15 +433,16 @@ public void select(Iterable<Node> nodes) {
433433
+ node);
434434
}
435435
Version version = Version.fromString(node.getVersion());
436-
if (false == (version.onOrAfter(range[0]) && version.onOrBefore(range[1]))) {
436+
boolean skip = skipVersionRanges.stream().anyMatch(v -> v.contains(version));
437+
if (false == skip) {
437438
itr.remove();
438439
}
439440
}
440441
}
441442

442443
@Override
443444
public String toString() {
444-
return "version between [" + range[0] + "] and [" + range[1] + "]";
445+
return "version ranges "+skipVersionRanges;
445446
}
446447
};
447448
}

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/SkipSection.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.IOException;
2929
import java.util.ArrayList;
30+
import java.util.Collections;
3031
import java.util.List;
3132

3233
/**
@@ -98,33 +99,30 @@ public static SkipSection parse(XContentParser parser) throws IOException {
9899

99100
public static final SkipSection EMPTY = new SkipSection();
100101

101-
private final Version lowerVersion;
102-
private final Version upperVersion;
102+
private final List<VersionRange> versionRanges;
103103
private final List<String> features;
104104
private final String reason;
105105

106106
private SkipSection() {
107-
this.lowerVersion = null;
108-
this.upperVersion = null;
107+
this.versionRanges = new ArrayList<>();
109108
this.features = new ArrayList<>();
110109
this.reason = null;
111110
}
112111

113112
public SkipSection(String versionRange, List<String> features, String reason) {
114113
assert features != null;
115-
Version[] versions = parseVersionRange(versionRange);
116-
this.lowerVersion = versions[0];
117-
this.upperVersion = versions[1];
114+
this.versionRanges = parseVersionRanges(versionRange);
115+
assert versionRanges.isEmpty() == false;
118116
this.features = features;
119117
this.reason = reason;
120118
}
121119

122120
public Version getLowerVersion() {
123-
return lowerVersion;
121+
return versionRanges.get(0).getLower();
124122
}
125123

126124
public Version getUpperVersion() {
127-
return upperVersion;
125+
return versionRanges.get(versionRanges.size() - 1).getUpper();
128126
}
129127

130128
public List<String> getFeatures() {
@@ -139,10 +137,8 @@ public boolean skip(Version currentVersion) {
139137
if (isEmpty()) {
140138
return false;
141139
}
142-
boolean skip = lowerVersion != null && upperVersion != null && currentVersion.onOrAfter(lowerVersion)
143-
&& currentVersion.onOrBefore(upperVersion);
144-
skip |= Features.areAllSupported(features) == false;
145-
return skip;
140+
boolean skip = versionRanges.stream().anyMatch(range -> range.contains(currentVersion));
141+
return skip || Features.areAllSupported(features) == false;
146142
}
147143

148144
public boolean isVersionCheck() {
@@ -153,24 +149,30 @@ public boolean isEmpty() {
153149
return EMPTY.equals(this);
154150
}
155151

156-
static Version[] parseVersionRange(String versionRange) {
157-
if (versionRange == null) {
158-
return new Version[] { null, null };
152+
static List<VersionRange> parseVersionRanges(String rawRanges) {
153+
if (rawRanges == null) {
154+
return Collections.singletonList(new VersionRange(null, null));
159155
}
160-
if (versionRange.trim().equals("all")) {
161-
return new Version[]{VersionUtils.getFirstVersion(), Version.CURRENT};
162-
}
163-
String[] skipVersions = versionRange.split("-");
164-
if (skipVersions.length > 2) {
165-
throw new IllegalArgumentException("version range malformed: " + versionRange);
156+
if (rawRanges.trim().equals("all")) {
157+
return Collections.singletonList(new VersionRange(VersionUtils.getFirstVersion(), Version.CURRENT));
166158
}
159+
String[] ranges = rawRanges.split(",");
160+
List<VersionRange> versionRanges = new ArrayList<>();
161+
for (String rawRange : ranges) {
162+
String[] skipVersions = rawRange.split("-", -1);
163+
if (skipVersions.length > 2) {
164+
throw new IllegalArgumentException("version range malformed: " + rawRanges);
165+
}
167166

168-
String lower = skipVersions[0].trim();
169-
String upper = skipVersions[1].trim();
170-
return new Version[] {
171-
lower.isEmpty() ? VersionUtils.getFirstVersion() : Version.fromString(lower),
172-
upper.isEmpty() ? Version.CURRENT : Version.fromString(upper)
173-
};
167+
String lower = skipVersions[0].trim();
168+
String upper = skipVersions[1].trim();
169+
VersionRange versionRange = new VersionRange(
170+
lower.isEmpty() ? VersionUtils.getFirstVersion() : Version.fromString(lower),
171+
upper.isEmpty() ? Version.CURRENT : Version.fromString(upper)
172+
);
173+
versionRanges.add(versionRange);
174+
}
175+
return versionRanges;
174176
}
175177

176178
public String getSkipMessage(String description) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
package org.elasticsearch.test.rest.yaml.section;
20+
21+
import org.elasticsearch.Version;
22+
23+
public class VersionRange {
24+
private final Version lower;
25+
private final Version upper;
26+
27+
public VersionRange(Version lower, Version upper) {
28+
this.lower = lower;
29+
this.upper = upper;
30+
}
31+
32+
public Version getLower() {
33+
return lower;
34+
}
35+
36+
public Version getUpper() {
37+
return upper;
38+
}
39+
40+
public boolean contains(Version currentVersion) {
41+
return lower != null && upper != null && currentVersion.onOrAfter(lower)
42+
&& currentVersion.onOrBefore(upper);
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "[" + lower + " - " + upper + "]";
48+
}
49+
}

test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SkipSectionTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@
3333

3434
public class SkipSectionTests extends AbstractClientYamlTestFragmentParserTestCase {
3535

36+
public void testSkipMultiRange() {
37+
SkipSection section = new SkipSection("6.0.0 - 6.1.0, 7.1.0 - 7.5.0",
38+
Collections.emptyList() , "foobar");
39+
40+
assertFalse(section.skip(Version.CURRENT));
41+
assertFalse(section.skip(Version.fromString("6.2.0")));
42+
assertFalse(section.skip(Version.fromString("7.0.0")));
43+
assertFalse(section.skip(Version.fromString("7.6.0")));
44+
45+
assertTrue(section.skip(Version.fromString("6.0.0")));
46+
assertTrue(section.skip(Version.fromString("6.1.0")));
47+
assertTrue(section.skip(Version.fromString("7.1.0")));
48+
assertTrue(section.skip(Version.fromString("7.5.0")));
49+
50+
section = new SkipSection("- 7.1.0, 7.2.0 - 7.5.0, 8.0.0 -",
51+
Collections.emptyList() , "foobar");
52+
assertTrue(section.skip(Version.fromString("7.0.0")));
53+
assertTrue(section.skip(Version.fromString("7.3.0")));
54+
assertTrue(section.skip(Version.fromString("8.0.0")));
55+
}
56+
3657
public void testSkip() {
3758
SkipSection section = new SkipSection("6.0.0 - 6.1.0",
3859
randomBoolean() ? Collections.emptyList() : Collections.singletonList("warnings"), "foobar");

0 commit comments

Comments
 (0)