github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/google/resource_compute_https_health_check_test.go (about)

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