2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.controlloop.participant.kubernetes.helm;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.mockStatic;
32 import java.util.List;
34 import org.junit.jupiter.api.AfterAll;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.MockedStatic;
41 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
42 import org.onap.policy.clamp.controlloop.participant.kubernetes.handler.ControlLoopElementHandler;
43 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
44 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
45 import org.onap.policy.common.utils.coder.Coder;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.springframework.test.context.junit.jupiter.SpringExtension;
50 @ExtendWith(SpringExtension.class)
51 class PodStatusValidatorTest {
54 private static final Coder CODER = new StandardCoder();
55 private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
56 private static List<ChartInfo> charts;
57 private static int timeout = 60;
58 private static int statusCheckInterval = 30;
62 private static PodStatusValidator podStatusValidator;
64 private static MockedStatic<HelmClient> mockedClient;
68 static void init() throws CoderException {
69 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
70 mockedClient = mockStatic(HelmClient.class);
71 podStatusValidator = new PodStatusValidator(charts.get(0), timeout, statusCheckInterval);
75 void clearPodStatusMap() {
76 ControlLoopElementHandler.getPodStatusMap().clear();
80 public static void close() {
86 void test_RunningPodState() {
87 String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nHelloWorld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h";
88 mockedClient.when(() -> HelmClient.executeCommand(any()))
89 .thenReturn(runningPod);
90 assertDoesNotThrow(() -> podStatusValidator.run());
91 assertThat(ControlLoopElementHandler.getPodStatusMap()).hasSize(1);
92 assertThat(ControlLoopElementHandler.getPodStatusMap()).containsKey(charts.get(0).getReleaseName());
93 assertThat(ControlLoopElementHandler.getPodStatusMap())
94 .containsValue(Map.of("HelloWorld-54777df9f8-qpzqr", "Running"));
99 void test_InvalidPodState() {
100 String invalidPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\nhellofromdocker-54777df9f8-qpzqr\t1/1\tInit\t0\t9h";
101 mockedClient.when(() -> HelmClient.executeCommand(any()))
102 .thenReturn(invalidPod);
103 assertThatThrownBy(() -> podStatusValidator.run())
104 .isInstanceOf(ServiceException.class).hasMessage("Error verifying the status of the pod. Exiting");
105 assertThat(ControlLoopElementHandler.getPodStatusMap()).isEmpty();