github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckRoute53HealthCheckDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccRoute53HealthCheckConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    23  				),
    24  			},
    25  			resource.TestStep{
    26  				Config: testAccRoute53HealthCheckConfigUpdate,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.foo"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_route53_health_check.foo", "failure_threshold", "5"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAWSRoute53HealthCheck_IpConfig(t *testing.T) {
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testAccCheckRoute53HealthCheckDestroy,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: testAccRoute53HealthCheckIpConfig,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckRoute53HealthCheckExists("aws_route53_health_check.bar"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testAccCheckRoute53HealthCheckDestroy(s *terraform.State) error {
    54  	conn := testAccProvider.Meta().(*AWSClient).r53conn
    55  
    56  	for _, rs := range s.RootModule().Resources {
    57  		if rs.Type != "aws_route53_health_check" {
    58  			continue
    59  		}
    60  
    61  		lopts := &route53.ListHealthChecksInput{}
    62  		resp, err := conn.ListHealthChecks(lopts)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		if len(resp.HealthChecks) == 0 {
    67  			return nil
    68  		}
    69  
    70  		for _, check := range resp.HealthChecks {
    71  			if *check.Id == rs.Primary.ID {
    72  				return fmt.Errorf("Record still exists: %#v", check)
    73  			}
    74  
    75  		}
    76  
    77  	}
    78  	return nil
    79  }
    80  
    81  func testAccCheckRoute53HealthCheckExists(n string) resource.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  		conn := testAccProvider.Meta().(*AWSClient).r53conn
    84  
    85  		rs, ok := s.RootModule().Resources[n]
    86  		if !ok {
    87  			return fmt.Errorf("Not found: %s", n)
    88  		}
    89  
    90  		fmt.Print(rs.Primary.ID)
    91  
    92  		if rs.Primary.ID == "" {
    93  			return fmt.Errorf("No health check ID is set")
    94  		}
    95  
    96  		lopts := &route53.ListHealthChecksInput{}
    97  		resp, err := conn.ListHealthChecks(lopts)
    98  		if err != nil {
    99  			return err
   100  		}
   101  		if len(resp.HealthChecks) == 0 {
   102  			return fmt.Errorf("Health Check does not exist")
   103  		}
   104  
   105  		for _, check := range resp.HealthChecks {
   106  			if *check.Id == rs.Primary.ID {
   107  				return nil
   108  			}
   109  
   110  		}
   111  		return fmt.Errorf("Health Check does not exist")
   112  	}
   113  }
   114  
   115  func testUpdateHappened(n string) resource.TestCheckFunc {
   116  	return nil
   117  }
   118  
   119  const testAccRoute53HealthCheckConfig = `
   120  resource "aws_route53_health_check" "foo" {
   121    fqdn = "dev.notexample.com"
   122    port = 80
   123    type = "HTTP"
   124    resource_path = "/"
   125    failure_threshold = "2"
   126    request_interval = "30"
   127  
   128    tags = {
   129      Name = "tf-test-health-check"
   130     }
   131  }
   132  `
   133  
   134  const testAccRoute53HealthCheckConfigUpdate = `
   135  resource "aws_route53_health_check" "foo" {
   136    fqdn = "dev.notexample.com"
   137    port = 80
   138    type = "HTTP"
   139    resource_path = "/"
   140    failure_threshold = "5"
   141    request_interval = "30"
   142  
   143    tags = {
   144      Name = "tf-test-health-check"
   145     }
   146  }
   147  `
   148  
   149  const testAccRoute53HealthCheckIpConfig = `
   150  resource "aws_route53_health_check" "bar" {
   151    ip_address = "1.2.3.4"
   152    port = 80
   153    type = "HTTP"
   154    resource_path = "/"
   155    failure_threshold = "2"
   156    request_interval = "30"
   157  
   158    tags = {
   159      Name = "tf-test-health-check"
   160     }
   161  }
   162  `