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 : Helm test functionality exposed via mojos and gradle tasks #3408

Merged
merged 1 commit into from
Oct 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.jkube.gradle.plugin.task.KubernetesHelmLintTask;
import org.eclipse.jkube.gradle.plugin.task.KubernetesHelmPushTask;
import org.eclipse.jkube.gradle.plugin.task.KubernetesHelmTask;
import org.eclipse.jkube.gradle.plugin.task.KubernetesHelmTestTask;
import org.eclipse.jkube.gradle.plugin.task.KubernetesHelmUninstallTask;
import org.eclipse.jkube.gradle.plugin.task.KubernetesLogTask;
import org.eclipse.jkube.gradle.plugin.task.KubernetesPushTask;
Expand Down Expand Up @@ -57,6 +58,7 @@ public Map<String, Collection<Class<? extends Task>>> getTaskPrecedence() {
ret.put("k8sHelmDependencyUpdate", Collections.singletonList(KubernetesHelmTask.class));
ret.put("k8sHelmInstall", Collections.singletonList(KubernetesHelmTask.class));
ret.put("k8sHelmUninstall", Arrays.asList(KubernetesHelmTask.class, KubernetesHelmInstallTask.class));
ret.put("k8sHelmTest", Arrays.asList(KubernetesHelmTask.class, KubernetesHelmInstallTask.class));
return ret;
}

