From a57d66abdb57c6b67409072a9dda68ebf0240a20 Mon Sep 17 00:00:00 2001 From: Guy Daich Date: Tue, 19 Mar 2024 13:09:32 -0500 Subject: [PATCH] add error-flow unit test Signed-off-by: Guy Daich --- internal/utils/protocov/protocov.go | 4 +++ internal/xds/translator/listener_test.go | 44 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 internal/xds/translator/listener_test.go diff --git a/internal/utils/protocov/protocov.go b/internal/utils/protocov/protocov.go index e8a59f38410..221b346e2f0 100644 --- a/internal/utils/protocov/protocov.go +++ b/internal/utils/protocov/protocov.go @@ -6,6 +6,7 @@ package protocov import ( + "errors" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" ) @@ -19,6 +20,9 @@ var ( ) func ToAnyWithError(msg proto.Message) (*anypb.Any, error) { + if msg == nil { + return nil, errors.New("empty message received") + } b, err := marshalOpts.Marshal(msg) if err != nil { return nil, err diff --git a/internal/xds/translator/listener_test.go b/internal/xds/translator/listener_test.go new file mode 100644 index 00000000000..fcdebe13d8a --- /dev/null +++ b/internal/xds/translator/listener_test.go @@ -0,0 +1,44 @@ +// Copyright Envoy Gateway Authors +// SPDX-License-Identifier: Apache-2.0 +// The full text of the Apache license is available in the LICENSE file at +// the root of the repo. + +package translator + +import ( + "errors" + "testing" + + hcmv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3" + "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/proto" +) + +func Test_toNetworkFilter(t *testing.T) { + tests := []struct { + name string + proto proto.Message + wantErr error + }{ + { + name: "valid filter", + proto: &hcmv3.HttpConnectionManager{}, + wantErr: nil, + }, + { + name: "invalid proto msg", + proto: nil, + wantErr: errors.New("empty message received"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := toNetworkFilter("name", tt.proto) + if tt.wantErr != nil { + assert.Equalf(t, tt.wantErr, err, "toNetworkFilter(%v)", tt.proto) + } else { + assert.Equalf(t, nil, err, "toNetworkFilter(%v)", tt.proto) + } + }) + } +}