Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderEkdahl authored and Chris Marchesi committed Apr 7, 2016
1 parent a51e536 commit 16e7a07
Showing 1 changed file with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccCloudFront(t *testing.T) {
func TestAccAWSCloudFront_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -21,15 +21,21 @@ func TestAccCloudFront(t *testing.T) {
resource.TestStep{
Config: testAccAWSCloudFrontConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudfrontInitial(
testAccCheckCloudfrontExistance(
"aws_cloudfront_web_distribution.main",
),
),
},
resource.TestStep{
Config: testAccAWSCloudFrontUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudfrontSecondary(
testAccCheckCloudfrontExistance(
"aws_cloudfront_web_distribution.main",
),
testAccCheckCloudfrontCheckDistributionDisabled(
"aws_cloudfront_web_distribution.main",
),
testAccCheckCloudfrontCheckDistributionAlias(
"aws_cloudfront_web_distribution.main",
),
),
Expand All @@ -46,60 +52,60 @@ func testAccCheckCloudFrontDestroy(s *terraform.State) error {
return nil
}

func testAccCheckCloudfrontInitial(cloudFrontResource string) resource.TestCheckFunc {
func testAccCheckCloudfrontExistance(cloudFrontResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
cloudFront, ok := s.RootModule().Resources[cloudFrontResource]
if !ok {
return fmt.Errorf("Not found: %s", cloudFrontResource)
}

if cloudFront.Primary.ID == "" {
return fmt.Errorf("No Id is set")
}
_, err := testAccAuxCloudfrontGetDistributionConfig(s, cloudFrontResource)

cloudfrontconn := testAccProvider.Meta().(*AWSClient).cloudfrontconn
return err
}
}

req := &cloudfront.GetDistributionInput{
Id: aws.String(cloudFront.Primary.ID),
}
func testAccCheckCloudfrontCheckDistributionDisabled(cloudFrontResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
dist, _ := testAccAuxCloudfrontGetDistributionConfig(s, cloudFrontResource)

_, err := cloudfrontconn.GetDistribution(req)
if err != nil {
return fmt.Errorf("Error retrieving CloudFront distribution: %s", err)
if *dist.DistributionConfig.Enabled != false {
return fmt.Errorf("CloudFront distribution should be disabled")
}

return nil
}
}

func testAccCheckCloudfrontSecondary(cloudFrontResource string) resource.TestCheckFunc {
func testAccCheckCloudfrontCheckDistributionAlias(cloudFrontResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
cloudFront, ok := s.RootModule().Resources[cloudFrontResource]
if !ok {
return fmt.Errorf("Not found: %s", cloudFrontResource)
dist, _ := testAccAuxCloudfrontGetDistributionConfig(s, cloudFrontResource)

if len(dist.DistributionConfig.Aliases.Items) != 1 {
return fmt.Errorf("CloudFront failed updating aliases")
}

cloudfrontconn := testAccProvider.Meta().(*AWSClient).cloudfrontconn
return nil
}
}

req := &cloudfront.GetDistributionInput{
Id: aws.String(cloudFront.Primary.ID),
}
func testAccAuxCloudfrontGetDistributionConfig(s *terraform.State, cloudFrontResource string) (*cloudfront.Distribution, error) {
cf, ok := s.RootModule().Resources[cloudFrontResource]
if !ok {
return nil, fmt.Errorf("Not found: %s", cloudFrontResource)
}

res, err := cloudfrontconn.GetDistribution(req)
if err != nil {
return fmt.Errorf("Error retrieving CloudFront distribution: %s", err)
}
if cf.Primary.ID == "" {
return nil, fmt.Errorf("No Id is set")
}

if len(res.Distribution.DistributionConfig.Aliases.Items) != 1 {
return fmt.Errorf("CloudFront failed updating aliases")
}
cloudfrontconn := testAccProvider.Meta().(*AWSClient).cloudfrontconn

if *res.Distribution.DistributionConfig.Enabled != false {
return fmt.Errorf("CloudFront failed updating enabled status")
}
req := &cloudfront.GetDistributionInput{
Id: aws.String(cf.Primary.ID),
}

return nil
res, err := cloudfrontconn.GetDistribution(req)
if err != nil {
return nil, fmt.Errorf("Error retrieving CloudFront distribution: %s", err)
}

return res.Distribution, nil
}

const testAccAWSCloudFrontConfig = `
Expand Down

0 comments on commit 16e7a07

Please sign in to comment.