Skip to content

Commit

Permalink
add error-flow unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Guy Daich <guy.daich@sap.com>
  • Loading branch information
guydc committed Mar 19, 2024
1 parent eda6cfb commit a57d66a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/utils/protocov/protocov.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package protocov

import (
"errors"

Check failure on line 9 in internal/utils/protocov/protocov.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/envoyproxy/gateway/ (goimports)
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions internal/xds/translator/listener_test.go
Original file line number Diff line number Diff line change
@@ -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)

Check failure on line 40 in internal/xds/translator/listener_test.go

View workflow job for this annotation

GitHub Actions / lint

error-nil: use assert.NoErrorf (testifylint)
}
})
}
}

0 comments on commit a57d66a

Please sign in to comment.