11package controllers
22
33import (
4- "context"
54 "encoding/json"
65 "path/filepath"
76 "sort"
@@ -100,10 +99,10 @@ func TestReconciliation(t *testing.T) {
10099 }
101100
102101 test .AssertNoError (t , reconciler .SetupWithManager (mgr ))
103- test .AssertNoError (t , k8sClient .Create (context . TODO (), test .NewNamespace ("test-ns" )))
102+ test .AssertNoError (t , k8sClient .Create (t . Context (), test .NewNamespace ("test-ns" )))
104103
105104 t .Run ("reconciling creation of new resources" , func (t * testing.T ) {
106- ctx := context . TODO ()
105+ ctx := t . Context ()
107106 gs := createAndReconcileToFinalizedState (t , k8sClient , reconciler , makeTestGitOpsSet (t ))
108107 defer deleteGitOpsSetAndFinalize (t , k8sClient , reconciler , gs )
109108
@@ -124,7 +123,7 @@ func TestReconciliation(t *testing.T) {
124123 })
125124
126125 t .Run ("reconciling creation of resources in different namespaces" , func (t * testing.T ) {
127- ctx := context . TODO ()
126+ ctx := t . Context ()
128127
129128 // https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation
130129 test .AssertNoError (t , k8sClient .Create (ctx , test .NewNamespace ("engineering-dev-ns" )))
@@ -156,7 +155,7 @@ func TestReconciliation(t *testing.T) {
156155 })
157156
158157 t .Run ("reconciling cleanup when deleted" , func (t * testing.T ) {
159- ctx := context . TODO ()
158+ ctx := t . Context ()
160159 gs := createAndReconcileToFinalizedState (t , k8sClient , reconciler , makeTestGitOpsSet (t ))
161160 // manually deletes below
162161
@@ -179,7 +178,7 @@ func TestReconciliation(t *testing.T) {
179178 })
180179
181180 t .Run ("error conditions" , func (t * testing.T ) {
182- ctx := context . TODO ()
181+ ctx := t . Context ()
183182 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
184183 gs .Spec .Templates = []templatesv1.GitOpsSetTemplate {
185184 {
@@ -218,7 +217,7 @@ func TestReconciliation(t *testing.T) {
218217 })
219218
220219 t .Run ("reconciling removal of resources" , func (t * testing.T ) {
221- ctx := context . TODO ()
220+ ctx := t . Context ()
222221 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
223222 gs .Spec .Generators = []templatesv1.GitOpsSetGenerator {
224223 {
@@ -271,7 +270,7 @@ func TestReconciliation(t *testing.T) {
271270 })
272271
273272 t .Run ("reconciling update of resources" , func (t * testing.T ) {
274- ctx := context . TODO ()
273+ ctx := t . Context ()
275274 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
276275 gs .Spec .Templates = []templatesv1.GitOpsSetTemplate {
277276 {
@@ -358,7 +357,7 @@ func TestReconciliation(t *testing.T) {
358357 })
359358
360359 t .Run ("reconciling update of configmaps" , func (t * testing.T ) {
361- ctx := context . TODO ()
360+ ctx := t . Context ()
362361 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
363362 gs .Spec .Templates = []templatesv1.GitOpsSetTemplate {
364363 {
@@ -450,7 +449,7 @@ func TestReconciliation(t *testing.T) {
450449 })
451450
452451 t .Run ("reconciling with no generated resources" , func (t * testing.T ) {
453- ctx := context . TODO ()
452+ ctx := t . Context ()
454453 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
455454 // No templates to generate resources from
456455 gs .Spec .Templates = []templatesv1.GitOpsSetTemplate {}
@@ -475,7 +474,7 @@ func TestReconciliation(t *testing.T) {
475474 })
476475
477476 t .Run ("reconciling update of deleted resource" , func (t * testing.T ) {
478- ctx := context . TODO ()
477+ ctx := t . Context ()
479478 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
480479 gs .Spec .Templates = []templatesv1.GitOpsSetTemplate {
481480 {
@@ -533,7 +532,7 @@ func TestReconciliation(t *testing.T) {
533532 })
534533
535534 t .Run ("service account impersonation" , func (t * testing.T ) {
536- ctx := context . TODO ()
535+ ctx := t . Context ()
537536 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
538537 gs .Spec .ServiceAccountName = "test-sa"
539538 })
@@ -564,7 +563,7 @@ func TestReconciliation(t *testing.T) {
564563 })
565564
566565 t .Run ("default service account impersonation" , func (t * testing.T ) {
567- ctx := context . TODO ()
566+ ctx := t . Context ()
568567 gs := makeTestGitOpsSet (t )
569568 gs = createAndReconcileToFinalizedState (t , k8sClient , reconciler , gs )
570569 defer deleteGitOpsSetAndFinalize (t , k8sClient , reconciler , gs )
@@ -598,7 +597,7 @@ func TestReconciliation(t *testing.T) {
598597 })
599598
600599 t .Run ("reconciling update of resources with service account" , func (t * testing.T ) {
601- ctx := context . TODO ()
600+ ctx := t . Context ()
602601 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
603602 gs .Spec .Templates = []templatesv1.GitOpsSetTemplate {
604603 {
@@ -701,7 +700,7 @@ func TestReconciliation(t *testing.T) {
701700 })
702701
703702 t .Run ("reconciling with annotation-triggered reconciliation" , func (t * testing.T ) {
704- ctx := context . TODO ()
703+ ctx := t . Context ()
705704 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
706705 gs .ObjectMeta .Annotations = map [string ]string {
707706 fluxMeta .ReconcileRequestAnnotation : time .Now ().Format (time .RFC3339Nano ),
@@ -722,7 +721,7 @@ func TestReconciliation(t *testing.T) {
722721 })
723722
724723 t .Run ("reconciling creation when suspended" , func (t * testing.T ) {
725- ctx := context . TODO ()
724+ ctx := t . Context ()
726725 gs := createAndReconcileToFinalizedState (t , k8sClient , reconciler , makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
727726 gs .Spec .Suspend = true
728727 }))
@@ -737,7 +736,7 @@ func TestReconciliation(t *testing.T) {
737736 })
738737
739738 t .Run ("reconciling when gitrepository has no artifact" , func (t * testing.T ) {
740- ctx := context . TODO ()
739+ ctx := t . Context ()
741740 emptyGR := test .NewGitRepository ()
742741 test .AssertNoError (t , k8sClient .Create (ctx , test .ToUnstructured (t , emptyGR )))
743742 defer deleteObject (t , k8sClient , emptyGR )
@@ -768,7 +767,7 @@ func TestReconciliation(t *testing.T) {
768767 })
769768
770769 t .Run ("error conditions - existing resource" , func (t * testing.T ) {
771- ctx := context . TODO ()
770+ ctx := t . Context ()
772771 gs := makeTestGitOpsSet (t , func (gs * templatesv1.GitOpsSet ) {
773772 gs .Spec .Templates = []templatesv1.GitOpsSetTemplate {
774773 {
@@ -1064,7 +1063,7 @@ func deleteAllKustomizations(t *testing.T, cl client.Client) {
10641063 u := & unstructured.Unstructured {}
10651064 u .SetGroupVersionKind (kustomizationGVK )
10661065
1067- err := cl .DeleteAllOf (context . TODO (), u , client .InNamespace ("default" ))
1066+ err := cl .DeleteAllOf (t . Context (), u , client .InNamespace ("default" ))
10681067 if client .IgnoreNotFound (err ) != nil {
10691068 t .Fatal (err )
10701069 }
@@ -1075,7 +1074,7 @@ func assertResourceDoesNotExist(t *testing.T, cl client.Client, gs *kustomizev1.
10751074 check := & unstructured.Unstructured {}
10761075 check .SetGroupVersionKind (kustomizationGVK )
10771076
1078- if err := cl .Get (context . TODO (), client .ObjectKeyFromObject (gs ), check ); ! apierrors .IsNotFound (err ) {
1077+ if err := cl .Get (t . Context (), client .ObjectKeyFromObject (gs ), check ); ! apierrors .IsNotFound (err ) {
10791078 t .Fatalf ("object %v still exists" , gs )
10801079 }
10811080}
@@ -1084,7 +1083,7 @@ func assertKustomizationsExist(t *testing.T, cl client.Client, ns string, want .
10841083 t .Helper ()
10851084 gss := & unstructured.UnstructuredList {}
10861085 gss .SetGroupVersionKind (kustomizationGVK )
1087- test .AssertNoError (t , cl .List (context . TODO (), gss , client .InNamespace (ns )))
1086+ test .AssertNoError (t , cl .List (t . Context (), gss , client .InNamespace (ns )))
10881087
10891088 existingNames := func (l []unstructured.Unstructured ) []string {
10901089 names := []string {}
@@ -1105,7 +1104,7 @@ func assertNoKustomizationsExistInNamespace(t *testing.T, cl client.Client, ns s
11051104 t .Helper ()
11061105 gss := & unstructured.UnstructuredList {}
11071106 gss .SetGroupVersionKind (kustomizationGVK )
1108- test .AssertNoError (t , cl .List (context . TODO (), gss , client .InNamespace (ns )))
1107+ test .AssertNoError (t , cl .List (t . Context (), gss , client .InNamespace (ns )))
11091108
11101109 if len (gss .Items ) != 0 {
11111110 t .Fatalf ("want no Kustomizations to exist, got %v" , len (gss .Items ))
@@ -1136,15 +1135,15 @@ func assertInventoryHasNoItems(t *testing.T, gs *templatesv1.GitOpsSet) {
11361135
11371136func deleteObject (t * testing.T , cl client.Client , obj client.Object ) {
11381137 t .Helper ()
1139- if err := cl .Delete (context . TODO (), obj ); err != nil {
1138+ if err := cl .Delete (t . Context (), obj ); err != nil {
11401139 t .Fatal (err )
11411140 }
11421141}
11431142
11441143// Create the provided GitOpsSet and ensure that it has been setup for
11451144// finalization.
11461145func createAndReconcileToFinalizedState (t * testing.T , k8sClient client.Client , r * GitOpsSetReconciler , gs * templatesv1.GitOpsSet ) * templatesv1.GitOpsSet {
1147- test .AssertNoError (t , k8sClient .Create (context . TODO (), gs ))
1146+ test .AssertNoError (t , k8sClient .Create (t . Context (), gs ))
11481147 reconcileAndAssertFinalizerExists (t , k8sClient , r , gs )
11491148
11501149 return gs
@@ -1155,7 +1154,7 @@ func createAndReconcileToFinalizedState(t *testing.T, k8sClient client.Client, r
11551154// This is needed because the reconciler returns after applying the finalizer to
11561155// avoid race conditions.
11571156func reconcileAndAssertFinalizerExists (t * testing.T , cl client.Client , reconciler * GitOpsSetReconciler , gs * templatesv1.GitOpsSet ) {
1158- ctx := context . TODO ()
1157+ ctx := t . Context ()
11591158 _ , err := reconciler .Reconcile (ctx , ctrl.Request {NamespacedName : client .ObjectKeyFromObject (gs )})
11601159 test .AssertNoError (t , err )
11611160
@@ -1258,7 +1257,7 @@ func createRBACForServiceAccount(t *testing.T, cl client.Client, serviceAccountN
12581257 ObjectMeta : metav1.ObjectMeta {Name : "test-role" , Namespace : namespace },
12591258 Rules : rules ,
12601259 }
1261- if err := cl .Create (context . TODO (), role ); err != nil {
1260+ if err := cl .Create (t . Context (), role ); err != nil {
12621261 t .Fatalf ("failed to write role: %s" , err )
12631262 }
12641263 t .Cleanup (func () {
@@ -1280,7 +1279,7 @@ func createRBACForServiceAccount(t *testing.T, cl client.Client, serviceAccountN
12801279 APIGroup : "rbac.authorization.k8s.io" ,
12811280 },
12821281 }
1283- if err := cl .Create (context . TODO (), binding ); err != nil {
1282+ if err := cl .Create (t . Context (), binding ); err != nil {
12841283 t .Fatalf ("failed to write role-binding: %s" , err )
12851284 }
12861285 t .Cleanup (func () {
@@ -1289,7 +1288,7 @@ func createRBACForServiceAccount(t *testing.T, cl client.Client, serviceAccountN
12891288}
12901289func deleteGitOpsSetAndFinalize (t * testing.T , cl client.Client , reconciler * GitOpsSetReconciler , gs * templatesv1.GitOpsSet ) {
12911290 t .Helper ()
1292- ctx := context . TODO ()
1291+ ctx := t . Context ()
12931292 if gs .Spec .Suspend {
12941293 gs .Spec .Suspend = false
12951294 test .AssertNoError (t , cl .Update (ctx , gs ))
0 commit comments