github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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_withChildHealthChecks(t *testing.T) {
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:     func() { testAccPreCheck(t) },
    47  		Providers:    testAccProviders,
    48  		CheckDestroy: testAccCheckRoute53HealthCheckDestroy,
    49  		Steps: []resource.TestStep{
    50  			resource.TestStep{
    51  				Config: testAccRoute53HealthCheckConfig_withChildHealthChecks,
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func TestAccAWSRoute53HealthCheck_IpConfig(t *testing.T) {
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckRoute53HealthCheckDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccRoute53HealthCheckIpConfig,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.bar"),
    70  				),
    71  			},
    72  		},
    73  	})
    74  }
    75  
    76  func TestAccAWSRoute53HealthCheck_CloudWatchAlarmCheck(t *testing.T) {
    77  	resource.Test(t, resource.TestCase{
    78  		PreCheck:     func() { testAccPreCheck(t) },
    79  		Providers:    testAccProviders,
    80  		CheckDestroy: testAccCheckRoute53HealthCheckDestroy,
    81  		Steps: []resource.TestStep{
    82  			resource.TestStep{
    83  				Config: testAccRoute53HealthCheckCloudWatchAlarm,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    86  					resource.TestCheckResourceAttr(
    87  						"aws_route53_health_check.foo", "cloudwatch_alarm_name", "cloudwatch-healthcheck-alarm"),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func testAccCheckRoute53HealthCheckDestroy(s *terraform.State) error {
    95  	conn := testAccProvider.Meta().(*AWSClient).r53conn
    96  
    97  	for _, rs := range s.RootModule().Resources {
    98  		if rs.Type != "aws_route53_health_check" {
    99  			continue
   100  		}
   101  
   102  		lopts := &route53.ListHealthChecksInput{}
   103  		resp, err := conn.ListHealthChecks(lopts)
   104  		if err != nil {
   105  			return err
   106  		}
   107  		if len(resp.HealthChecks) == 0 {
   108  			return nil
   109  		}
   110  
   111  		for _, check := range resp.HealthChecks {
   112  			if *check.Id == rs.Primary.ID {
   113  				return fmt.Errorf("Record still exists: %#v", check)
   114  			}
   115  
   116  		}
   117  
   118  	}
   119  	return nil
   120  }
   121  
   122  func testAccCheckRoute53HealthCheckExists(n string) resource.TestCheckFunc {
   123  	return func(s *terraform.State) error {
   124  		conn := testAccProvider.Meta().(*AWSClient).r53conn
   125  
   126  		rs, ok := s.RootModule().Resources[n]
   127  		if !ok {
   128  			return fmt.Errorf("Not found: %s", n)
   129  		}
   130  
   131  		fmt.Print(rs.Primary.ID)
   132  
   133  		if rs.Primary.ID == "" {
   134  			return fmt.Errorf("No health check ID is set")
   135  		}
   136  
   137  		lopts := &route53.ListHealthChecksInput{}
   138  		resp, err := conn.ListHealthChecks(lopts)
   139  		if err != nil {
   140  			return err
   141  		}
   142  		if len(resp.HealthChecks) == 0 {
   143  			return fmt.Errorf("Health Check does not exist")
   144  		}
   145  
   146  		for _, check := range resp.HealthChecks {
   147  			if *check.Id == rs.Primary.ID {
   148  				return nil
   149  			}
   150  
   151  		}
   152  		return fmt.Errorf("Health Check does not exist")
   153  	}
   154  }
   155  
   156  func testUpdateHappened(n string) resource.TestCheckFunc {
   157  	return nil
   158  }
   159  
   160  const testAccRoute53HealthCheckConfig = `
   161  resource "aws_route53_health_check" "foo" {
   162    fqdn = "dev.notexample.com"
   163    port = 80
   164    type = "HTTP"
   165    resource_path = "/"
   166    failure_threshold = "2"
   167    request_interval = "30"
   168    measure_latency = true
   169    invert_healthcheck = true
   170  
   171    tags = {
   172      Name = "tf-test-health-check"
   173     }
   174  }
   175  `
   176  
   177  const testAccRoute53HealthCheckConfigUpdate = `
   178  resource "aws_route53_health_check" "foo" {
   179    fqdn = "dev.notexample.com"
   180    port = 80
   181    type = "HTTP"
   182    resource_path = "/"
   183    failure_threshold = "5"
   184    request_interval = "30"
   185    measure_latency = true
   186    invert_healthcheck = false
   187  
   188    tags = {
   189      Name = "tf-test-health-check"
   190     }
   191  }
   192  `
   193  
   194  const testAccRoute53HealthCheckIpConfig = `
   195  resource "aws_route53_health_check" "bar" {
   196    ip_address = "1.2.3.4"
   197    port = 80
   198    type = "HTTP"
   199    resource_path = "/"
   200    failure_threshold = "2"
   201    request_interval = "30"
   202  
   203    tags = {
   204      Name = "tf-test-health-check"
   205     }
   206  }
   207  `
   208  
   209  const testAccRoute53HealthCheckConfig_withChildHealthChecks = `
   210  resource "aws_route53_health_check" "child1" {
   211    fqdn = "child1.notexample.com"
   212    port = 80
   213    type = "HTTP"
   214    resource_path = "/"
   215    failure_threshold = "2"
   216    request_interval = "30"
   217  }
   218  
   219  resource "aws_route53_health_check" "foo" {
   220    type = "CALCULATED"
   221    child_health_threshold = 1
   222    child_healthchecks = ["${aws_route53_health_check.child1.id}"]
   223  
   224    tags = {
   225      Name = "tf-test-calculated-health-check"
   226     }
   227  }
   228  `
   229  
   230  const testAccRoute53HealthCheckCloudWatchAlarm = `
   231  resource "aws_cloudwatch_metric_alarm" "foobar" {
   232      alarm_name = "cloudwatch-healthcheck-alarm"
   233      comparison_operator = "GreaterThanOrEqualToThreshold"
   234      evaluation_periods = "2"
   235      metric_name = "CPUUtilization"
   236      namespace = "AWS/EC2"
   237      period = "120"
   238      statistic = "Average"
   239      threshold = "80"
   240      alarm_description = "This metric monitor ec2 cpu utilization"
   241  }
   242  
   243  resource "aws_route53_health_check" "foo" {
   244    type = "CLOUDWATCH_METRIC"
   245    cloudwatch_alarm_name = "${aws_cloudwatch_metric_alarm.foobar.alarm_name}"
   246    cloudwatch_alarm_region = "us-west-2"
   247    insufficient_data_health_status = "Healthy"
   248  }
   249  `