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
This commit is contained in:
Andy Wilkinson
2020-03-31 12:24:25 +01:00
4 changed files with 22 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -48,8 +48,6 @@ public class DiskSpaceHealthIndicatorProperties {
}
public void setPath(File path) {
Assert.isTrue(path.exists(), () -> "Path '" + path + "' does not exist");
Assert.isTrue(path.canRead(), () -> "Path '" + path + "' cannot be read");
this.path = path;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -58,6 +58,12 @@ class DiskSpaceHealthContributorAutoConfigurationTests {
});
}
@Test
void runWhenPathDoesNotExistShouldCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.diskspace.path=does/not/exist")
.run((context) -> assertThat(context).hasSingleBean(DiskSpaceHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.diskspace.enabled:false")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -68,7 +68,7 @@ public class DiskSpaceHealthIndicator extends AbstractHealthIndicator {
builder.down();
}
builder.withDetail("total", this.path.getTotalSpace()).withDetail("free", diskFreeInBytes)
.withDetail("threshold", this.threshold.toBytes());
.withDetail("threshold", this.threshold.toBytes()).withDetail("exists", this.path.exists());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -52,7 +52,6 @@ class DiskSpaceHealthIndicatorTests {
void setUp() {
MockitoAnnotations.initMocks(this);
given(this.fileMock.exists()).willReturn(true);
given(this.fileMock.canRead()).willReturn(true);
this.healthIndicator = new DiskSpaceHealthIndicator(this.fileMock, THRESHOLD);
}
@@ -66,6 +65,7 @@ class DiskSpaceHealthIndicatorTests {
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
assertThat(health.getDetails().get("exists")).isEqualTo(true);
}
@Test
@@ -78,6 +78,16 @@ class DiskSpaceHealthIndicatorTests {
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
assertThat(health.getDetails().get("exists")).isEqualTo(true);
}
@Test
void whenPathDoesNotExistDiskSpaceIsDown() {
Health health = new DiskSpaceHealthIndicator(new File("does/not/exist"), THRESHOLD).health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails().get("free")).isEqualTo(0L);
assertThat(health.getDetails().get("total")).isEqualTo(0L);
assertThat(health.getDetails().get("exists")).isEqualTo(false);
}
}