@@ -26,30 +26,69 @@ import (
2626 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/randomid"
2727 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
2828 testcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller"
29- testmain "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main"
3029 testvariable "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/resourcefixture/variable"
3130 "github.com/google/go-cmp/cmp"
3231
3332 corev1 "k8s.io/api/core/v1"
34- apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
35- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3633 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
34+ "k8s.io/apimachinery/pkg/runtime/schema"
3735 "k8s.io/apimachinery/pkg/types"
36+ "k8s.io/client-go/rest"
3837 "sigs.k8s.io/controller-runtime/pkg/client"
3938 "sigs.k8s.io/controller-runtime/pkg/manager"
4039 "sigs.k8s.io/controller-runtime/pkg/reconcile"
4140)
4241
4342var (
44- mgr manager.Manager
45-
46- fakeCRD = newTestKindCRD ()
43+ fakeCRDGVK = schema.GroupVersionKind {
44+ Group : "test.cnrm.cloud.google.com" ,
45+ Version : "v1beta1" ,
46+ Kind : "TestKind" ,
47+ }
4748)
4849
50+ func runManager (t * testing.T , restConfig * rest.Config ) manager.Manager {
51+ mgrOpts := manager.Options {}
52+ mgrOpts .Metrics .BindAddress = "0" // no metrics
53+
54+ mgr , err := manager .New (restConfig , mgrOpts )
55+ if err != nil {
56+ t .Fatalf ("error creating manager: %v" , err )
57+ }
58+
59+ ctx := context .TODO ()
60+ ctx , cancel := context .WithCancel (ctx )
61+
62+ errChan := make (chan error )
63+ go func () {
64+ err := mgr .Start (ctx )
65+ if err != nil {
66+ t .Errorf ("error from manager: %v" , err )
67+ }
68+ errChan <- err
69+ }()
70+
71+ t .Cleanup (func () {
72+ cancel ()
73+ err := <- errChan
74+ if err != nil {
75+ t .Fatalf ("error from manager: %v" , err )
76+ }
77+ })
78+
79+ return mgr
80+ }
81+
4982func TestReconcile_UnmanagedResource (t * testing.T ) {
50- t .Parallel ()
83+ ctx := context .TODO ()
84+ h := test .NewKubeHarness (ctx , t )
85+ client := h .GetClient ()
86+
87+ h .CreateDummyCRD (fakeCRDGVK )
88+
89+ mgr := runManager (t , h .GetRESTConfig ())
90+
5191 testID := testvariable .NewUniqueID ()
52- client := mgr .GetClient ()
5392 testcontroller .EnsureNamespaceExistsT (t , client , k8s .SystemNamespace )
5493 testcontroller .EnsureNamespaceExistsT (t , client , testID )
5594
@@ -60,22 +99,22 @@ func TestReconcile_UnmanagedResource(t *testing.T) {
6099 resource := newTestKindUnstructured (resourceNN )
61100 test .EnsureObjectExists (t , resource , client )
62101
63- reconciler , err := unmanageddetector .NewReconciler (mgr , fakeCRD )
102+ reconciler , err := unmanageddetector .NewReconciler (mgr , fakeCRDGVK )
64103 if err != nil {
65- t .Fatal ( fmt . Errorf ( "error creating reconciler: %w " , err ) )
104+ t .Fatalf ( "error creating reconciler: %v " , err )
66105 }
67- res , err := reconciler .Reconcile (context . TODO () , reconcile.Request {NamespacedName : resourceNN })
106+ res , err := reconciler .Reconcile (ctx , reconcile.Request {NamespacedName : resourceNN })
68107 if err != nil {
69- t .Fatal ( fmt . Errorf ( "unexpected error during reconciliation: %w " , err ) )
108+ t .Fatalf ( "unexpected error during reconciliation: %v " , err )
70109 }
71110 emptyResult := reconcile.Result {}
72111 if got , want := res , emptyResult ; ! reflect .DeepEqual (got , want ) {
73112 t .Fatalf ("unexpected diff in reconcile result (-want +got): \n %v" , cmp .Diff (want , got ))
74113 }
75114
76- condition , found , err := getCurrentCondition (context . TODO () , client , resource )
115+ condition , found , err := getCurrentCondition (ctx , client , resource )
77116 if err != nil {
78- t .Fatal ( fmt . Errorf ( "error getting resource's condition: %w " , err ) )
117+ t .Fatalf ( "error getting resource's condition: %v " , err )
79118 }
80119 if ! found {
81120 t .Fatalf ("got nil condition for resource, want non-nil condition with reason '%v'" , k8s .Unmanaged )
@@ -89,9 +128,15 @@ func TestReconcile_UnmanagedResource(t *testing.T) {
89128}
90129
91130func TestReconcile_ManagedResource (t * testing.T ) {
92- t .Parallel ()
131+ ctx := context .TODO ()
132+ h := test .NewKubeHarness (ctx , t )
133+ client := h .GetClient ()
134+
135+ h .CreateDummyCRD (fakeCRDGVK )
136+
137+ mgr := runManager (t , h .GetRESTConfig ())
138+
93139 testID := testvariable .NewUniqueID ()
94- client := mgr .GetClient ()
95140 testcontroller .EnsureNamespaceExistsT (t , client , k8s .SystemNamespace )
96141 testcontroller .EnsureNamespaceExistsT (t , client , testID )
97142
@@ -105,48 +150,33 @@ func TestReconcile_ManagedResource(t *testing.T) {
105150 controller := newControllerUnstructuredForNamespace (resourceNN .Namespace )
106151 test .EnsureObjectExists (t , controller , client )
107152
108- reconciler , err := unmanageddetector .NewReconciler (mgr , fakeCRD )
153+ reconciler , err := unmanageddetector .NewReconciler (mgr , fakeCRDGVK )
109154 if err != nil {
110- t .Fatal ( fmt . Errorf ( "error creating reconciler: %w " , err ) )
155+ t .Fatalf ( "error creating reconciler: %v " , err )
111156 }
112- res , err := reconciler .Reconcile (context . TODO () , reconcile.Request {NamespacedName : resourceNN })
157+ res , err := reconciler .Reconcile (ctx , reconcile.Request {NamespacedName : resourceNN })
113158 if err != nil {
114- t .Fatal ( fmt . Errorf ( "unexpected error during reconciliation: %w " , err ) )
159+ t .Fatalf ( "unexpected error during reconciliation: %v " , err )
115160 }
116161 emptyResult := reconcile.Result {}
117162 if got , want := res , emptyResult ; ! reflect .DeepEqual (got , want ) {
118163 t .Fatalf ("unexpected diff in reconcile result (-want +got): \n %v" , cmp .Diff (want , got ))
119164 }
120165
121- condition , found , err := getCurrentCondition (context . TODO () , client , resource )
166+ condition , found , err := getCurrentCondition (ctx , client , resource )
122167 if err != nil {
123- t .Fatal ( fmt . Errorf ( "error getting resource's condition: %w " , err ) )
168+ t .Fatalf ( "error getting resource's condition: %v " , err )
124169 }
125170 if found {
126171 t .Fatalf ("got non-nil condition '%v' for resource, want nil condition" , condition )
127172 }
128173}
129174
130- func newTestKindCRD () * apiextensions.CustomResourceDefinition {
131- crd := test .CRDForGVK (metav1.GroupVersionKind {
132- Group : "test.cnrm.cloud.google.com" ,
133- Version : "v1beta1" ,
134- Kind : "TestKind" ,
135- })
136- // Enable the status subresource for this CRD. This is needed to allow
137- // UpdateStatus() calls to work on custom resources belonging to this CRD
138- // on the API server.
139- crd .Spec .Versions [0 ].Subresources = & apiextensions.CustomResourceSubresources {
140- Status : & apiextensions.CustomResourceSubresourceStatus {},
141- }
142- return crd
143- }
144-
145175func newTestKindUnstructured (nn types.NamespacedName ) * unstructured.Unstructured {
146176 return & unstructured.Unstructured {
147177 Object : map [string ]interface {}{
148- "apiVersion" : fmt .Sprintf ("%v/%v" , fakeCRD . Spec . Group , k8s . GetVersionFromCRD ( fakeCRD ) ),
149- "kind" : fakeCRD . Spec . Names .Kind ,
178+ "apiVersion" : fmt .Sprintf ("%v/%v" , fakeCRDGVK . Group , fakeCRDGVK . Version ),
179+ "kind" : fakeCRDGVK .Kind ,
150180 "metadata" : map [string ]interface {}{
151181 "namespace" : nn .Namespace ,
152182 "name" : nn .Name ,
@@ -204,7 +234,3 @@ func getCurrentCondition(ctx context.Context, c client.Client, u *unstructured.U
204234 condition , found = k8s .GetReadyCondition (resource )
205235 return condition , found , nil
206236}
207-
208- func TestMain (m * testing.M ) {
209- testmain .ForUnitTestsWithCRDs (m , []* apiextensions.CustomResourceDefinition {fakeCRD }, & mgr )
210- }
0 commit comments