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
pull/20743/head
Andy Wilkinson 5 years ago
commit c19c9b8dd6

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { 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; this.path = path;
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Test
void runWhenDisabledShouldNotCreateIndicator() { void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.diskspace.enabled:false") this.contextRunner.withPropertyValues("management.health.diskspace.enabled:false")

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.down();
} }
builder.withDetail("total", this.path.getTotalSpace()).withDetail("free", diskFreeInBytes) builder.withDetail("total", this.path.getTotalSpace()).withDetail("free", diskFreeInBytes)
.withDetail("threshold", this.threshold.toBytes()); .withDetail("threshold", this.threshold.toBytes()).withDetail("exists", this.path.exists());
} }
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,7 +52,6 @@ class DiskSpaceHealthIndicatorTests {
void setUp() { void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
given(this.fileMock.exists()).willReturn(true); given(this.fileMock.exists()).willReturn(true);
given(this.fileMock.canRead()).willReturn(true);
this.healthIndicator = new DiskSpaceHealthIndicator(this.fileMock, THRESHOLD); 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("threshold")).isEqualTo(THRESHOLD.toBytes());
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace); assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes()); assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes());
assertThat(health.getDetails().get("exists")).isEqualTo(true);
} }
@Test @Test
@ -78,6 +78,16 @@ class DiskSpaceHealthIndicatorTests {
assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes()); assertThat(health.getDetails().get("threshold")).isEqualTo(THRESHOLD.toBytes());
assertThat(health.getDetails().get("free")).isEqualTo(freeSpace); assertThat(health.getDetails().get("free")).isEqualTo(freeSpace);
assertThat(health.getDetails().get("total")).isEqualTo(TOTAL_SPACE.toBytes()); 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);
} }
} }

Loading…
Cancel
Save