Skip to content

Commit

Permalink
xds: add buildXdsCluster test (envoyproxy#1257)
Browse files Browse the repository at this point in the history
* gateway: add build xds cluster test

Fixes: envoyproxy#1078

Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com>
  • Loading branch information
cnvergence authored Apr 6, 2023
1 parent 9867c6a commit aeb2905
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/xds/translator/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/envoyproxy/gateway/internal/ir"
)

const (
extensionOptionsKey = "envoy.extensions.upstreams.http.v3.HttpProtocolOptions"
)

func buildXdsCluster(routeName string, tSocket *corev3.TransportSocket, protocol ProtocolType, endpointType EndpointType) *clusterv3.Cluster {
clusterName := routeName
cluster := &clusterv3.Cluster{
Expand Down Expand Up @@ -112,7 +116,7 @@ func buildTypedExtensionProtocolOptions() map[string]*anypb.Any {
anyProtocolOptions, _ := anypb.New(&protocolOptions)

extensionOptions := map[string]*anypb.Any{
"envoy.extensions.upstreams.http.v3.HttpProtocolOptions": anyProtocolOptions,
extensionOptionsKey: anyProtocolOptions,
}

return extensionOptions
Expand Down
67 changes: 67 additions & 0 deletions internal/xds/translator/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 (
"testing"

bootstrapv3 "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v3"
clusterv3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"sigs.k8s.io/yaml"

"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/xds/bootstrap"
"github.com/envoyproxy/gateway/internal/xds/server/runner"
)

const (
envoyGatewayXdsServerHost = "envoy-gateway"
xdsClusterName = "xds_cluster"
)

func TestBuildXdsCluster(t *testing.T) {
bootstrapXdsCluster := getXdsClusterObjFromBootstrap(t)

dynamicXdsCluster := buildXdsCluster(bootstrapXdsCluster.Name, bootstrapXdsCluster.TransportSocket, HTTP2, DefaultEndpointType)

require.Equal(t, bootstrapXdsCluster.Name, dynamicXdsCluster.Name)
require.Equal(t, bootstrapXdsCluster.ClusterDiscoveryType, dynamicXdsCluster.ClusterDiscoveryType)
require.Equal(t, bootstrapXdsCluster.TransportSocket, dynamicXdsCluster.TransportSocket)
assert.True(t, proto.Equal(bootstrapXdsCluster.TransportSocket, dynamicXdsCluster.TransportSocket))
assert.True(t, proto.Equal(bootstrapXdsCluster.ConnectTimeout, dynamicXdsCluster.ConnectTimeout))
assert.True(t, proto.Equal(bootstrapXdsCluster.TypedExtensionProtocolOptions[extensionOptionsKey], dynamicXdsCluster.TypedExtensionProtocolOptions[extensionOptionsKey]))
}

func TestBuildXdsClusterLoadAssignment(t *testing.T) {
bootstrapXdsCluster := getXdsClusterObjFromBootstrap(t)
destinations := []*ir.RouteDestination{{Host: envoyGatewayXdsServerHost, Port: runner.XdsServerPort}}

dynamicXdsClusterLoadAssignment := buildXdsClusterLoadAssignment(bootstrapXdsCluster.Name, destinations)

assert.True(t, proto.Equal(bootstrapXdsCluster.LoadAssignment.Endpoints[0].LbEndpoints[0], dynamicXdsClusterLoadAssignment.Endpoints[0].LbEndpoints[0]))
}

func getXdsClusterObjFromBootstrap(t *testing.T) *clusterv3.Cluster {
bootstrapObj := &bootstrapv3.Bootstrap{}
bootstrapStr, err := bootstrap.GetRenderedBootstrapConfig()
require.NoError(t, err)
jsonData, err := yaml.YAMLToJSON([]byte(bootstrapStr))
require.NoError(t, err)
err = protojson.Unmarshal(jsonData, bootstrapObj)
require.NoError(t, err)

for _, cluster := range bootstrapObj.StaticResources.Clusters {
if cluster.Name == xdsClusterName {
return cluster
}
}

return nil
}

0 comments on commit aeb2905

Please sign in to comment.