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