|
5 | 5 | package logging_test |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "fmt" |
8 | 9 | "reflect" |
9 | 10 | "testing" |
| 11 | + "time" |
10 | 12 |
|
11 | 13 | "github.com/go-logr/logr" |
12 | 14 | . "github.com/onsi/ginkgo/v2" |
13 | 15 | . "github.com/onsi/gomega" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
14 | 17 |
|
| 18 | + "github.com/openmcp-project/controller-utils/pkg/collections" |
15 | 19 | "github.com/openmcp-project/controller-utils/pkg/logging" |
16 | 20 | ) |
17 | 21 |
|
@@ -43,4 +47,117 @@ var _ = Describe("Logging Framework Tests", func() { |
43 | 47 | Expect(reflect.DeepEqual(log, compareToLogger)).To(BeTrue(), "calling log.WithValues should not modify the logger") |
44 | 48 | }) |
45 | 49 |
|
| 50 | + Context("LogRequeue", func() { |
| 51 | + |
| 52 | + var log logging.Logger |
| 53 | + var sink *TestLogSink |
| 54 | + |
| 55 | + BeforeEach(func() { |
| 56 | + sink = NewTestLogSink(logging.DEBUG) |
| 57 | + log = logging.Wrap(logr.New(sink)) |
| 58 | + }) |
| 59 | + |
| 60 | + It("should not log anything if RequeueAfter is 0", func() { |
| 61 | + log.LogRequeue(reconcile.Result{}) |
| 62 | + Expect(sink.Messages.Size()).To(Equal(0)) |
| 63 | + }) |
| 64 | + |
| 65 | + It("should log a message if RequeueAfter is set", func() { |
| 66 | + now := time.Now() |
| 67 | + requeueAfter := 42 * time.Second |
| 68 | + log.LogRequeue(reconcile.Result{RequeueAfter: requeueAfter}) |
| 69 | + Expect(sink.Messages.Size()).To(Equal(1)) |
| 70 | + msg := sink.Messages.Poll() |
| 71 | + Expect(msg.Verbosity).To(Equal(logging.LevelToVerbosity(logging.DEBUG))) |
| 72 | + Expect(msg.Message).To(Equal("Requeuing object for reconciliation")) |
| 73 | + Expect(msg.KeysAndVals).To(HaveKeyWithValue("after", requeueAfter.String())) |
| 74 | + Expect(msg.KeysAndVals).To(HaveKey("at")) |
| 75 | + at, err := time.Parse(time.RFC3339, msg.KeysAndVals["at"].(string)) |
| 76 | + Expect(err).NotTo(HaveOccurred()) |
| 77 | + Expect(at).To(BeTemporally("~", now.Add(requeueAfter), time.Second)) |
| 78 | + }) |
| 79 | + |
| 80 | + It("should log at the provided verbosity level", func() { |
| 81 | + now := time.Now() |
| 82 | + requeueAfter := 42 * time.Second |
| 83 | + log.LogRequeue(reconcile.Result{RequeueAfter: requeueAfter}, logging.INFO) |
| 84 | + Expect(sink.Messages.Size()).To(Equal(1)) |
| 85 | + msg := sink.Messages.Poll() |
| 86 | + Expect(msg.Verbosity).To(Equal(logging.LevelToVerbosity(logging.INFO))) |
| 87 | + Expect(msg.Message).To(Equal("Requeuing object for reconciliation")) |
| 88 | + Expect(msg.KeysAndVals).To(HaveKeyWithValue("after", requeueAfter.String())) |
| 89 | + Expect(msg.KeysAndVals).To(HaveKey("at")) |
| 90 | + at, err := time.Parse(time.RFC3339, msg.KeysAndVals["at"].(string)) |
| 91 | + Expect(err).NotTo(HaveOccurred()) |
| 92 | + Expect(at).To(BeTemporally("~", now.Add(requeueAfter), time.Second)) |
| 93 | + }) |
| 94 | + |
| 95 | + }) |
| 96 | + |
46 | 97 | }) |
| 98 | + |
| 99 | +func NewTestLogSink(level logging.LogLevel) *TestLogSink { |
| 100 | + return &TestLogSink{ |
| 101 | + Messages: collections.NewLinkedList[*LogMessage](), |
| 102 | + enabledLevel: level, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +type TestLogSink struct { |
| 107 | + Messages collections.Queue[*LogMessage] |
| 108 | + enabledLevel logging.LogLevel |
| 109 | +} |
| 110 | + |
| 111 | +type LogMessage struct { |
| 112 | + Error error |
| 113 | + Verbosity int |
| 114 | + Message string |
| 115 | + KeysAndVals map[string]any |
| 116 | +} |
| 117 | + |
| 118 | +// Enabled implements logr.LogSink. |
| 119 | +func (t *TestLogSink) Enabled(level int) bool { |
| 120 | + return t.enabledLevel >= logging.LogLevel(level) |
| 121 | +} |
| 122 | + |
| 123 | +// Error implements logr.LogSink. |
| 124 | +func (t *TestLogSink) Error(err error, msg string, keysAndValues ...any) { |
| 125 | + t.log(int(logging.ERROR), err, msg, keysAndValues...) |
| 126 | +} |
| 127 | + |
| 128 | +// Info implements logr.LogSink. |
| 129 | +func (t *TestLogSink) Info(level int, msg string, keysAndValues ...any) { |
| 130 | + t.log(level, nil, msg, keysAndValues...) |
| 131 | +} |
| 132 | + |
| 133 | +func (t *TestLogSink) log(verbosity int, err error, msg string, keysAndValues ...any) { |
| 134 | + kv := make(map[string]any, (len(keysAndValues)+1)/2) |
| 135 | + for i := 0; i < len(keysAndValues)-1; i += 2 { |
| 136 | + k, ok := keysAndValues[i].(string) |
| 137 | + if !ok { |
| 138 | + k = fmt.Sprint(keysAndValues[i]) |
| 139 | + } |
| 140 | + kv[k] = keysAndValues[i+1] |
| 141 | + } |
| 142 | + _ = t.Messages.Push(&LogMessage{ |
| 143 | + Error: err, |
| 144 | + Verbosity: verbosity, |
| 145 | + Message: msg, |
| 146 | + KeysAndVals: kv, |
| 147 | + }) |
| 148 | +} |
| 149 | + |
| 150 | +// Init implements logr.LogSink. |
| 151 | +func (t *TestLogSink) Init(_ logr.RuntimeInfo) {} |
| 152 | + |
| 153 | +// WithName implements logr.LogSink. |
| 154 | +func (t *TestLogSink) WithName(name string) logr.LogSink { |
| 155 | + panic("not implemented") |
| 156 | +} |
| 157 | + |
| 158 | +// WithValues implements logr.LogSink. |
| 159 | +func (t *TestLogSink) WithValues(keysAndValues ...any) logr.LogSink { |
| 160 | + panic("not implemented") |
| 161 | +} |
| 162 | + |
| 163 | +var _ logr.LogSink = &TestLogSink{} |
0 commit comments