-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add integration tests for troubleshoot (#16223)
* draft * expose internal admin port and add proxy test * update tests * move comment * add failure case, fix lint issues * cleanup * handle error * revert changes to service interface * address review comments * fix merge conflict * merge the tests so cluster is created once * fix other test
- Loading branch information
malizz
authored
Feb 14, 2023
1 parent
8fbd15a
commit 247211d
Showing
8 changed files
with
196 additions
and
61 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
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
51 changes: 51 additions & 0 deletions
51
test/integration/consul-container/libs/topology/service_topology.go
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,51 @@ | ||
package topology | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"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/stretchr/testify/require" | ||
) | ||
|
||
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 | ||
} |
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
68 changes: 68 additions & 0 deletions
68
test/integration/consul-container/test/troubleshoot/troubleshoot_test.go
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,68 @@ | ||
package troubleshoot | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
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" | ||
) | ||
|
||
func TestTroubleshootProxy(t *testing.T) { | ||
t.Parallel() | ||
cluster, _, _ := topology.NewPeeringCluster(t, 1, &libcluster.BuildOptions{ | ||
Datacenter: "dc1", | ||
InjectAutoEncryption: true, | ||
}) | ||
|
||
serverService, clientService := topology.CreateServices(t, cluster) | ||
|
||
clientSidecar, ok := clientService.(*libservice.ConnectContainer) | ||
require.True(t, ok) | ||
_, clientAdminPort := clientSidecar.GetInternalAdminAddr() | ||
|
||
t.Run("upstream exists and is healthy", func(t *testing.T) { | ||
require.Eventually(t, func() bool { | ||
output, err := clientSidecar.Exec(context.Background(), | ||
[]string{"consul", "troubleshoot", "upstreams", | ||
"-envoy-admin-endpoint", fmt.Sprintf("localhost:%v", clientAdminPort)}) | ||
require.NoError(t, err) | ||
upstreamExists := assert.Contains(t, output, libservice.StaticServerServiceName) | ||
|
||
output, err = clientSidecar.Exec(context.Background(), []string{"consul", "troubleshoot", "proxy", | ||
"-envoy-admin-endpoint", fmt.Sprintf("localhost:%v", clientAdminPort), | ||
"-upstream-envoy-id", libservice.StaticServerServiceName}) | ||
require.NoError(t, err) | ||
certsValid := strings.Contains(output, "certificates are valid") | ||
listenersExist := strings.Contains(output, fmt.Sprintf("listener for upstream \"%s\" found", libservice.StaticServerServiceName)) | ||
routesExist := strings.Contains(output, fmt.Sprintf("route for upstream \"%s\" found", libservice.StaticServerServiceName)) | ||
healthyEndpoints := strings.Contains(output, "✓ healthy endpoints for cluster") | ||
return upstreamExists && certsValid && listenersExist && routesExist && healthyEndpoints | ||
}, 60*time.Second, 10*time.Second) | ||
}) | ||
|
||
t.Run("terminate upstream and check if client sees it as unhealthy", func(t *testing.T) { | ||
err := serverService.Terminate() | ||
require.NoError(t, err) | ||
|
||
require.Eventually(t, func() bool { | ||
output, err := clientSidecar.Exec(context.Background(), []string{"consul", "troubleshoot", "proxy", | ||
"-envoy-admin-endpoint", fmt.Sprintf("localhost:%v", clientAdminPort), | ||
"-upstream-envoy-id", libservice.StaticServerServiceName}) | ||
require.NoError(t, err) | ||
|
||
certsValid := strings.Contains(output, "certificates are valid") | ||
listenersExist := strings.Contains(output, fmt.Sprintf("listener for upstream \"%s\" found", libservice.StaticServerServiceName)) | ||
routesExist := strings.Contains(output, fmt.Sprintf("route for upstream \"%s\" found", libservice.StaticServerServiceName)) | ||
endpointUnhealthy := strings.Contains(output, "no healthy endpoints for cluster") | ||
return certsValid && listenersExist && routesExist && endpointUnhealthy | ||
}, 60*time.Second, 10*time.Second) | ||
}) | ||
} |