91aff830f8f512a223e073901c3b367f3176163b
[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.junit.jupiter.api.Assertions.assertDoesNotThrow;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doReturn;
29
30 import java.io.File;
31 import java.util.List;
32 import java.util.Map;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
40 import org.onap.policy.clamp.acm.participant.kubernetes.handler.AutomationCompositionElementHandler;
41 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
42 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
47
48 @ExtendWith(SpringExtension.class)
49 class PodStatusValidatorTest {
50
51     private static final Coder CODER = new StandardCoder();
52     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
53     private static int TIMEOUT = 2;
54     private static int STATUS_CHECK_INTERVAL = 1;
55     private static List<ChartInfo> charts;
56
57     @InjectMocks
58     private PodStatusValidator podStatusValidator = new PodStatusValidator(charts.get(0), TIMEOUT,
59             STATUS_CHECK_INTERVAL);
60
61     @InjectMocks
62     private PodStatusValidator podValidatorWithPodName = new PodStatusValidator(charts.get(2), TIMEOUT,
63             STATUS_CHECK_INTERVAL);
64
65
66     @Mock
67     private HelmClient client;
68
69     @BeforeAll
70     static void init() throws CoderException {
71         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
72     }
73
74     @AfterEach
75     void clearPodStatusMap() {
76         AutomationCompositionElementHandler.getPodStatusMap().clear();
77     }
78
79
80     @Test
81     void test_RunningPodState() throws ServiceException {
82         String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nHelloWorld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h";
83         doReturn(runningPod).when(client).executeCommand(any());
84         assertDoesNotThrow(() -> podStatusValidator.run());
85         assertThat(AutomationCompositionElementHandler.getPodStatusMap()).hasSize(1);
86         assertThat(AutomationCompositionElementHandler.getPodStatusMap()).containsKey(charts.get(0).getReleaseName());
87         assertThat(AutomationCompositionElementHandler.getPodStatusMap())
88             .containsValue(Map.of("HelloWorld-54777df9f8-qpzqr", "Running"));
89     }
90
91     @Test
92     void test_InvalidPodState() throws ServiceException {
93         String invalidPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\nhellofromdocker-54777df9f8-qpzqr\t1/1\tInit\t0\t9h";
94         doReturn(invalidPod).when(client).executeCommand(any());
95         assertThrows(ServiceException.class, () -> podStatusValidator.run());
96         assertThat(AutomationCompositionElementHandler.getPodStatusMap()).isEmpty();
97     }
98
99     // Use case scenario: Hard coded pod name
100     @Test
101     void test_RunningPodStateWithPodName() throws ServiceException {
102         String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nhelloallworld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h";
103         doReturn(runningPod).when(client).executeCommand(any());
104         assertDoesNotThrow(() -> podValidatorWithPodName.run());
105         assertThat(AutomationCompositionElementHandler.getPodStatusMap()).hasSize(1);
106         assertThat(AutomationCompositionElementHandler.getPodStatusMap()).containsKey(charts.get(2).getReleaseName());
107         assertThat(AutomationCompositionElementHandler.getPodStatusMap())
108             .containsValue(Map.of("helloallworld-54777df9f8-qpzqr", "Running"));
109     }
110 }