Expand All @@ -76,6 +78,7 @@ protected void jKubeApply(Project project) {
register(project, "k8sHelmDependencyUpdate", KubernetesHelmDependencyUpdateTask.class);
register(project, "k8sHelmInstall", KubernetesHelmInstallTask.class);
register(project, "k8sHelmUninstall", KubernetesHelmUninstallTask.class);
register(project, "k8sHelmTest", KubernetesHelmTestTask.class);
register(project, "k8sRemoteDev", KubernetesRemoteDevTask.class);
register(project, "k8sWatch", KubernetesWatchTask.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.gradle.plugin.task;

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;

import javax.inject.Inject;

public class KubernetesHelmTestTask extends AbstractHelmTask {
@Inject
public KubernetesHelmTestTask(Class<? extends KubernetesExtension> extensionClass) {
super(extensionClass);
setDescription("Runs the tests for a release.");
}

@Override
public void run() {
try {
jKubeServiceHub.getHelmService().test(kubernetesExtension.helm);
} catch (Exception exp) {
kitLogger.error("Error performing helm test", exp);
throw new IllegalStateException(exp.getMessage(), exp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void getTaskPrecedence_withValidProject_shouldReturnTaskPrecedence() {
final Map<String, Collection<Class<? extends Task>>> result = new KubernetesPlugin().getTaskPrecedence();
// Then
assertThat(result)
.hasSize(9)
.hasSize(10)
.containsEntry("k8sApply", Collections.singletonList(KubernetesResourceTask.class))
.containsEntry("k8sDebug",
Arrays.asList(KubernetesBuildTask.class, KubernetesResourceTask.class, KubernetesApplyTask.class))
Expand All @@ -92,6 +92,7 @@ void getTaskPrecedence_withValidProject_shouldReturnTaskPrecedence() {
.containsEntry("k8sHelmPush", Collections.singletonList(KubernetesHelmTask.class))
.containsEntry("k8sHelmLint", Collections.singletonList(KubernetesHelmTask.class))
.containsEntry("k8sHelmInstall", Collections.singletonList(KubernetesHelmTask.class))
.containsEntry("k8sHelmUninstall", Arrays.asList(KubernetesHelmTask.class, KubernetesHelmInstallTask.class));
.containsEntry("k8sHelmUninstall", Arrays.asList(KubernetesHelmTask.class, KubernetesHelmInstallTask.class))
.containsEntry("k8sHelmTest", Arrays.asList(KubernetesHelmTask.class, KubernetesHelmInstallTask.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.gradle.plugin.task;

import com.marcnuri.helm.Helm;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.gradle.plugin.TestKubernetesExtension;
import org.eclipse.jkube.kit.common.access.ClusterConfiguration;
import org.eclipse.jkube.kit.common.util.AsyncUtil;
import org.eclipse.jkube.kit.resource.helm.HelmConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.eclipse.jkube.kit.common.util.KubernetesMockServerUtil.prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@EnableKubernetesMockClient(crud = true)
class KubernetesHelmTestTaskTest {
@RegisterExtension
private final TaskEnvironmentExtension taskEnvironment = new TaskEnvironmentExtension();
private KubernetesClient kubernetesClient;
private KubernetesMockServer server;
private TestKubernetesExtension extension;

@BeforeEach
void setUp() throws IOException {
extension = new TestKubernetesExtension();
// Remove after https://github.com/fabric8io/kubernetes-client/issues/6062 is fixed
prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints(server);
Helm.create().withDir(taskEnvironment.getRoot().toPath()).withName("empty-project").call();
Path helmChartOutputDir = taskEnvironment.getRoot().toPath().resolve("build").resolve("jkube").resolve("helm");
Files.createDirectories(helmChartOutputDir.resolve("kubernetes"));
FileUtils.copyDirectory(taskEnvironment.getRoot().toPath().resolve("empty-project").toFile(), helmChartOutputDir.resolve("kubernetes").toFile());
System.setProperty("jkube.kubernetesTemplate", taskEnvironment.getRoot().getAbsolutePath());
extension.helm = HelmConfig.builder()
.disableOpenAPIValidation(true)
.outputDir(helmChartOutputDir.toString()).build();
extension.access = ClusterConfiguration.from(kubernetesClient.getConfiguration()).build();
extension.isUseColor = false;
when(taskEnvironment.project.getName()).thenReturn("empty-project");
when(taskEnvironment.project.getVersion()).thenReturn("0.1.0");
when(taskEnvironment.project.getExtensions().getByType(KubernetesExtension.class)).thenReturn(extension);
}

@AfterEach
void tearDown() {
System.clearProperty("jkube.kubernetesTemplate");
}

@Test
@DisplayName("when Helm Release Installed on Kubernetes Cluster, then run tests on Helm Release")
void runTask_withHelmReleasePresentInKubernetesCluster_shouldSucceed() throws IOException {
// Given
// OpenAPI validation endpoints required by helm test
server.expect().get().withPath("/openapi/v3?timeout=32s")
.andReturn(200, IOUtils.toString(Objects.requireNonNull(KubernetesHelmTestTaskTest.class.getResourceAsStream("/helm-test-task/kubernetes-openapi-v3-schema.json")), StandardCharsets.UTF_8))
.always();
server.expect().get().withPath("/openapi/v3/api/v1?timeout=32s")
.andReturn(200, IOUtils.toString(Objects.requireNonNull(KubernetesHelmTestTaskTest.class.getResourceAsStream("/helm-test-task/kubernetes-openapi-v3-api-v1-schema-pod.json")), StandardCharsets.UTF_8))
.always();
KubernetesHelmTestTask kubernetesHelmTestTask = new KubernetesHelmTestTask(KubernetesExtension.class);
kubernetesHelmTestTask.init();
kubernetesHelmTestTask.jKubeServiceHub.getHelmService().install(extension.helm);
// Should be removed once https://github.com/fabric8io/kubernetes-client/issues/6220 gets fixed
Secret secret = kubernetesClient.secrets().withName("sh.helm.release.v1.empty-project.v1").get();
server.expect().get().withPath("/api/v1/namespaces/test/secrets?labelSelector=name%3Dempty-project%2Cowner%3Dhelm")
.andReturn(200, new SecretListBuilder()
.addToItems(secret)
.build())
.once();
// When
CompletableFuture<Boolean> helmTestTask = AsyncUtil.async(() -> {
kubernetesHelmTestTask.runTask();
return true;
});

kubernetesClient.pods().withName("empty-project-test-connection")
.waitUntilCondition(Objects::nonNull, 5, TimeUnit.SECONDS);
kubernetesClient.pods().withName("empty-project-test-connection").editStatus(p -> new PodBuilder(p)
.editOrNewStatus()
.withPhase("Succeeded")
.endStatus()
.build());
// Then
assertThat(helmTestTask).succeedsWithin(5, TimeUnit.SECONDS);
verify(taskEnvironment.logger, times(1)).lifecycle("k8s: Testing Helm Chart empty-project 0.1.0");
verify(taskEnvironment.logger, times(2)).lifecycle("k8s: NAME: empty-project");
verify(taskEnvironment.logger, times(2)).lifecycle("k8s: STATUS: deployed");
verify(taskEnvironment.logger, times(2)).lifecycle("k8s: REVISION: 1");
verify(taskEnvironment.logger, times(2)).lifecycle("k8s: Phase: Succeeded");
}

@Test
@DisplayName("Helm Release not installed on Kubernetes cluster, then throw exception")
void execute_whenReleaseNotPresent_thenThrowException() {
// Given
KubernetesHelmTestTask kubernetesHelmTestTask = new KubernetesHelmTestTask(KubernetesExtension.class);

// When + Then
assertThatIllegalStateException()
.isThrownBy(kubernetesHelmTestTask::runTask)
.withMessageContaining(" not found");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"swagger": "2.0",
"info": {
"title": "Kubernetes",
"version": "v1.28.0"
},
"paths": {
"/api/v1/namespaces/{namespace}/pods/{name}": {
"patch": {
"parameters": [
{
"uniqueItems": true,
"type": "string",
"name": "fieldValidation",
"in": "query"
}
],
"x-kubernetes-group-version-kind": {
"group": "",
"kind": "Pod",
"version": "v1"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"paths": {
"api/v1": {
"serverRelativeURL": "/openapi/v3/api/v1"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.jkube.gradle.plugin.task.OpenShiftHelmLintTask;
import org.eclipse.jkube.gradle.plugin.task.OpenShiftHelmPushTask;
import org.eclipse.jkube.gradle.plugin.task.OpenShiftHelmTask;
import org.eclipse.jkube.gradle.plugin.task.OpenShiftHelmTestTask;
import org.eclipse.jkube.gradle.plugin.task.OpenShiftHelmUninstallTask;
import org.eclipse.jkube.gradle.plugin.task.OpenShiftPushTask;
import org.eclipse.jkube.gradle.plugin.task.OpenShiftRemoteDevTask;
Expand Down Expand Up @@ -62,6 +63,7 @@ public Map<String, Collection<Class<? extends Task>>> getTaskPrecedence() {
ret.put("ocHelmDependencyUpdate", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class));
ret.put("ocHelmInstall", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class));
ret.put("ocHelmUninstall", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class, KubernetesHelmInstallTask.class, OpenShiftHelmInstallTask.class));
ret.put("ocHelmTest", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class, KubernetesHelmInstallTask.class, OpenShiftHelmInstallTask.class));
return ret;
}

Expand All @@ -81,6 +83,7 @@ protected void jKubeApply(Project project) {
register(project, "ocHelmDependencyUpdate", OpenShiftHelmDependencyUpdateTask.class);
register(project, "ocHelmInstall", OpenShiftHelmInstallTask.class);
register(project, "ocHelmUninstall", OpenShiftHelmUninstallTask.class);
register(project, "ocHelmTest", OpenShiftHelmTestTask.class);
register(project, "ocRemoteDev", OpenShiftRemoteDevTask.class);
register(project, "ocWatch", OpenShiftWatchTask.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.gradle.plugin.task;

import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;

import javax.inject.Inject;

public class OpenShiftHelmTestTask extends KubernetesHelmTestTask implements OpenShiftJKubeTask {
@Inject
public OpenShiftHelmTestTask(Class<? extends OpenShiftExtension> extensionClass) {
super(extensionClass);
setDescription("Runs the tests for a release.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void configurePrecedence_withValidProject_shouldReturnTaskPrecedence() {
final Map<String, Collection<Class<? extends Task>>> result = new OpenShiftPlugin().getTaskPrecedence();
// Then
assertThat(result)
.hasSize(9)
.hasSize(10)
.containsEntry("ocApply", Arrays.asList(KubernetesResourceTask.class, OpenShiftResourceTask.class))
.containsEntry("ocDebug", Arrays.asList(KubernetesBuildTask.class, OpenShiftBuildTask.class,
KubernetesResourceTask.class, OpenShiftResourceTask.class, KubernetesApplyTask.class, OpenShiftApplyTask.class))
Expand All @@ -51,6 +51,7 @@ void configurePrecedence_withValidProject_shouldReturnTaskPrecedence() {
.containsEntry("ocHelmPush", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class))
.containsEntry("ocHelmLint", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class))
.containsEntry("ocHelmInstall", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class))
.containsEntry("ocHelmUninstall", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class, KubernetesHelmInstallTask.class, OpenShiftHelmInstallTask.class));
.containsEntry("ocHelmUninstall", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class, KubernetesHelmInstallTask.class, OpenShiftHelmInstallTask.class))
.containsEntry("ocHelmTest", Arrays.asList(KubernetesHelmTask.class, OpenShiftHelmTask.class, KubernetesHelmInstallTask.class, OpenShiftHelmInstallTask.class));
}
}
Loading