• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / external-dns / 14923095442

09 May 2025 06:43AM UTC coverage: 72.681% (+0.2%) from 72.504%
14923095442

Pull #5368

github

ivankatliarchuk
fix(log testing): re-use logger library testing functionality

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
Pull Request #5368: fix(log testing): re-use logger library testing functionality

0 of 31 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

14877 of 20469 relevant lines covered (72.68%)

690.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/internal/testutils/log.go
1
/*
2
Copyright 2025 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 testutils
18

19
import (
20
        "strings"
21
        "testing"
22

23
        log "github.com/sirupsen/logrus"
24
        "github.com/stretchr/testify/assert"
25

26
        "github.com/sirupsen/logrus/hooks/test"
27
)
28

29
// LogsUnderTestWithLogLevel redirects log(s) output to a buffer for testing purposes
30
//
31
// Usage: LogsUnderTestWithLogLevel(t)
32
// Example:
33
//
34
//        hook := testutils.LogsUnderTestWithLogLevel(log.DebugLevel, t)
35
//        ... do something that logs ...
36
//
37
// testutils.TestHelperLogContains("expected debug log message", hook, t)
NEW
38
func LogsUnderTestWithLogLevel(level log.Level, t *testing.T) *test.Hook {
×
39
        t.Helper()
×
NEW
40
        logger, hook := test.NewNullLogger()
×
NEW
41

×
NEW
42
        log.AddHook(hook)
×
NEW
43
        log.SetOutput(logger.Out)
×
44
        log.SetLevel(level)
×
NEW
45
        return hook
×
NEW
46
}
×
47

48
// TestHelperLogContains verifies that a specific log message is present
49
// in the captured log entries. It asserts that the provided message `msg`
50
// appears in at least one of the log entries recorded by the `hook`.
51
//
52
// Parameters:
53
// - msg: The log message that should be found.
54
// - hook: The test hook capturing log entries.
55
// - t: The testing object used for assertions.
56
//
57
// Usage:
58
// This helper is used in tests to ensure that certain log messages are
59
// logged during the execution of the code under test.
NEW
60
func TestHelperLogContains(msg string, hook *test.Hook, t *testing.T) {
×
NEW
61
        t.Helper()
×
NEW
62
        isContains := false
×
NEW
63
        for _, entry := range hook.AllEntries() {
×
NEW
64
                if strings.Contains(entry.Message, msg) {
×
NEW
65
                        isContains = true
×
NEW
66
                }
×
67
        }
NEW
68
        assert.True(t, isContains, "Expected log message not found: %s", msg)
×
69
}
70

71
// TestHelperLogNotContains verifies that a specific log message is not present
72
// in the captured log entries. It asserts that the provided message `msg`
73
// does not appear in any of the log entries recorded by the `hook`.
74
//
75
// Parameters:
76
// - msg: The log message that should not be found.
77
// - hook: The test hook capturing log entries.
78
// - t: The testing object used for assertions.
79
//
80
// Usage:
81
// This helper is used in tests to ensure that certain log messages are not
82
// logged during the execution of the code under test.
NEW
83
func TestHelperLogNotContains(msg string, hook *test.Hook, t *testing.T) {
×
NEW
84
        t.Helper()
×
NEW
85
        isContains := false
×
NEW
86
        for _, entry := range hook.AllEntries() {
×
NEW
87
                if strings.Contains(entry.Message, msg) {
×
NEW
88
                        isContains = true
×
NEW
89
                }
×
90
        }
NEW
91
        assert.False(t, isContains, "Expected log message found when should not: %s", msg)
×
92
}
93

94
// TestHelperLogContainsWithLogLevel verifies that a specific log message with a given log level
95
// is present in the captured log entries. It asserts that the provided message `msg`
96
// appears in at least one of the log entries recorded by the `hook` with the specified log level.
97
//
98
// Parameters:
99
// - msg: The log message that should be found.
100
// - level: The log level that the message should have.
101
// - hook: The test hook capturing log entries.
102
// - t: The testing object used for assertions.
103
//
104
// Usage:
105
// This helper is used in tests to ensure that certain log messages with a specific log level
106
// are logged during the execution of the code under test.
NEW
107
func TestHelperLogContainsWithLogLevel(msg string, level log.Level, hook *test.Hook, t *testing.T) {
×
NEW
108
        t.Helper()
×
NEW
109
        isContains := false
×
NEW
110
        for _, entry := range hook.AllEntries() {
×
NEW
111
                if strings.Contains(entry.Message, msg) && entry.Level == level {
×
NEW
112
                        isContains = true
×
NEW
113
                }
×
114
        }
NEW
115
        assert.True(t, isContains, "Expected log message not found: %s with level %s", msg, level)
×
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc