3ef89a7c1be6cce5a3e5327774b2f62c07ff5ad9
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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
28 import io.kubernetes.client.openapi.ApiException;
29 import java.io.IOException;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.onap.policy.clamp.acm.participant.kserve.exception.KserveException;
33 import org.springframework.boot.test.mock.mockito.MockBean;
34 import org.springframework.test.context.junit.jupiter.SpringExtension;
35
36
37 @ExtendWith(SpringExtension.class)
38 class InferenceServiceValidatorTest {
39
40     private static int TIMEOUT = 2;
41     private static int STATUS_CHECK_INTERVAL = 1;
42
43     @MockBean
44     private KserveClient kserveClient;
45
46     String inferenceSvcName = "inference-test";
47     String namespace = "test";
48
49     @Test
50     void test_runningPodState() throws IOException, ApiException {
51         doReturn("True").when(kserveClient).getInferenceServiceStatus(any(), any());
52         var inferenceServiceValidator =
53                 new InferenceServiceValidator(inferenceSvcName, namespace, TIMEOUT, STATUS_CHECK_INTERVAL,
54                         kserveClient);
55         assertDoesNotThrow(inferenceServiceValidator::run);
56     }
57
58     @Test
59     void test_EmptyPodState() throws IOException, ApiException {
60         doReturn("").when(kserveClient).getInferenceServiceStatus(any(), any());
61         var inferenceServiceValidator =
62                 new InferenceServiceValidator("", namespace, TIMEOUT, STATUS_CHECK_INTERVAL,
63                         kserveClient);
64         assertThatThrownBy(inferenceServiceValidator::run).isInstanceOf(KserveException.class)
65                 .hasMessage("Error verifying the status of the inference service. Exiting");
66     }
67
68     @Test
69     void test_PodFailureState() throws IOException, ApiException {
70         doReturn("False").when(kserveClient).getInferenceServiceStatus(any(), any());
71         var inferenceServiceValidator =
72                 new InferenceServiceValidator(inferenceSvcName, namespace, TIMEOUT, STATUS_CHECK_INTERVAL,
73                         kserveClient);
74         assertThatThrownBy(inferenceServiceValidator::run).isInstanceOf(KserveException.class)
75                 .hasMessage("Error verifying the status of the inference service. Exiting");
76     }
77
78 }