Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of add integration tests for troubleshoot into release/1.15.x #16261

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/integration/consul-container/libs/service/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type ConnectContainer struct {

var _ Service = (*ConnectContainer)(nil)

func (g ConnectContainer) Exec(ctx context.Context, cmd []string) (int, io.Reader, error) {
return g.container.Exec(ctx, cmd)
}

func (g ConnectContainer) Export(partition, peer string, client *api.Client) error {
return fmt.Errorf("ConnectContainer export unimplemented")
}
Expand Down
4 changes: 4 additions & 0 deletions test/integration/consul-container/libs/service/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type exampleContainer struct {

var _ Service = (*exampleContainer)(nil)

func (g exampleContainer) Exec(ctx context.Context, cmd []string) (int, io.Reader, error) {
return g.container.Exec(ctx, cmd)
}

func (g exampleContainer) Export(partition, peerName string, client *api.Client) error {
config := &api.ExportedServicesConfigEntry{
Name: partition,
Expand Down
4 changes: 4 additions & 0 deletions test/integration/consul-container/libs/service/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type gatewayContainer struct {

var _ Service = (*gatewayContainer)(nil)

func (g gatewayContainer) Exec(ctx context.Context, cmd []string) (int, io.Reader, error) {
return g.container.Exec(ctx, cmd)
}

func (g gatewayContainer) Export(partition, peer string, client *api.Client) error {
return fmt.Errorf("gatewayContainer export unimplemented")
}
Expand Down
7 changes: 6 additions & 1 deletion test/integration/consul-container/libs/service/service.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package service

import "github.com/hashicorp/consul/api"
import (
"context"
"github.com/hashicorp/consul/api"
"io"
)

// Service represents a process that will be registered with the
// Consul catalog, including Consul components such as sidecars and gateways
type Service interface {
// Export a service to the peering cluster
Exec(ctx context.Context, cmd []string) (int, io.Reader, error)
Export(partition, peer string, client *api.Client) error
GetAddr() (string, int)
GetAdminAddr() (string, int)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package troubleshoot

import (
"context"
"fmt"
"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"
"github.com/hashicorp/consul/test/integration/consul-container/libs/topology"
"github.com/stretchr/testify/require"
"io"
"testing"
)

func TestTroubleshootUpstream(t *testing.T) {
t.Parallel()

cluster, _, _ := topology.NewPeeringCluster(t, 1, &libcluster.BuildOptions{
Datacenter: "dc1",
InjectAutoEncryption: true,
})

_, clientService := createServices(t, cluster)

clientSidecar, ok := clientService.(*libservice.ConnectContainer)
require.True(t, ok)
ip, port := clientSidecar.GetAdminAddr()
_, outputReader, err := clientSidecar.Exec(context.Background(), []string{"consul", "troubleshoot", "upstreams", "-envoy-admin-endpoint", fmt.Sprintf("%s:%v", ip, port)})
buf, err := io.ReadAll(outputReader)
require.NoError(t, err)
require.Contains(t, string(buf), libservice.StaticServerServiceName)
}

func createServices(t *testing.T, cluster *libcluster.Cluster) (libservice.Service, libservice.Service) {
node := cluster.Agents[0]
client := node.GetClient()

// Register service as HTTP
serviceDefault := &api.ServiceConfigEntry{
Kind: api.ServiceDefaults,
Name: libservice.StaticServerServiceName,
Protocol: "http",
}

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,
}

// Create a service and proxy instance
_, serverConnectProxy, err := libservice.CreateAndRegisterStaticServerAndSidecar(node, serviceOpts)
require.NoError(t, err)

libassert.CatalogServiceExists(t, client, fmt.Sprintf("%s-sidecar-proxy", libservice.StaticServerServiceName))
libassert.CatalogServiceExists(t, client, libservice.StaticServerServiceName)

// Create a client proxy instance with the server as an upstream
clientConnectProxy, err := libservice.CreateAndRegisterStaticClientSidecar(node, "", false)
require.NoError(t, err)

libassert.CatalogServiceExists(t, client, fmt.Sprintf("%s-sidecar-proxy", libservice.StaticClientServiceName))

return serverConnectProxy, clientConnectProxy
}