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

chore: Add final actions in test context #1231

Merged
merged 1 commit into from
Oct 10, 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 @@ -68,7 +68,7 @@ public class KnativeSettings {

private static final String AUTO_REMOVE_RESOURCES_PROPERTY = KNATIVE_PROPERTY_PREFIX + "auto.remove.resources";
private static final String AUTO_REMOVE_RESOURCES_ENV = KNATIVE_ENV_PREFIX + "AUTO_REMOVE_RESOURCES";
private static final String AUTO_REMOVE_RESOURCES_DEFAULT = "true";
private static final String AUTO_REMOVE_RESOURCES_DEFAULT = "false";

private static final String VERIFY_BROKER_RESPONSE_PROPERTY = KNATIVE_PROPERTY_PREFIX + "verify.broker.resources";
private static final String VERIFY_BROKER_RESPONSE_ENV = KNATIVE_ENV_PREFIX + "VERIFY_BROKER_RESPONSE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.citrusframework.AbstractTestActionBuilder;
import org.citrusframework.actions.AbstractTestAction;
import org.citrusframework.context.TestContext;
import org.citrusframework.knative.KnativeSettings;
import org.citrusframework.kubernetes.ClusterType;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.spi.ReferenceResolverAware;
Expand All @@ -38,13 +39,16 @@ public abstract class AbstractKnativeAction extends AbstractTestAction implement
private final ClusterType clusterType;
private final String namespace;

private final boolean autoRemoveResources;

public AbstractKnativeAction(String name, Builder<?, ?> builder) {
super("knative:" + name, builder);

this.knativeClient = builder.knativeClient;
this.kubernetesClient = builder.kubernetesClient;
this.namespace = builder.namespace;
this.clusterType = builder.clusterType;
this.autoRemoveResources = builder.autoRemoveResources;
}

@Override
Expand All @@ -66,6 +70,11 @@ public ClusterType clusterType(TestContext context) {
return KnativeAction.super.clusterType(context);
}

@Override
public boolean isAutoRemoveResources() {
return autoRemoveResources;
}

@Override
public String getNamespace() {
return namespace;
Expand All @@ -84,6 +93,8 @@ public static abstract class Builder<T extends KnativeAction, B extends Builder<

protected ReferenceResolver referenceResolver;

private boolean autoRemoveResources = KnativeSettings.isAutoRemoveResources();

/**
* Use a custom Kubernetes client.
*/
Expand Down Expand Up @@ -116,6 +127,11 @@ public B clusterType(ClusterType clusterType) {
return self;
}

public B autoRemoveResources(boolean enabled) {
this.autoRemoveResources = enabled;
return self;
}

public B withReferenceResolver(ReferenceResolver referenceResolver) {
this.referenceResolver = referenceResolver;
return self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public interface KnativeAction extends TestAction {
*/
String getNamespace();

/**
* Should remove Knative resources automatically after test.
* @return
*/
boolean isAutoRemoveResources();

/**
* Resolves namespace name from given test context using the stored test variable.
* Fallback to the namespace given in Knative environment settings when no test variable is present.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.citrusframework.knative.actions.AbstractKnativeAction;
import org.citrusframework.kubernetes.KubernetesSettings;

import static org.citrusframework.knative.actions.KnativeActionBuilder.knative;

public class CreateBrokerAction extends AbstractKnativeAction {

private final String brokerName;
Expand Down Expand Up @@ -100,6 +102,13 @@ private void createBroker(TestContext context) {
.resource(broker)
.createOr(Updatable::update);

if (isAutoRemoveResources()) {
context.doFinally(knative().client(getKubernetesClient()).client(getKnativeClient())
.brokers()
.delete(resolvedBrokerName)
.inNamespace(getNamespace()));
}

logger.info(String.format("Successfully created Knative broker '%s' in namespace %s", resolvedBrokerName, brokerNamespace));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.citrusframework.knative.actions.AbstractKnativeAction;
import org.citrusframework.kubernetes.KubernetesSettings;

import static org.citrusframework.knative.actions.KnativeActionBuilder.knative;

public class CreateTriggerAction extends AbstractKnativeAction {

private final String triggerName;
Expand All @@ -50,6 +52,7 @@ public CreateTriggerAction(Builder builder) {

@Override
public void doExecute(TestContext context) {
String resolvedTriggerName = context.replaceDynamicContentInString(triggerName);
TriggerSpecBuilder triggerSpec = new TriggerSpecBuilder()
.withBroker(brokerName(brokerName, context));

Expand All @@ -61,7 +64,7 @@ public void doExecute(TestContext context) {
.withApiVersion(String.format("%s/%s", KnativeSupport.knativeEventingGroup(), KnativeSupport.knativeApiVersion()))
.withNewMetadata()
.withNamespace(namespace(context))
.withName(context.replaceDynamicContentInString(triggerName))
.withName(context.replaceDynamicContentInString(resolvedTriggerName))
.withLabels(KnativeSettings.getDefaultLabels())
.endMetadata()
.withSpec(triggerSpec.build())
Expand All @@ -71,6 +74,13 @@ public void doExecute(TestContext context) {
.inNamespace(namespace(context))
.resource(trigger)
.createOr(Updatable::update);

if (isAutoRemoveResources()) {
context.doFinally(knative().client(getKubernetesClient()).client(getKnativeClient())
.trigger()
.delete(resolvedTriggerName)
.inNamespace(getNamespace()));
}
}

private void addFilterOnAttributes(TriggerSpecBuilder triggerSpec, TestContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.citrusframework.knative.actions.AbstractKnativeAction;
import org.citrusframework.kubernetes.KubernetesSettings;

import static org.citrusframework.knative.actions.KnativeActionBuilder.knative;

public class CreateChannelAction extends AbstractKnativeAction {

private final String channelName;
Expand All @@ -37,11 +39,12 @@ public CreateChannelAction(Builder builder) {

@Override
public void doExecute(TestContext context) {
String resolvedChannelName = context.replaceDynamicContentInString(channelName);
Channel channel = new ChannelBuilder()
.withApiVersion(String.format("%s/%s", KnativeSupport.knativeMessagingGroup(), KnativeSupport.knativeApiVersion()))
.withNewMetadata()
.withNamespace(namespace(context))
.withName(context.replaceDynamicContentInString(channelName))
.withName(context.replaceDynamicContentInString(resolvedChannelName))
.withLabels(KnativeSettings.getDefaultLabels())
.endMetadata()
.build();
Expand All @@ -50,6 +53,13 @@ public void doExecute(TestContext context) {
.inNamespace(namespace(context))
.resource(channel)
.createOr(Updatable::update);

if (isAutoRemoveResources()) {
context.doFinally(knative().client(getKubernetesClient()).client(getKnativeClient())
.channels()
.delete(resolvedChannelName)
.inNamespace(getNamespace()));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.citrusframework.knative.actions.AbstractKnativeAction;
import org.citrusframework.kubernetes.KubernetesSettings;

import static org.citrusframework.knative.actions.KnativeActionBuilder.knative;

public class CreateSubscriptionAction extends AbstractKnativeAction {

private final String subscriptionName;
Expand Down Expand Up @@ -69,6 +71,13 @@ public void doExecute(TestContext context) {
.inNamespace(namespace(context))
.resource(subscription)
.createOr(Updatable::update);

if (isAutoRemoveResources()) {
context.doFinally(knative().client(getKubernetesClient()).client(getKnativeClient())
.subscriptions()
.delete(subscriptionName)
.inNamespace(getNamespace()));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public CreateBroker inNamespace(String namespace) {
return this;
}

@Override
public CreateBroker autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public CreateChannel inNamespace(String namespace) {
return this;
}

@Override
public CreateChannel autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public CreateSubscription inNamespace(String namespace) {
return this;
}

@Override
public CreateSubscription autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public CreateTrigger inNamespace(String namespace) {
return this;
}

@Override
public CreateTrigger autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public DeleteBroker inNamespace(String namespace) {
return this;
}

@Override
public DeleteBroker autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public DeleteChannel client(KnativeClient client) {
return this;
}

@Override
public DeleteChannel autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public DeleteChannel inNamespace(String namespace) {
this.delegate.inNamespace(namespace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public DeleteResource inNamespace(String namespace) {
return this;
}

@Override
public DeleteResource autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public DeleteSubscription inNamespace(String namespace) {
return this;
}

@Override
public DeleteSubscription autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public DeleteTrigger inNamespace(String namespace) {
return this;
}

@Override
public DeleteTrigger autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public ReceiveEvent inNamespace(String namespace) {
return this;
}

@Override
public ReceiveEvent autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public ReceiveEventAction build() {
return delegate.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public SendEvent inNamespace(String namespace) {
return this;
}

@Override
public SendEvent autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public SendEventAction build() {
return delegate.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public VerifyBroker inNamespace(String namespace) {
return this;
}

@Override
public VerifyBroker autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public CreateBroker inNamespace(String namespace) {
return this;
}

@Override
public CreateBroker autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public CreateChannel inNamespace(String namespace) {
return this;
}

@Override
public CreateChannel autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public CreateSubscription inNamespace(String namespace) {
return this;
}

@Override
public CreateSubscription autoRemoveResources(boolean enabled) {
this.delegate.autoRemoveResources(enabled);
return this;
}

@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
this.delegate.setReferenceResolver(referenceResolver);
Expand Down
Loading
Loading