Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
korenyoni committed Jul 1, 2021
1 parent 9035040 commit 9242891
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 27 deletions.
25 changes: 15 additions & 10 deletions examples/complete/custom-origins.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
locals {
additional_custom_origins_enabled = local.enabled && var.additional_custom_origins_enabled
default_custom_origin_configuration = {
domain_name = null
origin_id = null
Expand All @@ -13,17 +14,17 @@ locals {
origin_read_timeout = 60
}
}
additional_custom_origins = var.additional_custom_origins_enabled ? [
additional_custom_origins = local.additional_custom_origins_enabled ? [
merge(local.default_custom_origin_configuration, {
domain_name = module.additional_custom_origin.s3_bucket_website_endpoint
origin_id = module.additional_custom_origin.hostname
domain_name = module.additional_custom_origin[0].s3_bucket_website_endpoint
origin_id = module.additional_custom_origin[0].hostname
}),
merge(local.default_custom_origin_configuration, {
domain_name = module.additional_custom_failover_origin.s3_bucket_website_endpoint
origin_id = module.additional_custom_failover_origin.hostname
domain_name = module.additional_custom_failover_origin[0].s3_bucket_website_endpoint
origin_id = module.additional_custom_failover_origin[0].hostname
})
] : []
additional_custom_origin_groups = var.additional_custom_origins_enabled ? [{
additional_custom_origin_groups = local.additional_custom_origins_enabled ? [{
primary_origin_id = local.additional_custom_origins[0].origin_id
failover_origin_id = local.additional_custom_origins[1].origin_id
failover_criteria = var.origin_group_failover_criteria_status_codes
Expand All @@ -34,17 +35,19 @@ locals {
module "additional_custom_origin_label" {
source = "cloudposse/label/null"
version = "0.24.1"
enabled = var.additional_custom_origins_enabled
enabled = local.additional_custom_origins_enabled

attributes = ["web"]

context = module.this.context
}

module "additional_custom_origin" {
count = local.additional_s3_origins_enabled ? 1 : 0 # https://github.com/cloudposse/terraform-aws-s3-website/issues/65

source = "cloudposse/s3-website/aws"
version = "0.16.0"
enabled = var.additional_custom_origins_enabled
enabled = local.additional_custom_origins_enabled

force_destroy = true
hostname = format("%s.%s", module.additional_custom_origin_label.id, var.parent_zone_name)
Expand All @@ -55,17 +58,19 @@ module "additional_custom_origin" {
module "additional_custom_failover_origin_label" {
source = "cloudposse/label/null"
version = "0.24.1"
enabled = var.additional_custom_origins_enabled
enabled = local.additional_custom_origins_enabled

attributes = ["web", "fo"]

context = module.this.context
}

module "additional_custom_failover_origin" {
count = local.additional_s3_origins_enabled ? 1 : 0 # https://github.com/cloudposse/terraform-aws-s3-website/issues/65

source = "cloudposse/s3-website/aws"
version = "0.16.0"
enabled = var.additional_custom_origins_enabled
enabled = local.additional_custom_origins_enabled

force_destroy = true
hostname = format("%s.%s", module.additional_custom_failover_origin_label.id, var.parent_zone_name)
Expand Down
10 changes: 5 additions & 5 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ module "cloudfront_s3_cdn" {
additional_bucket_policy = local.enabled ? data.aws_iam_policy_document.document[0].json : ""

custom_origins = local.additional_custom_origins
s3_origins = concat([{
domain_name = module.s3_bucket.bucket_regional_domain_name
origin_id = module.s3_bucket.bucket_id
origin_path = null
s3_origins = concat([{
domain_name = module.s3_bucket.bucket_regional_domain_name
origin_id = module.s3_bucket.bucket_id
origin_path = null
s3_origin_config = {
origin_access_identity = ""
}
}], local.additional_s3_origins)
origin_groups = concat([{
origin_groups = concat([{
primary_origin_id = null # will get translated to the origin id of the origin created by this module.
failover_origin_id = module.s3_bucket.bucket_id
failover_criteria = var.origin_group_failover_criteria_status_codes
Expand Down
23 changes: 12 additions & 11 deletions examples/complete/s3-origins.tf
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
locals {
additional_s3_origins_enabled = local.enabled && var.additional_s3_origins_enabled
default_s3_origin_configuration = {
domain_name = null
origin_id = null
origin_path = null
domain_name = null
origin_id = null
origin_path = null
s3_origin_config = {
origin_access_identity = ""
}
}
additional_s3_origins = var.additional_s3_origins_enabled ? [
additional_s3_origins = local.additional_s3_origins_enabled ? [
merge(local.default_s3_origin_configuration, {
domain_name = module.additional_s3_origin.bucket_regional_domain_name
origin_id = module.additional_s3_origin.bucket_id
domain_name = module.additional_s3_origin.bucket_regional_domain_name
origin_id = module.additional_s3_origin.bucket_id
}),
merge(local.default_s3_origin_configuration, {
domain_name = module.additional_s3_failover_origin.bucket_regional_domain_name
origin_id = module.additional_s3_failover_origin.bucket_id
domain_name = module.additional_s3_failover_origin.bucket_regional_domain_name
origin_id = module.additional_s3_failover_origin.bucket_id
})
] : []
additional_s3_origin_groups = var.additional_s3_origins_enabled ? [{
additional_s3_origin_groups = local.additional_s3_origins_enabled ? [{
primary_origin_id = local.additional_s3_origins[0].origin_id
failover_origin_id = local.additional_s3_origins[1].origin_id
failover_criteria = var.origin_group_failover_criteria_status_codes
Expand All @@ -27,7 +28,7 @@ locals {
module "additional_s3_origin" {
source = "cloudposse/s3-bucket/aws"
version = "0.36.0"
enabled = var.additional_s3_origins_enabled
enabled = local.additional_s3_origins_enabled

acl = "private"
force_destroy = true
Expand All @@ -41,7 +42,7 @@ module "additional_s3_origin" {
module "additional_s3_failover_origin" {
source = "cloudposse/s3-bucket/aws"
version = "0.36.0"
enabled = var.additional_s3_origins_enabled
enabled = local.additional_s3_origins_enabled

acl = "private"
force_destroy = true
Expand Down
2 changes: 1 addition & 1 deletion examples/complete/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ variable "additional_s3_origins_enabled" {
variable "origin_group_failover_criteria_status_codes" {
type = list(string)
description = "List of HTTP Status Codes to use as the failover criteria for origin groups."
default = [
default = [
403,
404,
500,
Expand Down
87 changes: 87 additions & 0 deletions test/src/examples_complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package test
import (
"encoding/json"
"math/rand"
"reflect"
"sort"
"strconv"
"testing"
"time"
Expand All @@ -11,6 +13,20 @@ import (
"github.com/stretchr/testify/assert"
)

type OriginGroup struct {
FailoverCriteria []FailoverCriteria `json:"failover_criteria"`
Member []Origin `json:"member"`
OriginId string `json:"origin_id"`
}

type FailoverCriteria struct {
StatusCodes []string `json:"status_codes"`
}

type Origin struct {
OriginId string `json:"origin_id"`
}

func TestExamplesComplete(t *testing.T) {
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
Expand Down Expand Up @@ -67,6 +83,77 @@ func testExamplesCompleteEnabled(t *testing.T) {
defer func() { recover() }()
assert.Equal(t, `arn:aws:s3:::`+expectedS3BucketName+`/testprefix/*`, getTestResource(policyString),
"Templating of var.additional_bucket_policy failed")

// Validate that Origins are in Origin Groups
originGroups := []OriginGroup{}
terraform.OutputStruct(t, terraformOptions, "cf_origin_groups", &originGroups)
originIds := terraform.OutputList(t, terraformOptions, "origin_ids")
originIdsInGroups := []string{}
for _, originGroup := range originGroups {
for _, member := range originGroup.Member {
originIdsInGroups = append(originIdsInGroups, member.OriginId)
}
}
sort.Strings(originIds)
sort.Strings(originIdsInGroups)
assert.True(t, reflect.DeepEqual(originIds, originIdsInGroups))
}

// Test the Terraform module in examples/complete using Terratest with extra origins (extra-origins.us-east-2.tfvars)
func testExamplesCompleteExtraOrigins(t *testing.T) {
t.Parallel()

rand.Seed(time.Now().UnixNano() + 1)

attributes := []string{strconv.Itoa(rand.Intn(100000))}

terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../../examples/complete",
Upgrade: true,
// Variables to pass to our Terraform code using -var-file options
VarFiles: []string{"extra-origins.us-east-2.tfvars"},
Vars: map[string]interface{}{
"attributes": attributes,
},
}

// At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)

// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
terraform.Apply(t, terraformOptions)

// Run `terraform output` to get the value of an output variable
cfArn := terraform.Output(t, terraformOptions, "cf_arn")
// Verify we're getting back the outputs we expect
assert.Contains(t, cfArn, "arn:aws:cloudfront::")

// Run `terraform output` to get the value of an output variable
s3BucketName := terraform.Output(t, terraformOptions, "s3_bucket")
expectedS3BucketName := "eg-test-cloudfront-s3-cdn-extra-" + attributes[0] + "-origin"
// Verify we're getting back the outputs we expect
assert.Equal(t, expectedS3BucketName, s3BucketName)

policyString := terraform.Output(t, terraformOptions, "s3_bucket_policy")
assert.NotPanics(t, func() { getTestResource(policyString) }, "Could not parse S3 Bucket Policy")
defer func() { recover() }()
assert.Equal(t, `arn:aws:s3:::`+expectedS3BucketName+`/testprefix/*`, getTestResource(policyString),
"Templating of var.additional_bucket_policy failed")

// Validate that Origins are in Origin Groups
originGroups := []OriginGroup{}
terraform.OutputStruct(t, terraformOptions, "cf_origin_groups", &originGroups)
originIds := terraform.OutputList(t, terraformOptions, "cf_origin_ids")
originIdsInGroups := []string{}
for _, originGroup := range originGroups {
for _, member := range originGroup.Member {
originIdsInGroups = append(originIdsInGroups, member.OriginId)
}
}
sort.Strings(originIds)
sort.Strings(originIdsInGroups)
assert.True(t, reflect.DeepEqual(originIds, originIdsInGroups))
}

func testExamplesCompleteDisabled(t *testing.T) {
Expand Down

0 comments on commit 9242891

Please sign in to comment.