@@ -17,11 +17,13 @@ limitations under the License.
1717package client_test
1818
1919import (
20+ "bufio"
2021 "context"
2122 "encoding/json"
2223 "errors"
2324 "fmt"
2425 "reflect"
26+ "strings"
2527 "sync/atomic"
2628 "time"
2729
@@ -226,6 +228,90 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
226228 Expect (client .IgnoreNotFound (err )).NotTo (HaveOccurred ())
227229 })
228230
231+ Describe ("WarningHandler" , func () {
232+ It ("should log warnings when warning suppression is disabled" , func () {
233+ 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+ })
237+ Expect (err ).NotTo (HaveOccurred ())
238+ Expect (cl ).NotTo (BeNil ())
239+
240+ tns := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "ws-disabled" }}
241+ tns , err = clientset .CoreV1 ().Namespaces ().Create (ctx , tns , metav1.CreateOptions {})
242+ Expect (err ).NotTo (HaveOccurred ())
243+ Expect (tns ).NotTo (BeNil ())
244+ defer deleteNamespace (ctx , tns )
245+
246+ toCreate := & pkg.ChaosPod {
247+ ObjectMeta : metav1.ObjectMeta {
248+ Name : "example" ,
249+ Namespace : tns .Name ,
250+ },
251+ // The ChaosPod CRD does not define Status, so the field is unknown to the API server,
252+ // but field validation is not strict by default, so the API server returns a warning,
253+ // and we need a warning to check whether suppression works.
254+ Status : pkg.ChaosPodStatus {},
255+ }
256+ err = cl .Create (ctx , toCreate )
257+ Expect (err ).NotTo (HaveOccurred ())
258+ Expect (cl ).NotTo (BeNil ())
259+
260+ scanner := bufio .NewScanner (& log )
261+ for scanner .Scan () {
262+ line := scanner .Text ()
263+ if strings .Contains (
264+ line ,
265+ "unknown field \" status\" " ,
266+ ) {
267+ return
268+ }
269+ }
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 ())
299+
300+ scanner := bufio .NewScanner (& log )
301+ for scanner .Scan () {
302+ line := scanner .Text ()
303+ if strings .Contains (
304+ line ,
305+ "unknown field \" status\" " ,
306+ ) {
307+ defer Fail ("expected to find zero API server warnings in the client log" )
308+ break
309+ }
310+ }
311+ deleteNamespace (ctx , tns )
312+ })
313+ })
314+
229315 Describe ("New" , func () {
230316 It ("should return a new Client" , func () {
231317 cl , err := client .New (cfg , client.Options {})
0 commit comments