Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions felix/dataplane/external/ext_dataplane_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2025 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package extdataplane

import (
"reflect"
"testing"

"google.golang.org/protobuf/reflect/protoregistry"

"github.com/projectcalico/calico/felix/proto"
"github.com/projectcalico/calico/libcalico-go/lib/logutils"
)

func TestWrapPayloadWithEnvelopeMainline(t *testing.T) {
logutils.ConfigureLoggingForTestingT(t)
msg := &proto.ConfigUpdate{
Config: map[string]string{
"key1": "value1",
},
}
enveloped, err := WrapPayloadWithEnvelope(msg, 10)
if err != nil {
t.Fatalf("Unexpected error wrapping payload: %v", err)
}
if enveloped.SequenceNumber != 10 {
t.Errorf("Expected SeqNum 10 but got %v", enveloped.SequenceNumber)
}

out := enveloped.GetConfigUpdate()
if !reflect.DeepEqual(out, msg) {
t.Errorf("Expected %v but got %v", msg, out)
}
}

func TestAllPayloadTypes(t *testing.T) {
logutils.ConfigureLoggingForTestingT(t)
env := &proto.ToDataplane{}
payload := env.ProtoReflect().Descriptor().Oneofs().ByName("payload")
t.Log("Number of payload types:", payload.Fields().Len())
for i := 0; i < payload.Fields().Len(); i++ {
field := payload.Fields().Get(i)
msgDesc := field.Message()
msgType, err := protoregistry.GlobalTypes.FindMessageByName(msgDesc.FullName())
if err != nil {
t.Fatalf("Failed to find message type for %v: %v", msgDesc.FullName(), err)
}
msg := msgType.New().Interface()
enveloped, err := WrapPayloadWithEnvelope(msg, 42)
if err != nil {
t.Errorf("Unexpected error wrapping payload of type %v: %v", msgDesc.FullName(), err)
continue
}
if enveloped.SequenceNumber != 42 {
t.Errorf("Expected SeqNum 42 but got %v for payload type %v", enveloped.SequenceNumber, msgDesc.FullName())
}
// Use reflection to get the payload back out.
gotField := enveloped.ProtoReflect().WhichOneof(payload)
if gotField == nil {
t.Errorf("Expected payload field to be set for type %v but was nil", msgDesc.FullName())
continue
}
if gotField.FullName() != field.FullName() {
t.Errorf("Expected payload field %v but got %v", field.FullName(), gotField.FullName())
continue
}
}
}

func BenchmarkWrapPayloadWithEnvelope(b *testing.B) {
logutils.ConfigureLoggingForTestingT(b)
msg := &proto.ConfigUpdate{
Config: map[string]string{
"key1": "value1",
},
}

b.ReportAllocs()
for b.Loop() {
_, err := WrapPayloadWithEnvelope(msg, 123)
if err != nil {
b.Fatalf("Unexpected error wrapping payload: %v", err)
}
}
}
12 changes: 6 additions & 6 deletions libcalico-go/lib/logutils/logutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func SafeParseLogLevel(logLevel string) log.Level {
// for logrus. typically, it should be used via the ConfigureLoggingForTestingT
// helper.
type TestingTWriter struct {
T *testing.T
T testing.TB
}

func (l TestingTWriter) Write(p []byte) (n int, err error) {
Expand All @@ -610,9 +610,9 @@ func (l TestingTWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

// RedirectLogrusToTestingT redirects logrus output to the given testing.T. It
// RedirectLogrusToTestingT redirects logrus output to the given testing.TB (e.g., *testing.T or *testing.B). It
// returns a func() that can be called to restore the original log output.
func RedirectLogrusToTestingT(t *testing.T) (cancel func()) {
func RedirectLogrusToTestingT(t testing.TB) (cancel func()) {
oldOut := log.StandardLogger().Out
cancel = func() {
log.SetOutput(oldOut)
Expand All @@ -624,10 +624,10 @@ func RedirectLogrusToTestingT(t *testing.T) (cancel func()) {
var confForTestingOnce sync.Once

// ConfigureLoggingForTestingT configures logrus to write to the logger of the
// given testing.T. It should be called at the start of each "go test" that
// wants to capture log output. It registers a cleanup with the testing.T to
// given testing.TB (e.g., *testing.T or *testing.B). It should be called at the start of each "go test" that
// wants to capture log output. It registers a cleanup with the testing.TB to
// remove the log redirection at the end of the test.
func ConfigureLoggingForTestingT(t *testing.T) {
func ConfigureLoggingForTestingT(t testing.TB) {
confForTestingOnce.Do(func() {
log.SetFormatter(&Formatter{Component: "test"})
})
Expand Down
Loading