From 50a0c1a8461331597e68acfd6b5c6a750dd78967 Mon Sep 17 00:00:00 2001 From: Dmitry Kireev Date: Tue, 4 Jun 2024 15:46:18 +0000 Subject: [PATCH 1/4] Deprecation fixes - Removed hashicorp/template usage, switched to templatefile function. - Upgraded Monitoring module --- autoscaling.tf | 2 +- data.tf | 12 ------------ locals.tf | 10 ++++++++++ versions.tf | 3 --- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/autoscaling.tf b/autoscaling.tf index 898648a..2918a2e 100644 --- a/autoscaling.tf +++ b/autoscaling.tf @@ -50,7 +50,7 @@ module "autoscaling" { ] target_group_arns = var.app_type == "web" || var.app_type == "tcp-app" ? module.alb[0].target_group_arns : [] - user_data = var.ecs_launch_type == "EC2" ? base64encode(data.template_file.asg_ecs_ec2_user_data.rendered) : null + user_data = var.ecs_launch_type == "EC2" ? base64encode(local.asg_ecs_ec2_user_data) : null vpc_zone_identifier = var.public_ecs_service ? var.public_subnets : var.private_subnets health_check_type = var.autoscaling_health_check_type diff --git a/data.tf b/data.tf index d4b1a77..c9482df 100644 --- a/data.tf +++ b/data.tf @@ -1,18 +1,6 @@ data "aws_caller_identity" "current" {} data "aws_region" "current" {} -data "template_file" "asg_ecs_ec2_user_data" { - template = file("${path.module}/templates/ecs_ec2_user_data.sh.tpl") - - vars = { - ecs_cluster_name = local.ecs_cluster_name - service = local.name - env = var.env - ec2_service_group = var.ec2_service_group - ec2_eip_enabled = tostring(var.ec2_eip_enabled) - } -} - data "aws_iam_instance_profile" "this" { count = var.ecs_launch_type == "EC2" ? 1 : 0 name = module.autoscaling.iam_instance_profile_id diff --git a/locals.tf b/locals.tf index 34d22e6..90be808 100644 --- a/locals.tf +++ b/locals.tf @@ -198,4 +198,14 @@ locals { } } ] + + asg_ecs_ec2_user_data = templatefile( + "${path.module}/templates/ecs_ec2_user_data.sh.tpl", + { + ecs_cluster_name = local.ecs_cluster_name + service = local.name + env = var.env + ec2_service_group = var.ec2_service_group + ec2_eip_enabled = tostring(var.ec2_eip_enabled) + }, ) } diff --git a/versions.tf b/versions.tf index 894d0fa..1182721 100644 --- a/versions.tf +++ b/versions.tf @@ -5,8 +5,5 @@ terraform { aws = { source = "hashicorp/aws" } - template = { - source = "hashicorp/template" - } } } From 27118d6e3d221b7196b12df2cda0c8884a86528c Mon Sep 17 00:00:00 2001 From: Dmitry Kireev Date: Tue, 4 Jun 2024 15:46:30 +0000 Subject: [PATCH 2/4] Deprecation fixes - Removed hashicorp/template usage, switched to templatefile function. - Upgraded Monitoring module --- monitoring.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monitoring.tf b/monitoring.tf index 5ae8c74..b789c28 100644 --- a/monitoring.tf +++ b/monitoring.tf @@ -16,7 +16,7 @@ module "route_53_health_check" { count = var.route53_health_check_enabled ? 1 : 0 source = "registry.terraform.io/hazelops/route53-healthcheck/aws" - version = "~> 1.0" + version = "~> 2.0" enabled = var.route53_health_check_enabled env = var.env From e0239397bb7f0bfcb8aeebcd12bfb5f5e06cf4fe Mon Sep 17 00:00:00 2001 From: Dmitry Kireev Date: Tue, 4 Jun 2024 16:50:50 +0000 Subject: [PATCH 3/4] Add Complete Worker Ec2 test --- .github/workflows/run.e2e-tests.yml | 51 ++++++++ test/examples_complete-worker-ec2_test.go | 150 ++++++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 test/examples_complete-worker-ec2_test.go diff --git a/.github/workflows/run.e2e-tests.yml b/.github/workflows/run.e2e-tests.yml index 6cccb53..40cdaa3 100644 --- a/.github/workflows/run.e2e-tests.yml +++ b/.github/workflows/run.e2e-tests.yml @@ -70,6 +70,7 @@ jobs: go mod tidy go test -v -timeout 60m -run TestExamplesCompleteWorker + worker-scheduled: runs-on: ubuntu-latest timeout-minutes: 60 @@ -275,3 +276,53 @@ jobs: cd test go mod tidy go test -v -timeout 60m -run TestExamplesWorkerAutoScheduled + + complete-worker-ec2: + runs-on: ubuntu-latest + timeout-minutes: 60 + env: + ENV: examples6 + needs: + - worker-scheduled-autoscale + steps: + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: 1.18.x + + - name: Checkout Code + uses: actions/checkout@v2 + + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_SA }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_SA }} + aws-region: ${{ env.AWS_REGION }} + + - name: Generate Test SSH Key + run: ssh-keygen -q -f ~/.ssh/id_rsa + + - name: IZE setup + uses: hazelops/action-setup-ize@0.0.1 + with: + version: ${{ env.IZE_VERSION }} + + - name: IZE init + run: ize init + + - name: IZE create AWS Profile + run: ize gen aws-profile + + - name: IZE gen tfenv + run: ize gen tfenv + + - name: Copy generated files + run: | + cp -R .ize/env/${{ env.ENV }}/*.* examples/${{ github.job }}/ + + - name: Go TF Test + run: | + cd test + go mod tidy + go test -v -timeout 60m -run TestExamplesCompleteWorkerEc2 diff --git a/test/examples_complete-worker-ec2_test.go b/test/examples_complete-worker-ec2_test.go new file mode 100644 index 0000000..f19499a --- /dev/null +++ b/test/examples_complete-worker-ec2_test.go @@ -0,0 +1,150 @@ +package test + +import ( + //"github.com/gruntwork-io/terratest/modules/random" + "fmt" + "github.com/gruntwork-io/terratest/modules/terraform" + test_structure "github.com/gruntwork-io/terratest/modules/test-structure" + "github.com/stretchr/testify/assert" + "io" + "os" + //"strings" + "testing" +) + +func CopyFileCompleteWorkerEc2(src, dst string) (err error) { + sfi, err := os.Stat(src) + if err != nil { + return + } + if !sfi.Mode().IsRegular() { + // cannot copy non-regular files (e.g., directories, + // symlinks, devices, etc.) + return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String()) + } + dfi, err := os.Stat(dst) + if err != nil { + if !os.IsNotExist(err) { + return + } + } else { + if !(dfi.Mode().IsRegular()) { + return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String()) + } + if os.SameFile(sfi, dfi) { + return + } + } + if err = os.Link(src, dst); err == nil { + return + } + err = copyFileContentsCompleteWorkerEc2(src, dst) + return +} + +func copyFileContentsCompleteWorkerEc2(src, dst string) (err error) { + in, err := os.Open(src) + if err != nil { + return + } + defer in.Close() + out, err := os.Create(dst) + if err != nil { + return + } + defer func() { + cerr := out.Close() + if err == nil { + err = cerr + } + }() + if _, err = io.Copy(out, in); err != nil { + return + } + err = out.Sync() + return +} + +func cleanupExamplesCompleteWorkerEc2(t *testing.T, terraformOptions *terraform.Options, tempTestFolder string) { + terraform.Destroy(t, terraformOptions) + os.RemoveAll(tempTestFolder) +} + +// Test the Terraform module in examples/complete using Terratest. +func TestExamplesCompleteWorkerEc2(t *testing.T) { + t.Parallel() + // randID := strings.ToLower(random.UniqueId()) + // attributes := []string{randID} + + rootFolder := "../" + terraformFolderRelativeToRoot := "examples/complete-worker-ec2" + + tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + fullRootPath := rootFolder + terraformFolderRelativeToRoot + + // Copy terraform.tfvars + fmt.Printf("Copying %s to %s\n", fullRootPath+"/terraform.tfvars", tempTestFolder+"/terraform.tfvars") + err := CopyFileCompleteWorkerEc2(fullRootPath+"/terraform.tfvars", tempTestFolder+"/terraform.tfvars") + if err != nil { + fmt.Printf("CopyFile failed %q\n", err) + } else { + fmt.Printf("CopyFile succeeded\n") + } + dir, _ := os.ReadDir(tempTestFolder) + for _, d := range dir { + t.Log(d.Name()) + } + varFiles := []string{tempTestFolder + "/terraform.tfvars"} + + terraformOptions := &terraform.Options{ + // The path to where our Terraform code is located + TerraformDir: tempTestFolder, + Upgrade: true, + + // Variables to pass to our Terraform code using -var-file options + VarFiles: varFiles, + /*Vars: map[string]interface{}{ + "attributes": attributes, + }, + */ + } + + // At the end of the test, run `terraform destroy` to clean up any resources that were created + defer cleanupExamplesCompleteWorkerEc2(t, terraformOptions, tempTestFolder) + + // This will run `terraform init` and `terraform apply` and fail the test if there are any errors + terraform.InitAndApply(t, terraformOptions) + + // Run `terraform output` to get the value of an output variable + vpcCidr := terraform.Output(t, terraformOptions, "vpc_cidr") + // Verify we're getting back the outputs we expect + assert.Equal(t, "10.4.0.0/16", vpcCidr) + + // Run `terraform output` to get the value of an output variable + privateSubnetCidrs := terraform.OutputList(t, terraformOptions, "private_subnet_cidrs") + // Verify we're getting back the outputs we expect + assert.Equal(t, []string{"10.4.20.0/23"}, privateSubnetCidrs) + + // Run `terraform output` to get the value of an output variable + cloudWatchLogGroup := terraform.Output(t, terraformOptions, "cloudwatch_log_group") + // Verify we're getting back the outputs we expect + assert.Equal(t, "examples2-complete-worker-ec2", cloudWatchLogGroup) + + // Run `terraform output` to get the value of an output variable + ecsClusterName := terraform.Output(t, terraformOptions, "ecs_cluster_name") + // Verify we're getting back the outputs we expect + assert.Equal(t, "examples2-tftest-complete-worker-ec2", ecsClusterName) + + /* + // Run `terraform output` to get the value of an output variable + proxyEndpoint := terraform.Output(t, terraformOptions, "proxy_endpoint") + // Verify we're getting back the outputs we expect + assert.Contains(t, proxyEndpoint, "eg-test-rds-proxy-"+randID) + + // Run `terraform output` to get the value of an output variable + proxyTargetEndpoint := terraform.Output(t, terraformOptions, "proxy_target_endpoint") + instanceAddress := terraform.Output(t, terraformOptions, "instance_address") + // Verify we're getting back the outputs we expect + assert.Equal(t, proxyTargetEndpoint, instanceAddress) + */ +} From 4456800756696da29fd57741bf3a35bb6f505900 Mon Sep 17 00:00:00 2001 From: Dmitry Kireev Date: Tue, 4 Jun 2024 17:10:37 +0000 Subject: [PATCH 4/4] Fix test name --- test/examples_complete-worker-ec2_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/examples_complete-worker-ec2_test.go b/test/examples_complete-worker-ec2_test.go index f19499a..9cd989e 100644 --- a/test/examples_complete-worker-ec2_test.go +++ b/test/examples_complete-worker-ec2_test.go @@ -65,13 +65,13 @@ func copyFileContentsCompleteWorkerEc2(src, dst string) (err error) { return } -func cleanupExamplesCompleteWorkerEc2(t *testing.T, terraformOptions *terraform.Options, tempTestFolder string) { +func cleanupExamplesWorkerEc2(t *testing.T, terraformOptions *terraform.Options, tempTestFolder string) { terraform.Destroy(t, terraformOptions) os.RemoveAll(tempTestFolder) } // Test the Terraform module in examples/complete using Terratest. -func TestExamplesCompleteWorkerEc2(t *testing.T) { +func TestExamplesWorkerEc2(t *testing.T) { t.Parallel() // randID := strings.ToLower(random.UniqueId()) // attributes := []string{randID} @@ -110,7 +110,7 @@ func TestExamplesCompleteWorkerEc2(t *testing.T) { } // At the end of the test, run `terraform destroy` to clean up any resources that were created - defer cleanupExamplesCompleteWorkerEc2(t, terraformOptions, tempTestFolder) + defer cleanupExamplesWorkerEc2(t, terraformOptions, tempTestFolder) // This will run `terraform init` and `terraform apply` and fail the test if there are any errors terraform.InitAndApply(t, terraformOptions)