diff --git a/fullstack-examples/build.gradle.kts b/fullstack-examples/build.gradle.kts index 520a32d60..8149a3c2c 100644 --- a/fullstack-examples/build.gradle.kts +++ b/fullstack-examples/build.gradle.kts @@ -16,6 +16,7 @@ import com.hedera.fullstack.gradle.plugin.HelmInstallChartTask import com.hedera.fullstack.gradle.plugin.HelmReleaseExistsTask +import com.hedera.fullstack.gradle.plugin.HelmTestChartTask import com.hedera.fullstack.gradle.plugin.HelmUninstallChartTask plugins { @@ -46,7 +47,6 @@ tasks.register("helmInstallNginxChart") { namespace.set("nginx-ns") release.set("nginx-release") chart.set("oci://ghcr.io/nginxinc/charts/nginx-ingress") - skipIfExists.set(true) } tasks.register("helmUninstallNginxChart") { @@ -60,6 +60,11 @@ tasks.register("helmNginxExists") { release.set("nginx-release") } +tasks.register("helmTestNginxChart") { + namespace.set("nginx-ns") + release.set("nginx-release") +} + // This task will succeed because it only uninstalls if the release exists tasks.register("helmUninstallNotAChart") { release.set("not-a-release") @@ -69,6 +74,7 @@ tasks.register("helmUninstallNotAChart") { tasks.check { dependsOn("helmInstallNginxChart") dependsOn("helmNginxExists") + dependsOn("helmTestNginxChart") dependsOn("helmUninstallNginxChart") dependsOn("helmUninstallNotAChart") } diff --git a/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmTestChartTask.java b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmTestChartTask.java new file mode 100644 index 000000000..512b93b27 --- /dev/null +++ b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmTestChartTask.java @@ -0,0 +1,75 @@ +/* + * 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 com.hedera.fullstack.helm.client.model.test.TestChartOptionsBuilder; +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 HelmTestChartTask extends DefaultTask { + @Input + @Optional + @Option(option = "namespace", description = "The namespace to use when installing the chart") + public abstract Property getNamespace(); + + @Input + @Optional + @Option(option = "filter", description = "The filter to use when choosing the chart to test") + public abstract Property getFilter(); + + @Input + @Optional + @Option(option = "timeout", description = "The timeout to use when testing the chart") + public abstract Property getTestTimeout(); + + @Input + @Option(option = "release", description = "The name of the release to install") + public abstract Property getRelease(); + + @TaskAction + void testChart() { + HelmClientBuilder helmClientBuilder = HelmClient.builder(); + if (getNamespace().isPresent()) { + helmClientBuilder.defaultNamespace(getNamespace().get()); + } + HelmClient helmClient = helmClientBuilder.build(); + TestChartOptionsBuilder optionsBuilder = TestChartOptionsBuilder.builder(); + if (getFilter().isPresent()) { + optionsBuilder.filter(getFilter().get()); + } + if (getTestTimeout().isPresent()) { + optionsBuilder.timeout(getTestTimeout().get()); + } + try { + final String releaseName = getRelease().getOrNull(); + Objects.requireNonNull(releaseName, "release name must be specified"); + helmClient.testChart(releaseName, optionsBuilder.build()); + } catch (Exception e) { + this.getProject() + .getLogger() + .error("HelmTestChartTask.testChart() An ERROR occurred while testing the chart: ", e); + throw e; + } + } +} diff --git a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java index 22688caa1..f5112c6c9 100644 --- a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java +++ b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java @@ -110,6 +110,14 @@ void testHelmInstallChartTaskSimple() { task.getRelease().set(RELEASE_NAME); }); helmReleaseExistsTask.releaseExists(); + HelmTestChartTask helmTestChartTask = project.getTasks() + .create("helmTestChartTask", HelmTestChartTask.class, task -> { + task.getNamespace().set(namespace); + task.getRelease().set(RELEASE_NAME); + task.getFilter().set("test"); + task.getTestTimeout().set("15m"); + }); + helmTestChartTask.testChart(); HelmUninstallChartTask helmUninstallChartTask = project.getTasks() .create("helmUninstallChart", HelmUninstallChartTask.class, task -> { task.getNamespace().set(namespace); diff --git a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmTestChartTaskTest.java b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmTestChartTaskTest.java new file mode 100644 index 000000000..e25bfbd33 --- /dev/null +++ b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmTestChartTaskTest.java @@ -0,0 +1,46 @@ +/* + * 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.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +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; + +class HelmTestChartTaskTest { + private static Project project; + + @BeforeAll + static void beforeAll() { + project = ProjectBuilder.builder().build(); + } + + @Test + @DisplayName("test an error is thrown when the release is not found") + void testErrorThrownWhenReleaseNotFound() { + NullPointerException e = assertThrows(NullPointerException.class, () -> { + HelmTestChartTask helmTestChartTask = project.getTasks() + .create("helmTestChartTaskNonExistingRelease", HelmTestChartTask.class, task -> {}); + helmTestChartTask.testChart(); + }); + assertThat(e.getMessage()).isEqualTo("release name must be specified"); + } +}