Skip to content

Commit

Permalink
Merge pull request #49 from hazelops/fix/deprecate-template-provider
Browse files Browse the repository at this point in the history
Deprecate template provider
  • Loading branch information
AutomationD authored Jun 4, 2024
2 parents 104c74a + 4456800 commit 041cee0
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 17 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/run.e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
go mod tidy
go test -v -timeout 60m -run TestExamplesCompleteWorker
worker-scheduled:
runs-on: ubuntu-latest
timeout-minutes: 60
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion autoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions data.tf
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}, )
}
2 changes: 1 addition & 1 deletion monitoring.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
150 changes: 150 additions & 0 deletions test/examples_complete-worker-ec2_test.go
Original file line number Diff line number Diff line change
@@ -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 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 TestExamplesWorkerEc2(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 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)

// 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)
*/
}
3 changes: 0 additions & 3 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ terraform {
aws = {
source = "hashicorp/aws"
}
template = {
source = "hashicorp/template"
}
}
}

0 comments on commit 041cee0

Please sign in to comment.