javakubernetes-apiserverfabric8-kubernetes-client

Fabric8: Mock Kubernetes Server doesn't actually create a resource without 'expect' set


My expectation is that the annotation @EnableKubernetesMockClient, enables the KubernetesMockServer in CRUD mode, which means, I don't have to mock the PUT operations through API. With that expectation I wrote the following code:

@EnableKubernetesMockClient
public class Test {

    KubernetesMockServer server;
    KubernetesClient client;

    @Test
    public void test_create_custom_job() {

        MyJobDetails myJobDetails = new MyJobDetails();
        String jobName = "job1-202211200345-prod";
        myJobDetails.setname("job1-202211200345-prod");
        //Other setters to follow
        ...

        PlatformOperationsService svc = new PlatformOperationsService(client);
        svc.createCustomJob(myJobDetails);

        // Expect no other job to exist
        Job createdJob = client.batch().v1().jobs().list().getItems().get(0);

        assertTrue(jobName.equals(createdJob.getMetadata().getName()));
        // Other asserts will follow to see if all the other inputs were supplied correctly
        ...
        

    }
    
 }

However I get the following exception from the line : Job createdJob = client.batch().v1().jobs().list().getItems().get(0);

Failure executing: POST at: https://localhost:51913/apis/batch/v1/namespaces/test/jobs. Message: Not Found.
io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: POST at: https://localhost:51913/apis/batch/v1/namespaces/test/jobs. Message: Not Found.
    at app//io.fabric8.kubernetes.client.KubernetesClientException.copyAsCause(KubernetesClientException.java:238)
    at app//io.fabric8.kubernetes.client.dsl.internal.OperationSupport.waitForResult(OperationSupport.java:517)
    at app//io.fabric8.kubernetes.client.dsl.internal.OperationSupport.handleResponse(OperationSupport.java:551)
    at app//io.fabric8.kubernetes.client.dsl.internal.OperationSupport.handleResponse(OperationSupport.java:535)
    at app//io.fabric8.kubernetes.client.dsl.internal.OperationSupport.handleCreate(OperationSupport.java:328)
    at app//io.fabric8.kubernetes.client.dsl.internal.BaseOperation.handleCreate(BaseOperation.java:675)
    at app//io.fabric8.kubernetes.client.dsl.internal.BaseOperation.handleCreate(BaseOperation.java:88)
    at app//io.fabric8.kubernetes.client.dsl.internal.CreateOnlyResourceOperation.create(CreateOnlyResourceOperation.java:42)
    at app//io.fabric8.kubernetes.client.dsl.internal.BaseOperation.create(BaseOperation.java:1073)
    at app//io.fabric8.kubernetes.client.dsl.internal.BaseOperation.create(BaseOperation.java:88)

So I am not sure if my expectation is right or if I am missing something to make this work. If my expectation is wrong then there is no point in using this Mock framework for my usage as I don't want to mock and test the same set of objects in the same test method. Any help is much appreciated!


Solution

  • My expectation is that the annotation @EnableKubernetesMockClient, enables the KubernetesMockServer in CRUD mode

    It does not. There is an additional parameter to the annotation - @EnableKubernetesMockClient(crud = true)