Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gradle Helm dependency update task #393

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to put "../charts/hedera-network" in a variable that can be used across the modules and its build.gradle.kts file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

putting it in a variable is easy enough. However, this is the only place in gradle that it is used currently. It sounds like with your full cluster chart that this may not even get used with the gradle build script. We also have another PR that is about to merge that makes both fullstack-examples and fullstack-gradle-plugin separate projects.

}

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");
}
}