ccdb31f82db7ea7cdfeb1ecf84c543ce1dd918f2
[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.handler;
22
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;
31
32 import io.kubernetes.client.openapi.ApiException;
33 import jakarta.validation.ValidationException;
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.Map;
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;
47
48 class AcElementHandlerTest {
49
50     private final CommonTestData commonTestData = new CommonTestData();
51
52     private static ToscaServiceTemplate serviceTemplate;
53     private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
54             "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
55
56     @BeforeAll
57     static void init() {
58         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
59     }
60
61     @Test
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();
68
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());
76
77         assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(compositionElement, element));
78         assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(compositionElement, element));
79     }
80
81     @Test
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());
92
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));
99     }
100
101     @Test
102     void test_automationCompositionElementWrongData() {
103         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
104         var element = commonTestData.getAutomationCompositionElement();
105
106         var kserveClient = mock(KserveClient.class);
107         var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
108         var automationCompositionElementHandler =
109                 new AutomationCompositionElementHandler(participantIntermediaryApi, kserveClient);
110
111         var compositionElementEmpty = commonTestData.getCompositionElement(Map.of());
112         assertThrows(ValidationException.class,
113                 () -> automationCompositionElementHandler.deploy(compositionElementEmpty, element));
114
115         var compositionElementWrong = commonTestData.getCompositionElement(Map.of("kserveInferenceEntities", "1"));
116         assertThrows(KserveException.class,
117                 () -> automationCompositionElementHandler.deploy(compositionElementWrong, element));
118
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));
124     }
125
126     @Test
127     void test_AutomationCompositionElementUpdate()
128             throws IOException, ApiException, ExecutionException, InterruptedException {
129         var kserveClient = mock(KserveClient.class);
130         doReturn(true).when(kserveClient).deployInferenceService(any(), any());
131
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());
139
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));
146
147     }
148
149     @Test
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);
157
158         assertDoesNotThrow(() -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris",
159                 "kserve-test", 1, 1));
160     }
161 }