Skip to content

Improvements to the junit extension #891

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

Merged
merged 1 commit into from
Feb 4, 2022
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
83 changes: 0 additions & 83 deletions .github/workflows/e2e-test-mysql.yml

This file was deleted.

79 changes: 0 additions & 79 deletions .github/workflows/e2e-test-tomcat.yml

This file was deleted.

50 changes: 50 additions & 0 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Integration and end to end tests which runs locally and deploys the Operator to a Kubernetes
# (Minikube) cluster and creates custom resources to verify the operator's functionality
name: Integration & End to End tests
on:
pull_request:
branches: [ main ]
push:
branches:
- main

jobs:
sample_operators_tests:
strategy:
matrix:
sample_dir:
- "sample-operators/mysql-schema"
- "sample-operators/tomcat-operator"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Minikube-Kubernetes
uses: manusa/[email protected]
with:
minikube version: v1.24.0
kubernetes version: v1.23.0
github token: ${{ secrets.GITHUB_TOKEN }}
driver: docker

- name: Set up Java and Maven
uses: actions/setup-java@v2
with:
java-version: 17
distribution: temurin
cache: 'maven'

- name: Build SDK
run: mvn install -DskipTests

- name: Run integration tests in local mode
working-directory: ${{ matrix.sample_dir }}
run: |
mvn test -P end-to-end-tests

- name: Run E2E tests as a deployment
working-directory: ${{ matrix.sample_dir }}
run: |
eval $(minikube -p minikube docker-env)
mvn jib:dockerBuild test -P end-to-end-tests -Dtest.deployment=remote
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package io.javaoperatorsdk.operator.junit;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

import org.junit.jupiter.api.extension.*;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
import io.fabric8.kubernetes.client.utils.Utils;
import io.javaoperatorsdk.operator.api.config.BaseConfigurationService;
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
import io.javaoperatorsdk.operator.api.config.Version;

public abstract class AbstractOperatorExtension implements HasKubernetesClient,
BeforeAllCallback,
BeforeEachCallback,
AfterAllCallback,
AfterEachCallback {

protected final KubernetesClient kubernetesClient;
protected final ConfigurationService configurationService;
protected final List<HasMetadata> infrastructure;
protected Duration infrastructureTimeout;
protected final boolean oneNamespacePerClass;
protected final boolean preserveNamespaceOnError;
protected final boolean waitForNamespaceDeletion;

protected String namespace;

protected AbstractOperatorExtension(
ConfigurationService configurationService,
List<HasMetadata> infrastructure,
Duration infrastructureTimeout,
boolean oneNamespacePerClass,
boolean preserveNamespaceOnError,
boolean waitForNamespaceDeletion) {

this.kubernetesClient = new DefaultKubernetesClient();
this.configurationService = configurationService;
this.infrastructure = infrastructure;
this.infrastructureTimeout = infrastructureTimeout;
this.oneNamespacePerClass = oneNamespacePerClass;
this.preserveNamespaceOnError = preserveNamespaceOnError;
this.waitForNamespaceDeletion = waitForNamespaceDeletion;
}


@Override
public void beforeAll(ExtensionContext context) throws Exception {
beforeAllImpl(context);
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
beforeEachImpl(context);
}

@Override
public void afterAll(ExtensionContext context) throws Exception {
afterAllImpl(context);
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
afterEachImpl(context);
}

@Override
public KubernetesClient getKubernetesClient() {
return kubernetesClient;
}

public String getNamespace() {
return namespace;
}

public <T extends HasMetadata> NonNamespaceOperation<T, KubernetesResourceList<T>, Resource<T>> resources(
Class<T> type) {
return kubernetesClient.resources(type).inNamespace(namespace);
}

public <T extends HasMetadata> T get(Class<T> type, String name) {
return kubernetesClient.resources(type).inNamespace(namespace).withName(name).get();
}

public <T extends HasMetadata> T create(Class<T> type, T resource) {
return kubernetesClient.resources(type).inNamespace(namespace).create(resource);
}

public <T extends HasMetadata> T replace(Class<T> type, T resource) {
return kubernetesClient.resources(type).inNamespace(namespace).replace(resource);
}

public <T extends HasMetadata> boolean delete(Class<T> type, T resource) {
return kubernetesClient.resources(type).inNamespace(namespace).delete(resource);
}

protected void beforeAllImpl(ExtensionContext context) {
if (oneNamespacePerClass) {
namespace = context.getRequiredTestClass().getSimpleName();
namespace += "-";
namespace += UUID.randomUUID();
namespace = KubernetesResourceUtil.sanitizeName(namespace).toLowerCase(Locale.US);
namespace = namespace.substring(0, Math.min(namespace.length(), 63));

before(context);
}
}

protected void beforeEachImpl(ExtensionContext context) {
if (!oneNamespacePerClass) {
namespace = context.getRequiredTestClass().getSimpleName();
namespace += "-";
namespace += context.getRequiredTestMethod().getName();
namespace += "-";
namespace += UUID.randomUUID();
namespace = KubernetesResourceUtil.sanitizeName(namespace).toLowerCase(Locale.US);
namespace = namespace.substring(0, Math.min(namespace.length(), 63));

before(context);
}
}

protected abstract void before(ExtensionContext context);

protected void afterAllImpl(ExtensionContext context) {
if (oneNamespacePerClass) {
after(context);
}
}

protected void afterEachImpl(ExtensionContext context) {
if (!oneNamespacePerClass) {
after(context);
}
}

protected abstract void after(ExtensionContext context);

public static abstract class AbstractBuilder {
protected ConfigurationService configurationService;
protected final List<HasMetadata> infrastructure;
protected Duration infrastructureTimeout;
protected boolean preserveNamespaceOnError;
protected boolean waitForNamespaceDeletion;
protected boolean oneNamespacePerClass;

protected AbstractBuilder() {
this.configurationService = new BaseConfigurationService(Version.UNKNOWN);

this.infrastructure = new ArrayList<>();
this.infrastructureTimeout = Duration.ofMinutes(1);

this.preserveNamespaceOnError = Utils.getSystemPropertyOrEnvVar(
"josdk.it.preserveNamespaceOnError",
false);

this.waitForNamespaceDeletion = Utils.getSystemPropertyOrEnvVar(
"josdk.it.waitForNamespaceDeletion",
true);

this.oneNamespacePerClass = Utils.getSystemPropertyOrEnvVar(
"josdk.it.oneNamespacePerClass",
false);
}
}
}
Loading