Skip to content

Commit c19c9b8

Browse files
committed
Merge pull request #20580 from Phoosha
* gh-20580: Polish "Improve handling of non-existent path in disk space health check" Improve handling of non-existent path in disk space health check Closes gh-20580
2 parents 960ab15 + f238812 commit c19c9b8

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,6 +58,12 @@ void thresholdCanBeCustomized() {
5858
});
5959
}
6060

61+
@Test
62+
void runWhenPathDoesNotExistShouldCreateIndicator() {
63+
this.contextRunner.withPropertyValues("management.health.diskspace.path=does/not/exist")
64+
.run((context) -> assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class));
65+
}
66+
6167
@Test
6268
void runWhenDisabledShouldNotCreateIndicator() {
6369
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,7 +68,7 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
6868
builder.down();
6969
}
7070
builder.withDetail("total", this.path.getTotalSpace()).withDetail("free", diskFreeInBytes)
71-
.withDetail("threshold", this.threshold.toBytes());
71+
.withDetail("threshold", this.threshold.toBytes()).withDetail("exists", this.path.exists());
7272
}
7373

7474
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,6 @@ class DiskSpaceHealthIndicatorTests {
5252
void setUp() {
5353
MockitoAnnotations.initMocks(this);
5454
given(this.fileMock.exists()).willReturn(true);
55-
given(this.fileMock.canRead()).willReturn(true);
5655
this.healthIndicator = new DiskSpaceHealthIndicator(this.fileMock, THRESHOLD);
5756
}
5857

@@ -66,6 +65,7 @@ void diskSpaceIsUp() {
6665
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
6766
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
6867
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
68+
assertThat(health.getDetails().get("exists")).isEqualTo(true);
6969
}
7070

7171
@Test
@@ -78,6 +78,16 @@ void diskSpaceIsDown() {
7878
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
7979
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
8080
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
81+
assertThat(health.getDetails().get("exists")).isEqualTo(true);
82+
}
83+
84+
@Test
85+
void whenPathDoesNotExistDiskSpaceIsDown() {
86+
Health health = new DiskSpaceHealthIndicator(new File("does/not/exist"), THRESHOLD).health();
87+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
88+
assertThat(health.getDetails().get("free")).isEqualTo(0L);
89+
assertThat(health.getDetails().get("total")).isEqualTo(0L);
90+
assertThat(health.getDetails().get("exists")).isEqualTo(false);
8191
}
8292

8393
}

0 commit comments

Comments
 (0)