Skip to content

Commit

Permalink
feat: add gradle Helm dependency update task (#393)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
  • Loading branch information
jeromy-cannon authored Oct 17, 2023
1 parent 591cebb commit 8e3ead5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fullstack-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import com.hedera.fullstack.gradle.plugin.HelmDependencyUpdateTask
import com.hedera.fullstack.gradle.plugin.HelmInstallChartTask
import com.hedera.fullstack.gradle.plugin.HelmReleaseExistsTask
import com.hedera.fullstack.gradle.plugin.HelmTestChartTask
Expand Down Expand Up @@ -60,6 +61,10 @@ tasks.register<HelmReleaseExistsTask>("helmNginxExists") {
release.set("nginx-release")
}

tasks.register<HelmDependencyUpdateTask>("helmDependencyUpdate") {
chartName.set("../charts/hedera-network")
}

tasks.register<HelmTestChartTask>("helmTestNginxChart") {
namespace.set("nginx-ns")
release.set("nginx-release")
Expand All @@ -76,5 +81,6 @@ tasks.check {
dependsOn("helmNginxExists")
dependsOn("helmTestNginxChart")
dependsOn("helmUninstallNginxChart")
dependsOn("helmDependencyUpdate")
dependsOn("helmUninstallNotAChart")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.fullstack.gradle.plugin;

import com.hedera.fullstack.helm.client.HelmClient;
import com.hedera.fullstack.helm.client.HelmClientBuilder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import org.gradle.api.DefaultTask;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;

public abstract class HelmDependencyUpdateTask extends DefaultTask {
@Input
@Option(option = "chartName", description = "The name of the chart to run the dependency update against")
public abstract Property<String> getChartName();

@Input
@Optional
@Option(option = "workingDirectory", description = "The working directory to run the dependency update from")
public abstract Property<String> getWorkingDirectory();

@TaskAction
void dependencyUpdate() {
HelmClientBuilder helmClientBuilder = HelmClient.builder();
try {
final String chartName = getChartName().getOrNull();
Objects.requireNonNull(chartName, "chartName must be set");
final String workingDirectory = getWorkingDirectory().getOrNull();
if (workingDirectory != null && !workingDirectory.isEmpty() && !workingDirectory.isBlank()) {
Path workingDirectoryPath = Paths.get(workingDirectory);
helmClientBuilder.workingDirectory(workingDirectoryPath);
}
HelmClient helmClient = helmClientBuilder.build();
helmClient.dependencyUpdate(chartName);
} catch (Exception e) {
this.getProject()
.getLogger()
.error(
"HelmDependencyUpdateTask.dependencyUpdate() An ERROR occurred while running the dependency update: ",
e);
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.fullstack.gradle.plugin;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.hedera.fullstack.helm.client.HelmConfigurationException;
import java.io.File;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class HelmDependencyUpdateTaskTest {
private static final String CHART = "../charts/hedera-network";
private static Project project;

@BeforeAll
static void beforeAll() {
project = ProjectBuilder.builder().build();
}

@Test
@DisplayName("Test Helm Dependency Update Task")
void testDependencyUpdate() {
HelmDependencyUpdateTask helmDependencyUpdateTask = project.getTasks()
.create("helmDependencyUpdateTask", HelmDependencyUpdateTask.class, task -> {
task.getChartName().set(CHART);
task.getWorkingDirectory().set(new File(".").toPath().toString());
});
assertThat(helmDependencyUpdateTask.getChartName().get()).isEqualTo(CHART);
helmDependencyUpdateTask.dependencyUpdate();
}

@Test
@DisplayName("Test Helm Dependency Update Task with no chart name")
void testDependencyUpdateWithNoChartName() {
HelmDependencyUpdateTask helmDependencyUpdateTask = project.getTasks()
.create("helmDependencyUpdateNoChartTask", HelmDependencyUpdateTask.class, task -> {});
assertThat(helmDependencyUpdateTask.getChartName().getOrNull()).isNull();
NullPointerException e = assertThrows(NullPointerException.class, helmDependencyUpdateTask::dependencyUpdate);
assertThat(e.getMessage()).contains("chartName must be set");
}

@Test
@DisplayName("Test Helm Dependency Update Task with no bad working directory")
void testDependencyUpdateWithBadWorkingDirectory() {
HelmDependencyUpdateTask helmDependencyUpdateTask = project.getTasks()
.create("helmDependencyUpdateBadWorkingDirectoryTask", HelmDependencyUpdateTask.class, task -> {
task.getChartName().set(CHART);
task.getWorkingDirectory().set("xyz");
});
assertThat(helmDependencyUpdateTask.getChartName().get()).isEqualTo(CHART);
HelmConfigurationException e =
assertThrows(HelmConfigurationException.class, helmDependencyUpdateTask::dependencyUpdate);
assertThat(e.getMessage()).contains("No such file or directory");
}
}

0 comments on commit 8e3ead5

Please sign in to comment.