Skip to content

Commit

Permalink
Moved all scenarios to use the registry pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Ganeshrockz committed Dec 20, 2023
1 parent 779963a commit 810fced
Show file tree
Hide file tree
Showing 11 changed files with 508 additions and 485 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/nightly-ecs-examples-validator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
needs:
- terraform-fmt
- go-fmt-and-lint-acceptance
- get-go-version
strategy:
matrix:
name:
Expand All @@ -85,6 +86,7 @@ jobs:
multi-cluster:
needs:
- single-cluster
- get-go-version
strategy:
matrix:
name:
Expand Down
12 changes: 12 additions & 0 deletions test/acceptance/examples/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import (
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios"
clusterpeering "github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/cluster-peering"
"github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/ec2"
"github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/fargate"
"github.com/hashicorp/terraform-aws-consul-ecs/test/acceptance/examples/scenarios/hcp"
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"
"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"
)
Expand Down Expand Up @@ -72,6 +78,12 @@ func setupScenarios() scenarios.ScenarioRegistry {
reg := scenarios.NewScenarioRegistry()

fargate.RegisterScenario(reg)
ec2.RegisterScenario(reg)
clusterpeering.RegisterScenario(reg)
hcp.RegisterScenario(reg)
sameness.RegisterScenario(reg)
wan.RegisterScenario(reg)
localityawarerouting.RegisterScenario(reg)

return reg
}
113 changes: 56 additions & 57 deletions test/acceptance/examples/scenarios/cluster-peering/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,68 @@ import (
"github.com/stretchr/testify/require"
)

