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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.participant.kubernetes.helm;
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.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.fail;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.mockStatic;
33 import static org.mockito.Mockito.when;
36 import java.io.IOException;
37 import java.nio.file.Path;
38 import java.util.List;
39 import org.junit.jupiter.api.AfterAll;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.mockito.MockedStatic;
46 import org.mockito.Mockito;
47 import org.mockito.Spy;
48 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
49 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
50 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
51 import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository;
52 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartStore;
53 import org.onap.policy.common.utils.coder.Coder;
54 import org.onap.policy.common.utils.coder.CoderException;
55 import org.onap.policy.common.utils.coder.StandardCoder;
56 import org.springframework.test.context.junit.jupiter.SpringExtension;
57 import org.springframework.util.FileSystemUtils;
60 @ExtendWith(SpringExtension.class)
61 class HelmClientTest {
63 private static final Coder CODER = new StandardCoder();
64 private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
65 private static List<ChartInfo> charts;
69 private HelmClient helmClient = new HelmClient();
72 ChartStore chartStore;
77 private static MockedStatic<HelmClient> mockedClient;
80 static void init() throws CoderException {
81 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
82 //Mock static method for bash command execution
83 mockedClient = mockStatic(HelmClient.class);
87 public static void close() throws IOException {
89 FileSystemUtils.deleteRecursively(Path.of("target/tmp"));
93 void test_installChart() {
94 mockedClient.when(() -> HelmClient.executeCommand(any()))
95 .thenReturn("success");
96 doReturn(new File("/target/tmp/override.yaml")).when(chartStore)
97 .getOverrideFile(any());
98 var chartinfo = charts.get(0);
100 assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
101 chartinfo.setNamespace("");
102 assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
104 mockedClient.when(() -> HelmClient.executeCommand(any())).thenReturn("");
105 assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
110 void test_addRepository() {
111 mockedClient.when(() -> HelmClient.executeCommand(any())).thenReturn("");
112 when(repo.getRepoName()).thenReturn("RepoName");
113 when(repo.getAddress()).thenReturn("http://localhost:8080");
114 assertDoesNotThrow(() -> helmClient.addRepository(repo));
116 mockedClient.when(() -> HelmClient.executeCommand(any()))
117 .thenReturn("failed");
118 assertDoesNotThrow(() -> helmClient.addRepository(repo));
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");
130 File tmpFile = new File(tmpPath + charts.get(1).getChartId().getName());
131 if (!tmpFile.mkdirs()) {
132 fail("Couldn't create dirs");
135 doReturn(Path.of(tmpPath)).when(chartStore).getAppPath(charts.get(1).getChartId());
137 doReturn(null).when(helmClient).verifyConfiguredRepo(charts.get(1));
139 String localRepoName = helmClient.findChartRepository(charts.get(1));
140 assertNotNull(localRepoName);
141 assertThat(localRepoName).endsWith(charts.get(0).getChartId().getVersion());
145 void test_uninstallChart() throws ServiceException {
146 helmClient.uninstallChart(charts.get(0));
147 mockedClient.when(() -> HelmClient.executeCommand(any())).thenThrow(new ServiceException("error in execution"));
149 assertThatThrownBy(() -> helmClient.uninstallChart(charts.get(0)))
150 .isInstanceOf(ServiceException.class);
154 void test_verifyConfiguredRepoForInvalidChart() throws IOException, ServiceException {
155 mockedClient.when(() -> HelmClient.executeCommand(Mockito.any()))
157 String configuredRepo = helmClient.verifyConfiguredRepo(charts.get(1));
158 assertNull(configuredRepo);