This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils/proto: add unit test and remove duplicated code
Adds a unit test for ProtoToYAML and directly leverages the yaml pkg to perform JSON -> YAML conversions. Signed-off-by: Shashank Ram <[email protected]>
- Loading branch information
1 parent
102419a
commit 141475a
Showing
3 changed files
with
101 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} | ||
} |