-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
health: add
logstash.forceApiStatus: green
escape hatch (#16535)
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
logstash-core/src/test/java/org/logstash/health/HealthObserverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.logstash.health; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
|
||
import java.util.Objects; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.logstash.health.HealthObserver.FORCE_API_STATUS_PROPERTY; | ||
|
||
public class HealthObserverTest { | ||
|
||
private final HealthObserver healthObserver = new HealthObserver(); | ||
|
||
@Test | ||
public void testStatusWhenNotForcedPropagates() { | ||
withUnsetSystemProperty(FORCE_API_STATUS_PROPERTY, () -> { | ||
for (Status reportStatus : Status.values()) { | ||
withIndicator(new TestIndicator(reportStatus), () -> { | ||
assertThat(String.format("Status[%s] should propagate", reportStatus), healthObserver.getStatus(), Matchers.is(reportStatus)); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testStatusWhenForcedGreenEmitsGreen() { | ||
withSystemProperty(FORCE_API_STATUS_PROPERTY, "green", () -> { | ||
for (Status reportStatus : Status.values()) { | ||
withIndicator(new TestIndicator(reportStatus), () -> { | ||
assertThat(String.format("Status[%s] should not propagate", reportStatus), healthObserver.getStatus(), Matchers.is(Status.GREEN)); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testStatusWhenForcedNonsensePropagates() { | ||
withSystemProperty(FORCE_API_STATUS_PROPERTY, "nonsense", () -> { | ||
for (Status reportStatus : Status.values()) { | ||
withIndicator(new TestIndicator(reportStatus), () -> { | ||
assertThat(String.format("Status[%s] should propagate", reportStatus), healthObserver.getStatus(), Matchers.is(reportStatus)); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
void withUnsetSystemProperty(final String propertyName, final Runnable action) { | ||
withSystemProperty(propertyName, null, action); | ||
} | ||
|
||
void withSystemProperty(final String name, final String value, final Runnable action) { | ||
synchronized (System.class) { | ||
final String oldValue; | ||
if (Objects.isNull(value)) { | ||
oldValue = System.clearProperty(name); | ||
} else { | ||
oldValue = System.setProperty(name, value); | ||
} | ||
try { | ||
action.run(); | ||
} finally { | ||
if (oldValue != null) { | ||
System.setProperty(name, oldValue); | ||
} else { | ||
System.clearProperty(name); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void withIndicator(Indicator<?> indicator, Runnable runnable) { | ||
this.healthObserver.getIndicator().attachIndicator("single", indicator); | ||
try { | ||
runnable.run(); | ||
} finally { | ||
this.healthObserver.getIndicator().detachIndicator("single", indicator); | ||
} | ||
} | ||
|
||
static class TestIndicator implements Indicator<Indicator.Report> { | ||
private final Status status; | ||
|
||
public TestIndicator(final Status status) { | ||
this.status = status; | ||
} | ||
|
||
@Override | ||
public Report report(ReportContext reportContext) { | ||
return () -> this.status; | ||
} | ||
} | ||
} |