Skip to content

Commit db565cf

Browse files
Phooshawilkinsona
authored andcommitted
Improve handling of non-existent path in disk space health check
See gh-20580
1 parent 960ab15 commit db565cf

File tree

5 files changed

+131
-4
lines changed

5 files changed

+131
-4
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthIndicatorProperties.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public File getPath() {
4848
}
4949

5050
public void setPath(File path) {
51-
Assert.isTrue(path.exists(), () -> "Path '" + path + "' does not exist");
52-
Assert.isTrue(path.canRead(), () -> "Path '" + path + "' cannot be read");
5351
this.path = path;
5452
}
5553

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/system/DiskSpaceHealthContributorAutoConfigurationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.system;
1818

19+
import java.util.UUID;
20+
1921
import org.junit.jupiter.api.Test;
2022

2123
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
@@ -58,6 +60,13 @@ void thresholdCanBeCustomized() {
5860
});
5961
}
6062

63+
@Test
64+
void pathIsNotRequiredToExist() {
65+
String randomPath = "IDoNOTeXiST" + UUID.randomUUID().toString();
66+
this.contextRunner.withPropertyValues("management.health.diskspace.path=" + randomPath)
67+
.run((context) -> assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class));
68+
}
69+
6170
@Test
6271
void runWhenDisabledShouldNotCreateIndicator() {
6372
this.contextRunner.withPropertyValues("management.health.diskspace.enabled:false")

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public DiskSpaceHealthIndicator(File path, DataSize threshold) {
5959
@Override
6060
protected void doHealthCheck(Health.Builder builder) throws Exception {
6161
long diskFreeInBytes = this.path.getUsableSpace();
62-
if (diskFreeInBytes >= this.threshold.toBytes()) {
62+
// return value of 0L means "the abstract pathname does not name a
63+
// partition" which for our purposes means it's not usable i.e DOWN
64+
if (diskFreeInBytes >= this.threshold.toBytes() && diskFreeInBytes != 0L) {
6365
builder.up();
6466
}
6567
else {
@@ -68,7 +70,9 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
6870
builder.down();
6971
}
7072
builder.withDetail("total", this.path.getTotalSpace()).withDetail("free", diskFreeInBytes)
71-
.withDetail("threshold", this.threshold.toBytes());
73+
.withDetail("threshold", this.threshold.toBytes()).withDetail("exists", this.path.exists())
74+
.withDetail("canRead", this.path.canRead()).withDetail("canWrite", this.path.canWrite())
75+
.withDetail("canExecute", this.path.canExecute());
7276
}
7377

7478
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.system;
18+
19+
import java.io.File;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
import org.mockito.Mock;
24+
import org.mockito.MockitoAnnotations;
25+
26+
import org.springframework.boot.actuate.health.Health;
27+
import org.springframework.boot.actuate.health.HealthIndicator;
28+
import org.springframework.boot.actuate.health.Status;
29+
import org.springframework.util.unit.DataSize;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.BDDMockito.given;
33+
34+
/**
35+
* Tests for the {@link DiskSpaceHealthIndicator} {@code path} parameter.
36+
*
37+
* @author Andreas Born
38+
*/
39+
class DiskSpaceHealthIndicatorPathTests {
40+
41+
private static final DataSize THRESHOLD = DataSize.ofKilobytes(1);
42+
43+
private static final DataSize ZERO_THRESHOLD = DataSize.ofBytes(0);
44+
45+
private static final DataSize TOTAL_SPACE = DataSize.ofKilobytes(10);
46+
47+
@Mock
48+
private File fileMock;
49+
50+
private HealthIndicator healthIndicator;
51+
52+
@BeforeEach
53+
void setUp() {
54+
MockitoAnnotations.initMocks(this);
55+
given(this.fileMock.exists()).willReturn(false);
56+
given(this.fileMock.canRead()).willReturn(false);
57+
given(this.fileMock.canWrite()).willReturn(false);
58+
given(this.fileMock.canExecute()).willReturn(false);
59+
this.healthIndicator = new DiskSpaceHealthIndicator(this.fileMock, THRESHOLD);
60+
}
61+
62+
@Test
63+
void diskSpaceIsDown() {
64+
Health health = this.healthIndicator.health();
65+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
66+
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
67+
assertThat(health.getDetails().get("free")).isEqualTo(0L);
68+
assertThat(health.getDetails().get("total")).isEqualTo(0L);
69+
assertThat(health.getDetails().get("exists")).isEqualTo(false);
70+
assertThat(health.getDetails().get("canRead")).isEqualTo(false);
71+
assertThat(health.getDetails().get("canWrite")).isEqualTo(false);
72+
assertThat(health.getDetails().get("canExecute")).isEqualTo(false);
73+
}
74+
75+
@Test
76+
void diskSpaceIsDownWhenThresholdIsZero() {
77+
this.healthIndicator = new DiskSpaceHealthIndicator(this.fileMock, ZERO_THRESHOLD);
78+
Health health = this.healthIndicator.health();
79+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
80+
assertThat(health.getDetails().get("threshold")).isEqualTo(ZERO_THRESHOLD.toBytes());
81+
assertThat(health.getDetails().get("free")).isEqualTo(0L);
82+
assertThat(health.getDetails().get("total")).isEqualTo(0L);
83+
assertThat(health.getDetails().get("exists")).isEqualTo(false);
84+
assertThat(health.getDetails().get("canRead")).isEqualTo(false);
85+
assertThat(health.getDetails().get("canWrite")).isEqualTo(false);
86+
assertThat(health.getDetails().get("canExecute")).isEqualTo(false);
87+
}
88+
89+
@Test
90+
void diskSpaceIsUpWhenPathOnlyExists() {
91+
long freeSpace = THRESHOLD.toBytes() + 10;
92+
given(this.fileMock.getUsableSpace()).willReturn(freeSpace);
93+
given(this.fileMock.getTotalSpace()).willReturn(TOTAL_SPACE.toBytes());
94+
given(this.fileMock.exists()).willReturn(true);
95+
Health health = this.healthIndicator.health();
96+
assertThat(health.getStatus()).isEqualTo(Status.UP);
97+
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
98+
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
99+
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
100+
assertThat(health.getDetails().get("exists")).isEqualTo(true);
101+
assertThat(health.getDetails().get("canRead")).isEqualTo(false);
102+
assertThat(health.getDetails().get("canWrite")).isEqualTo(false);
103+
assertThat(health.getDetails().get("canExecute")).isEqualTo(false);
104+
}
105+
106+
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicatorTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ void setUp() {
5353
MockitoAnnotations.initMocks(this);
5454
given(this.fileMock.exists()).willReturn(true);
5555
given(this.fileMock.canRead()).willReturn(true);
56+
given(this.fileMock.canWrite()).willReturn(true);
57+
given(this.fileMock.canExecute()).willReturn(true);
5658
this.healthIndicator = new DiskSpaceHealthIndicator(this.fileMock, THRESHOLD);
5759
}
5860

@@ -66,6 +68,10 @@ void diskSpaceIsUp() {
6668
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
6769
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
6870
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
71+
assertThat(health.getDetails().get("exists")).isEqualTo(true);
72+
assertThat(health.getDetails().get("canRead")).isEqualTo(true);
73+
assertThat(health.getDetails().get("canWrite")).isEqualTo(true);
74+
assertThat(health.getDetails().get("canExecute")).isEqualTo(true);
6975
}
7076

7177
@Test
@@ -78,6 +84,10 @@ void diskSpaceIsDown() {
7884
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
7985
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
8086
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
87+
assertThat(health.getDetails().get("exists")).isEqualTo(true);
88+
assertThat(health.getDetails().get("canRead")).isEqualTo(true);
89+
assertThat(health.getDetails().get("canWrite")).isEqualTo(true);
90+
assertThat(health.getDetails().get("canExecute")).isEqualTo(true);
8191
}
8292

8393
}

0 commit comments

Comments
 (0)