github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_route53_health_check_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/aws/aws-sdk-go/service/route53"
    11  )
    12  
    13  func TestAccAWSRoute53HealthCheck_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:      func() { testAccPreCheck(t) },
    16  		IDRefreshName: "aws_route53_health_check.foo",
    17  		Providers:     testAccProviders,
    18  		CheckDestroy:  testAccCheckRoute53HealthCheckDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccRoute53HealthCheckConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    24  					resource.TestCheckResourceAttr(
    25  						"aws_route53_health_check.foo", "measure_latency", "true"),
    26  					resource.TestCheckResourceAttr(
    27  						"aws_route53_health_check.foo", "invert_healthcheck", "true"),
    28  				),
    29  			},
    30  			resource.TestStep{
    31  				Config: testAccRoute53HealthCheckConfigUpdate,
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_route53_health_check.foo", "failure_threshold", "5"),
    36  					resource.TestCheckResourceAttr(
    37  						"aws_route53_health_check.foo", "invert_healthcheck", "false"),
    38  				),
    39  			},
    40  		},
    41  	})
    42  }
    43  
    44  func TestAccAWSRoute53HealthCheck_withSearchString(t *testing.T) {
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:      func() { testAccPreCheck(t) },
    47  		IDRefreshName: "aws_route53_health_check.foo",
    48  		Providers:     testAccProviders,
    49  		CheckDestroy:  testAccCheckRoute53HealthCheckDestroy,
    50  		Steps: []resource.TestStep{
    51  			resource.TestStep{
    52  				Config: testAccRoute53HealthCheckConfigWithSearchString,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    55  					resource.TestCheckResourceAttr(
    56  						"aws_route53_health_check.foo", "invert_healthcheck", "false"),
    57  					resource.TestCheckResourceAttr(
    58  						"aws_route53_health_check.foo", "search_string", "OK"),
    59  				),
    60  			},
    61  			resource.TestStep{
    62  				Config: testAccRoute53HealthCheckConfigWithSearchStringUpdate,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    65  					resource.TestCheckResourceAttr(
    66  						"aws_route53_health_check.foo", "invert_healthcheck", "true"),
    67  					resource.TestCheckResourceAttr(
    68  						"aws_route53_health_check.foo", "search_string", "FAILED"),
    69  				),
    70  			},
    71  		},
    72  	})
    73  }
    74  
    75  func TestAccAWSRoute53HealthCheck_withChildHealthChecks(t *testing.T) {
    76  	resource.Test(t, resource.TestCase{
    77  		PreCheck:     func() { testAccPreCheck(t) },
    78  		Providers:    testAccProviders,
    79  		CheckDestroy: testAccCheckRoute53HealthCheckDestroy,
    80  		Steps: []resource.TestStep{
    81  			resource.TestStep{
    82  				Config: testAccRoute53HealthCheckConfig_withChildHealthChecks,
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func TestAccAWSRoute53HealthCheck_IpConfig(t *testing.T) {
    92  	resource.Test(t, resource.TestCase{
    93  		PreCheck:     func() { testAccPreCheck(t) },
    94  		Providers:    testAccProviders,
    95  		CheckDestroy: testAccCheckRoute53HealthCheckDestroy,
    96  		Steps: []resource.TestStep{
    97  			resource.TestStep{
    98  				Config: testAccRoute53HealthCheckIpConfig,
    99  				Check: resource.ComposeTestCheckFunc(
   100  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.bar"),
   101  				),
   102  			},
   103  		},
   104  	})
   105  }
   106  
   107  func TestAccAWSRoute53HealthCheck_CloudWatchAlarmCheck(t *testing.T) {
   108  	resource.Test(t, resource.TestCase{
   109  		PreCheck:     func() { testAccPreCheck(t) },
   110  		Providers:    testAccProviders,
   111  		CheckDestroy: testAccCheckRoute53HealthCheckDestroy,
   112  		Steps: []resource.TestStep{
   113  			resource.TestStep{
   114  				Config: testAccRoute53HealthCheckCloudWatchAlarm,
   115  				Check: resource.ComposeTestCheckFunc(
   116  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
   117  					resource.TestCheckResourceAttr(
   118  						"aws_route53_health_check.foo", "cloudwatch_alarm_name", "cloudwatch-healthcheck-alarm"),
   119  				),
   120  			},
   121  		},
   122  	})
   123  }
   124  
   125  func TestAccAWSRoute53HealthCheck_withSNI(t *testing.T) {
   126  	resource.Test(t, resource.TestCase{
   127  		PreCheck:      func() { testAccPreCheck(t) },
   128  		IDRefreshName: "aws_route53_health_check.foo",
   129  		Providers:     testAccProviders,
   130  		CheckDestroy:  testAccCheckRoute53HealthCheckDestroy,
   131  		Steps: []resource.TestStep{
   132  			resource.TestStep{
   133  				Config: testAccRoute53HealthCheckConfigWithoutSNI,
   134  				Check: resource.ComposeTestCheckFunc(
   135  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
   136  					resource.TestCheckResourceAttr(
   137  						"aws_route53_health_check.foo", "enable_sni", "true"),
   138  				),
   139  			},
   140  			resource.TestStep{
   141  				Config: testAccRoute53HealthCheckConfigWithSNIDisabled,
   142  				Check: resource.ComposeTestCheckFunc(
   143  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
   144  					resource.TestCheckResourceAttr(
   145  						"aws_route53_health_check.foo", "enable_sni", "false"),
   146  				),
   147  			},
   148  			resource.TestStep{
   149  				Config: testAccRoute53HealthCheckConfigWithSNI,
   150  				Check: resource.ComposeTestCheckFunc(
   151  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
   152  					resource.TestCheckResourceAttr(
   153  						"aws_route53_health_check.foo", "enable_sni", "true"),
   154  				),
   155  			},
   156  		},
   157  	})
   158  }
   159  
   160  func testAccCheckRoute53HealthCheckDestroy(s *terraform.State) error {
   161  	conn := testAccProvider.Meta().(*AWSClient).r53conn
   162  
   163  	for _, rs := range s.RootModule().Resources {
   164  		if rs.Type != "aws_route53_health_check" {
   165  			continue
   166  		}
   167  
   168  		lopts := &route53.ListHealthChecksInput{}
   169  		resp, err := conn.ListHealthChecks(lopts)
   170  		if err != nil {
   171  			return err
   172  		}
   173  		if len(resp.HealthChecks) == 0 {
   174  			return nil
   175  		}
   176  
   177  		for _, check := range resp.HealthChecks {
   178  			if *check.Id == rs.Primary.ID {
   179  				return fmt.Errorf("Record still exists: %#v", check)
   180  			}
   181  
   182  		}
   183  
   184  	}
   185  	return nil
   186  }
   187  
   188  func testAccCheckRoute53HealthCheckExists(n string) resource.TestCheckFunc {
   189  	return func(s *terraform.State) error {
   190  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   191  
   192  		rs, ok := s.RootModule().Resources[n]
   193  		if !ok {
   194  			return fmt.Errorf("Not found: %s", n)
   195  		}
   196  
   197  		fmt.Print(rs.Primary.ID)
   198  
   199  		if rs.Primary.ID == "" {
   200  			return fmt.Errorf("No health check ID is set")
   201  		}
   202  
   203  		lopts := &route53.ListHealthChecksInput{}
   204  		resp, err := conn.ListHealthChecks(lopts)
   205  		if err != nil {
   206  			return err
   207  		}
   208  		if len(resp.HealthChecks) == 0 {
   209  			return fmt.Errorf("Health Check does not exist")
   210  		}
   211  
   212  		for _, check := range resp.HealthChecks {
   213  			if *check.Id == rs.Primary.ID {
   214  				return nil
   215  			}
   216  
   217  		}
   218  		return fmt.Errorf("Health Check does not exist")
   219  	}
   220  }
   221  
   222  func testUpdateHappened(n string) resource.TestCheckFunc {
   223  	return nil
   224  }
   225  
   226  const testAccRoute53HealthCheckConfig = `
   227  resource "aws_route53_health_check" "foo" {
   228    fqdn = "dev.notexample.com"
   229    port = 80
   230    type = "HTTP"
   231    resource_path = "/"
   232    failure_threshold = "2"
   233    request_interval = "30"
   234    measure_latency = true
   235    invert_healthcheck = true
   236  
   237    tags = {
   238      Name = "tf-test-health-check"
   239     }
   240  }
   241  `
   242  
   243  const testAccRoute53HealthCheckConfigUpdate = `
   244  resource "aws_route53_health_check" "foo" {
   245    fqdn = "dev.notexample.com"
   246    port = 80
   247    type = "HTTP"
   248    resource_path = "/"
   249    failure_threshold = "5"
   250    request_interval = "30"
   251    measure_latency = true
   252    invert_healthcheck = false
   253  
   254    tags = {
   255      Name = "tf-test-health-check"
   256     }
   257  }
   258  `
   259  
   260  const testAccRoute53HealthCheckIpConfig = `
   261  resource "aws_route53_health_check" "bar" {
   262    ip_address = "1.2.3.4"
   263    port = 80
   264    type = "HTTP"
   265    resource_path = "/"
   266    failure_threshold = "2"
   267    request_interval = "30"
   268  
   269    tags = {
   270      Name = "tf-test-health-check"
   271     }
   272  }
   273  `
   274  
   275  const testAccRoute53HealthCheckConfig_withChildHealthChecks = `
   276  resource "aws_route53_health_check" "child1" {
   277    fqdn = "child1.notexample.com"
   278    port = 80
   279    type = "HTTP"
   280    resource_path = "/"
   281    failure_threshold = "2"
   282    request_interval = "30"
   283  }
   284  
   285  resource "aws_route53_health_check" "foo" {
   286    type = "CALCULATED"
   287    child_health_threshold = 1
   288    child_healthchecks = ["${aws_route53_health_check.child1.id}"]
   289  
   290    tags = {
   291      Name = "tf-test-calculated-health-check"
   292     }
   293  }
   294  `
   295  
   296  const testAccRoute53HealthCheckCloudWatchAlarm = `
   297  resource "aws_cloudwatch_metric_alarm" "foobar" {
   298      alarm_name = "cloudwatch-healthcheck-alarm"
   299      comparison_operator = "GreaterThanOrEqualToThreshold"
   300      evaluation_periods = "2"
   301      metric_name = "CPUUtilization"
   302      namespace = "AWS/EC2"
   303      period = "120"
   304      statistic = "Average"
   305      threshold = "80"
   306      alarm_description = "This metric monitors ec2 cpu utilization"
   307  }
   308  
   309  resource "aws_route53_health_check" "foo" {
   310    type = "CLOUDWATCH_METRIC"
   311    cloudwatch_alarm_name = "${aws_cloudwatch_metric_alarm.foobar.alarm_name}"
   312    cloudwatch_alarm_region = "us-west-2"
   313    insufficient_data_health_status = "Healthy"
   314  }
   315  `
   316  
   317  const testAccRoute53HealthCheckConfigWithSearchString = `
   318  resource "aws_route53_health_check" "foo" {
   319    fqdn = "dev.notexample.com"
   320    port = 80
   321    type = "HTTP_STR_MATCH"
   322    resource_path = "/"
   323    failure_threshold = "2"
   324    request_interval = "30"
   325    measure_latency = true
   326    invert_healthcheck = false
   327    search_string = "OK"
   328  
   329    tags = {
   330      Name = "tf-test-health-check"
   331     }
   332  }
   333  `
   334  
   335  const testAccRoute53HealthCheckConfigWithSearchStringUpdate = `
   336  resource "aws_route53_health_check" "foo" {
   337    fqdn = "dev.notexample.com"
   338    port = 80
   339    type = "HTTP_STR_MATCH"
   340    resource_path = "/"
   341    failure_threshold = "5"
   342    request_interval = "30"
   343    measure_latency = true
   344    invert_healthcheck = true
   345    search_string = "FAILED"
   346  
   347    tags = {
   348      Name = "tf-test-health-check"
   349     }
   350  }
   351  `
   352  
   353  const testAccRoute53HealthCheckConfigWithoutSNI = `
   354  resource "aws_route53_health_check" "foo" {
   355    fqdn = "dev.notexample.com"
   356    port = 443
   357    type = "HTTPS"
   358    resource_path = "/"
   359    failure_threshold = "2"
   360    request_interval = "30"
   361    measure_latency = true
   362    invert_healthcheck = true
   363  
   364    tags = {
   365      Name = "tf-test-health-check"
   366     }
   367  }
   368  `
   369  
   370  const testAccRoute53HealthCheckConfigWithSNI = `
   371  resource "aws_route53_health_check" "foo" {
   372  	fqdn = "dev.notexample.com"
   373    port = 443
   374    type = "HTTPS"
   375    resource_path = "/"
   376    failure_threshold = "2"
   377    request_interval = "30"
   378    measure_latency = true
   379    invert_healthcheck = true
   380    enable_sni = true
   381  
   382    tags = {
   383      Name = "tf-test-health-check"
   384     }
   385  }
   386  `
   387  
   388  const testAccRoute53HealthCheckConfigWithSNIDisabled = `
   389  resource "aws_route53_health_check" "foo" {
   390  	fqdn = "dev.notexample.com"
   391    port = 443
   392    type = "HTTPS"
   393    resource_path = "/"
   394    failure_threshold = "2"
   395    request_interval = "30"
   396    measure_latency = true
   397    invert_healthcheck = true
   398    enable_sni = false
   399  
   400    tags = {
   401      Name = "tf-test-health-check"
   402     }
   403  }
   404  `