Skip to content

Commit

Permalink
Fixes fabric8io#2306: Make KubernetesServer CRUD mode work with infor…
Browse files Browse the repository at this point in the history
…mers

This is addressed by making sure that MockWebServer returns a metadata object
for list queries.

Signed-off-by: Lalith Suresh <lsuresh@vmware.com>
  • Loading branch information
lalithsuresh committed Aug 10, 2020
1 parent e8c47dc commit d4fe65c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Expand Up @@ -39,7 +39,8 @@ private static String join(String sep, Collection<String> collection) {
@Override
public String compose(Collection<String> collection) {
return String.format(
"{\"apiVersion\":\"v1\",\"kind\":\"List\", \"items\": [%s]}",
"{\"apiVersion\":\"v1\",\"kind\":\"List\", \"items\": [%s], " +
"\"metadata\": {\"resourceVersion\": \"\", \"selfLink\": \"\"}}",
join(",", collection));
}
}
@@ -0,0 +1,72 @@
/**
* Copyright (C) 2020 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.mock;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
import io.fabric8.kubernetes.client.informers.SharedInformerFactory;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@EnableRuleMigrationSupport
public class CrudInformerTest {
@Rule
public KubernetesServer server = new KubernetesServer(true, true);

// https://github.com/fabric8io/kubernetes-client/issues/2306
@Test
void testCrudInformer() throws InterruptedException {
Pod podToCreate = new PodBuilder().withNewMetadata().withName("pod1").endMetadata().build();
KubernetesClient client = server.getClient();
SharedInformerFactory factory = client.informers();
SharedIndexInformer<Pod> podInformer = factory.sharedIndexInformerFor(Pod.class, PodList.class, 4000);
BlockingQueue<Pod> events = new LinkedBlockingQueue<>();
podInformer.addEventHandler(
new ResourceEventHandler<Pod>() {
@Override
public void onAdd(Pod obj) {
events.add(obj);
}

@Override
public void onUpdate(Pod oldObj, Pod newObj) {
}

@Override
public void onDelete(Pod oldObj, boolean deletedFinalStateUnknown) {
}
});
factory.startAllRegisteredInformers();
client.pods().create(podToCreate);
Pod readPod = events.poll(5, TimeUnit.SECONDS);
assertNotNull(readPod);
assertEquals(readPod.getMetadata().getName(), podToCreate.getMetadata().getName());
factory.stopAllRegisteredInformers();
}
}

0 comments on commit d4fe65c

Please sign in to comment.