Skip to content

fix: replace label selector mock server test with IT #1375

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 2 commits into from
Aug 24, 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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.javaoperatorsdk.operator;

import java.time.Duration;
import java.util.Collections;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
import io.javaoperatorsdk.operator.sample.labelselector.LabelSelectorTestCustomResource;
import io.javaoperatorsdk.operator.sample.labelselector.LabelSelectorTestReconciler;

import static io.javaoperatorsdk.operator.sample.labelselector.LabelSelectorTestReconciler.LABEL_KEY;
import static io.javaoperatorsdk.operator.sample.labelselector.LabelSelectorTestReconciler.LABEL_VALUE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

class LabelSelectorIT {

@RegisterExtension
LocallyRunOperatorExtension operator =
LocallyRunOperatorExtension.builder().withReconciler(new LabelSelectorTestReconciler())
.build();


@Test
void filtersCustomResourceByLabel() {
operator.create(resource("r1", true));
operator.create(resource("r2", false));

await().pollDelay(Duration.ofMillis(150)).untilAsserted(() -> {
assertThat(
operator.getReconcilerOfType(LabelSelectorTestReconciler.class).getNumberOfExecutions())
.isEqualTo(1);
});
}

LabelSelectorTestCustomResource resource(String name, boolean addLabel) {
var res = new LabelSelectorTestCustomResource();
res.setMetadata(new ObjectMetaBuilder()
.withName(name)
.withLabels(addLabel ? Map.of(LABEL_KEY, LABEL_VALUE)
: Collections.emptyMap())
.build());
return res;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.javaoperatorsdk.operator.sample.labelselector;

import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.ShortNames;
import io.fabric8.kubernetes.model.annotation.Version;

@Group("sample.javaoperatorsdk")
@Version("v1")
@ShortNames("lst")
public class LabelSelectorTestCustomResource
extends CustomResource<Void, Void>
implements Namespaced {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.javaoperatorsdk.operator.sample.labelselector;

import java.util.concurrent.atomic.AtomicInteger;

import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider;

import static io.javaoperatorsdk.operator.sample.labelselector.LabelSelectorTestReconciler.LABEL_KEY;
import static io.javaoperatorsdk.operator.sample.labelselector.LabelSelectorTestReconciler.LABEL_VALUE;

@ControllerConfiguration(labelSelector = LABEL_KEY + "=" + LABEL_VALUE)
public class LabelSelectorTestReconciler
implements Reconciler<LabelSelectorTestCustomResource>, TestExecutionInfoProvider {

public static final String LABEL_KEY = "app";
public static final String LABEL_VALUE = "myapp";

private final AtomicInteger numberOfExecutions = new AtomicInteger(0);

@Override
public UpdateControl<LabelSelectorTestCustomResource> reconcile(
LabelSelectorTestCustomResource resource, Context<LabelSelectorTestCustomResource> context) {
numberOfExecutions.addAndGet(1);
return UpdateControl.noUpdate();
}

public int getNumberOfExecutions() {
return numberOfExecutions.get();
}

}