f72a534038adc2eb1332aa4312659004111924ed
[policy/clamp.git] /
1 /*-
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
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.controlloop.participant.kubernetes.helm;
22
23
24
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;
30
31 import java.io.File;
32 import java.util.List;
33 import java.util.Map;
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;
49
50 @ExtendWith(SpringExtension.class)
51 class PodStatusValidatorTest {
52
53
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;
59
60
61     @InjectMocks
62     private static PodStatusValidator podStatusValidator;
63
64     private static MockedStatic<HelmClient> mockedClient;
65
66
67     @BeforeAll
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);
72     }
73
74     @AfterEach
75     void clearPodStatusMap() {
76         ControlLoopElementHandler.getPodStatusMap().clear();
77     }
78
79     @AfterAll
80     public static void close() {
81         mockedClient.close();
82     }
83
84
85     @Test
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"));
95     }
96
97
98     @Test
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();
106     }
107
108 }