|
| 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 | +} |
0 commit comments