335dbcb21754597094b1044b65dc26ebeb1c4e55
[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 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.mockStatic;
32 import static org.mockito.Mockito.when;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.nio.file.Path;
37 import java.util.List;
38 import org.junit.jupiter.api.AfterAll;
39 import org.junit.jupiter.api.BeforeAll;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.extension.ExtendWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.MockedStatic;
45 import org.mockito.Mockito;
46 import org.mockito.Spy;
47 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
48 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
49 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
50 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.HelmRepository;
51 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartStore;
52 import org.onap.policy.common.utils.coder.Coder;
53 import org.onap.policy.common.utils.coder.CoderException;
54 import org.onap.policy.common.utils.coder.StandardCoder;
55 import org.springframework.test.context.junit.jupiter.SpringExtension;
56 import org.springframework.util.FileSystemUtils;
57
58
59 @ExtendWith(SpringExtension.class)
60 class HelmClientTest {
61
62     private static final Coder CODER = new StandardCoder();
63     private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
64     private static List<ChartInfo> charts;
65
66     @InjectMocks
67     @Spy
68     private HelmClient helmClient = new HelmClient();
69
70     @Mock
71     ChartStore chartStore;
72
73     @Mock
74     HelmRepository repo;
75
76     private static MockedStatic<HelmClient> mockedClient;
77
78     @BeforeAll
79     static void init() throws CoderException {
80         charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
81         //Mock static method for bash command execution
82         mockedClient = mockStatic(HelmClient.class);
83     }
84
85     @AfterAll
86     public static void close() throws IOException {
87         mockedClient.close();
88         FileSystemUtils.deleteRecursively(Path.of("target/tmp"));
89     }
90
91     @Test
92     void test_installChart() throws IOException {
93         mockedClient.when(() -> HelmClient.executeCommand(any()))
94             .thenReturn("success");
95         doReturn(new File("/target/tmp/override.yaml")).when(chartStore)
96             .getOverrideFile(any());
97         var chartinfo = charts.get(0);
98
99         assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
100         chartinfo.setNamespace("");
101         assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
102
103         mockedClient.when(() -> HelmClient.executeCommand(any()))
104             .thenReturn(new String());
105         assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
106
107     }
108
109     @Test
110     void test_addRepository() throws IOException {
111         mockedClient.when(() -> HelmClient.executeCommand(any()))
112             .thenReturn(new String());
113         when(repo.getRepoName()).thenReturn("RepoName");
114         assertDoesNotThrow(() -> helmClient.addRepository(repo));
115
116         mockedClient.when(() -> HelmClient.executeCommand(any()))
117             .thenReturn("failed");
118         assertDoesNotThrow(() -> helmClient.addRepository(repo));
119     }
120
121     @Test
122     void test_findChartRepository() throws IOException, ServiceException {
123         String tmpPath = "target/tmp/dummyChart/1.0/";
124         mockedClient.when(() -> HelmClient.executeCommand(Mockito.any()))
125             .thenReturn("nginx-stable/nginx-ingress\t0.9.3\t1.11.3"
126                 + " \tNGINX Ingress Controller");
127         String configuredRepo = helmClient.findChartRepository(charts.get(1));
128         assertThat(configuredRepo).isEqualTo("nginx-stable");
129
130         File tmpFile = new File(tmpPath + charts.get(1).getChartId().getName());
131         tmpFile.mkdirs();
132         doReturn(Path.of(tmpPath)).when(chartStore).getAppPath(charts.get(1).getChartId());
133
134         doReturn(null).when(helmClient).verifyConfiguredRepo(charts.get(1));
135
136         String localRepoName = helmClient.findChartRepository(charts.get(1));
137         assertNotNull(localRepoName);
138         assertThat(localRepoName).endsWith(charts.get(0).getChartId().getVersion());
139     }
140
141     @Test
142     void test_uninstallChart() throws ServiceException {
143         helmClient.uninstallChart(charts.get(0));
144         mockedClient.when(() -> HelmClient.executeCommand(any())).thenThrow(new ServiceException("error in execution"));
145
146         assertThatThrownBy(() -> helmClient.uninstallChart(charts.get(0)))
147             .isInstanceOf(ServiceException.class);
148     }
149
150     @Test
151     void test_verifyConfiguredRepoForInvalidChart() throws IOException, ServiceException {
152         mockedClient.when(() -> HelmClient.executeCommand(Mockito.any()))
153             .thenReturn("");
154         String configuredRepo = helmClient.verifyConfiguredRepo(charts.get(1));
155         assertNull(configuredRepo);
156     }
157
158 }