forked from envoyproxy/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xds: add buildXdsCluster test (envoyproxy#1257)
* gateway: add build xds cluster test Fixes: envoyproxy#1078 Signed-off-by: Karol Szwaj <karol.szwaj@gmail.com>
- Loading branch information
1 parent
9867c6a
commit aeb2905
Showing
2 changed files
with
72 additions
and
1 deletion.
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
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 | ||
} |