Skip to content

Commit 3d75a27

Browse files
committed
Allow testing for expected log entries in testNodeSourceEndpoints
This commit adds the required logic to be able to test for the existence (and absence) of certain log messages in testNodeSourceEndpoints. As an example, this is implemented for the tests around excludeUnschedulable. A side effect of using LogsToBuffer is that tests can't run in parallel due to the log buffer being shared across all parallel test cases. As such, these specific tests are now executed one after another.
1 parent 15e2b03 commit 3d75a27

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

source/node_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package source
1818

1919
import (
2020
"context"
21+
log "github.com/sirupsen/logrus"
22+
"sigs.k8s.io/external-dns/internal/testutils"
2123
"testing"
2224

2325
"github.com/stretchr/testify/assert"
@@ -106,6 +108,8 @@ func testNodeSourceEndpoints(t *testing.T) {
106108
excludeUnschedulable bool // default to false
107109
expected []*endpoint.Endpoint
108110
expectError bool
111+
expectedLogs []string
112+
expectedAbsentLogs []string
109113
}{
110114
{
111115
title: "node with short hostname returns one endpoint",
@@ -331,6 +335,9 @@ func testNodeSourceEndpoints(t *testing.T) {
331335
unschedulable: true,
332336
excludeUnschedulable: true,
333337
expected: []*endpoint.Endpoint{},
338+
expectedLogs: []string{
339+
"Skipping node node1 because it is unschedulable",
340+
},
334341
},
335342
{
336343
title: "unschedulable node returns node with excludeUnschedulable=false",
@@ -341,11 +348,14 @@ func testNodeSourceEndpoints(t *testing.T) {
341348
expected: []*endpoint.Endpoint{
342349
{RecordType: "A", DNSName: "node1", Targets: endpoint.Targets{"1.2.3.4"}},
343350
},
351+
expectedAbsentLogs: []string{
352+
"Skipping node node1 because it is unschedulable",
353+
},
344354
},
345355
} {
346356
tc := tc
347357
t.Run(tc.title, func(t *testing.T) {
348-
t.Parallel()
358+
buf := testutils.LogsToBuffer(log.DebugLevel, t)
349359

350360
labelSelector := labels.Everything()
351361
if tc.labelSelector != "" {
@@ -394,6 +404,14 @@ func testNodeSourceEndpoints(t *testing.T) {
394404

395405
// Validate returned endpoints against desired endpoints.
396406
validateEndpoints(t, endpoints, tc.expected)
407+
408+
for _, entry := range tc.expectedLogs {
409+
assert.Contains(t, buf.String(), entry)
410+
}
411+
412+
for _, entry := range tc.expectedAbsentLogs {
413+
assert.NotContains(t, buf.String(), entry)
414+
}
397415
})
398416
}
399417
}

0 commit comments

Comments
 (0)