github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 testAccCheckRoute53HealthCheckDestroy(s *terraform.State) error {
   126  	conn := testAccProvider.Meta().(*AWSClient).r53conn
   127  
   128  	for _, rs := range s.RootModule().Resources {
   129  		if rs.Type != "aws_route53_health_check" {
   130  			continue
   131  		}
   132  
   133  		lopts := &route53.ListHealthChecksInput{}
   134  		resp, err := conn.ListHealthChecks(lopts)
   135  		if err != nil {
   136  			return err
   137  		}
   138  		if len(resp.HealthChecks) == 0 {
   139  			return nil
   140  		}
   141  
   142  		for _, check := range resp.HealthChecks {
   143  			if *check.Id == rs.Primary.ID {
   144  				return fmt.Errorf("Record still exists: %#v", check)
   145  			}
   146  
   147  		}
   148  
   149  	}
   150  	return nil
   151  }
   152  
   153  func testAccCheckRoute53HealthCheckExists(n string) resource.TestCheckFunc {
   154  	return func(s *terraform.State) error {
   155  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   156  
   157  		rs, ok := s.RootModule().Resources[n]
   158  		if !ok {
   159  			return fmt.Errorf("Not found: %s", n)
   160  		}
   161  
   162  		fmt.Print(rs.Primary.ID)
   163  
   164  		if rs.Primary.ID == "" {
   165  			return fmt.Errorf("No health check ID is set")
   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 fmt.Errorf("Health Check does not exist")
   175  		}
   176  
   177  		for _, check := range resp.HealthChecks {
   178  			if *check.Id == rs.Primary.ID {
   179  				return nil
   180  			}
   181  
   182  		}
   183  		return fmt.Errorf("Health Check does not exist")
   184  	}
   185  }
   186  
   187  func testUpdateHappened(n string) resource.TestCheckFunc {
   188  	return nil
   189  }
   190  
   191  const testAccRoute53HealthCheckConfig = `
   192  resource "aws_route53_health_check" "foo" {
   193    fqdn = "dev.notexample.com"
   194    port = 80
   195    type = "HTTP"
   196    resource_path = "/"
   197    failure_threshold = "2"
   198    request_interval = "30"
   199    measure_latency = true
   200    invert_healthcheck = true
   201  
   202    tags = {
   203      Name = "tf-test-health-check"
   204     }
   205  }
   206  `
   207  
   208  const testAccRoute53HealthCheckConfigUpdate = `
   209  resource "aws_route53_health_check" "foo" {
   210    fqdn = "dev.notexample.com"
   211    port = 80
   212    type = "HTTP"
   213    resource_path = "/"
   214    failure_threshold = "5"
   215    request_interval = "30"
   216    measure_latency = true
   217    invert_healthcheck = false
   218  
   219    tags = {
   220      Name = "tf-test-health-check"
   221     }
   222  }
   223  `
   224  
   225  const testAccRoute53HealthCheckIpConfig = `
   226  resource "aws_route53_health_check" "bar" {
   227    ip_address = "1.2.3.4"
   228    port = 80
   229    type = "HTTP"
   230    resource_path = "/"
   231    failure_threshold = "2"
   232    request_interval = "30"
   233  
   234    tags = {
   235      Name = "tf-test-health-check"
   236     }
   237  }
   238  `
   239  
   240  const testAccRoute53HealthCheckConfig_withChildHealthChecks = `
   241  resource "aws_route53_health_check" "child1" {
   242    fqdn = "child1.notexample.com"
   243    port = 80
   244    type = "HTTP"
   245    resource_path = "/"
   246    failure_threshold = "2"
   247    request_interval = "30"
   248  }
   249  
   250  resource "aws_route53_health_check" "foo" {
   251    type = "CALCULATED"
   252    child_health_threshold = 1
   253    child_healthchecks = ["${aws_route53_health_check.child1.id}"]
   254  
   255    tags = {
   256      Name = "tf-test-calculated-health-check"
   257     }
   258  }
   259  `
   260  
   261  const testAccRoute53HealthCheckCloudWatchAlarm = `
   262  resource "aws_cloudwatch_metric_alarm" "foobar" {
   263      alarm_name = "cloudwatch-healthcheck-alarm"
   264      comparison_operator = "GreaterThanOrEqualToThreshold"
   265      evaluation_periods = "2"
   266      metric_name = "CPUUtilization"
   267      namespace = "AWS/EC2"
   268      period = "120"
   269      statistic = "Average"
   270      threshold = "80"
   271      alarm_description = "This metric monitor ec2 cpu utilization"
   272  }
   273  
   274  resource "aws_route53_health_check" "foo" {
   275    type = "CLOUDWATCH_METRIC"
   276    cloudwatch_alarm_name = "${aws_cloudwatch_metric_alarm.foobar.alarm_name}"
   277    cloudwatch_alarm_region = "us-west-2"
   278    insufficient_data_health_status = "Healthy"
   279  }
   280  `
   281  
   282  const testAccRoute53HealthCheckConfigWithSearchString = `
   283  resource "aws_route53_health_check" "foo" {
   284    fqdn = "dev.notexample.com"
   285    port = 80
   286    type = "HTTP_STR_MATCH"
   287    resource_path = "/"
   288    failure_threshold = "2"
   289    request_interval = "30"
   290    measure_latency = true
   291    invert_healthcheck = false
   292    search_string = "OK"
   293  
   294    tags = {
   295      Name = "tf-test-health-check"
   296     }
   297  }
   298  `
   299  
   300  const testAccRoute53HealthCheckConfigWithSearchStringUpdate = `
   301  resource "aws_route53_health_check" "foo" {
   302    fqdn = "dev.notexample.com"
   303    port = 80
   304    type = "HTTP_STR_MATCH"
   305    resource_path = "/"
   306    failure_threshold = "5"
   307    request_interval = "30"
   308    measure_latency = true
   309    invert_healthcheck = true
   310    search_string = "FAILED"
   311  
   312    tags = {
   313      Name = "tf-test-health-check"
   314     }
   315  }
   316  `