github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_http_health_check_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"google.golang.org/api/compute/v1"
    10  )
    11  
    12  func TestAccComputeHttpHealthCheck_basic(t *testing.T) {
    13  	var healthCheck compute.HttpHealthCheck
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckComputeHttpHealthCheckDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccComputeHttpHealthCheck_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckComputeHttpHealthCheckExists(
    24  						"google_compute_http_health_check.foobar", &healthCheck),
    25  					testAccCheckComputeHttpHealthCheckRequestPath(
    26  						"/health_check", &healthCheck),
    27  					testAccCheckComputeHttpHealthCheckThresholds(
    28  						3, 3, &healthCheck),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccComputeHttpHealthCheck_update(t *testing.T) {
    36  	var healthCheck compute.HttpHealthCheck
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testAccCheckComputeHttpHealthCheckDestroy,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: testAccComputeHttpHealthCheck_update1,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckComputeHttpHealthCheckExists(
    47  						"google_compute_http_health_check.foobar", &healthCheck),
    48  					testAccCheckComputeHttpHealthCheckRequestPath(
    49  						"/not_default", &healthCheck),
    50  					testAccCheckComputeHttpHealthCheckThresholds(
    51  						2, 2, &healthCheck),
    52  				),
    53  			},
    54  			resource.TestStep{
    55  				Config: testAccComputeHttpHealthCheck_update2,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckComputeHttpHealthCheckExists(
    58  						"google_compute_http_health_check.foobar", &healthCheck),
    59  					testAccCheckComputeHttpHealthCheckRequestPath(
    60  						"/", &healthCheck),
    61  					testAccCheckComputeHttpHealthCheckThresholds(
    62  						10, 10, &healthCheck),
    63  				),
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  func testAccCheckComputeHttpHealthCheckDestroy(s *terraform.State) error {
    70  	config := testAccProvider.Meta().(*Config)
    71  
    72  	for _, rs := range s.RootModule().Resources {
    73  		if rs.Type != "google_compute_http_health_check" {
    74  			continue
    75  		}
    76  
    77  		_, err := config.clientCompute.HttpHealthChecks.Get(
    78  			config.Project, rs.Primary.ID).Do()
    79  		if err == nil {
    80  			return fmt.Errorf("HttpHealthCheck still exists")
    81  		}
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func testAccCheckComputeHttpHealthCheckExists(n string, healthCheck *compute.HttpHealthCheck) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		rs, ok := s.RootModule().Resources[n]
    90  		if !ok {
    91  			return fmt.Errorf("Not found: %s", n)
    92  		}
    93  
    94  		if rs.Primary.ID == "" {
    95  			return fmt.Errorf("No ID is set")
    96  		}
    97  
    98  		config := testAccProvider.Meta().(*Config)
    99  
   100  		found, err := config.clientCompute.HttpHealthChecks.Get(
   101  			config.Project, rs.Primary.ID).Do()
   102  		if err != nil {
   103  			return err
   104  		}
   105  
   106  		if found.Name != rs.Primary.ID {
   107  			return fmt.Errorf("HttpHealthCheck not found")
   108  		}
   109  
   110  		*healthCheck = *found
   111  
   112  		return nil
   113  	}
   114  }
   115  
   116  func testAccCheckComputeHttpHealthCheckRequestPath(path string, healthCheck *compute.HttpHealthCheck) resource.TestCheckFunc {
   117  	return func(s *terraform.State) error {
   118  		if healthCheck.RequestPath != path {
   119  			return fmt.Errorf("RequestPath doesn't match: expected %s, got %s", path, healthCheck.RequestPath)
   120  		}
   121  
   122  		return nil
   123  	}
   124  }
   125  
   126  func testAccCheckComputeHttpHealthCheckThresholds(healthy, unhealthy int64, healthCheck *compute.HttpHealthCheck) resource.TestCheckFunc {
   127  	return func(s *terraform.State) error {
   128  		if healthCheck.HealthyThreshold != healthy {
   129  			return fmt.Errorf("HealthyThreshold doesn't match: expected %d, got %d", healthy, healthCheck.HealthyThreshold)
   130  		}
   131  
   132  		if healthCheck.UnhealthyThreshold != unhealthy {
   133  			return fmt.Errorf("UnhealthyThreshold doesn't match: expected %d, got %d", unhealthy, healthCheck.UnhealthyThreshold)
   134  		}
   135  
   136  		return nil
   137  	}
   138  }
   139  
   140  const testAccComputeHttpHealthCheck_basic = `
   141  resource "google_compute_http_health_check" "foobar" {
   142  	check_interval_sec = 3
   143  	description = "Resource created for Terraform acceptance testing"
   144  	healthy_threshold = 3
   145  	host = "foobar"
   146  	name = "terraform-test"
   147  	port = "80"
   148  	request_path = "/health_check"
   149  	timeout_sec = 2
   150  	unhealthy_threshold = 3
   151  }
   152  `
   153  
   154  const testAccComputeHttpHealthCheck_update1 = `
   155  resource "google_compute_http_health_check" "foobar" {
   156  	name = "terraform-test"
   157  	description = "Resource created for Terraform acceptance testing"
   158  	request_path = "/not_default"
   159  }
   160  `
   161  
   162  /* Change description, restore request_path to default, and change
   163  * thresholds from defaults */
   164  const testAccComputeHttpHealthCheck_update2 = `
   165  resource "google_compute_http_health_check" "foobar" {
   166  	name = "terraform-test"
   167  	description = "Resource updated for Terraform acceptance testing"
   168  	healthy_threshold = 10
   169  	unhealthy_threshold = 10
   170  }
   171  `