Skip to content

Commit

Permalink
r/aws_amplify_branch: Test 'backend_environment_arn'.
Browse files Browse the repository at this point in the history
Acceptance test output:

% make testacc TEST=./aws TESTARGS='-run=TestAccAWSAmplify_serial/Branch/BackendEnvironmentArn'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSAmplify_serial/Branch/BackendEnvironmentArn -timeout 180m
=== RUN   TestAccAWSAmplify_serial
=== RUN   TestAccAWSAmplify_serial/Branch
=== RUN   TestAccAWSAmplify_serial/Branch/BackendEnvironmentArn
=== PAUSE TestAccAWSAmplify_serial/Branch/BackendEnvironmentArn
=== CONT  TestAccAWSAmplify_serial/Branch/BackendEnvironmentArn
--- PASS: TestAccAWSAmplify_serial (28.27s)
    --- PASS: TestAccAWSAmplify_serial/Branch (0.00s)
        --- PASS: TestAccAWSAmplify_serial/Branch/BackendEnvironmentArn (28.27s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	31.532s
  • Loading branch information
ewbankkit committed Jun 2, 2021
1 parent 3009c08 commit f904e05
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 56 deletions.
2 changes: 0 additions & 2 deletions aws/resource_aws_amplify_backend_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ func testAccAWSAmplifyBackendEnvironment_DeploymentArtifacts_StackName(t *testin
})
}

// testAccAWSAmplifyBackendEnvironmentConfigDeploymentArtifactsAndStackName(rname, environmentName)

func testAccCheckAWSAmplifyBackendEnvironmentExists(resourceName string, v *amplify.BackendEnvironment) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down
10 changes: 1 addition & 9 deletions aws/resource_aws_amplify_branch.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package aws

import (
"context"
"fmt"
"log"
"regexp"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/amplify"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
Expand All @@ -28,13 +26,7 @@ func resourceAwsAmplifyBranch() *schema.Resource {
State: schema.ImportStatePassthrough,
},

CustomizeDiff: customdiff.Sequence(
SetTagsDiff,
customdiff.ForceNewIfChange("backend_environment_arn", func(_ context.Context, old, new, meta interface{}) bool {
// Any existing value cannot be cleared.
return new.(string) == ""
}),
),
CustomizeDiff: SetTagsDiff,

Schema: map[string]*schema.Schema{
"app_id": {
Expand Down
65 changes: 20 additions & 45 deletions aws/resource_aws_amplify_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/amplify"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -132,12 +131,12 @@ func testAccAWSAmplifyBranch_Tags(t *testing.T) {
}

func testAccAWSAmplifyBranch_BackendEnvironmentArn(t *testing.T) {
var branch1, branch2, branch3 amplify.Branch
var branch amplify.Branch
rName := acctest.RandomWithPrefix("tf-acc-test")
environmentName1 := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
environmentName2 := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
environmentName := acctest.RandStringFromCharSet(9, acctest.CharSetAlpha)
resourceName := "aws_amplify_branch.test"
backendEnvironmentResourceName := "aws_amplify_backend_environment.test"
backendEnvironment1ResourceName := "aws_amplify_backend_environment.test1"
backendEnvironment2ResourceName := "aws_amplify_backend_environment.test2"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSAmplify(t) },
Expand All @@ -146,10 +145,10 @@ func testAccAWSAmplifyBranch_BackendEnvironmentArn(t *testing.T) {
CheckDestroy: testAccCheckAWSAmplifyBranchDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAmplifyBranchConfigBackendEnvironmentARN(rName, environmentName1),
Config: testAccAWSAmplifyBranchConfigBackendEnvironmentARN(rName, environmentName, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAmplifyBranchExists(resourceName, &branch1),
resource.TestCheckResourceAttrPair(resourceName, "backend_environment_arn", backendEnvironmentResourceName, "arn"),
testAccCheckAWSAmplifyBranchExists(resourceName, &branch),
resource.TestCheckResourceAttrPair(resourceName, "backend_environment_arn", backendEnvironment1ResourceName, "arn"),
),
},
{
Expand All @@ -158,19 +157,10 @@ func testAccAWSAmplifyBranch_BackendEnvironmentArn(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccAWSAmplifyBranchConfigBackendEnvironmentARN(rName, environmentName2),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAmplifyBranchExists(resourceName, &branch2),
testAccCheckAWSAmplifyBranchNotRecreated(&branch1, &branch2),
resource.TestCheckResourceAttrPair(resourceName, "backend_environment_arn", backendEnvironmentResourceName, "arn"),
),
},
{
Config: testAccAWSAmplifyBranchConfigName(rName),
Config: testAccAWSAmplifyBranchConfigBackendEnvironmentARN(rName, environmentName, 2),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAmplifyBranchExists(resourceName, &branch3),
testAccCheckAWSAmplifyBranchRecreated(&branch2, &branch3),
resource.TestCheckResourceAttr(resourceName, "backend_environment_arn", ""),
testAccCheckAWSAmplifyBranchExists(resourceName, &branch),
resource.TestCheckResourceAttrPair(resourceName, "backend_environment_arn", backendEnvironment2ResourceName, "arn"),
),
},
},
Expand Down Expand Up @@ -434,26 +424,6 @@ func testAccCheckAWSAmplifyBranchDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSAmplifyBranchNotRecreated(before, after *amplify.Branch) resource.TestCheckFunc {
return func(s *terraform.State) error {
if before, after := aws.TimeValue(before.CreateTime), aws.TimeValue(after.CreateTime); before != after {
return fmt.Errorf("Amplify Branch (%s/%s) recreated", before, after)
}

return nil
}
}

func testAccCheckAWSAmplifyBranchRecreated(before, after *amplify.Branch) resource.TestCheckFunc {
return func(s *terraform.State) error {
if before, after := aws.TimeValue(before.CreateTime), aws.TimeValue(after.CreateTime); before == after {
return fmt.Errorf("Amplify Branch (%s) not recreated", before)
}

return nil
}
}

func testAccAWSAmplifyBranchConfigName(rName string) string {
return fmt.Sprintf(`
resource "aws_amplify_app" "test" {
Expand Down Expand Up @@ -502,24 +472,29 @@ resource "aws_amplify_branch" "test" {
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccAWSAmplifyBranchConfigBackendEnvironmentARN(rName, environmentName string) string {
func testAccAWSAmplifyBranchConfigBackendEnvironmentARN(rName, environmentName string, index int) string {
return fmt.Sprintf(`
resource "aws_amplify_app" "test" {
name = %[1]q
}
resource "aws_amplify_backend_environment" "test" {
resource "aws_amplify_backend_environment" "test1" {
app_id = aws_amplify_app.test.id
environment_name = %[2]q
environment_name = "%[2]sa"
}
resource "aws_amplify_backend_environment" "test2" {
app_id = aws_amplify_app.test.id
environment_name = "%[2]sb"
}
resource "aws_amplify_branch" "test" {
app_id = aws_amplify_app.test.id
branch_name = %[1]q
backend_environment_arn = aws_amplify_backend_environment.test.arn
backend_environment_arn = aws_amplify_backend_environment.test%[3]d.arn
}
`, rName, environmentName)
`, rName, environmentName, index)
}

/*
Expand Down

0 comments on commit f904e05

Please sign in to comment.