@@ -17,21 +17,25 @@ limitations under the License.
1717package mutators
1818
1919import (
20+ "context"
2021 "encoding/json"
22+ "slices"
2123 "testing"
2224
2325 asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001"
2426 asocontainerservicev1preview "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231102preview"
2527 "github.com/Azure/azure-service-operator/v2/pkg/genruntime"
2628 "github.com/google/go-cmp/cmp"
2729 . "github.com/onsi/gomega"
30+ "k8s.io/apimachinery/pkg/api/meta"
2831 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2932 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3033 "k8s.io/apimachinery/pkg/runtime"
3134 "k8s.io/utils/ptr"
3235 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3336 expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
3437 "sigs.k8s.io/cluster-api/util/secret"
38+ "sigs.k8s.io/controller-runtime/pkg/client"
3539 fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
3640 "sigs.k8s.io/controller-runtime/pkg/conversion"
3741
@@ -727,9 +731,11 @@ func TestSetManagedClusterAgentPoolProfiles(t *testing.T) {
727731 },
728732 }
729733
730- c := fakeClientBuilder ().
731- WithLists (asoManagedMachinePools , machinePools ).
732- Build ()
734+ c := reverseListingClient {
735+ WithWatch : fakeClientBuilder ().
736+ WithLists (asoManagedMachinePools , machinePools ).
737+ Build (),
738+ }
733739
734740 cluster := & clusterv1.Cluster {ObjectMeta : metav1.ObjectMeta {Name : clusterName }}
735741 err := setManagedClusterAgentPoolProfiles (ctx , c , namespace , cluster , "" , umc )
@@ -855,3 +861,21 @@ func apUnstructured(g Gomega, ap *asocontainerservicev1.ManagedClustersAgentPool
855861 g .Expect (s .Convert (ap , u , nil )).To (Succeed ())
856862 return u
857863}
864+
865+ // reverseListingClient lists objects in the reverse order of the testing client.
866+ type reverseListingClient struct {
867+ client.WithWatch
868+ }
869+
870+ func (c reverseListingClient ) List (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error {
871+ err := c .WithWatch .List (ctx , list , opts ... )
872+ if err != nil {
873+ return err
874+ }
875+ objs , err := meta .ExtractList (list )
876+ if err != nil {
877+ return err
878+ }
879+ slices .Reverse (objs )
880+ return meta .SetList (list , objs )
881+ }
0 commit comments