'Fake client behaviour when mocking k8s for testing
I am using the fake
clientset to perform some mocking in a k8s
cli tool I am creating.
Therefore I am creating a Job
resource
jobs := clientset.BatchV1().Jobs(mynamespace)
_, err = jobs.Create(context.TODO(), job, metav1.CreateOptions{})
The clienset has been created as follows:
clientset := testclient.NewSimpleClientset()
where
testclient "k8s.io/client-go/kubernetes/fake"
The job seems to be created, i.e. err
is nil
on the call above.
I then (from another function call) try to list the corresponding pods (using the exact same clientset
)
podList, err = clientset.CoreV1().Pods(myNamespace).List(context.TODO(), labelOptions)
However the length of the corresponding list is always zero
len(podList.Items) = 0
Shouldn't the call using the fake
clienset create all related mock resources? (such as the job's pod)
Solution 1:[1]
Shouldn't the call using the fake clienset create all related mock resources? (such as the job's pod)
No, the fake clientset does nothing except put objects into cache.
In a real kubernetes cluster, JobController
reconciles jobs and creates pods.
I suggest you testing in a real kubernetes cluster, such as K3s.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | zwtop |