Skip to content

Commit

Permalink
Merge pull request openservicemesh#3535 from shashankram/protoutil
Browse files Browse the repository at this point in the history
utils/proto: add unit test and remove duplicated code
  • Loading branch information
shashankram authored Jun 7, 2021
2 parents 102419a + 141475a commit 3e62fd0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 21 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/envoyproxy/go-control-plane v0.9.9
github.com/fatih/color v1.10.0
github.com/ghodss/yaml v1.0.0
github.com/go-logr/logr v0.2.1 // indirect
github.com/golang/mock v1.4.1
github.com/golang/protobuf v1.4.3
Expand Down
23 changes: 2 additions & 21 deletions pkg/utils/proto.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package utils

import (
"github.com/ghodss/yaml"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
yaml "gopkg.in/yaml.v2"
)

// ProtoToYAML converts a Proto message to it's YAML representation in bytes
Expand All @@ -16,29 +16,10 @@ func ProtoToYAML(m protoreflect.ProtoMessage) ([]byte, error) {
return nil, err
}

configYAML, err := jsonToYAML(configJSON)
configYAML, err := yaml.JSONToYAML(configJSON)
if err != nil {
log.Error().Err(err).Msgf("Error marshaling xDS struct into YAML")
return nil, err
}
return configYAML, err
}

// jsonToYAML converts a JSON representation in bytes to the corresponding YAML representation in bytes
// Reference impl taken from https://github.com/ghodss/yaml/blob/master/yaml.go#L87
func jsonToYAML(jb []byte) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err := yaml.Unmarshal([]byte(jb), &jsonObj)
if err != nil {
return nil, err
}

// Marshal this object into YAML.
return yaml.Marshal(jsonObj)
}
98 changes: 98 additions & 0 deletions pkg/utils/proto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package utils

import (
"testing"
"time"

xds_cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
xds_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
xds_endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
xds_transport_sockets "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/wrappers"
tassert "github.com/stretchr/testify/assert"
"google.golang.org/protobuf/reflect/protoreflect"
)

func TestProtoToYAML(t *testing.T) {
assert := tassert.New(t)

testCases := []struct {
name string
proto protoreflect.ProtoMessage
expectedYAML string
}{
{
name: "XDS cluster proto",
proto: &xds_cluster.Cluster{
TransportSocketMatches: nil,
Name: "foo",
AltStatName: "foo",
ClusterDiscoveryType: &xds_cluster.Cluster_Type{Type: xds_cluster.Cluster_STATIC},
EdsClusterConfig: nil,
ConnectTimeout: ptypes.DurationProto(1 * time.Second),
LoadAssignment: &xds_endpoint.ClusterLoadAssignment{
ClusterName: "foo",
Endpoints: []*xds_endpoint.LocalityLbEndpoints{
{
Locality: nil,
LbEndpoints: []*xds_endpoint.LbEndpoint{{
HostIdentifier: &xds_endpoint.LbEndpoint_Endpoint{
Endpoint: &xds_endpoint.Endpoint{
Address: &xds_core.Address{
Address: &xds_core.Address_SocketAddress{
SocketAddress: &xds_core.SocketAddress{
Protocol: xds_core.SocketAddress_TCP,
Address: "127.0.0.1",
PortSpecifier: &xds_core.SocketAddress_PortValue{
PortValue: 80,
},
},
},
},
},
},
LoadBalancingWeight: &wrappers.UInt32Value{
Value: 100,
},
}},
},
},
},
},
expectedYAML: `alt_stat_name: foo
connect_timeout: 1s
load_assignment:
cluster_name: foo
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: 127.0.0.1
port_value: 80
load_balancing_weight: 100
name: foo
type: STATIC
`,
},
{
name: "TLS params proto",
proto: &xds_transport_sockets.TlsParameters{
TlsMinimumProtocolVersion: xds_transport_sockets.TlsParameters_TLSv1_2,
TlsMaximumProtocolVersion: xds_transport_sockets.TlsParameters_TLSv1_3,
},
expectedYAML: `tls_maximum_protocol_version: TLSv1_3
tls_minimum_protocol_version: TLSv1_2
`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual, err := ProtoToYAML(tc.proto)
assert.Nil(err)
assert.Equal(tc.expectedYAML, string(actual))
})
}
}

0 comments on commit 3e62fd0

Please sign in to comment.