From cd203bfe9ef085f6bb2bcf544f7089844557fbd0 Mon Sep 17 00:00:00 2001 From: Ganeshrockz Date: Wed, 31 Jan 2024 16:42:15 +0530 Subject: [PATCH] Add scenario test for TGW with TProxy --- .../nightly-ecs-examples-validator.yml | 4 + test/acceptance/examples/main_test.go | 2 + .../terminating-gateway-tproxy/main.go | 93 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 test/acceptance/examples/scenarios/terminating-gateway-tproxy/main.go diff --git a/.github/workflows/nightly-ecs-examples-validator.yml b/.github/workflows/nightly-ecs-examples-validator.yml index 10ba8636..57cd76a2 100644 --- a/.github/workflows/nightly-ecs-examples-validator.yml +++ b/.github/workflows/nightly-ecs-examples-validator.yml @@ -56,12 +56,16 @@ jobs: name: - API Gateway - Terminating Gateway + - Terminating Gateway Transparent Proxy include: - name: API Gateway scenario: API_GATEWAY - name: Terminating Gateway scenario: TERMINATING_GATEWAY + + - name: Terminating Gateway Transparent Proxy + scenario: TERMINATING_GATEWAY_TPROXY fail-fast: false uses: ./.github/workflows/reusable-ecs-example-validator.yml with: diff --git a/test/acceptance/examples/main_test.go b/test/acceptance/examples/main_test.go index 2603161f..864e0de6 100644 --- a/test/acceptance/examples/main_test.go +++ b/test/acceptance/examples/main_test.go @@ -23,6 +23,7 @@ import ( localityawarerouting "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/locality-aware-routing" sameness "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/service-sameness" terminatinggateway "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/terminating-gateway" + terminatinggatewaytproxy "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/terminating-gateway-tproxy" "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/wan-federation" "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/framework/logger" "github.com/stretchr/testify/require" @@ -95,6 +96,7 @@ func setupScenarios() scenarios.ScenarioRegistry { localityawarerouting.RegisterScenario(reg) apigateway.RegisterScenario(reg) terminatinggateway.RegisterScenario(reg) + terminatinggatewaytproxy.RegisterScenario(reg) ec2tproxy.RegisterScenario(reg) return reg diff --git a/test/acceptance/examples/scenarios/terminating-gateway-tproxy/main.go b/test/acceptance/examples/scenarios/terminating-gateway-tproxy/main.go new file mode 100644 index 00000000..f4a56221 --- /dev/null +++ b/test/acceptance/examples/scenarios/terminating-gateway-tproxy/main.go @@ -0,0 +1,93 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package terminatinggatewaytproxy + +import ( + "encoding/json" + "fmt" + "strings" + "testing" + "time" + + "github.com/hashicorp/serf/testutil/retry" + "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios" + "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/common" + "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/framework/logger" + "github.com/stretchr/testify/require" +) + +type TFOutputs struct { + ConsulServerLBAddr string `json:"consul_server_lb_address"` + ConsulServerToken string `json:"consul_server_bootstrap_token"` + MeshClientLBAddr string `json:"mesh_client_lb_address"` +} + +func RegisterScenario(r scenarios.ScenarioRegistry) { + tfResourcesName := fmt.Sprintf("ecs-%s", common.GenerateRandomStr(6)) + + r.Register(scenarios.ScenarioRegistration{ + Name: "TERMINATING_GATEWAY_TPROXY", + FolderName: "terminating-gateway-transparent-proxy", + TerraformInputVars: getTerraformVars(tfResourcesName), + Validate: validate(tfResourcesName), + }) +} + +func getTerraformVars(tfResName string) scenarios.TerraformInputVarsHook { + return func() (map[string]interface{}, error) { + vars := map[string]interface{}{ + "region": "us-west-2", + "name": tfResName, + } + + publicIP, err := common.GetPublicIP() + if err != nil { + return nil, err + } + vars["lb_ingress_ip"] = publicIP + + return vars, nil + } +} + +func validate(tfResName string) scenarios.ValidateHook { + return func(t *testing.T, data []byte) { + logger.Log(t, "Fetching required output terraform variables") + + var tfOutputs *TFOutputs + require.NoError(t, json.Unmarshal(data, &tfOutputs)) + + consulServerLBAddr := tfOutputs.ConsulServerLBAddr + meshClientLBAddr := tfOutputs.MeshClientLBAddr + meshClientLBAddr = strings.TrimSuffix(meshClientLBAddr, "/ui") + + logger.Log(t, "Setting up the Consul client") + consulClient, err := common.SetupConsulClient(t, consulServerLBAddr) + require.NoError(t, err) + + clientAppName := fmt.Sprintf("%s-example-client-app", tfResName) + serverAppName := fmt.Sprintf("%s-external-server-app", tfResName) + + consulClient.EnsureServiceReadiness(clientAppName, nil) + consulClient.EnsureServiceReadiness(serverAppName, nil) + + // Perform assertions by hitting the client app's LB + logger.Log(t, "calling client app's load balancer to see if the server app is reachable") + var upstreamResp common.UpstreamCallResponse + retry.RunWith(&retry.Timer{Timeout: 3 * time.Minute, Wait: 10 * time.Second}, t, func(r *retry.R) { + resp, err := common.GetFakeServiceResponse(meshClientLBAddr) + require.NoError(r, err) + + require.Equal(r, 200, resp.Code) + require.Equal(r, "Hello World", resp.Body) + require.NotNil(r, resp.UpstreamCalls) + + upstreamResp = resp.UpstreamCalls[fmt.Sprintf("http://%s.virtual.consul", serverAppName)] + require.NotNil(r, upstreamResp) + require.Equal(r, serverAppName, upstreamResp.Name) + require.Equal(r, 200, upstreamResp.Code) + require.Equal(r, "Hello World", upstreamResp.Body) + }) + } +}