|
| 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 System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Linq; |
| 20 | +using System.Text; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 23 | +using Monai.Deploy.InformaticsGateway.Repositories; |
| 24 | +using Monai.Deploy.InformaticsGateway.Services.Http; |
| 25 | +using Moq; |
| 26 | +using Xunit; |
| 27 | + |
| 28 | +namespace Monai.Deploy.InformaticsGateway.Test.Services.Http |
| 29 | +{ |
| 30 | + public class MonaiHealthCheckTest |
| 31 | + { |
| 32 | + private readonly Mock<IMonaiServiceLocator> _monaiServiceLocator; |
| 33 | + |
| 34 | + public MonaiHealthCheckTest() |
| 35 | + { |
| 36 | + _monaiServiceLocator = new Mock<IMonaiServiceLocator>(); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async Task GivenAllServicesRunning_WhenCheckHealthAsyncIsCalled_ReturnsHealthy() |
| 41 | + { |
| 42 | + _monaiServiceLocator.Setup(p => p.GetServiceStatus()).Returns(new Dictionary<string, Api.Rest.ServiceStatus>() |
| 43 | + { |
| 44 | + { "A", Api.Rest.ServiceStatus.Running }, |
| 45 | + { "B", Api.Rest.ServiceStatus.Running }, |
| 46 | + { "C", Api.Rest.ServiceStatus.Running }, |
| 47 | + }); |
| 48 | + |
| 49 | + var svc = new MonaiHealthCheck(_monaiServiceLocator.Object); |
| 50 | + var result = await svc.CheckHealthAsync(null); |
| 51 | + Assert.Equal(HealthStatus.Healthy, result.Status); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task GivenSomeServicesNotRunning_WhenCheckHealthAsyncIsCalled_ReturnsDegraded() |
| 56 | + { |
| 57 | + _monaiServiceLocator.Setup(p => p.GetServiceStatus()).Returns(new Dictionary<string, Api.Rest.ServiceStatus>() |
| 58 | + { |
| 59 | + { "A", Api.Rest.ServiceStatus.Running }, |
| 60 | + { "B", Api.Rest.ServiceStatus.Cancelled }, |
| 61 | + { "C", Api.Rest.ServiceStatus.Stopped }, |
| 62 | + }); |
| 63 | + |
| 64 | + var svc = new MonaiHealthCheck(_monaiServiceLocator.Object); |
| 65 | + var result = await svc.CheckHealthAsync(null); |
| 66 | + Assert.Equal(HealthStatus.Degraded, result.Status); |
| 67 | + Assert.Equal(Api.Rest.ServiceStatus.Cancelled, result.Data["B"]); |
| 68 | + Assert.Equal(Api.Rest.ServiceStatus.Stopped, result.Data["C"]); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public async Task GivenAllServicesNotRunning_WhenCheckHealthAsyncIsCalled_ReturnsUnhealthy() |
| 73 | + { |
| 74 | + _monaiServiceLocator.Setup(p => p.GetServiceStatus()).Returns(new Dictionary<string, Api.Rest.ServiceStatus>() |
| 75 | + { |
| 76 | + { "A", Api.Rest.ServiceStatus.Stopped }, |
| 77 | + { "B", Api.Rest.ServiceStatus.Cancelled }, |
| 78 | + { "C", Api.Rest.ServiceStatus.Stopped }, |
| 79 | + }); |
| 80 | + |
| 81 | + var svc = new MonaiHealthCheck(_monaiServiceLocator.Object); |
| 82 | + var result = await svc.CheckHealthAsync(null); |
| 83 | + |
| 84 | + Assert.Equal(HealthStatus.Unhealthy, result.Status); |
| 85 | + Assert.Equal(Api.Rest.ServiceStatus.Stopped, result.Data["A"]); |
| 86 | + Assert.Equal(Api.Rest.ServiceStatus.Cancelled, result.Data["B"]); |
| 87 | + Assert.Equal(Api.Rest.ServiceStatus.Stopped, result.Data["C"]); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments