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.doThrow;
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.Spy;
46 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
47 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
48 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartList;
49 import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository;
50 import org.onap.policy.clamp.acm.participant.kubernetes.service.ChartStore;
51 import org.onap.policy.common.utils.coder.Coder;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.springframework.test.context.junit.jupiter.SpringExtension;
55 import org.springframework.util.FileSystemUtils;
58 @ExtendWith(SpringExtension.class)
59 class HelmClientTest {
61 private static final Coder CODER = new StandardCoder();
62 private static final String CHART_INFO_YAML = "src/test/resources/ChartList.json";
63 private static List<ChartInfo> charts;
67 private HelmClient helmClient = new HelmClient();
70 ChartStore chartStore;
77 static void init() throws CoderException {
78 charts = CODER.decode(new File(CHART_INFO_YAML), ChartList.class).getCharts();
82 public static void close() throws IOException {
83 FileSystemUtils.deleteRecursively(Path.of("target/tmp"));
87 void test_installChart() throws ServiceException {
88 doReturn("success").when(helmClient).executeCommand(any());
89 doReturn(new File("/target/tmp/override.yaml")).when(chartStore)
90 .getOverrideFile(any());
91 var chartinfo = charts.get(0);
93 assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
94 chartinfo.setNamespace("");
95 assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
97 doReturn("").when(helmClient).executeCommand(any());
98 assertDoesNotThrow(() -> helmClient.installChart(chartinfo));
103 void test_addRepository() throws ServiceException {
104 doReturn("").when(helmClient).executeCommand(any());
105 when(repo.getRepoName()).thenReturn("RepoName");
106 when(repo.getAddress()).thenReturn("http://localhost:8080");
107 assertDoesNotThrow(() -> helmClient.addRepository(repo));
109 doReturn("failed").when(helmClient).executeCommand(any());
110 assertDoesNotThrow(() -> helmClient.addRepository(repo));
114 void test_findChartRepository() throws IOException, ServiceException {
115 String tmpPath = "target/tmp/dummyChart/1.0/";
116 doReturn("nginx-stable/nginx-ingress\t0.9.3\t1.11.3"
117 + " \tNGINX Ingress Controller").when(helmClient).executeCommand(any());
119 String configuredRepo = helmClient.findChartRepository(charts.get(1));
120 assertThat(configuredRepo).isEqualTo("nginx-stable");
122 File tmpFile = new File(tmpPath + charts.get(1).getChartId().getName());
123 if (!tmpFile.mkdirs()) {
124 fail("Couldn't create dirs");
127 doReturn(Path.of(tmpPath)).when(chartStore).getAppPath(charts.get(1).getChartId());
129 doReturn(null).when(helmClient).verifyConfiguredRepo(charts.get(1));
131 String localRepoName = helmClient.findChartRepository(charts.get(1));
132 assertNotNull(localRepoName);
133 assertThat(localRepoName).endsWith(charts.get(0).getChartId().getVersion());
137 void test_uninstallChart() throws ServiceException {
138 doReturn("success").when(helmClient).executeCommand(any());
139 helmClient.uninstallChart(charts.get(0));
140 doThrow(ServiceException.class).when(helmClient).executeCommand(any());
142 assertThatThrownBy(() -> helmClient.uninstallChart(charts.get(0)))
143 .isInstanceOf(ServiceException.class);
147 void test_verifyConfiguredRepoForInvalidChart() throws IOException, ServiceException {
148 doReturn("").when(helmClient).executeCommand(any());
149 String configuredRepo = helmClient.verifyConfiguredRepo(charts.get(1));
150 assertNull(configuredRepo);