Skip to content

Commit

Permalink
test(openshift-gradle-plugin): updated helm tests to use kube-api-test
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Jan 14, 2025
1 parent 66b7473 commit 339a9af
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 152 deletions.
27 changes: 27 additions & 0 deletions gradle-plugin/openshift/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
<groupId>io.fabric8</groupId>
<artifactId>openshift-server-mock</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kube-api-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -105,4 +109,27 @@
It's unrealistic to add a dependency for each of the gradle subprojects
-->
</dependencies>

<profiles>
<profile>
<id>java-8</id>
<activation>
<jdk>1.8</jdk>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration combine.self="override">
<testExcludes>
<testExclude>**/OpenShiftHelmInstallTaskTest.java</testExclude>
<testExclude>**/OpenShiftHelmTestTaskTest.java</testExclude>
<testExclude>**/OpenShiftHelmUninstallTaskTest.java</testExclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
package org.eclipse.jkube.gradle.plugin.task;

import com.marcnuri.helm.Helm;
import io.fabric8.kubeapitest.junit.EnableKubeAPIServer;
import io.fabric8.kubeapitest.junit.KubeConfig;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import org.apache.commons.io.FileUtils;
import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;
import org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension;
Expand All @@ -32,25 +34,29 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.TimeUnit;

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

@EnableKubernetesMockClient(crud = true)
@EnableKubeAPIServer
class OpenShiftHelmInstallTaskTest {
@RegisterExtension
private final TaskEnvironmentExtension taskEnvironment = new TaskEnvironmentExtension();
@KubeConfig
static String kubeConfigYaml;
private KubernetesClient kubernetesClient;
private KubernetesMockServer server;
private TestOpenShiftExtension extension;

@BeforeEach
void setUp() throws IOException {
extension = new TestOpenShiftExtension();
// Remove after https://github.com/fabric8io/kubernetes-client/issues/6062 is fixed
prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints(server);
kubernetesClient = new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(kubeConfigYaml)).build();
kubernetesClient.apps().deployments().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.pods().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.configMaps().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.secrets().withTimeout(1, TimeUnit.SECONDS).delete();
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("openshift"));
Expand All @@ -76,6 +82,7 @@ void setUp() throws IOException {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
package org.eclipse.jkube.gradle.plugin.task;

import com.marcnuri.helm.Helm;
import io.fabric8.kubeapitest.junit.EnableKubeAPIServer;
import io.fabric8.kubeapitest.junit.KubeConfig;
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.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;
import org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension;
import org.eclipse.jkube.kit.common.access.ClusterConfiguration;
Expand All @@ -34,7 +33,6 @@
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;
Expand All @@ -43,24 +41,27 @@

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)
@EnableKubeAPIServer
class OpenShiftHelmTestTaskTest {
@RegisterExtension
private final TaskEnvironmentExtension taskEnvironment = new TaskEnvironmentExtension();
@KubeConfig
static String kubeConfigYaml;
private KubernetesClient kubernetesClient;
private KubernetesMockServer server;
private TestOpenShiftExtension extension;

@BeforeEach
void setUp() throws IOException {
extension = new TestOpenShiftExtension();
// Remove after https://github.com/fabric8io/kubernetes-client/issues/6062 is fixed
prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints(server);
kubernetesClient = new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(kubeConfigYaml)).build();
kubernetesClient.apps().deployments().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.pods().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.configMaps().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.secrets().withTimeout(1, TimeUnit.SECONDS).delete();
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("openshift"));
Expand All @@ -78,30 +79,17 @@ void setUp() throws IOException {

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

@Test
@DisplayName("when Helm Release Installed on OpenShift Cluster, then test Helm Release")
void runTask_withHelmReleasePresentInKubernetesCluster_shouldSucceed() throws IOException {
void runTask_withHelmReleasePresentInKubernetesCluster_shouldSucceed() {
// Given
// OpenAPI validation endpoints required by helm test
server.expect().get().withPath("/openapi/v3?timeout=32s")
.andReturn(200, IOUtils.toString(Objects.requireNonNull(OpenShiftHelmTestTaskTest.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(OpenShiftHelmTestTaskTest.class.getResourceAsStream("/helm-test-task/kubernetes-openapi-v3-api-v1-schema-pod.json")), StandardCharsets.UTF_8))
.always();
OpenShiftHelmTestTask openShiftHelmTestTask = new OpenShiftHelmTestTask(OpenShiftExtension.class);
openShiftHelmTestTask.init();
openShiftHelmTestTask.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> openShiftHelmTest = AsyncUtil.async(() -> {
openShiftHelmTestTask.runTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
package org.eclipse.jkube.gradle.plugin.task;

import com.marcnuri.helm.Helm;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretListBuilder;
import io.fabric8.kubeapitest.junit.EnableKubeAPIServer;
import io.fabric8.kubeapitest.junit.KubeConfig;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import org.apache.commons.io.FileUtils;
import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;
import org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension;
Expand All @@ -33,25 +33,29 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

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

@EnableKubernetesMockClient(crud = true)
@EnableKubeAPIServer
class OpenShiftHelmUninstallTaskTest {
@RegisterExtension
private final TaskEnvironmentExtension taskEnvironment = new TaskEnvironmentExtension();
@KubeConfig
static String kubeConfigYaml;
private KubernetesClient kubernetesClient;
private KubernetesMockServer server;
private TestOpenShiftExtension extension;

@BeforeEach
void setUp() throws IOException {
extension = new TestOpenShiftExtension();
// Remove after https://github.com/fabric8io/kubernetes-client/issues/6062 is fixed
prepareMockWebServerExpectationsForAggregatedDiscoveryEndpoints(server);
kubernetesClient = new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(kubeConfigYaml)).build();
kubernetesClient.apps().deployments().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.pods().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.configMaps().withTimeout(1, TimeUnit.SECONDS).delete();
kubernetesClient.secrets().withTimeout(1, TimeUnit.SECONDS).delete();
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("openshift"));
Expand All @@ -70,6 +74,7 @@ void setUp() throws IOException {

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

Expand All @@ -80,14 +85,6 @@ void runTask_withHelmReleasePresentInKubernetesCluster_shouldSucceed() {
OpenShiftHelmUninstallTask openShiftHelmUninstallTask = new OpenShiftHelmUninstallTask(OpenShiftExtension.class);
openShiftHelmUninstallTask.init();
openShiftHelmUninstallTask.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
openShiftHelmUninstallTask.runTask();
// Then
Expand Down

This file was deleted.

0 comments on commit 339a9af

Please sign in to comment.