-
Notifications
You must be signed in to change notification settings - Fork 3.5k
setting: enforce non-nullable string (restore 8.15.x behavior) #17522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This pull request does not have a backport label. Could you fix it @yaauie? 🙏
|
|
💚 Build Succeeded
|
I would propose to share common test code in inner abstract classes, that contains all the shared test methods, like: diff --git a/logstash-core/src/test/java/org/logstash/settings/SettingNullableStringTest.java b/logstash-core/src/test/java/org/logstash/settings/SettingNullableStringTest.java
index aaa3cee7c..38b585537 100644
--- a/logstash-core/src/test/java/org/logstash/settings/SettingNullableStringTest.java
+++ b/logstash-core/src/test/java/org/logstash/settings/SettingNullableStringTest.java
@@ -14,58 +14,46 @@ import static org.junit.Assert.*;
@RunWith(Enclosed.class)
public class SettingNullableStringTest {
- public static class WithValueConstraintCase{
- private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
- private SettingString sut;
-
- @Before
- public void setUp() throws Exception {
- sut = new SettingNullableString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
- }
-
- @Test
- public void whenSetConstrainedToValueNotPresentInPossibleValuesThenThrowAHelpfulError() {
- IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
- sut.set("d");
- });
- assertThat(ex.getMessage(), containsString("Invalid value \"mytext: d\""));
- }
+ private abstract static class ValuesSharedChecks {
+ protected SettingString sut;
@Test
- public void whenSetConstrainedToValuePresentInPossibleValuesThenSetValue() {
+ public void whenSetToNonNullValueThenSetValue() {
sut.set("a");
assertEquals("a", sut.value());
}
@Test
- public void whenSetConstrainedToNullThenSetValue() {
+ public void whenSetToNullThenSetValue() {
sut.set(null);
assertNull(sut.value());
}
}
- public static class WithoutValueConstraintCase {
- private SettingString sut;
+ public static class WithValueConstraintCase extends ValuesSharedChecks {
+ private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
@Before
public void setUp() throws Exception {
- sut = new SettingNullableString("mytext", "foo", true);
+ sut = new SettingNullableString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
}
@Test
- public void whenSetUnconstrainedToNonNullValueThenSetValue() {
- sut.set("a");
-
- assertEquals("a", sut.value());
+ public void whenSetConstrainedToValueNotPresentInPossibleValuesThenThrowAHelpfulError() {
+ IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
+ sut.set("d");
+ });
+ assertThat(ex.getMessage(), containsString("Invalid value \"mytext: d\""));
}
+ }
- @Test
- public void whenSetUnconstrainedToNullThenSetValue() {
- sut.set(null);
+ public static class WithoutValueConstraintCase extends ValuesSharedChecks {
- assertNull(sut.value());
+ @Before
+ public void setUp() throws Exception {
+ sut = new SettingNullableString("mytext", "foo", true);
}
}
}
\ No newline at end of file
diff --git a/logstash-core/src/test/java/org/logstash/settings/SettingStringTest.java b/logstash-core/src/test/java/org/logstash/settings/SettingStringTest.java
index 4661bcbbe..f740405e6 100644
--- a/logstash-core/src/test/java/org/logstash/settings/SettingStringTest.java
+++ b/logstash-core/src/test/java/org/logstash/settings/SettingStringTest.java
@@ -33,32 +33,18 @@ import static org.junit.Assert.assertThrows;
@RunWith(Enclosed.class)
public class SettingStringTest {
- public static class WithValueConstraintCase {
- private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
- private SettingString sut;
-
- @Before
- public void setUp() throws Exception {
- sut = new SettingString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
- }
-
- @Test
- public void whenSetValueNotPresentInPossibleValuesThenThrowAHelpfulError() {
- IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
- sut.set("d");
- });
- assertThat(ex.getMessage(), containsString("Invalid value \"mytext: d\""));
- }
+ private abstract static class ValuesSharedChecks {
+ protected SettingString sut;
@Test
- public void whenSetConstrainedToValuePresentInPossibleValuesThenSetValue() {
+ public void whenSetToValuePresentInPossibleValuesThenSetValue() {
sut.set("a");
assertEquals("a", sut.value());
}
@Test
- public void whenSetConstrainedToNullThenThrowAHelpfulError() {
+ public void whenSetToNullThenThrowAHelpfulError() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
sut.set(null);
});
@@ -66,27 +52,28 @@ public class SettingStringTest {
}
}
- public static class WithoutValueConstraintCase {
- private SettingString sut;
+ public static class WithValueConstraintCase extends ValuesSharedChecks {
+ private static final List<String> POSSIBLE_VALUES = List.of("a", "b", "c");
@Before
public void setUp() throws Exception {
- sut = new SettingString("mytext", "foo", true);
- }
-
- @Test
- public void whenSetUnconstrainedToNonNullValueThenSetValue() {
- sut.set("a");
-
- assertEquals("a", sut.value());
+ sut = new SettingString("mytext", POSSIBLE_VALUES.iterator().next(), true, POSSIBLE_VALUES);
}
@Test
- public void whenSetUnconstrainedToNullThenThrowAHelpfulError() {
+ public void whenSetValueNotPresentInPossibleValuesThenThrowAHelpfulError() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
- sut.set(null);
+ sut.set("d");
});
- assertThat(ex.getMessage(), containsString("Setting \"mytext\" must be a String"));
+ assertThat(ex.getMessage(), containsString("Invalid value \"mytext: d\""));
+ }
+ }
+
+ public static class WithoutValueConstraintCase extends ValuesSharedChecks {
+
+ @Before
+ public void setUp() throws Exception {
+ sut = new SettingString("mytext", "foo", true);
}
}
}
\ No newline at end of file But present two possible issues:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a comment to avoid code duplication, but feel free to skip it because potentially introduce some unclear error reports.
LGTM
I agree on both fronts. My first pass used shared abstract base class, but I elected for duplication because in the end it made the test output more clear. If we were using Junit Jupiter, we could use |
(cherry picked from commit 712b37e)
(cherry picked from commit 712b37e)
(cherry picked from commit 712b37e)
(cherry picked from commit 712b37e)
(cherry picked from commit 712b37e)
) (cherry picked from commit 712b37e) Co-authored-by: Rye Biesemeyer <[email protected]>
) (cherry picked from commit 712b37e) Co-authored-by: Rye Biesemeyer <[email protected]>
) (cherry picked from commit 712b37e) Co-authored-by: Rye Biesemeyer <[email protected]>
) (cherry picked from commit 712b37e) Co-authored-by: Rye Biesemeyer <[email protected]>
Release notes
What does this PR do?
Ensures that
SettingString
is correctly non-nullable andSettingNullableString
is nullable.Why is it important/What is the impact to the user?
Improves error messages and reliability of behaviour when Logstash is misconfigured.
Checklist
[ ] I have made corresponding changes to the documentation[ ] I have made corresponding change to the default configuration files (and/or docker env variables)How to test this PR locally
With Logstash 8.15.5, set a non-nullable string setting to null in the
logstash.yml
and run Logstash, observing that it refuses to start and gives guidance about the wrong settingRepeat with Logstash 8.17.4 and observe that runtime errors that are less clear occur later
Repeat with this patch, and observe the restored behaviour:
Related issues
Caused-By: #16576