github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_target_pool_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  )
    11  
    12  func TestAccComputeTargetPool_basic(t *testing.T) {
    13  
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckComputeTargetPoolDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccComputeTargetPool_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckComputeTargetPoolExists(
    23  						"google_compute_target_pool.foobar"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckComputeTargetPoolDestroy(s *terraform.State) error {
    31  	config := testAccProvider.Meta().(*Config)
    32  
    33  	for _, rs := range s.RootModule().Resources {
    34  		if rs.Type != "google_compute_target_pool" {
    35  			continue
    36  		}
    37  
    38  		_, err := config.clientCompute.TargetPools.Get(
    39  			config.Project, config.Region, rs.Primary.ID).Do()
    40  		if err == nil {
    41  			return fmt.Errorf("TargetPool still exists")
    42  		}
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func testAccCheckComputeTargetPoolExists(n string) resource.TestCheckFunc {
    49  	return func(s *terraform.State) error {
    50  		rs, ok := s.RootModule().Resources[n]
    51  		if !ok {
    52  			return fmt.Errorf("Not found: %s", n)
    53  		}
    54  
    55  		if rs.Primary.ID == "" {
    56  			return fmt.Errorf("No ID is set")
    57  		}
    58  
    59  		config := testAccProvider.Meta().(*Config)
    60  
    61  		found, err := config.clientCompute.TargetPools.Get(
    62  			config.Project, config.Region, rs.Primary.ID).Do()
    63  		if err != nil {
    64  			return err
    65  		}
    66  
    67  		if found.Name != rs.Primary.ID {
    68  			return fmt.Errorf("TargetPool not found")
    69  		}
    70  
    71  		return nil
    72  	}
    73  }
    74  
    75  var testAccComputeTargetPool_basic = fmt.Sprintf(`
    76  resource "google_compute_http_health_check" "foobar" {
    77  	name = "healthcheck-test-%s"
    78  	host = "example.com"
    79  }
    80  
    81  resource "google_compute_target_pool" "foobar" {
    82  	description = "Resource created for Terraform acceptance testing"
    83  	instances = ["us-central1-a/foo", "us-central1-b/bar"]
    84  	name = "tpool-test-%s"
    85  	session_affinity = "CLIENT_IP_PROTO"
    86  	health_checks = [
    87  		"${google_compute_http_health_check.foobar.name}"
    88  	]
    89  }`, acctest.RandString(10), acctest.RandString(10))