5bf7bf13b22223132c58cefd3c8f7a78634f99cb
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.kserve.k8s;
22
23 import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28
29 import io.kubernetes.client.openapi.ApiException;
30 import java.io.IOException;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.clamp.acm.participant.kserve.exception.KserveException;
33
34 class InferenceServiceValidatorTest {
35
36     private static final int TIMEOUT = 2;
37     private static final int STATUS_CHECK_INTERVAL = 1;
38
39     private static final String inferenceSvcName = "inference-test";
40     private static final String namespace = "test";
41
42     @Test
43     void test_runningPodState() throws IOException, ApiException {
44         var kserveClient = mock(KserveClient.class);
45         doReturn("True").when(kserveClient).getInferenceServiceStatus(any(), any());
46         var inferenceServiceValidator =
47                 new InferenceServiceValidator(inferenceSvcName, namespace, TIMEOUT, STATUS_CHECK_INTERVAL,
48                         kserveClient);
49         assertDoesNotThrow(inferenceServiceValidator::run);
50     }
51
52     @Test
53     void test_EmptyPodState() throws IOException, ApiException {
54         var kserveClient = mock(KserveClient.class);
55         doReturn("").when(kserveClient).getInferenceServiceStatus(any(), any());
56         var inferenceServiceValidator =
57                 new InferenceServiceValidator("", namespace, TIMEOUT, STATUS_CHECK_INTERVAL,
58                         kserveClient);
59         assertThatThrownBy(inferenceServiceValidator::run).isInstanceOf(KserveException.class)
60                 .cause().hasMessage("Kserve setup is unavailable for inference service to be deployed");
61     }
62
63     @Test
64     void test_PodFailureState() throws IOException, ApiException {
65         var kserveClient = mock(KserveClient.class);
66         doReturn("False").when(kserveClient).getInferenceServiceStatus(any(), any());
67         var inferenceServiceValidator =
68                 new InferenceServiceValidator(inferenceSvcName, namespace, TIMEOUT, STATUS_CHECK_INTERVAL,
69                         kserveClient);
70         assertThatThrownBy(inferenceServiceValidator::run).isInstanceOf(KserveException.class)
71                 .hasMessage("Error verifying the status of the inference service. Exiting");
72     }
73
74 }