Skip to content

Commit f238812

Browse files
committed
Polish "Improve handling of non-existent path in disk space health check"
See gh-20580
1 parent db565cf commit f238812

File tree

5 files changed

+17
-130
lines changed

5 files changed

+17
-130
lines changed

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

Lines changed: 1 addition & 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.

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

Lines changed: 3 additions & 6 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.
@@ -16,8 +16,6 @@
1616

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

19-
import java.util.UUID;
20-
2119
import org.junit.jupiter.api.Test;
2220

2321
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
@@ -61,9 +59,8 @@ void thresholdCanBeCustomized() {
6159
}
6260

6361
@Test
64-
void pathIsNotRequiredToExist() {
65-
String randomPath = "IDoNOTeXiST" + UUID.randomUUID().toString();
66-
this.contextRunner.withPropertyValues("management.health.diskspace.path=" + randomPath)
62+
void runWhenPathDoesNotExistShouldCreateIndicator() {
63+
this.contextRunner.withPropertyValues("management.health.diskspace.path=does/not/exist")
6764
.run((context) -> assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class));
6865
}
6966

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

Lines changed: 3 additions & 7 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.
@@ -59,9 +59,7 @@ public DiskSpaceHealthIndicator(File path, DataSize threshold) {
5959
@Override
6060
protected void doHealthCheck(Health.Builder builder) throws Exception {
6161
long diskFreeInBytes = this.path.getUsableSpace();
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) {
62+
if (diskFreeInBytes >= this.threshold.toBytes()) {
6563
builder.up();
6664
}
6765
else {
@@ -70,9 +68,7 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
7068
builder.down();
7169
}
7270
builder.withDetail("total", this.path.getTotalSpace()).withDetail("free", diskFreeInBytes)
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());
71+
.withDetail("threshold", this.threshold.toBytes()).withDetail("exists", this.path.exists());
7672
}
7773

7874
}

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

Lines changed: 0 additions & 106 deletions
This file was deleted.

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

Lines changed: 10 additions & 10 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,9 +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);
56-
given(this.fileMock.canWrite()).willReturn(true);
57-
given(this.fileMock.canExecute()).willReturn(true);
5855
this.healthIndicator = new DiskSpaceHealthIndicator(this.fileMock, THRESHOLD);
5956
}
6057

@@ -69,9 +66,6 @@ void diskSpaceIsUp() {
6966
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
7067
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
7168
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);
7569
}
7670

7771
@Test
@@ -85,9 +79,15 @@ void diskSpaceIsDown() {
8579
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
8680
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
8781
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);
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);
9191
}
9292

9393
}

0 commit comments

Comments
 (0)