2

I'm trying to list CRD objects using Limit as follows:

...
someObjList := v1alpha1.SomeObjList{}
_ = cl.List(ctx, &someObjList, client.InNamespace(lib.Namespace), &listOptions)

When testing, I add 25 objects to a fake client then call the list function with a Limit of 10:

opts := client.ListOptions{
    Limit: 10,
}
result, _ := lib.List(context.Background(), opts)
assert.Equal(t, 10, len(result))

But the test fails with:

assertion failed: 10 (int) != 25 (int)

I'm not sure if I need to add something specific to the fake client in order to make this work. This is the current client I use in tests:

func makeClient(t *testing.T, clusterState ...runtime.Object) client.Client {
    scheme := runtime.NewScheme()
    schemeBuilder := runtime.SchemeBuilder{
        ...
    }
    err := schemeBuilder.AddToScheme(scheme)
    if err != nil {
        t.Fatal(err)
    }

    return fake.NewClientBuilder().
        WithScheme(scheme).
        WithRuntimeObjects(clusterState...).
        Build()
}
Sara Taha
  • 73
  • 7
  • Turned out that the fake client-go ignores the `limit` parameter as mentioned here: https://github.com/kubernetes/client-go/issues/793 So to write pagination tests we need to use a real API server which envtest provides: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest – Sara Taha May 17 '22 at 08:30

0 Answers0