2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2023-02-14 22:22:09 +00:00
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2023-08-01 20:49:39 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-02-14 22:22:09 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
libassert "github.com/hashicorp/consul/test/integration/consul-container/libs/assert"
|
|
|
|
libcluster "github.com/hashicorp/consul/test/integration/consul-container/libs/cluster"
|
|
|
|
libservice "github.com/hashicorp/consul/test/integration/consul-container/libs/service"
|
|
|
|
)
|
|
|
|
|
2023-07-21 01:30:22 +00:00
|
|
|
// CreateServices
|
|
|
|
func CreateServices(t *testing.T, cluster *libcluster.Cluster, protocol string) (libservice.Service, libservice.Service) {
|
2023-02-14 22:22:09 +00:00
|
|
|
node := cluster.Agents[0]
|
|
|
|
client := node.GetClient()
|
|
|
|
|
|
|
|
// Register service as HTTP
|
|
|
|
serviceDefault := &api.ServiceConfigEntry{
|
|
|
|
Kind: api.ServiceDefaults,
|
|
|
|
Name: libservice.StaticServerServiceName,
|
2023-07-21 01:30:22 +00:00
|
|
|
Protocol: protocol,
|
2023-02-14 22:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ok, _, err := client.ConfigEntries().Set(serviceDefault, nil)
|
|
|
|
require.NoError(t, err, "error writing HTTP service-default")
|
|
|
|
require.True(t, ok, "did not write HTTP service-default")
|
|
|
|
|
|
|
|
// Create a service and proxy instance
|
|
|
|
serviceOpts := &libservice.ServiceOpts{
|
|
|
|
Name: libservice.StaticServerServiceName,
|
|
|
|
ID: "static-server",
|
|
|
|
HTTPPort: 8080,
|
|
|
|
GRPCPort: 8079,
|
|
|
|
}
|
|
|
|
|
2023-07-21 01:30:22 +00:00
|
|
|
if protocol == "grpc" {
|
|
|
|
serviceOpts.RegisterGRPC = true
|
|
|
|
}
|
|
|
|
|
2023-02-14 22:22:09 +00:00
|
|
|
// Create a service and proxy instance
|
|
|
|
_, serverConnectProxy, err := libservice.CreateAndRegisterStaticServerAndSidecar(node, serviceOpts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-02 22:40:07 +00:00
|
|
|
libassert.CatalogServiceExists(t, client, fmt.Sprintf("%s-sidecar-proxy", libservice.StaticServerServiceName), nil)
|
|
|
|
libassert.CatalogServiceExists(t, client, libservice.StaticServerServiceName, nil)
|
2023-02-14 22:22:09 +00:00
|
|
|
|
|
|
|
// Create a client proxy instance with the server as an upstream
|
2023-04-26 16:49:38 +00:00
|
|
|
clientConnectProxy, err := libservice.CreateAndRegisterStaticClientSidecar(node, "", false, false)
|
2023-02-14 22:22:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-02 22:40:07 +00:00
|
|
|
libassert.CatalogServiceExists(t, client, fmt.Sprintf("%s-sidecar-proxy", libservice.StaticClientServiceName), nil)
|
2023-02-14 22:22:09 +00:00
|
|
|
|
|
|
|
return serverConnectProxy, clientConnectProxy
|
|
|
|
}
|