From 8e3ead50a4151a18d83cf1c78f232efafa306c39 Mon Sep 17 00:00:00 2001 From: Jeromy Cannon Date: Tue, 17 Oct 2023 08:57:16 -0500 Subject: [PATCH] feat: add gradle Helm dependency update task (#393) Signed-off-by: Jeromy Cannon --- fullstack-examples/build.gradle.kts | 6 ++ .../plugin/HelmDependencyUpdateTask.java | 63 ++++++++++++++++ .../plugin/HelmDependencyUpdateTaskTest.java | 74 +++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmDependencyUpdateTask.java create mode 100644 fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmDependencyUpdateTaskTest.java diff --git a/fullstack-examples/build.gradle.kts b/fullstack-examples/build.gradle.kts index 8149a3c2c..a8e39fadc 100644 --- a/fullstack-examples/build.gradle.kts +++ b/fullstack-examples/build.gradle.kts @@ -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 @@ -60,6 +61,10 @@ tasks.register("helmNginxExists") { release.set("nginx-release") } +tasks.register("helmDependencyUpdate") { + chartName.set("../charts/hedera-network") +} + tasks.register("helmTestNginxChart") { namespace.set("nginx-ns") release.set("nginx-release") @@ -76,5 +81,6 @@ tasks.check { dependsOn("helmNginxExists") dependsOn("helmTestNginxChart") dependsOn("helmUninstallNginxChart") + dependsOn("helmDependencyUpdate") dependsOn("helmUninstallNotAChart") } diff --git a/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmDependencyUpdateTask.java b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmDependencyUpdateTask.java new file mode 100644 index 000000000..38105ee47 --- /dev/null +++ b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmDependencyUpdateTask.java @@ -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 getChartName(); + + @Input + @Optional + @Option(option = "workingDirectory", description = "The working directory to run the dependency update from") + public abstract Property 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; + } + } +} diff --git a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmDependencyUpdateTaskTest.java b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmDependencyUpdateTaskTest.java new file mode 100644 index 000000000..668c87cc5 --- /dev/null +++ b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmDependencyUpdateTaskTest.java @@ -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"); + } +}