Skip to content

Commit a7baa23

Browse files
committed
Verify that client respects option to suppress warnings
1 parent b09cb19 commit a7baa23

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

pkg/client/client_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ limitations under the License.
1717
package client_test
1818

1919
import (
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+
245+
toCreate := &pkg.ChaosPod{
246+
ObjectMeta: metav1.ObjectMeta{
247+
Name: "example",
248+
Namespace: tns.Name,
249+
},
250+
// The ChaosPod CRD does not define Status, so the field is unknown to the API server,
251+
// but field validation is not strict by default, so the API server returns a warning,
252+
// and we need a warning to check whether suppression works.
253+
Status: pkg.ChaosPodStatus{},
254+
}
255+
err = cl.Create(ctx, toCreate)
256+
Expect(err).NotTo(HaveOccurred())
257+
Expect(cl).NotTo(BeNil())
258+
259+
scanner := bufio.NewScanner(&log)
260+
for scanner.Scan() {
261+
line := scanner.Text()
262+
if strings.Contains(
263+
line,
264+
"unknown field \"status\"",
265+
) {
266+
return
267+
}
268+
}
269+
defer Fail("expected to find one API server warning in the client log")
270+
deleteNamespace(ctx, tns)
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

Comments
 (0)