<p>As part of writing logic for listing pods within a give k8s node I've the following api call:</p>
<pre><code>func ListRunningPodsByNodeName(kubeClient kubernetes.Interface, nodeName string (*v1.PodList, error) {
  return kubeClient.
    CoreV1().
    Pods(&quot;&quot;).
    List(context.TODO(), metav1.ListOptions{
        FieldSelector: &quot;spec.nodeName=&quot; + nodeName,
    })
}
</code></pre>
<p>In order to test <strong>ListRunningPodsByNodeName</strong> using the fake client provided by k8s, I came up with the following test initialization:</p>
<pre><code>func TestListRunningPodsByNodeName(t *testing.T) {

    // happy path
    kubeClient := fake.NewSimpleClientset(&amp;v1.Pod{
    ObjectMeta: metav1.ObjectMeta{
        Name:        &quot;pod1&quot;,
        Namespace:   &quot;default&quot;,
        Annotations: map[string]string{},
    },
        Spec: v1.PodSpec{
            NodeName: &quot;foo&quot;,
        },
    }, &amp;v1.Pod{
            ObjectMeta: metav1.ObjectMeta{
                    Name:        &quot;pod2&quot;,
                    Namespace:   &quot;default&quot;,
                    Annotations: map[string]string{},
            },
            Spec: v1.PodSpec{
                NodeName: &quot;bar&quot;,
            },
    })

    got, _ := ListRunningPodsByNodeName(kubeClient, &quot;foo&quot;)

    for i, pod := range got.Items {
        fmt.Println(fmt.Sprintf(&quot;[%2d] %s&quot;, i, pod.GetName()))
    }

    t.Errorf(&quot;Error, expecting only one pod&quot;)
}
</code></pre>
<p>When debugging, I got <strong>pod1</strong> and <strong>pod2</strong> Pods returned despite I'm filtering by those running within <strong>foo</strong> node. Using this same approach for filtering by certain metadata work like a charm but can't make this to work in case of filtering by nodeName. ¿Anyone knows why please? I suspect it might be a limitation with the fake client capabilities but not completely sure to open an issue yet</p>
<p>thanks by advance</p>

Borja Tur
<p>The fake k8s client does not support filtering by field selector (see <a href="https://github.com/kubernetes/client-go/issues/326#issuecomment-412993326" rel="nofollow noreferrer">this comment</a>). When unit testing with the fake k8s client, it's best to assume that the k8s client will work as expected in the real world (return the correct pods based on your field selector query). In your test, provide the pods to the fake k8s client that <strong>your application</strong> expects and test your own logic, rather than also testing the query logic of the k8s client.</p>
<p>If it's absolutely critical that the fake client perform the filtering for you, you may be able to use the fake client reactors to inject this custom behavior into the fake client. It just means more boilerplate code.</p>
<blockquote>
<p>Anything non-generic (like field selection behavior) can be injected in your tests by adding reactors that deal with specific types of actions, use additional info in the action (in this case, ListAction#GetListRestrictions().Fields), and customize the data returned</p>
</blockquote>
<p>I haven't tested this at all but hopefully it gives you something to start with.</p>
<pre><code>client := fake.NewSimpleClientset()
client.AddReactor(&quot;*&quot;, &quot;MyResource&quot;, func(action testing.Action) (handled bool, ret runtime.Object, err error) {
    // Add custom filtering logic here
})
</code></pre>
