Skip to content

Commit

Permalink
HelmUninstallChartTask code/examples/test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Jeromy Cannon <[email protected]>
  • Loading branch information
jeromy-cannon committed Oct 2, 2023
1 parent 6240d28 commit c7aaf6c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
8 changes: 6 additions & 2 deletions fullstack-examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import com.hedera.fullstack.gradle.plugin.HelmInstallChartTask
import com.hedera.fullstack.gradle.plugin.HelmUninstallChartTask

plugins {
id("java")
Expand Down Expand Up @@ -46,9 +47,12 @@ tasks.register<HelmInstallChartTask>("helmInstallNginxChart") {
chart.set("oci://ghcr.io/nginxinc/charts/nginx-ingress")
}

// TODO: task register helmUninstallNginxChart
tasks.register<HelmUninstallChartTask>("helmUninstallNginxChart") {
namespace.set("nginx-ns")
release.set("nginx-release")
}

tasks.check {
dependsOn("helmInstallNginxChart")
// TODO: depends on helmUninstallNginxChart
dependsOn("helmUninstallNginxChart")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 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 HelmUninstallChartTask extends DefaultTask {
@Input
@Optional
@Option(option = "namespace", description = "The namespace to use when installing the chart")
public abstract Property<String> getNamespace();

@Input
@Option(option = "release", description = "The name of the release to install")
public abstract Property<String> getRelease();

@TaskAction
void uninstallChart() {
HelmClientBuilder helmClientBuilder = HelmClient.builder();
if (getNamespace().isPresent()) {
helmClientBuilder.defaultNamespace(getNamespace().get());
}
HelmClient helmClient = helmClientBuilder.build();
try {
helmClient.uninstallChart(getRelease().getOrNull());
} catch (Exception e) {
this.getProject()
.getLogger()
.error(
"HelmUninstallChartTask.uninstallChart() An ERROR occurred while uninstalling the chart: ",
e);
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ void testHelmInstallChartTaskSimple() {
});
assertEquals(RELEASE_NAME, helmInstallChartTask.getRelease().get());
helmInstallChartTask.installChart();
HelmUninstallChartTask helmUninstallChartTask = project.getTasks()
.create("helmUninstallChart", HelmUninstallChartTask.class, task -> {
task.getNamespace().set("simple-test");
task.getRelease().set(RELEASE_NAME);
});
helmUninstallChartTask.uninstallChart();
} finally {
suppressExceptions(() -> helmClient.uninstallChart(RELEASE_NAME));
suppressExceptions(() -> helmClient.removeRepository(REPOSITORY));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.junit.jupiter.api.Assertions.assertThrows;

import com.hedera.fullstack.helm.client.HelmExecutionException;
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 HelmUninstallChartTaskTest {
private static Project project;

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

@Test
@DisplayName("test an error is thrown when the chart is not found")
void testErrorThrownWhenChartNotFound() {
assertThrows(HelmExecutionException.class, () -> {
HelmUninstallChartTask helmUninstallChartTask = project.getTasks()
.create("helmUninstallNonExistingChartChart", HelmUninstallChartTask.class, task -> {
task.getNamespace().set("test-failure");
task.getRelease().set("not-a-release");
});
helmUninstallChartTask.uninstallChart();
});
}
}

0 comments on commit c7aaf6c

Please sign in to comment.