|
| 1 | +/* |
| 2 | + * Copyright 2022 MONAI Consortium |
| 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 | + * http://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 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 18 | +using Microsoft.Extensions.Logging; |
| 19 | +using Monai.Deploy.Storage.API; |
| 20 | +using Moq; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace Monai.Deploy.Storage.MinIO.Tests |
| 24 | +{ |
| 25 | + public class MinIoAdminHealthCheckTest |
| 26 | + { |
| 27 | + private readonly Mock<IStorageAdminService> _storageAdminService; |
| 28 | + private readonly Mock<ILogger<MinIoAdminHealthCheck>> _logger; |
| 29 | + |
| 30 | + public MinIoAdminHealthCheckTest() |
| 31 | + { |
| 32 | + _storageAdminService = new Mock<IStorageAdminService>(); |
| 33 | + _logger = new Mock<ILogger<MinIoAdminHealthCheck>>(); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task CheckHealthAsync_WhenConnectionThrows_ReturnUnhealthy() |
| 38 | + { |
| 39 | + _storageAdminService.Setup(p => p.HasConnectionAsync()).Throws(new Exception("error")); |
| 40 | + |
| 41 | + var healthCheck = new MinIoAdminHealthCheck(_storageAdminService.Object, _logger.Object); |
| 42 | + var results = await healthCheck.CheckHealthAsync(new HealthCheckContext()).ConfigureAwait(false); |
| 43 | + |
| 44 | + Assert.Equal(HealthStatus.Unhealthy, results.Status); |
| 45 | + Assert.NotNull(results.Exception); |
| 46 | + Assert.Equal("error", results.Exception.Message); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task CheckHealthAsync_WhenConnectionSucceeds_ReturnHealthy() |
| 51 | + { |
| 52 | + _storageAdminService.Setup(p => p.HasConnectionAsync()).ReturnsAsync(true); |
| 53 | + _storageAdminService.Setup(p => p.GetConnectionAsync()).ReturnsAsync(new List<string>() { "strings" }); |
| 54 | + var healthCheck = new MinIoAdminHealthCheck(_storageAdminService.Object, _logger.Object); |
| 55 | + var results = await healthCheck.CheckHealthAsync(new HealthCheckContext()).ConfigureAwait(false); |
| 56 | + |
| 57 | + Assert.Equal(HealthStatus.Healthy, results.Status); |
| 58 | + Assert.Null(results.Exception); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments