Skip to content
Merged
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
99 changes: 30 additions & 69 deletions src/test/java/io/strimzi/ValidatingWebhookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public void testEvictionAllowed() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-kafka"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, false);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -82,9 +80,7 @@ public void testEvictionDenied() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-kafka"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -104,9 +100,7 @@ public void testEvictionDeniedWithCustomStatusCode() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-kafka"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true, 429);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -126,9 +120,7 @@ public void testEvictionCorrectUid() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-kafka"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels, "POD-UID"));
Expand All @@ -153,9 +145,7 @@ public void testEvictionWrongUid() {
.withUid("ANOTHER-POD-UUID")
.endMetadata()
.build();
when(podResource.get()).thenReturn(pod);
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(pod);

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels, "WRONG-UID"));
Expand Down Expand Up @@ -187,9 +177,7 @@ public void testV1Beta1Eviction() {
.withRequest(admissionRequest)
.build();

when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(admissionReview);
Expand All @@ -212,9 +200,7 @@ public void testEvictionOnPodWithoutAnnotations() {
Pod mockedPod = mockedPod(false, labels);
mockedPod.getMetadata().setAnnotations(null);

when(podResource.get()).thenReturn(mockedPod);
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod);

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -238,9 +224,7 @@ public void testEvictionOnPodWithExistingAnnotations() {
Pod mockedPod = mockedPod(false, labels);
mockedPod.getMetadata().setAnnotations(Map.of("someAnno1", "someValue1", "someAnno2", "someValue2"));

when(podResource.get()).thenReturn(mockedPod);
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod);

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -261,9 +245,7 @@ public void testEvictionOnPodWithExistingAnnotations() {
public void testEmptyLabel() {
final Map<String, String> labels = Collections.emptyMap();

when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -276,9 +258,7 @@ public void testEmptyLabel() {

@Test
public void testNullLabel() {
when(podResource.get()).thenReturn(mockedPod(false, null));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, null));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, null));
Expand All @@ -296,9 +276,7 @@ public void testCorrectLabelWrongValue() {
"strimzi.io/name", "my-cluster-foo"
);

when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -316,9 +294,7 @@ public void testCorrectLabelCorrectKafkaValue() {
"strimzi.io/name", "my-cluster-kafka"
);

when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -339,9 +315,7 @@ public void testCorrectLabelCorrectZooKeeperValue() {
"strimzi.io/name", "my-cluster-zookeeper"
);

when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -362,9 +336,7 @@ public void testCorrectKafkaLabelWithDisabledKafkaDraining() {
"strimzi.io/name", "my-cluster-kafka"
);

when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, false, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand All @@ -381,9 +353,7 @@ public void testCorrectZooKeeperLabelWithDisabledZooKeeperDraining() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-zookeeper"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, false, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand Down Expand Up @@ -438,9 +408,7 @@ public void testNoKindLabel() {
final Map<String, String> labels = Map.of(
"strimzi.io/name", "my-cluster-zookeeper"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand Down Expand Up @@ -473,9 +441,7 @@ public void testAlreadyHasRuLabel() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-zookeeper"
);
when(podResource.get()).thenReturn(mockedPod(true, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(true, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(reviewRequest(false, labels));
Expand Down Expand Up @@ -509,9 +475,7 @@ public void testEvictionWithoutNamespace() {
.withRequest(admissionRequest)
.build();

when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(request);
Expand Down Expand Up @@ -545,9 +509,7 @@ public void testNoNamespaceAnywhere() {
.withRequest(admissionRequest)
.build();

when(podResource.get()).thenReturn(mockedPod(true, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(true, labels));

ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true);
AdmissionReview reviewResponse = webhook.webhook(request);
Expand Down Expand Up @@ -612,9 +574,7 @@ public void testNamespaceFilteringEmptyDrainNamespaces() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-kafka"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

// Empty drainNamespaces should watch all namespaces
ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true, "");
Expand All @@ -635,9 +595,7 @@ public void testNamespaceFilteringWithWatchedNamespace() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-kafka"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

// my-namespace is in the drain namespaces list
ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true, "my-namespace,other-namespace");
Expand Down Expand Up @@ -701,9 +659,7 @@ public void testNamespaceFilteringWithSpacesInList() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-kafka"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

// Test with spaces around namespace names
ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true, " my-namespace , other-namespace ");
Expand All @@ -724,9 +680,7 @@ public void testNamespaceFilteringWithWildcard() {
"strimzi.io/kind", "Kafka",
"strimzi.io/name", "my-cluster-kafka"
);
when(podResource.get()).thenReturn(mockedPod(false, labels));
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
ArgumentCaptor<Pod> podCaptor = stubPodGetAndPatch(mockedPod(false, labels));

// "*" should watch all namespaces (same as empty string)
ValidatingWebhook webhook = new ValidatingWebhook(client, true, true, true, "*");
Expand All @@ -740,4 +694,11 @@ public void testNamespaceFilteringWithWildcard() {
verify(podResource, times(1)).patch((Pod) any());
assertThat(podCaptor.getValue().getMetadata().getAnnotations().get("strimzi.io/manual-rolling-update"), is("true"));
}

private ArgumentCaptor<Pod> stubPodGetAndPatch(Pod currentPod) {
ArgumentCaptor<Pod> podCaptor = ArgumentCaptor.forClass(Pod.class);
when(podResource.get()).thenReturn(currentPod);
when(podResource.patch(podCaptor.capture())).thenReturn(new Pod());
return podCaptor;
}
}