@@ -13,6 +13,8 @@ import (
1313 "github.com/stretchr/testify/require"
1414 "k8s.io/apimachinery/pkg/types"
1515 "k8s.io/client-go/rest"
16+ "k8s.io/klog/v2"
17+ "k8s.io/klog/v2/ktesting"
1618 ctrlruntime "sigs.k8s.io/controller-runtime/pkg/client"
1719
1820 "github.com/jetstack/preflight/api"
@@ -34,13 +36,17 @@ import (
3436//
3537// [1] https://github.com/kubernetes-sigs/controller-runtime/issues/2341
3638func TestVenConnClient_PostDataReadingsWithOptions (t * testing.T ) {
39+ ctx , cancel := context .WithCancel (context .Background ())
40+ defer cancel ()
41+ log := ktesting .NewLogger (t , ktesting .NewConfig (ktesting .Verbosity (10 )))
42+ ctx = klog .NewContext (ctx , log )
3743 _ , restconf , kclient := testutil .WithEnvtest (t )
3844 for _ , obj := range testutil .Parse (testutil .VenConnRBAC ) {
39- require .NoError (t , kclient .Create (context . Background () , obj ))
45+ require .NoError (t , kclient .Create (ctx , obj ))
4046 }
4147 t .Parallel ()
4248
43- t .Run ("valid accessToken" , run_TestVenConnClient_PostDataReadingsWithOptions (restconf , kclient , testcase {
49+ t .Run ("valid accessToken" , run_TestVenConnClient_PostDataReadingsWithOptions (ctx , restconf , kclient , testcase {
4450 given : testutil .Undent (`
4551 apiVersion: jetstack.io/v1alpha1
4652 kind: VenafiConnection
@@ -93,7 +99,7 @@ func TestVenConnClient_PostDataReadingsWithOptions(t *testing.T) {
9399 ` ),
94100 expectReadyCondMsg : "ea744d098c2c1c6044e4c4e9d3bf7c2a68ef30553db00f1714886cedf73230f1" ,
95101 }))
96- t .Run ("error when the apiKey field is used" , run_TestVenConnClient_PostDataReadingsWithOptions (restconf , kclient , testcase {
102+ t .Run ("error when the apiKey field is used" , run_TestVenConnClient_PostDataReadingsWithOptions (ctx , restconf , kclient , testcase {
97103 // Why isn't it possible to use the 'apiKey' field? Although the
98104 // Kubernetes Discovery endpoint works with an API key, we have decided
99105 // to not support it because it isn't recommended.
@@ -152,7 +158,7 @@ func TestVenConnClient_PostDataReadingsWithOptions(t *testing.T) {
152158 expectReadyCondMsg : "b099d634ccec56556da28028743475dab67f79d079b668bedc3ef544f7eed2f3" ,
153159 expectErr : "VenafiConnection error-when-the-apikey-field-is-used/venafi-components: the agent cannot be used with an API key" ,
154160 }))
155- t .Run ("error when the tpp field is used" , run_TestVenConnClient_PostDataReadingsWithOptions (restconf , kclient , testcase {
161+ t .Run ("error when the tpp field is used" , run_TestVenConnClient_PostDataReadingsWithOptions (ctx , restconf , kclient , testcase {
156162 // IMPORTANT: The user may think they can use 'tpp', spend time
157163 // debugging and making the venafi connection work, and then find out
158164 // that it doesn't work. The reason is because as of now, we don't first
@@ -220,10 +226,11 @@ type testcase struct {
220226
221227// All tests share the same envtest (i.e., the same apiserver and etcd process),
222228// so each test needs to be contained in its own Kubernetes namespace.
223- func run_TestVenConnClient_PostDataReadingsWithOptions (restcfg * rest.Config , kclient ctrlruntime.WithWatch , test testcase ) func (t * testing.T ) {
229+ func run_TestVenConnClient_PostDataReadingsWithOptions (ctx context. Context , restcfg * rest.Config , kclient ctrlruntime.WithWatch , test testcase ) func (t * testing.T ) {
224230 return func (t * testing.T ) {
225231 t .Helper ()
226-
232+ log := ktesting .NewLogger (t , ktesting .NewConfig (ktesting .Verbosity (10 )))
233+ ctx := klog .NewContext (ctx , log )
227234 fakeVenafiCloud , certCloud , fakeVenafiAssert := testutil .FakeVenafiCloud (t )
228235 fakeTPP , certTPP := testutil .FakeTPP (t )
229236 fakeVenafiAssert (func (t testing.TB , r * http.Request ) {
@@ -263,18 +270,17 @@ func run_TestVenConnClient_PostDataReadingsWithOptions(restcfg *rest.Config, kcl
263270 name: ` + testNameToNamespace (t )))... )
264271 givenObjs = append (givenObjs , testutil .Parse (test .given )... )
265272 for _ , obj := range givenObjs {
266- require .NoError (t , kclient .Create (context . Background () , obj ))
273+ require .NoError (t , kclient .Create (ctx , obj ))
267274 }
268-
269- err = cl .PostDataReadingsWithOptions ([]* api.DataReading {}, client.Options {ClusterName : "test cluster name" })
275+ err = cl .PostDataReadingsWithOptions (ctx , []* api.DataReading {}, client.Options {ClusterName : "test cluster name" })
270276 if test .expectErr != "" {
271277 assert .EqualError (t , err , test .expectErr )
272278 } else {
273279 require .NoError (t , err )
274280 }
275281
276282 got := v1alpha1.VenafiConnection {}
277- err = kclient .Get (context . Background () , types.NamespacedName {Name : "venafi-components" , Namespace : testNameToNamespace (t )}, & got )
283+ err = kclient .Get (ctx , types.NamespacedName {Name : "venafi-components" , Namespace : testNameToNamespace (t )}, & got )
278284 require .NoError (t , err )
279285 require .Len (t , got .Status .Conditions , 1 )
280286 assert .Equal (t , test .expectReadyCondMsg , got .Status .Conditions [0 ].Message )
0 commit comments