07dc021b1ef9f95f7eeb236fed746e8f02140b57
[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.handler;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.mock;
31
32 import io.kubernetes.client.openapi.ApiException;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.UUID;
37 import java.util.concurrent.ExecutionException;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Future;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Spy;
47 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
48 import org.onap.policy.clamp.acm.participant.kserve.exception.KserveException;
49 import org.onap.policy.clamp.acm.participant.kserve.k8s.KserveClient;
50 import org.onap.policy.clamp.acm.participant.kserve.utils.CommonTestData;
51 import org.onap.policy.clamp.acm.participant.kserve.utils.ToscaUtils;
52 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
53 import org.onap.policy.clamp.models.acm.concepts.DeployState;
54 import org.onap.policy.clamp.models.acm.concepts.LockState;
55 import org.onap.policy.models.base.PfModelException;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
57 import org.springframework.test.context.junit.jupiter.SpringExtension;
58
59 @ExtendWith(SpringExtension.class)
60 class AcElementHandlerTest {
61
62     private final KserveClient kserveClient = mock(KserveClient.class);
63
64     private ParticipantIntermediaryApi participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
65
66     @InjectMocks
67     @Spy
68     private AutomationCompositionElementHandler automationCompositionElementHandler =
69             new AutomationCompositionElementHandler(participantIntermediaryApi, kserveClient);
70
71     @Mock
72     private ExecutorService executor;
73     @Mock
74     private Future<String> result;
75
76     private final CommonTestData commonTestData = new CommonTestData();
77
78     private static ToscaServiceTemplate serviceTemplate;
79     private static final String KSERVE_AUTOMATION_COMPOSITION_ELEMENT =
80             "onap.policy.clamp.ac.element.KserveAutomationCompositionElement";
81
82     @BeforeAll
83     static void init() {
84         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
85     }
86
87     @BeforeEach
88     void startMocks() throws ExecutionException, InterruptedException, IOException, ApiException {
89         doReturn(true).when(kserveClient).deployInferenceService(any(), any());
90         doReturn(true).when(automationCompositionElementHandler).checkInferenceServiceStatus(any(), any(), anyInt(),
91                 anyInt());
92     }
93
94     @Test
95     void test_automationCompositionElementStateChange() throws PfModelException {
96         var automationCompositionId = commonTestData.getAutomationCompositionId();
97         var element = commonTestData.getAutomationCompositionElement();
98         var automationCompositionElementId = element.getId();
99
100         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
101         automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
102                 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties());
103
104         assertDoesNotThrow(() -> automationCompositionElementHandler.undeploy(automationCompositionId,
105                 automationCompositionElementId));
106
107     }
108
109     @Test
110     void test_AutomationCompositionElementUpdate() throws IOException, ApiException {
111         var element = commonTestData.getAutomationCompositionElement();
112
113         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
114         assertDoesNotThrow(() -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(),
115                 element, nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
116         assertThat(automationCompositionElementHandler.getConfigRequestMap()).hasSize(1).containsKey(element.getId());
117
118         doThrow(new ApiException("Error installing the inference service")).when(kserveClient)
119                 .deployInferenceService(any(), any());
120
121         var elementId2 = UUID.randomUUID();
122         element.setId(elementId2);
123         assertThrows(KserveException.class,
124                 () -> automationCompositionElementHandler.deploy(commonTestData.getAutomationCompositionId(), element,
125                         nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties()));
126
127         assertThat(automationCompositionElementHandler.getConfigRequestMap().containsKey(elementId2)).isFalse();
128     }
129
130     @Test
131     void test_checkInferenceServiceStatus() throws ExecutionException, InterruptedException {
132         doReturn(result).when(executor).submit(any(Runnable.class), any());
133         doReturn("Done").when(result).get();
134         doReturn(true).when(result).isDone();
135         assertDoesNotThrow(() -> automationCompositionElementHandler.checkInferenceServiceStatus("sklearn-iris",
136                 "kserve-test", 1, 1));
137     }
138
139     @Test
140     void testUpdate() throws PfModelException {
141         var automationCompositionId = commonTestData.getAutomationCompositionId();
142         var element = commonTestData.getAutomationCompositionElement();
143         assertDoesNotThrow(
144                 () -> automationCompositionElementHandler.update(automationCompositionId, element, Map.of()));
145     }
146
147     @Test
148     void testLock() throws PfModelException {
149         assertDoesNotThrow(() -> automationCompositionElementHandler.lock(UUID.randomUUID(), UUID.randomUUID()));
150     }
151
152     @Test
153     void testUnlock() throws PfModelException {
154         assertDoesNotThrow(() -> automationCompositionElementHandler.unlock(UUID.randomUUID(), UUID.randomUUID()));
155     }
156
157     @Test
158     void testDelete() throws PfModelException {
159         assertDoesNotThrow(() -> automationCompositionElementHandler.delete(UUID.randomUUID(), UUID.randomUUID()));
160     }
161
162     @Test
163     void testPrime() throws PfModelException {
164         assertDoesNotThrow(() -> automationCompositionElementHandler.prime(UUID.randomUUID(), List.of()));
165     }
166
167     @Test
168     void testDeprime() throws PfModelException {
169         assertDoesNotThrow(() -> automationCompositionElementHandler.deprime(UUID.randomUUID()));
170     }
171
172     @Test
173     void testHandleRestartComposition() throws PfModelException {
174         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartComposition(UUID.randomUUID(),
175                 List.of(), AcTypeState.PRIMED));
176     }
177
178     @Test
179     void testHandleRestartInstanceDeploying() throws PfModelException {
180         var element = commonTestData.getAutomationCompositionElement();
181
182         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
183         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
184                 commonTestData.getAutomationCompositionId(), element,
185                 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYING,
186                 LockState.NONE));
187         assertThat(automationCompositionElementHandler.getConfigRequestMap()).containsKey(element.getId());
188     }
189
190     @Test
191     void testHandleRestartInstanceDeployed() throws PfModelException {
192         var element = commonTestData.getAutomationCompositionElement();
193
194         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
195         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
196                 commonTestData.getAutomationCompositionId(), element,
197                 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.DEPLOYED,
198                 LockState.LOCKED));
199         assertThat(automationCompositionElementHandler.getConfigRequestMap()).containsKey(element.getId());
200     }
201
202     @Test
203     void testHandleRestartInstanceUndeployed() throws PfModelException {
204         var element = commonTestData.getAutomationCompositionElement();
205
206         var nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
207         assertDoesNotThrow(() -> automationCompositionElementHandler.handleRestartInstance(
208                 commonTestData.getAutomationCompositionId(), element,
209                 nodeTemplatesMap.get(KSERVE_AUTOMATION_COMPOSITION_ELEMENT).getProperties(), DeployState.UNDEPLOYING,
210                 LockState.LOCKED));
211     }
212
213     @Test
214     void testMigrate() throws PfModelException {
215         var automationCompositionId = commonTestData.getAutomationCompositionId();
216         var element = commonTestData.getAutomationCompositionElement();
217         assertDoesNotThrow(() -> automationCompositionElementHandler.migrate(automationCompositionId, element,
218                 UUID.randomUUID(), Map.of()));
219     }
220 }