@@ -18,6 +18,7 @@ package client_test
1818
1919import (
2020 "bufio"
21+ "bytes"
2122 "context"
2223 "encoding/json"
2324 "errors"
@@ -43,6 +44,7 @@ import (
4344 "k8s.io/apimachinery/pkg/runtime/schema"
4445 "k8s.io/apimachinery/pkg/types"
4546 kscheme "k8s.io/client-go/kubernetes/scheme"
47+ "k8s.io/client-go/rest"
4648 "k8s.io/utils/ptr"
4749
4850 "sigs.k8s.io/controller-runtime/examples/crd/pkg"
@@ -229,15 +231,19 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
229231 })
230232
231233 Describe ("WarningHandler" , func () {
232- It ("should log warnings when warning suppression is disabled " , func () {
234+ It ("should log warnings with config.WarningHandler, if one is defined " , func () {
233235 cache := & fakeReader {}
234- cl , err := client .New (cfg , client.Options {
235- WarningHandler : client.WarningHandlerOptions {SuppressWarnings : false }, Cache : & client.CacheOptions {Reader : cache , DisableFor : []client.Object {& corev1.Namespace {}}},
236- })
236+
237+ testCfg := rest .CopyConfig (cfg )
238+
239+ var testLog bytes.Buffer
240+ testCfg .WarningHandler = rest .NewWarningWriter (& testLog , rest.WarningWriterOptions {})
241+
242+ cl , err := client .New (testCfg , client.Options {Cache : & client.CacheOptions {Reader : cache , DisableFor : []client.Object {& corev1.Namespace {}}}})
237243 Expect (err ).NotTo (HaveOccurred ())
238244 Expect (cl ).NotTo (BeNil ())
239245
240- tns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "ws-disabled " }}
246+ tns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "wh-defined " }}
241247 tns , err = clientset .CoreV1 ().Namespaces ().Create (ctx , tns , metav1.CreateOptions {})
242248 Expect (err ).NotTo (HaveOccurred ())
243249 Expect (tns ).NotTo (BeNil ())
@@ -257,45 +263,17 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
257263 Expect (err ).NotTo (HaveOccurred ())
258264 Expect (cl ).NotTo (BeNil ())
259265
260- scanner := bufio .NewScanner (& log )
261- for scanner .Scan () {
262- line := scanner .Text ()
266+ scannerTestLog := bufio .NewScanner (& testLog )
267+ for scannerTestLog .Scan () {
268+ line := scannerTestLog .Text ()
263269 if strings .Contains (
264270 line ,
265271 "unknown field \" status\" " ,
266272 ) {
267273 return
268274 }
269275 }
270- defer Fail ("expected to find one API server warning in the client log" )
271- })
272-
273- It ("should not log warnings when warning suppression is enabled" , func () {
274- cache := & fakeReader {}
275- cl , err := client .New (cfg , client.Options {
276- WarningHandler : client.WarningHandlerOptions {SuppressWarnings : true }, Cache : & client.CacheOptions {Reader : cache , DisableFor : []client.Object {& corev1.Namespace {}}},
277- })
278- Expect (err ).NotTo (HaveOccurred ())
279- Expect (cl ).NotTo (BeNil ())
280-
281- tns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "ws-enabled" }}
282- tns , err = clientset .CoreV1 ().Namespaces ().Create (ctx , tns , metav1.CreateOptions {})
283- Expect (err ).NotTo (HaveOccurred ())
284- Expect (tns ).NotTo (BeNil ())
285-
286- toCreate := & pkg.ChaosPod {
287- ObjectMeta : metav1.ObjectMeta {
288- Name : "example" ,
289- Namespace : tns .Name ,
290- },
291- // The ChaosPod CRD does not define Status, so the field is unknown to the API server,
292- // but field validation is not strict by default, so the API server returns a warning,
293- // and we need a warning to check whether suppression works.
294- Status : pkg.ChaosPodStatus {},
295- }
296- err = cl .Create (ctx , toCreate )
297- Expect (err ).NotTo (HaveOccurred ())
298- Expect (cl ).NotTo (BeNil ())
276+ defer Fail ("expected to find one API server warning logged the config.WarningHandler" )
299277
300278 scanner := bufio .NewScanner (& log )
301279 for scanner .Scan () {
@@ -308,7 +286,6 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
308286 break
309287 }
310288 }
311- deleteNamespace (ctx , tns )
312289 })
313290 })
314291
0 commit comments