-
Notifications
You must be signed in to change notification settings - Fork 41.1k
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
Add support for multiple paths in DiskSpaceHealthIndicator #27663
Closed
onobc
wants to merge
2
commits into
spring-projects:main
from
onobc:gh-18359-multipath-diskspace-indicator
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* Copyright 2012-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -16,21 +16,29 @@ | |
|
||
package org.springframework.boot.actuate.autoconfigure.system; | ||
|
||
import java.io.File; | ||
import java.util.Map; | ||
|
||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration; | ||
import org.springframework.boot.actuate.system.DiskSpaceHealthIndicator; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.boot.test.context.runner.ContextConsumer; | ||
import org.springframework.util.unit.DataSize; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.entry; | ||
|
||
/** | ||
* Tests for {@link DiskSpaceHealthContributorAutoConfiguration}. | ||
* | ||
* @author Phillip Webb | ||
* @author Stephane Nicoll | ||
* @author Chris Bono | ||
*/ | ||
class DiskSpaceHealthContributorAutoConfigurationTests { | ||
|
||
|
@@ -39,29 +47,49 @@ class DiskSpaceHealthContributorAutoConfigurationTests { | |
HealthContributorAutoConfiguration.class)); | ||
|
||
@Test | ||
void runShouldCreateIndicator() { | ||
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class)); | ||
void runShouldCreateIndicatorWithDefaultSinglePathAndThreshold() { | ||
this.contextRunner | ||
.run((context) -> validateIndicatorHasPathsExactly(entry(new File("."), DataSize.ofMegabytes(10)))); | ||
} | ||
|
||
@Test | ||
void thresholdMustBePositive() { | ||
this.contextRunner.withPropertyValues("management.health.diskspace.threshold=-10MB") | ||
.run((context) -> assertThat(context).hasFailed().getFailure() | ||
.hasMessageContaining("Failed to bind properties under 'management.health.diskspace'")); | ||
void pathCanBeCustomized() { | ||
this.contextRunner.withPropertyValues("management.health.diskspace.paths[0].path=..") | ||
.run((context) -> validateIndicatorHasPathsExactly(entry(new File(".."), DataSize.ofMegabytes(10)))); | ||
} | ||
|
||
@Test | ||
void thresholdCanBeCustomized() { | ||
this.contextRunner.withPropertyValues("management.health.diskspace.threshold=20MB").run((context) -> { | ||
assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class); | ||
assertThat(context.getBean(DiskSpaceHealthIndicator.class)).hasFieldOrPropertyWithValue("threshold", | ||
DataSize.ofMegabytes(20)); | ||
}); | ||
this.contextRunner.withPropertyValues("management.health.diskspace.paths[0].threshold=20MB") | ||
.run((context) -> validateIndicatorHasPathsExactly(entry(new File("."), DataSize.ofMegabytes(20)))); | ||
} | ||
|
||
@Test | ||
void pathAndThresholdCanBeCustomized() { | ||
this.contextRunner | ||
.withPropertyValues("management.health.diskspace.paths[0].path=..", | ||
"management.health.diskspace.paths[0].threshold=20MB") | ||
.run((context) -> validateIndicatorHasPathsExactly(entry(new File(".."), DataSize.ofMegabytes(20)))); | ||
} | ||
|
||
@Test | ||
void multiplePathsCanBeConfigured() { | ||
this.contextRunner.withPropertyValues("management.health.diskspace.paths[0].path=.", | ||
"management.health.diskspace.paths[1].path=..", "management.health.diskspace.paths[1].threshold=33MB") | ||
.run((context) -> validateIndicatorHasPathsExactly(entry(new File("."), DataSize.ofMegabytes(10)), | ||
entry(new File(".."), DataSize.ofMegabytes(33)))); | ||
} | ||
|
||
@Test | ||
void thresholdMustBePositive() { | ||
this.contextRunner.withPropertyValues("management.health.diskspace.paths[0].threshold=-10MB") | ||
.run((context) -> assertThat(context).hasFailed().getFailure().getCause().hasMessageContaining( | ||
"Failed to bind properties under 'management.health.diskspace.paths[0]'")); | ||
} | ||
|
||
@Test | ||
void runWhenPathDoesNotExistShouldCreateIndicator() { | ||
this.contextRunner.withPropertyValues("management.health.diskspace.path=does/not/exist") | ||
this.contextRunner.withPropertyValues("management.health.diskspace.paths[0].path=does/not/exist") | ||
.run((context) -> assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class)); | ||
} | ||
|
||
|
@@ -71,4 +99,13 @@ void runWhenDisabledShouldNotCreateIndicator() { | |
.run((context) -> assertThat(context).doesNotHaveBean(DiskSpaceHealthIndicator.class)); | ||
} | ||
|
||
@SafeVarargs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yuk.... Alternative is to use |
||
@SuppressWarnings("varargs") | ||
private final ContextConsumer<AssertableApplicationContext> validateIndicatorHasPathsExactly( | ||
Map.Entry<File, DataSize>... entries) { | ||
return (context) -> assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class) | ||
.getBean(DiskSpaceHealthIndicator.class).extracting("paths") | ||
.asInstanceOf(InstanceOfAssertFactories.map(File.class, DataSize.class)).containsExactly(entries); | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As stated in the overview comment, we could keep support of the
management.health.diskspace.path
(for singular case only) by mapping aprivate PathInfo path = new PathInfo();
here as well.