5f8b7dc78c8f673b63f221a582366c096f90818d
[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 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.mockStatic;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.Path;
35 import java.util.List;
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.Mock;
41 import org.mockito.MockedStatic;
42 import org.mockito.Mockito;
43 import org.mockito.Spy;
44 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
45 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
46 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
47 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartStore;
48 import org.onap.policy.common.utils.coder.Coder;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.springframework.test.context.junit.jupiter.SpringExtension;
52
53
54 @ExtendWith(SpringExtension.class)
55 class HelmClientTest {
56
57     private static final Coder CODER = new StandardCoder();
58     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
59     private static List<ChartInfo> charts;
60
61     @InjectMocks
62     @Spy
63     private HelmClient helmClient = new HelmClient();
64
65     @Mock
66     ChartStore chartStore;
67
68     private static MockedStatic<HelmClient> mockedClient;
69
70     @BeforeAll
71     static void init() throws CoderException {
72         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
73         //Mock static method for bash command execution
74         mockedClient = mockStatic(HelmClient.class);
75     }
76
77     @Test
78     void test_installChart() throws IOException {
79         mockedClient.when(() -> HelmClient.executeCommand(any()))
80             .thenReturn("success");
81         doReturn(new File("/target/tmp/override.yaml")).when(chartStore)
82             .getOverrideFile(any());
83         assertDoesNotThrow(() -> helmClient.installChart(charts.get(0)));
84     }
85
86     @Test
87     void test_findChartRepository() throws IOException, ServiceException {
88         mockedClient.when(() -> HelmClient.executeCommand(Mockito.any()))
89             .thenReturn("nginx-stable/nginx-ingress\t0.9.3\t1.11.3"
90                 + " \tNGINX Ingress Controller");
91         String configuredRepo = helmClient.findChartRepository(charts.get(1));
92
93         assertThat(configuredRepo).isEqualTo("nginx-stable");
94
95         doReturn(Path.of("/target/tmp/dummyChart/1.0")).when(chartStore).getAppPath(charts.get(1).getChartName(),
96             charts.get(1).getVersion());
97
98         doReturn(null).when(helmClient).verifyConfiguredRepo(charts.get(1));
99
100         String localRepoName = helmClient.findChartRepository(charts.get(1));
101         assertNotNull(localRepoName);
102         assertThat(localRepoName).endsWith(charts.get(0).getVersion());
103     }
104
105     @Test
106     void test_uninstallChart() throws ServiceException {
107         helmClient.uninstallChart(charts.get(0));
108         mockedClient.when(() -> HelmClient.executeCommand(any())).thenThrow(new ServiceException("error in execution"));
109
110         assertThatThrownBy(() -> helmClient.uninstallChart(charts.get(0)))
111             .isInstanceOf(ServiceException.class);
112     }
113
114     @Test
115     void test_verifyConfiguredRepoForInvalidChart() throws IOException, ServiceException {
116         mockedClient.when(() -> HelmClient.executeCommand(Mockito.any()))
117             .thenReturn("");
118         String configuredRepo = helmClient.verifyConfiguredRepo(charts.get(1));
119         assertNull(configuredRepo);
120     }
121
122 }