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