@@ -18,19 +18,15 @@ import (
1818 kerrors "k8s.io/apimachinery/pkg/api/errors"
1919 "k8s.io/apimachinery/pkg/api/resource"
2020 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
21- "k8s.io/apimachinery/pkg/runtime"
2221 "k8s.io/apimachinery/pkg/runtime/schema"
2322 "k8s.io/apimachinery/pkg/types"
24- "k8s.io/client-go/dynamic"
25- dynamicfake "k8s.io/client-go/dynamic/fake"
26- "k8s.io/client-go/kubernetes/fake"
2723
2824 api "github.com/hybrid-cloud-patterns/patterns-operator/api/v1alpha1"
2925 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30- "k8s.io/client-go/testing"
3126
3227 "sigs.k8s.io/controller-runtime/pkg/client"
3328
29+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
3430 runtimefake "sigs.k8s.io/controller-runtime/pkg/client/fake"
3531 "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
3632
@@ -839,120 +835,149 @@ var _ = Describe("NewArgoCD", func() {
839835var _ = Describe ("haveArgo" , func () {
840836
841837 var (
842- dynamicClient dynamic.Interface
843- kubeClient * fake.Clientset
838+ fakeClient client.Client
844839
845- gvr schema.GroupVersionResource
846840 name string
847841 namespace string
848842 )
849843
850844 BeforeEach (func () {
851- gvr = schema.GroupVersionResource {Group : "argoproj.io" , Version : "v1beta1" , Resource : "argocds" }
852- kubeClient = fake .NewSimpleClientset ()
853- dynamicClient = dynamicfake .NewSimpleDynamicClientWithCustomListKinds (runtime .NewScheme (), map [schema.GroupVersionResource ]string {
854- gvr : "ArgoCDList" ,
855- })
856845 name = argoName
857846 namespace = argoNS
858847 })
859848
860849 Context ("when the ArgoCD instance exists" , func () {
861- BeforeEach ( func () {
862- argoCD := & unstructured.Unstructured {
850+ It ( "should return true" , func () {
851+ unstructuredArgo := & unstructured.Unstructured {
863852 Object : map [string ]any {
864853 "apiVersion" : "argoproj.io/v1beta1" ,
865854 "kind" : "ArgoCD" ,
866855 "metadata" : map [string ]any {
867- "name" : name ,
868- "namespace" : namespace ,
856+ "name" : name ,
857+ "namespace" : namespace ,
858+ "resourceVersion" : "1" ,
869859 },
870860 },
871861 }
872- _ , err := dynamicClient .Resource (gvr ).Namespace (namespace ).Create (context .Background (), argoCD , metav1.CreateOptions {})
873- Expect (err ).ToNot (HaveOccurred ())
874- })
862+ unstructuredArgo .SetGroupVersionKind (schema.GroupVersionKind {
863+ Group : ArgoCDGroup ,
864+ Kind : ArgoCDResource ,
865+ Version : ArgoCDVersion ,
866+ })
867+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
868+ WithRuntimeObjects (unstructuredArgo ).Build ()
875869
876- It ("should return true" , func () {
877- result := haveArgo (dynamicClient , name , namespace )
870+ result , err := haveArgo (fakeClient , name , namespace )
878871 Expect (result ).To (BeTrue ())
872+ Expect (err ).ToNot (HaveOccurred ())
873+
879874 })
880875 })
881876
882877 Context ("when the ArgoCD instance does not exist" , func () {
883878 It ("should return false" , func () {
884- result := haveArgo (dynamicClient , name , namespace )
879+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
880+ WithRuntimeObjects ().Build ()
881+ result , err := haveArgo (fakeClient , name , namespace )
885882 Expect (result ).To (BeFalse ())
883+ Expect (err ).ToNot (HaveOccurred ())
884+
886885 })
887886 })
888887
889888 Context ("when there is an error retrieving the ArgoCD instance" , func () {
890- BeforeEach (func () {
891- kubeClient .PrependReactor ("get" , "argocds" , func (testing.Action ) (handled bool , ret runtime.Object , err error ) {
892- return true , nil , fmt .Errorf ("get error" )
893- })
894- })
895-
896889 It ("should return false" , func () {
897- result := haveArgo (dynamicClient , name , namespace )
890+ fakeClient = fake .NewClientBuilder ().WithInterceptorFuncs (
891+ interceptor.Funcs {Get : func (ctx context.Context , client client.WithWatch , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
892+ return fmt .Errorf ("get error" )
893+ }},
894+ ).WithScheme (testEnv .Scheme ).Build ()
895+
896+ result , err := haveArgo (fakeClient , name , namespace )
898897 Expect (result ).To (BeFalse ())
898+ Expect (err ).To (HaveOccurred ())
899+
899900 })
900901 })
901902})
902903
903904var _ = Describe ("CreateOrUpdateArgoCD" , func () {
904905 var (
905- dynamicClient dynamic.Interface
906- gvr schema.GroupVersionResource
907- name string
908- namespace string
906+ fakeClient client.Client
907+ name string
908+ namespace string
909909 )
910910
911911 BeforeEach (func () {
912- gvr = schema.GroupVersionResource {Group : ArgoCDGroup , Version : ArgoCDVersion , Resource : ArgoCDResource }
913- dynamicClient = dynamicfake .NewSimpleDynamicClientWithCustomListKinds (runtime .NewScheme (), map [schema.GroupVersionResource ]string {
914- gvr : "ArgoCDList" ,
915- })
916912 name = argoName
917913 namespace = argoNS
918914 })
919915
920916 Context ("when the ArgoCD instance does not exist" , func () {
921917 It ("should create a new ArgoCD instance" , func () {
922- err := createOrUpdateArgoCD (dynamicClient , nil , name , namespace )
918+
919+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
920+ WithRuntimeObjects ().Build ()
921+
922+ err := createOrUpdateArgoCD (fakeClient , name , namespace )
923923 Expect (err ).ToNot (HaveOccurred ())
924924
925- argoCD , err := dynamicClient .Resource (gvr ).Namespace (namespace ).Get (context .TODO (), name , metav1.GetOptions {})
925+ unstructuredArgo := & unstructured.Unstructured {}
926+ unstructuredArgo .SetGroupVersionKind (schema.GroupVersionKind {
927+ Group : ArgoCDGroup ,
928+ Kind : ArgoCDResource ,
929+ Version : ArgoCDVersion ,
930+ })
931+ err = fakeClient .Get (context .Background (), client.ObjectKey {
932+ Namespace : namespace ,
933+ Name : name ,
934+ }, unstructuredArgo )
935+
926936 Expect (err ).ToNot (HaveOccurred ())
927- Expect (argoCD .GetName ()).To (Equal (name ))
928- Expect (argoCD .GetNamespace ()).To (Equal (namespace ))
937+ Expect (unstructuredArgo .GetName ()).To (Equal (name ))
938+ Expect (unstructuredArgo .GetNamespace ()).To (Equal (namespace ))
929939 })
930940 })
931941
932942 Context ("when the ArgoCD instance exists" , func () {
933- BeforeEach (func () {
934- argoCD := & unstructured.Unstructured {
943+ It ("should update the existing ArgoCD instance" , func () {
944+
945+ unstructuredArgo := & unstructured.Unstructured {
935946 Object : map [string ]any {
936947 "apiVersion" : "argoproj.io/v1beta1" ,
937948 "kind" : "ArgoCD" ,
938949 "metadata" : map [string ]any {
939950 "name" : name ,
940951 "namespace" : namespace ,
941- "resourceVersion" : "1 " ,
952+ "resourceVersion" : "0 " ,
942953 },
943954 },
944955 }
945- _ , err := dynamicClient .Resource (gvr ).Namespace (namespace ).Create (context .TODO (), argoCD , metav1.CreateOptions {})
946- Expect (err ).ToNot (HaveOccurred ())
947- })
956+ unstructuredArgo .SetGroupVersionKind (schema.GroupVersionKind {
957+ Group : ArgoCDGroup ,
958+ Kind : ArgoCDResource ,
959+ Version : ArgoCDVersion ,
960+ })
948961
949- It ("should update the existing ArgoCD instance" , func () {
950- err := createOrUpdateArgoCD (dynamicClient , nil , name , namespace )
962+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
963+ WithRuntimeObjects (unstructuredArgo ).Build ()
964+
965+ err := createOrUpdateArgoCD (fakeClient , name , namespace )
951966 Expect (err ).ToNot (HaveOccurred ())
952967
953- argoCD , err := dynamicClient .Resource (gvr ).Namespace (namespace ).Get (context .TODO (), name , metav1.GetOptions {})
968+ updatedUnstructuredArgo := & unstructured.Unstructured {}
969+ updatedUnstructuredArgo .SetGroupVersionKind (schema.GroupVersionKind {
970+ Group : ArgoCDGroup ,
971+ Kind : ArgoCDResource ,
972+ Version : ArgoCDVersion ,
973+ })
974+ err = fakeClient .Get (context .Background (), client.ObjectKey {
975+ Namespace : namespace ,
976+ Name : name ,
977+ }, updatedUnstructuredArgo )
978+
954979 Expect (err ).ToNot (HaveOccurred ())
955- Expect (argoCD .GetResourceVersion ()).To (Equal ("1" )) // Ensure it has been updated
980+ Expect (updatedUnstructuredArgo .GetResourceVersion ()).To (Equal ("1" )) // Ensure it has been updated
956981 })
957982 })
958983})
0 commit comments