|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package apiwarnings |
| 18 | + |
| 19 | +import ( |
| 20 | + "regexp" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/go-logr/logr/funcr" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | +) |
| 26 | + |
| 27 | +func TestDiscardMatchingHandler(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + code int |
| 31 | + message string |
| 32 | + expressions []regexp.Regexp |
| 33 | + wantLogged bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "log, if no expressions are defined", |
| 37 | + code: 299, |
| 38 | + message: "non-matching warning", |
| 39 | + wantLogged: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "log, if warning does not match any expression", |
| 43 | + code: 299, |
| 44 | + message: "non-matching warning", |
| 45 | + expressions: []regexp.Regexp{}, |
| 46 | + wantLogged: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "do not log, if warning matches at least one expression", |
| 50 | + code: 299, |
| 51 | + message: "matching warning", |
| 52 | + expressions: []regexp.Regexp{ |
| 53 | + *regexp.MustCompile("^matching.*"), |
| 54 | + }, |
| 55 | + wantLogged: false, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "do not log, if code is not 299", |
| 59 | + code: 0, |
| 60 | + message: "warning", |
| 61 | + expressions: []regexp.Regexp{}, |
| 62 | + wantLogged: false, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "do not log, if message is empty", |
| 66 | + code: 299, |
| 67 | + message: "", |
| 68 | + expressions: []regexp.Regexp{}, |
| 69 | + wantLogged: false, |
| 70 | + }, |
| 71 | + } |
| 72 | + for _, tt := range tests { |
| 73 | + t.Run(tt.name, func(t *testing.T) { |
| 74 | + g := NewWithT(t) |
| 75 | + logged := false |
| 76 | + h := DiscardMatchingHandler{ |
| 77 | + Logger: funcr.New(func(_, _ string) { |
| 78 | + logged = true |
| 79 | + }, |
| 80 | + funcr.Options{}, |
| 81 | + ), |
| 82 | + Expressions: tt.expressions, |
| 83 | + } |
| 84 | + h.HandleWarningHeader(tt.code, "", tt.message) |
| 85 | + g.Expect(logged).To(Equal(tt.wantLogged)) |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestDiscardMatchingHandler_uninitialized(t *testing.T) { |
| 91 | + g := NewWithT(t) |
| 92 | + h := DiscardMatchingHandler{} |
| 93 | + g.Expect(func() { |
| 94 | + // Together, the code and message value ensure that the handler logs the message. |
| 95 | + h.HandleWarningHeader(299, "", "example") |
| 96 | + }).ToNot(Panic()) |
| 97 | +} |
0 commit comments