Skip to content

Commit 1fa5315

Browse files
authored
Fixes a subtle bug with DataPrepperVersion. A recent commit to parse the version from the Gradle project updated the public parse() method to support a full version string. However, this is not an allowable version to parse for the purposes of pipeline configurations. This change fixes that by bringing back the restriction on the public parse() method to support only major or major.minor patterns. Only when reading from the version string from the VersionProvider, do we now allow reading the full version string. (opensearch-project#6199)
Signed-off-by: David Venable <dlv@amazon.com>
1 parent c5190dc commit 1fa5315

3 files changed

Lines changed: 43 additions & 15 deletions

File tree

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/configuration/DataPrepperVersion.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
public class DataPrepperVersion {
1919
private static final String FULL_FORMAT = "%d.%d";
2020
private static final String SHORTHAND_FORMAT = "%d";
21-
private static final String VERSION_PATTERN_STRING = "^((\\d+)(\\.(\\d+))?(\\.(\\d+))?)(-SNAPSHOT)?$";
22-
private static final Pattern VERSION_PATTERN = Pattern.compile(VERSION_PATTERN_STRING);
21+
private static final Pattern VERSION_PATTERN = Pattern.compile("^((\\d+)(\\.(\\d+))?)$");
22+
private static final Pattern VERSION_FULL_PATTERN = Pattern.compile("^((\\d+)(\\.(\\d+))?(\\.(\\d+))?)(-SNAPSHOT)?$");
2323
private static final int MAJOR_VERSION_PATTERN_POSITION = 2;
2424
private static final int MINOR_VERSION_PATTERN_POSITION = 4;
2525

@@ -38,14 +38,17 @@ public static synchronized DataPrepperVersion getCurrentVersion() {
3838
final String versionString = ServiceLoader.load(VersionProvider.class).findFirst()
3939
.orElseThrow(() -> new RuntimeException("No Data Prepper version available."))
4040
.getVersionString();
41-
instance = parse(versionString);
41+
instance = parse(versionString, VERSION_FULL_PATTERN);
4242
}
4343
return instance;
4444
}
4545

4646
public static DataPrepperVersion parse(final String version) {
47+
return parse(version, VERSION_PATTERN);
48+
}
4749

48-
final Matcher result = VERSION_PATTERN.matcher(version);
50+
private static DataPrepperVersion parse(final String version, final Pattern versionPattern) {
51+
final Matcher result = versionPattern.matcher(version);
4952
if(result.find()) {
5053
String major = result.group(MAJOR_VERSION_PATTERN_POSITION);
5154
final int foundMajorVersion = Integer.parseInt(major);

data-prepper-api/src/test/java/org/opensearch/dataprepper/model/configuration/DataPrepperVersionTest.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.junit.jupiter.api.extension.ExtendWith;
1616
import org.junit.jupiter.params.ParameterizedTest;
1717
import org.junit.jupiter.params.provider.Arguments;
18+
import org.junit.jupiter.params.provider.CsvSource;
1819
import org.junit.jupiter.params.provider.MethodSource;
1920
import org.junit.jupiter.params.provider.ValueSource;
2021
import org.mockito.Mock;
@@ -67,11 +68,7 @@ private static Stream<Arguments> validDataPrepperVersions() {
6768
arguments("1.0", 1, Optional.of(0)),
6869
arguments("123423.0", 123423, Optional.of(0)),
6970
arguments("2.325", 2, Optional.of(325)),
70-
arguments("3.14", 3, Optional.of(14)),
71-
arguments("3.14.0", 3, Optional.of(14)),
72-
arguments("3.14.1", 3, Optional.of(14)),
73-
arguments("3.14.1-SNAPSHOT", 3, Optional.of(14)),
74-
arguments("11-SNAPSHOT", 11, Optional.empty())
71+
arguments("3.14", 3, Optional.of(14))
7572
);
7673
}
7774

@@ -82,6 +79,12 @@ void testInvalidVersionsCannotBeParsed(final String version) {
8279
assertThrows(IllegalArgumentException.class, () -> DataPrepperVersion.parse(version));
8380
}
8481

82+
@ParameterizedTest
83+
@ValueSource(strings = {"3.14.1", "3.14.1-SNAPSHOT", "11-SNAPSHOT"})
84+
void parse_throws_if_given_a_valid_version_that_cannot_be_a_configuration_version(final String version) {
85+
assertThrows(IllegalArgumentException.class, () -> DataPrepperVersion.parse(version));
86+
}
87+
8588
@ParameterizedTest
8689
@MethodSource("compatibleDataPrepperVersions")
8790
void testCompatibleWith_equalVersions(final String versionA, final String versionB) {
@@ -190,8 +193,16 @@ void testEquals_withDifferentObject() {
190193
}
191194

192195
@ParameterizedTest
193-
@ValueSource(strings = {"2.11", "2.13", "3.0", "3.1"})
194-
void getCurrentVersion_returns_value_from_VersionProvider(final String versionString) {
196+
@CsvSource({
197+
"2.11, 2.11",
198+
"2.13, 2.13",
199+
"3.0, 3.0",
200+
"3.1, 3.1",
201+
"3.14.1, 3.14",
202+
"3.14.20, 3.14",
203+
"3.14.1-SNAPSHOT, 3.14",
204+
"11-SNAPSHOT, 11"})
205+
void getCurrentVersion_returns_value_from_VersionProvider(final String versionString, final String expectedVersion) {
195206
final DataPrepperVersion currentVersion;
196207
when(serviceLoader.findFirst()).thenReturn(Optional.of(currentVersionProvider));
197208
when(currentVersionProvider.getVersionString()).thenReturn(versionString);
@@ -203,7 +214,7 @@ void getCurrentVersion_returns_value_from_VersionProvider(final String versionSt
203214
}
204215

205216
assertThat(currentVersion, is(notNullValue()));
206-
assertThat(currentVersion, is(equalTo(DataPrepperVersion.parse(versionString))));
217+
assertThat(currentVersion.toString(), is(equalTo(expectedVersion)));
207218
}
208219

209220
@Test
@@ -234,6 +245,19 @@ void getCurrentVersion_throws_if_no_VersionProvider() {
234245
}
235246
}
236247

248+
@ParameterizedTest
249+
@ValueSource(strings = {"2.", ".", ".1", "3.1.2.11", "3.14.1-RELEASE", "3.14.1-snapshot"})
250+
void getCurrentVersion_throws_for_invalid_DataPrepperVersion(final String versionString) {
251+
when(serviceLoader.findFirst()).thenReturn(Optional.of(currentVersionProvider));
252+
when(currentVersionProvider.getVersionString()).thenReturn(versionString);
253+
try (final MockedStatic<ServiceLoader> serviceLoaderStatic = mockStatic(ServiceLoader.class)) {
254+
serviceLoaderStatic.when(() -> ServiceLoader.load(VersionProvider.class))
255+
.thenReturn(serviceLoader);
256+
257+
assertThrows(IllegalArgumentException.class, DataPrepperVersion::getCurrentVersion);
258+
}
259+
}
260+
237261
@Test
238262
void testToString_shorthandVersion() {
239263
final DataPrepperVersion result = DataPrepperVersion.parse("2");

data-prepper-core/src/test/java/org/opensearch/dataprepper/model/configuration/DataPrepperVersionIT.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
public class DataPrepperVersionIT {
2020
@Test
2121
void getCurrentVersion_returns_expected_value_using_Java_SPI() {
22-
final String fullDataPrepperVersion = System.getProperty("project.version");
23-
2422
final DataPrepperVersion currentVersion = DataPrepperVersion.getCurrentVersion();
2523
assertThat(currentVersion, notNullValue());
24+
25+
final String fullDataPrepperVersion = System.getProperty("project.version");
26+
final String[] majorMinorPair = fullDataPrepperVersion.split("\\.");
2627
assertThat(fullDataPrepperVersion, containsString(currentVersion.toString()));
27-
assertThat(currentVersion, equalTo(DataPrepperVersion.parse(fullDataPrepperVersion)));
28+
assertThat(currentVersion, equalTo(DataPrepperVersion.parse(majorMinorPair[0] + "." + majorMinorPair[1])));
2829
}
2930
}

0 commit comments

Comments
 (0)