Skip to content

Commit

Permalink
feat: add gradle Helm test task (#403)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeromy Cannon <[email protected]>
  • Loading branch information
jeromy-cannon authored Oct 17, 2023
1 parent 0726725 commit 591cebb
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
8 changes: 7 additions & 1 deletion fullstack-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -46,7 +47,6 @@ tasks.register<HelmInstallChartTask>("helmInstallNginxChart") {
namespace.set("nginx-ns")
release.set("nginx-release")
chart.set("oci://ghcr.io/nginxinc/charts/nginx-ingress")
skipIfExists.set(true)
}

tasks.register<HelmUninstallChartTask>("helmUninstallNginxChart") {
Expand All @@ -60,6 +60,11 @@ tasks.register<HelmReleaseExistsTask>("helmNginxExists") {
release.set("nginx-release")
}

tasks.register<HelmTestChartTask>("helmTestNginxChart") {
namespace.set("nginx-ns")
release.set("nginx-release")
}

// This task will succeed because it only uninstalls if the release exists
tasks.register<HelmUninstallChartTask>("helmUninstallNotAChart") {
release.set("not-a-release")
Expand All @@ -69,6 +74,7 @@ tasks.register<HelmUninstallChartTask>("helmUninstallNotAChart") {
tasks.check {
dependsOn("helmInstallNginxChart")
dependsOn("helmNginxExists")
dependsOn("helmTestNginxChart")
dependsOn("helmUninstallNginxChart")
dependsOn("helmUninstallNotAChart")
}
Original file line number Diff line number Diff line change
@@ -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<String> getNamespace();

@Input
@Optional
@Option(option = "filter", description = "The filter to use when choosing the chart to test")
public abstract Property<String> getFilter();

@Input
@Optional
@Option(option = "timeout", description = "The timeout to use when testing the chart")
public abstract Property<String> getTestTimeout();

@Input
@Option(option = "release", description = "The name of the release to install")
public abstract Property<String> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 591cebb

Please sign in to comment.