github.com/anfernee/terraform@v0.6.16-0.20160430000239-06e5085a92f2/builtin/providers/aws/resource_aws_cloudfront_distribution_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/service/cloudfront"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  // TestAccAWSCloudFrontDistribution_S3Origin runs an
    17  // aws_cloudfront_distribution acceptance test with a single S3 origin.
    18  //
    19  // If you are testing manually and can't wait for deletion, set the
    20  // TF_TEST_CLOUDFRONT_RETAIN environment variable.
    21  func TestAccAWSCloudFrontDistribution_S3Origin(t *testing.T) {
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckCloudFrontDistributionDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccAWSCloudFrontDistributionS3Config,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckCloudFrontDistributionExistence(
    31  						"aws_cloudfront_distribution.s3_distribution",
    32  					),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  // TestAccAWSCloudFrontDistribution_customOriginruns an
    40  // aws_cloudfront_distribution acceptance test with a single custom origin.
    41  //
    42  // If you are testing manually and can't wait for deletion, set the
    43  // TF_TEST_CLOUDFRONT_RETAIN environment variable.
    44  func TestAccAWSCloudFrontDistribution_customOrigin(t *testing.T) {
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:     func() { testAccPreCheck(t) },
    47  		Providers:    testAccProviders,
    48  		CheckDestroy: testAccCheckCloudFrontDistributionDestroy,
    49  		Steps: []resource.TestStep{
    50  			resource.TestStep{
    51  				Config: testAccAWSCloudFrontDistributionCustomConfig,
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckCloudFrontDistributionExistence(
    54  						"aws_cloudfront_distribution.custom_distribution",
    55  					),
    56  				),
    57  			},
    58  		},
    59  	})
    60  }
    61  
    62  // TestAccAWSCloudFrontDistribution_multiOrigin runs an
    63  // aws_cloudfront_distribution acceptance test with multiple origins.
    64  //
    65  // If you are testing manually and can't wait for deletion, set the
    66  // TF_TEST_CLOUDFRONT_RETAIN environment variable.
    67  func TestAccAWSCloudFrontDistribution_multiOrigin(t *testing.T) {
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testAccCheckCloudFrontDistributionDestroy,
    72  		Steps: []resource.TestStep{
    73  			resource.TestStep{
    74  				Config: testAccAWSCloudFrontDistributionMultiOriginConfig,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckCloudFrontDistributionExistence(
    77  						"aws_cloudfront_distribution.multi_origin_distribution",
    78  					),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  // TestAccAWSCloudFrontDistribution_noOptionalItemsConfig runs an
    86  // aws_cloudfront_distribution acceptance test with no optional items set.
    87  //
    88  // If you are testing manually and can't wait for deletion, set the
    89  // TF_TEST_CLOUDFRONT_RETAIN environment variable.
    90  func TestAccAWSCloudFrontDistribution_noOptionalItemsConfig(t *testing.T) {
    91  	resource.Test(t, resource.TestCase{
    92  		PreCheck:     func() { testAccPreCheck(t) },
    93  		Providers:    testAccProviders,
    94  		CheckDestroy: testAccCheckCloudFrontDistributionDestroy,
    95  		Steps: []resource.TestStep{
    96  			resource.TestStep{
    97  				Config: testAccAWSCloudFrontDistributionNoOptionalItemsConfig,
    98  				Check: resource.ComposeTestCheckFunc(
    99  					testAccCheckCloudFrontDistributionExistence(
   100  						"aws_cloudfront_distribution.no_optional_items",
   101  					),
   102  				),
   103  			},
   104  		},
   105  	})
   106  }
   107  
   108  func TestAccAWSCloudFrontDistribution_noCustomErrorResponseConfig(t *testing.T) {
   109  	resource.Test(t, resource.TestCase{
   110  		PreCheck:     func() { testAccPreCheck(t) },
   111  		Providers:    testAccProviders,
   112  		CheckDestroy: testAccCheckCloudFrontDistributionDestroy,
   113  		Steps: []resource.TestStep{
   114  			resource.TestStep{
   115  				Config: testAccAWSCloudFrontDistributionNoCustomErroResponseInfo,
   116  				Check: resource.ComposeTestCheckFunc(
   117  					testAccCheckCloudFrontDistributionExistence(
   118  						"aws_cloudfront_distribution.no_custom_error_responses",
   119  					),
   120  				),
   121  			},
   122  		},
   123  	})
   124  }
   125  
   126  func testAccCheckCloudFrontDistributionDestroy(s *terraform.State) error {
   127  	for k, rs := range s.RootModule().Resources {
   128  		if rs.Type != "aws_cloudfront_distribution" {
   129  			continue
   130  		}
   131  		dist, err := testAccAuxCloudFrontGetDistributionConfig(s, k)
   132  		if err == nil {
   133  			if _, ok := os.LookupEnv("TF_TEST_CLOUDFRONT_RETAIN"); ok {
   134  				if *dist.DistributionConfig.Enabled != false {
   135  					return fmt.Errorf("CloudFront distribution should be disabled")
   136  				}
   137  				return nil
   138  			}
   139  			return fmt.Errorf("CloudFront distribution did not destroy")
   140  		}
   141  	}
   142  	return nil
   143  }
   144  
   145  func testAccCheckCloudFrontDistributionExistence(cloudFrontResource string) resource.TestCheckFunc {
   146  	return func(s *terraform.State) error {
   147  		_, err := testAccAuxCloudFrontGetDistributionConfig(s, cloudFrontResource)
   148  
   149  		return err
   150  	}
   151  }
   152  
   153  func testAccAuxCloudFrontGetDistributionConfig(s *terraform.State, cloudFrontResource string) (*cloudfront.Distribution, error) {
   154  	cf, ok := s.RootModule().Resources[cloudFrontResource]
   155  	if !ok {
   156  		return nil, fmt.Errorf("Not found: %s", cloudFrontResource)
   157  	}
   158  
   159  	if cf.Primary.ID == "" {
   160  		return nil, fmt.Errorf("No Id is set")
   161  	}
   162  
   163  	cloudfrontconn := testAccProvider.Meta().(*AWSClient).cloudfrontconn
   164  
   165  	req := &cloudfront.GetDistributionInput{
   166  		Id: aws.String(cf.Primary.ID),
   167  	}
   168  
   169  	res, err := cloudfrontconn.GetDistribution(req)
   170  	if err != nil {
   171  		return nil, fmt.Errorf("Error retrieving CloudFront distribution: %s", err)
   172  	}
   173  
   174  	return res.Distribution, nil
   175  }
   176  
   177  func testAccAWSCloudFrontDistributionRetainConfig() string {
   178  	if _, ok := os.LookupEnv("TF_TEST_CLOUDFRONT_RETAIN"); ok {
   179  		return "retain_on_delete = true"
   180  	}
   181  	return ""
   182  }
   183  
   184  var testAccAWSCloudFrontDistributionS3Config = fmt.Sprintf(`
   185  variable rand_id {
   186  	default = %d
   187  }
   188  
   189  resource "aws_s3_bucket" "s3_bucket" {
   190  	bucket = "mybucket.${var.rand_id}.s3.amazonaws.com"
   191  	acl = "public-read"
   192  }
   193  
   194  resource "aws_cloudfront_distribution" "s3_distribution" {
   195  	origin {
   196  		domain_name = "${aws_s3_bucket.s3_bucket.id}"
   197  		origin_id = "myS3Origin"
   198  		s3_origin_config {}
   199  	}
   200  	enabled = true
   201  	default_root_object = "index.html"
   202  	logging_config {
   203  		include_cookies = false
   204  		bucket = "mylogs.${var.rand_id}.s3.amazonaws.com"
   205  		prefix = "myprefix"
   206  	}
   207  	aliases = [ "mysite.${var.rand_id}.example.com", "yoursite.${var.rand_id}.example.com" ]
   208  	default_cache_behavior {
   209  		allowed_methods = [ "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT" ]
   210  		cached_methods = [ "GET", "HEAD" ]
   211  		target_origin_id = "myS3Origin"
   212  		forwarded_values {
   213  			query_string = false
   214  			cookies {
   215  				forward = "none"
   216  			}
   217  		}
   218  		viewer_protocol_policy = "allow-all"
   219  		min_ttl = 0
   220  		default_ttl = 3600
   221  		max_ttl = 86400
   222  	}
   223  	price_class = "PriceClass_200"
   224  	restrictions {
   225  		geo_restriction {
   226  			restriction_type = "whitelist"
   227  			locations = [ "US", "CA", "GB", "DE" ]
   228  		}
   229  	}
   230  	viewer_certificate {
   231  		cloudfront_default_certificate = true
   232  	}
   233  	%s
   234  }
   235  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int(), testAccAWSCloudFrontDistributionRetainConfig())
   236  
   237  var testAccAWSCloudFrontDistributionCustomConfig = fmt.Sprintf(`
   238  variable rand_id {
   239  	default = %d
   240  }
   241  
   242  resource "aws_cloudfront_distribution" "custom_distribution" {
   243  	origin {
   244  		domain_name = "www.example.com"
   245  		origin_id = "myCustomOrigin"
   246  		custom_origin_config {
   247  			http_port = 80
   248  			https_port = 443
   249  			origin_protocol_policy = "http-only"
   250  			origin_ssl_protocols = [ "SSLv3", "TLSv1" ]
   251  		}
   252  	}
   253  	enabled = true
   254  	comment = "Some comment"
   255  	default_root_object = "index.html"
   256  	logging_config {
   257  		include_cookies = false
   258  		bucket = "mylogs.${var.rand_id}.s3.amazonaws.com"
   259  		prefix = "myprefix"
   260  	}
   261  	aliases = [ "mysite.${var.rand_id}.example.com", "*.yoursite.${var.rand_id}.example.com" ]
   262  	default_cache_behavior {
   263  		allowed_methods = [ "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT" ]
   264  		cached_methods = [ "GET", "HEAD" ]
   265  		target_origin_id = "myCustomOrigin"
   266  		smooth_streaming = false
   267  		forwarded_values {
   268  			query_string = false
   269  			cookies {
   270  				forward = "all"
   271  			}
   272  		}
   273  		viewer_protocol_policy = "allow-all"
   274  		min_ttl = 0
   275  		default_ttl = 3600
   276  		max_ttl = 86400
   277  	}
   278  	price_class = "PriceClass_200"
   279  	restrictions {
   280  		geo_restriction {
   281  			restriction_type = "whitelist"
   282  			locations = [ "US", "CA", "GB", "DE" ]
   283  		}
   284  	}
   285  	viewer_certificate {
   286  		cloudfront_default_certificate = true
   287  	}
   288  	%s
   289  }
   290  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int(), testAccAWSCloudFrontDistributionRetainConfig())
   291  
   292  var testAccAWSCloudFrontDistributionMultiOriginConfig = fmt.Sprintf(`
   293  variable rand_id {
   294  	default = %d
   295  }
   296  
   297  resource "aws_s3_bucket" "s3_bucket" {
   298  	bucket = "mybucket.${var.rand_id}.s3.amazonaws.com"
   299  	acl = "public-read"
   300  }
   301  
   302  resource "aws_cloudfront_distribution" "multi_origin_distribution" {
   303  	origin {
   304  		domain_name = "${aws_s3_bucket.s3_bucket.id}"
   305  		origin_id = "myS3Origin"
   306  		s3_origin_config {}
   307  	}
   308  	origin {
   309  		domain_name = "www.example.com"
   310  		origin_id = "myCustomOrigin"
   311  		custom_origin_config {
   312  			http_port = 80
   313  			https_port = 443
   314  			origin_protocol_policy = "http-only"
   315  			origin_ssl_protocols = [ "SSLv3", "TLSv1" ]
   316  		}
   317  	}
   318  	enabled = true
   319  	comment = "Some comment"
   320  	default_root_object = "index.html"
   321  	logging_config {
   322  		include_cookies = false
   323  		bucket = "mylogs.${var.rand_id}.s3.amazonaws.com"
   324  		prefix = "myprefix"
   325  	}
   326  	aliases = [ "mysite.${var.rand_id}.example.com", "*.yoursite.${var.rand_id}.example.com" ]
   327  	default_cache_behavior {
   328  		allowed_methods = [ "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT" ]
   329  		cached_methods = [ "GET", "HEAD" ]
   330  		target_origin_id = "myS3Origin"
   331  		smooth_streaming = true
   332  		forwarded_values {
   333  			query_string = false
   334  			cookies {
   335  				forward = "all"
   336  			}
   337  		}
   338  		min_ttl = 100
   339  		default_ttl = 100
   340  		max_ttl = 100
   341  		viewer_protocol_policy = "allow-all"
   342  	}
   343  	cache_behavior {
   344  		allowed_methods = [ "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT" ]
   345  		cached_methods = [ "GET", "HEAD" ]
   346  		target_origin_id = "myS3Origin"
   347  		forwarded_values {
   348  			query_string = true
   349  			cookies {
   350  				forward = "none"
   351  			}
   352  		}
   353  		min_ttl = 50
   354  		default_ttl = 50
   355  		max_ttl = 50
   356  		viewer_protocol_policy = "allow-all"
   357  		path_pattern = "images1/*.jpg"
   358  	}
   359  	cache_behavior {
   360  		allowed_methods = [ "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT" ]
   361  		cached_methods = [ "GET", "HEAD" ]
   362  		target_origin_id = "myCustomOrigin"
   363  		forwarded_values {
   364  			query_string = true
   365  			cookies {
   366  				forward = "none"
   367  			}
   368  		}
   369  		min_ttl = 50
   370  		default_ttl = 50
   371  		max_ttl = 50
   372  		viewer_protocol_policy = "allow-all"
   373  		path_pattern = "images2/*.jpg"
   374  	}
   375  	price_class = "PriceClass_All"
   376  	custom_error_response {
   377  		error_code = 404
   378  		response_page_path = "/error-pages/404.html"
   379  		response_code = 200
   380  		error_caching_min_ttl = 30
   381  	}
   382  	restrictions {
   383  		geo_restriction {
   384  			restriction_type = "none"
   385  		}
   386  	}
   387  	viewer_certificate {
   388  		cloudfront_default_certificate = true
   389  	}
   390  	%s
   391  }
   392  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int(), testAccAWSCloudFrontDistributionRetainConfig())
   393  
   394  var testAccAWSCloudFrontDistributionNoCustomErroResponseInfo = fmt.Sprintf(`
   395  variable rand_id {
   396  	default = %d
   397  }
   398  
   399  resource "aws_cloudfront_distribution" "no_custom_error_responses" {
   400  	origin {
   401  		domain_name = "www.example.com"
   402  		origin_id = "myCustomOrigin"
   403  		custom_origin_config {
   404  			http_port = 80
   405  			https_port = 443
   406  			origin_protocol_policy = "http-only"
   407  			origin_ssl_protocols = [ "SSLv3", "TLSv1" ]
   408  		}
   409  	}
   410  	enabled = true
   411  	comment = "Some comment"
   412  	default_cache_behavior {
   413  		allowed_methods = [ "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT" ]
   414  		cached_methods = [ "GET", "HEAD" ]
   415  		target_origin_id = "myCustomOrigin"
   416  		smooth_streaming = false
   417  		forwarded_values {
   418  			query_string = false
   419  			cookies {
   420  				forward = "all"
   421  			}
   422  		}
   423  		viewer_protocol_policy = "allow-all"
   424  		min_ttl = 0
   425  		default_ttl = 3600
   426  		max_ttl = 86400
   427  	}
   428  	custom_error_response {
   429  		error_code = 404
   430  		error_caching_min_ttl = 30
   431  	}
   432  	restrictions {
   433  		geo_restriction {
   434  			restriction_type = "whitelist"
   435  			locations = [ "US", "CA", "GB", "DE" ]
   436  		}
   437  	}
   438  	viewer_certificate {
   439  		cloudfront_default_certificate = true
   440  	}
   441  	%s
   442  }
   443  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int(), testAccAWSCloudFrontDistributionRetainConfig())
   444  
   445  var testAccAWSCloudFrontDistributionNoOptionalItemsConfig = fmt.Sprintf(`
   446  variable rand_id {
   447  	default = %d
   448  }
   449  
   450  resource "aws_cloudfront_distribution" "no_optional_items" {
   451  	origin {
   452  		domain_name = "www.example.com"
   453  		origin_id = "myCustomOrigin"
   454  		custom_origin_config {
   455  			http_port = 80
   456  			https_port = 443
   457  			origin_protocol_policy = "http-only"
   458  			origin_ssl_protocols = [ "SSLv3", "TLSv1" ]
   459  		}
   460  	}
   461  	enabled = true
   462  	comment = "Some comment"
   463  	default_cache_behavior {
   464  		allowed_methods = [ "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT" ]
   465  		cached_methods = [ "GET", "HEAD" ]
   466  		target_origin_id = "myCustomOrigin"
   467  		smooth_streaming = false
   468  		forwarded_values {
   469  			query_string = false
   470  			cookies {
   471  				forward = "all"
   472  			}
   473  		}
   474  		viewer_protocol_policy = "allow-all"
   475  		min_ttl = 0
   476  		default_ttl = 3600
   477  		max_ttl = 86400
   478  	}
   479  	restrictions {
   480  		geo_restriction {
   481  			restriction_type = "whitelist"
   482  			locations = [ "US", "CA", "GB", "DE" ]
   483  		}
   484  	}
   485  	viewer_certificate {
   486  		cloudfront_default_certificate = true
   487  	}
   488  	%s
   489  }
   490  `, rand.New(rand.NewSource(time.Now().UnixNano())).Int(), testAccAWSCloudFrontDistributionRetainConfig())