fabric8io / kubernetes-client

Java client for Kubernetes & OpenShift
http://fabric8.io
Apache License 2.0
3.38k stars 1.46k forks source link

How to check if a Deployment is ready with the mock server #3985

Closed subnetmarco closed 2 years ago

subnetmarco commented 2 years ago

Is your enhancement related to a problem? Please describe

I am testing the creation of a new Deployment in a test with @EnableKubernetesMockClient(crud = true). I can successfully create the Deployment and - in my test - watch that indeed it has been created:

Watch watch = client.apps().deployments().inNamespace("ns1").watch(new Watcher<Deployment>() {
  @Override
  public void eventReceived(Action action, Deployment resource) {
    assertEquals(Action.ADDED, action);
    assertEquals("httpbin", resource.getMetadata().getName());
    eventLatch.countDown();
  }

  @Override
  public void onClose(WatcherException arg0) {
    // Do nothing
  }
});

What I am struggling with right now, is to close the loop and properly test that the Deployment is indeed running with the right amount of replicas (using the mock server), and I cannot seem to find any indication on how to do that. Any tips?

Describe the solution you'd like

Instructions - or an example - to properly test that the Deployment is running with the proper amount of replicas.

Describe alternatives you've considered

No response

Additional context

No response

shawkins commented 2 years ago

The mock support is not a full api server nor kubernetes platform, so entries that look like Pods are not created for a Deployment. You'd have your test logic add them via crud operations or a KubernetesServer expectation.

subnetmarco commented 2 years ago

@shawkins thanks for the pointers. Do you know where I can find a pragmatic example for using a KubernetesServer expectation to change the status of a deployment over time in the test?

rohanKanojia commented 2 years ago

Could you please try something like this? In mocking watch during expectations mode, try sending a WatchEvent with Deployment status field updated to match ready fields https://github.com/fabric8io/kubernetes-client/blob/15ba0dd6c7e452c8f53905f23e3c22d35e9b1d9b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java#L254-L258

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!