18c32d4746b748e603e3775bab670d9adcede284
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.controlloop.participant.kubernetes.helm;
23
24
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.mockStatic;
31
32 import java.io.File;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.jupiter.api.AfterAll;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.extension.ExtendWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.MockedStatic;
42 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
43 import org.onap.policy.clamp.controlloop.participant.kubernetes.handler.ControlLoopElementHandler;
44 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
45 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.springframework.test.context.junit.jupiter.SpringExtension;
50
51 @ExtendWith(SpringExtension.class)
52 class PodStatusValidatorTest {
53
54
55     private static final Coder CODER = new StandardCoder();
56     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
57     private static List<ChartInfo> charts;
58     private static int timeout = 60;
59     private static int statusCheckInterval = 30;
60
61
62     @InjectMocks
63     private static PodStatusValidator podStatusValidator;
64
65     private static MockedStatic<HelmClient> mockedClient;
66
67
68     @BeforeAll
69     static void init() throws CoderException {
70         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
71         mockedClient = mockStatic(HelmClient.class);
72         podStatusValidator = new PodStatusValidator(charts.get(0), timeout, statusCheckInterval);
73     }
74
75     @AfterEach
76     void clearPodStatusMap() {
77         ControlLoopElementHandler.getPodStatusMap().clear();
78     }
79
80     @AfterAll
81     public static void close() {
82         mockedClient.close();
83     }
84
85
86     @Test
87     void test_RunningPodState() {
88         String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nHelloWorld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h";
89         mockedClient.when(() -> HelmClient.executeCommand(any()))
90             .thenReturn(runningPod);
91         assertDoesNotThrow(() -> podStatusValidator.run());
92         assertThat(ControlLoopElementHandler.getPodStatusMap()).hasSize(1);
93         assertThat(ControlLoopElementHandler.getPodStatusMap()).containsKey(charts.get(0).getReleaseName());
94         assertThat(ControlLoopElementHandler.getPodStatusMap())
95             .containsValue(Map.of("HelloWorld-54777df9f8-qpzqr", "Running"));
96     }
97
98
99     @Test
100     void test_InvalidPodState() {
101         String invalidPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\nhellofromdocker-54777df9f8-qpzqr\t1/1\tInit\t0\t9h";
102         mockedClient.when(() -> HelmClient.executeCommand(any()))
103             .thenReturn(invalidPod);
104         assertThatThrownBy(() -> podStatusValidator.run())
105             .isInstanceOf(ServiceException.class).hasMessage("Error verifying the status of the pod. Exiting");
106         assertThat(ControlLoopElementHandler.getPodStatusMap()).isEmpty();
107     }
108
109 }