type clusterPeering struct {
name string
func RegisterScenario(r scenarios.ScenarioRegistry) {
tfResourcesName := common.GenerateRandomStr(4)

r.Register(scenarios.ScenarioRegistration{
Name: "CLUSTER_PEERING",
FolderName: "cluster-peering",
TerraformInputVars: getTerraformVars(tfResourcesName),
Validate: validate(tfResourcesName),
})
}

func New(name string) scenarios.Scenario {
// We go ahead with a simple name due to AWS restrictions
// on character length of resource names for certain resources.
return &clusterPeering{
name: "ecs",
}
}
func getTerraformVars(tfResName string) scenarios.TerraformInputVarsHook {
return func() (map[string]interface{}, error) {
vars := map[string]interface{}{
"region": "us-east-2",
"name": tfResName,
}

func (c *clusterPeering) GetFolderName() string {
return "cluster-peering"
}
publicIP, err := common.GetPublicIP()
if err != nil {
return nil, err
}
vars["lb_ingress_ip"] = publicIP

func (c *clusterPeering) GetTerraformVars() (map[string]interface{}, error) {
vars := map[string]interface{}{
"region": "us-east-2",
"name": c.name,
return vars, nil
}

publicIP, err := common.GetPublicIP()
if err != nil {
return nil, err
}
vars["lb_ingress_ip"] = publicIP

return vars, nil
}

func (c *clusterPeering) Validate(t *testing.T, outputVars map[string]interface{}) {
logger.Log(t, "Fetching required output terraform variables")
getOutputVariableValue := func(name string) string {
val, ok := outputVars[name].(string)
require.True(t, ok)
return val
func validate(tfResName string) scenarios.ValidateHook {
return func(t *testing.T, tfOutput map[string]interface{}) {
logger.Log(t, "Fetching required output terraform variables")
getOutputVariableValue := func(name string) string {
val, ok := tfOutput[name].(string)
require.True(t, ok)
return val
}

dc1ConsulServerURL := getOutputVariableValue("dc1_server_url")
dc2ConsulServerURL := getOutputVariableValue("dc2_server_url")
dc1ConsulServerToken := getOutputVariableValue("dc1_server_bootstrap_token")
dc2ConsulServerToken := getOutputVariableValue("dc2_server_bootstrap_token")

meshClientLBAddr := getOutputVariableValue("client_lb_address")
meshClientLBAddr = strings.TrimSuffix(meshClientLBAddr, "/ui")

logger.Log(t, "Setting up the Consul clients")
consulClientOne, err := common.SetupConsulClient(t, dc1ConsulServerURL, common.WithToken(dc1ConsulServerToken))
require.NoError(t, err)

consulClientTwo, err := common.SetupConsulClient(t, dc2ConsulServerURL, common.WithToken(dc2ConsulServerToken))
require.NoError(t, err)

clientAppName := fmt.Sprintf("%s-dc1-example-client-app", tfResName)
serverAppName := fmt.Sprintf("%s-dc2-example-server-app", tfResName)

consulClientOne.EnsureServiceReadiness(clientAppName, nil)
consulClientTwo.EnsureServiceReadiness(serverAppName, nil)
consulClientOne.EnsureServiceReadiness(fmt.Sprintf("%s-dc1-mesh-gateway", tfResName), nil)
consulClientTwo.EnsureServiceReadiness(fmt.Sprintf("%s-dc2-mesh-gateway", tfResName), 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 in the peer cluster is reachable")
common.ValidateFakeServiceResponse(t, meshClientLBAddr, serverAppName)
}

dc1ConsulServerURL := getOutputVariableValue("dc1_server_url")
dc2ConsulServerURL := getOutputVariableValue("dc2_server_url")
dc1ConsulServerToken := getOutputVariableValue("dc1_server_bootstrap_token")
dc2ConsulServerToken := getOutputVariableValue("dc2_server_bootstrap_token")

meshClientLBAddr := getOutputVariableValue("client_lb_address")
meshClientLBAddr = strings.TrimSuffix(meshClientLBAddr, "/ui")

logger.Log(t, "Setting up the Consul clients")
consulClientOne, err := common.SetupConsulClient(t, dc1ConsulServerURL, common.WithToken(dc1ConsulServerToken))
require.NoError(t, err)

consulClientTwo, err := common.SetupConsulClient(t, dc2ConsulServerURL, common.WithToken(dc2ConsulServerToken))
require.NoError(t, err)

clientAppName := fmt.Sprintf("%s-dc1-example-client-app", c.name)
serverAppName := fmt.Sprintf("%s-dc2-example-server-app", c.name)

consulClientOne.EnsureServiceReadiness(clientAppName, nil)
consulClientTwo.EnsureServiceReadiness(serverAppName, nil)
consulClientOne.EnsureServiceReadiness(fmt.Sprintf("%s-dc1-mesh-gateway", c.name), nil)
consulClientTwo.EnsureServiceReadiness(fmt.Sprintf("%s-dc2-mesh-gateway", c.name), 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 in the peer cluster is reachable")
common.ValidateFakeServiceResponse(t, meshClientLBAddr, serverAppName)
}
20 changes: 20 additions & 0 deletions test/acceptance/examples/scenarios/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ package common

import (
"io"
"math/rand"
"net/http"
"strings"
"time"
)

const (
characterSet = "abcdefghijklmnopqrstuvwxyz"
)

// This method relies on a third party API to retrieve
Expand All @@ -24,3 +31,16 @@ func GetPublicIP() (string, error) {

return string(ip), nil
}

// GenerateRandomStr generate a random string of a given length
// from the predefined characterSet.
//
// Note: The resulting string is always lowercased.
func GenerateRandomStr(length int) string {
rand.Seed(time.Now().UnixNano())
result := make([]byte, length)
for i := range result {
result[i] = characterSet[rand.Intn(len(characterSet))]
}
return strings.ToLower(string(result))
}
83 changes: 42 additions & 41 deletions test/acceptance/examples/scenarios/ec2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,59 @@ import (
"github.com/stretchr/testify/require"
)

type ec2 struct {
name string
}
func RegisterScenario(r scenarios.ScenarioRegistry) {
tfResourcesName := fmt.Sprintf("ecs-%s", common.GenerateRandomStr(6))

func New(name string) scenarios.Scenario {
return &ec2{
name: name,
}
r.Register(scenarios.ScenarioRegistration{
Name: "EC2",
FolderName: "dev-server-ec2",
TerraformInputVars: getTerraformVars(tfResourcesName),
Validate: validate(tfResourcesName),
})
}

func (e *ec2) GetFolderName() string {
return "dev-server-ec2"
}
func getTerraformVars(tfResName string) scenarios.TerraformInputVarsHook {
return func() (map[string]interface{}, error) {
vars := map[string]interface{}{
"region": "us-east-2",
"name": tfResName,
}

func (e *ec2) GetTerraformVars() (map[string]interface{}, error) {
vars := map[string]interface{}{
"region": "us-east-2",
"name": e.name,
}
publicIP, err := common.GetPublicIP()
if err != nil {
return nil, err
}
vars["lb_ingress_ip"] = publicIP

publicIP, err := common.GetPublicIP()
if err != nil {
return nil, err
return vars, nil
}
vars["lb_ingress_ip"] = publicIP

return vars, nil
}

func (e *ec2) Validate(t *testing.T, outputVars map[string]interface{}) {
logger.Log(t, "Fetching required output terraform variables")
getOutputVariableValue := func(name string) string {
val, ok := outputVars[name].(string)
require.True(t, ok)
return val
}
func validate(tfResName string) scenarios.ValidateHook {
return func(t *testing.T, tfOutput map[string]interface{}) {
logger.Log(t, "Fetching required output terraform variables")
getOutputVariableValue := func(name string) string {
val, ok := tfOutput[name].(string)
require.True(t, ok)
return val
}

consulServerLBAddr := getOutputVariableValue("consul_server_lb_address")
meshClientLBAddr := getOutputVariableValue("mesh_client_lb_address")
meshClientLBAddr = strings.TrimSuffix(meshClientLBAddr, "/ui")
consulServerLBAddr := getOutputVariableValue("consul_server_lb_address")
meshClientLBAddr := getOutputVariableValue("mesh_client_lb_address")
meshClientLBAddr = strings.TrimSuffix(meshClientLBAddr, "/ui")

logger.Log(t, "Setting up the Consul client")
consulClient, err := common.SetupConsulClient(t, consulServerLBAddr)
require.NoError(t, err)
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", e.name)
serverAppName := fmt.Sprintf("%s-example-server-app", e.name)
clientAppName := fmt.Sprintf("%s-example-client-app", tfResName)
serverAppName := fmt.Sprintf("%s-example-server-app", tfResName)

consulClient.EnsureServiceReadiness(clientAppName, nil)
consulClient.EnsureServiceReadiness(serverAppName, nil)
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")
common.ValidateFakeServiceResponse(t, meshClientLBAddr, serverAppName)
// 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")
common.ValidateFakeServiceResponse(t, meshClientLBAddr, serverAppName)
}
}
3 changes: 1 addition & 2 deletions test/acceptance/examples/scenarios/fargate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/random"
"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"
)

func RegisterScenario(r scenarios.ScenarioRegistry) {
tfResourcesName := strings.ToLower(fmt.Sprintf("ecs-%s", random.UniqueId()))
tfResourcesName := fmt.Sprintf("ecs-%s", common.GenerateRandomStr(6))

r.Register(scenarios.ScenarioRegistration{
Name: "FARGATE",
Expand Down
Loading

0 comments on commit 810fced

Please sign in to comment.