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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.kserve.handler;
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.spy;
32 import io.kubernetes.client.openapi.ApiException;
33 import jakarta.validation.ValidationException;
34 import java.io.IOException;
35 import java.util.HashMap;
37 import java.util.concurrent.ExecutionException;
38 import org.junit.jupiter.api.BeforeAll;
39 import org.junit.jupiter.api.Test;
40 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
41 import org.onap.policy.clamp.acm.participant.kserve.exception.KserveException;
42 import org.onap.policy.clamp.acm.participant.kserve.k8s.KserveClient;
43 import org.onap.policy.clamp.acm.participant.kserve.utils.CommonTestData;
44 import org.onap.policy.clamp.acm.participant.kserve.utils.ToscaUtils;
45 import org.onap.policy.models.base.PfModelException;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 class AcElementHandlerTest {
50 private final CommonTestData commonTestData = new CommonTestData();
52 private static ToscaServiceTemplate serviceTemplate;
53 private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
54 "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
58 serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
62 void test_automationCompositionElementStateChange()
63 throws ExecutionException, InterruptedException, IOException, ApiException {
64 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
65 var compositionElement = commonTestData.getCompositionElement(
66 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
67 var element = commonTestData.getAutomationCompositionElement();
69 var kserveClient = mock(KserveClient.class);
70 doReturn(true).when(kserveClient).deployInferenceService(any(), any());
71 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
72 var automationCompositionElementHandler =
73 spy(new AutomationCompositionElementHandler(participantIntermediaryApi, kserveClient));
74 doReturn(true).when(automationCompositionElementHandler)
75 .checkInferenceServiceStatus(any(), any(), anyInt(), anyInt());
77 assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(compositionElement, element));
78 assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(compositionElement, element));
82 void test_automationCompositionElementFailed()
83 throws ExecutionException, InterruptedException, IOException, ApiException {
84 var kserveClient = mock(KserveClient.class);
85 doReturn(false).when(kserveClient).deployInferenceService(any(), any());
86 doReturn(false).when(kserveClient).undeployInferenceService(any(), any());
87 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
88 var automationCompositionElementHandler =
89 spy(new AutomationCompositionElementHandler(participantIntermediaryApi, kserveClient));
90 doReturn(false).when(automationCompositionElementHandler)
91 .checkInferenceServiceStatus(any(), any(), anyInt(), anyInt());
93 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
94 var compositionElement = commonTestData.getCompositionElement(
95 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
96 var element = commonTestData.getAutomationCompositionElement();
97 assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(compositionElement, element));
98 assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(compositionElement, element));
102 void test_automationCompositionElementWrongData() {
103 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
104 var element = commonTestData.getAutomationCompositionElement();
106 var kserveClient = mock(KserveClient.class);
107 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
108 var automationCompositionElementHandler =
109 new AutomationCompositionElementHandler(participantIntermediaryApi, kserveClient);
111 var compositionElementEmpty = commonTestData.getCompositionElement(Map.of());
112 assertThrows(ValidationException.class,
113 () -> automationCompositionElementHandler.deploy(compositionElementEmpty, element));
115 var compositionElementWrong = commonTestData.getCompositionElement(Map.of("kserveInferenceEntities", "1"));
116 assertThrows(KserveException.class,
117 () -> automationCompositionElementHandler.deploy(compositionElementWrong, element));
119 var map = new HashMap<>(nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
120 map.put("uninitializedToPassiveTimeout", " ");
121 var compositionElementWrong2 = commonTestData.getCompositionElement(map);
122 assertThrows(KserveException.class,
123 () -> automationCompositionElementHandler.deploy(compositionElementWrong2, element));
127 void test_AutomationCompositionElementUpdate()
128 throws IOException, ApiException, ExecutionException, InterruptedException {
129 var kserveClient = mock(KserveClient.class);
130 doReturn(true).when(kserveClient).deployInferenceService(any(), any());
132 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
133 var automationCompositionElementHandler =
134 spy(new AutomationCompositionElementHandler(participantIntermediaryApi, kserveClient));
135 doReturn(true).when(automationCompositionElementHandler)
136 .checkInferenceServiceStatus(any(), any(), anyInt(), anyInt());
137 doThrow(new ApiException("Error installing the inference service")).when(kserveClient)
138 .deployInferenceService(any(), any());
140 var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
141 var compositionElement = commonTestData.getCompositionElement(
142 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
143 var element = commonTestData.getAutomationCompositionElement();
144 assertThrows(KserveException.class,
145 () -> automationCompositionElementHandler.deploy(compositionElement, element));
150 void test_checkInferenceServiceStatus() throws IOException, ApiException {
151 var kserveClient = mock(KserveClient.class);
152 doReturn("True").when(kserveClient).getInferenceServiceStatus(any(), any());
153 doReturn(true).when(kserveClient).deployInferenceService(any(), any());
154 var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
155 var automationCompositionElementHandler =
156 new AutomationCompositionElementHandler(participantIntermediaryApi, kserveClient);
158 assertDoesNotThrow(() -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris",
159 "kserve-test", 1, 1));