5640ac4be3d6613a07b3f970b5a3531f13901d75
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022,2024 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.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.doReturn;
28
29 import java.io.File;
30 import java.util.List;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
37 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
38 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
39 import org.onap.policy.common.utils.coder.Coder;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.models.base.PfModelException;
43 import org.springframework.test.context.junit.jupiter.SpringExtension;
44
45 @ExtendWith(SpringExtension.class)
46 class PodStatusValidatorTest {
47
48     private static final Coder CODER = new StandardCoder();
49     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
50     private static int TIMEOUT = 2;
51     private static int STATUS_CHECK_INTERVAL = 1;
52     private static List<ChartInfo> charts;
53
54     @InjectMocks
55     private PodStatusValidator podStatusValidator = new PodStatusValidator(charts.get(0), TIMEOUT,
56             STATUS_CHECK_INTERVAL);
57
58     @InjectMocks
59     private PodStatusValidator podValidatorWithPodName = new PodStatusValidator(charts.get(2), TIMEOUT,
60             STATUS_CHECK_INTERVAL);
61
62
63     @Mock
64     private HelmClient client;
65
66     @BeforeAll
67     static void init() throws CoderException {
68         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
69     }
70
71
72     @Test
73     void test_RunningPodState() throws ServiceException {
74         String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nHelloWorld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h";
75         doReturn(runningPod).when(client).executeCommand(any());
76         assertDoesNotThrow(() -> podStatusValidator.run());
77     }
78
79     @Test
80     void test_InvalidPodState() throws ServiceException {
81         String invalidPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\nhellofromdocker-54777df9f8-qpzqr\t1/1\tInit\t0\t9h";
82         doReturn(invalidPod).when(client).executeCommand(any());
83         assertThrows(PfModelException.class, () -> podStatusValidator.run());
84     }
85
86     // Use case scenario: Hard coded pod name
87     @Test
88     void test_RunningPodStateWithPodName() throws ServiceException {
89         String runningPod = "NAME\tREADY\tSTATUS\tRESTARTS\tAGE\r\nhelloallworld-54777df9f8-qpzqr\t1/1\tRunning\t0\t9h";
90         doReturn(runningPod).when(client).executeCommand(any());
91         assertDoesNotThrow(() -> podValidatorWithPodName.run());
92     }
93 }