github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_forwarding_rule_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 TestAccComputeForwardingRule_basic(t *testing.T) {
    13  	poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    14  	ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccComputeForwardingRule_basic(poolName, ruleName),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckComputeForwardingRuleExists(
    25  						"google_compute_forwarding_rule.foobar"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccComputeForwardingRule_ip(t *testing.T) {
    33  	addrName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    34  	poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    35  	ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccComputeForwardingRule_ip(addrName, poolName, ruleName),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckComputeForwardingRuleExists(
    46  						"google_compute_forwarding_rule.foobar"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testAccCheckComputeForwardingRuleDestroy(s *terraform.State) error {
    54  	config := testAccProvider.Meta().(*Config)
    55  
    56  	for _, rs := range s.RootModule().Resources {
    57  		if rs.Type != "google_compute_forwarding_rule" {
    58  			continue
    59  		}
    60  
    61  		_, err := config.clientCompute.ForwardingRules.Get(
    62  			config.Project, config.Region, rs.Primary.ID).Do()
    63  		if err == nil {
    64  			return fmt.Errorf("ForwardingRule still exists")
    65  		}
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func testAccCheckComputeForwardingRuleExists(n string) resource.TestCheckFunc {
    72  	return func(s *terraform.State) error {
    73  		rs, ok := s.RootModule().Resources[n]
    74  		if !ok {
    75  			return fmt.Errorf("Not found: %s", n)
    76  		}
    77  
    78  		if rs.Primary.ID == "" {
    79  			return fmt.Errorf("No ID is set")
    80  		}
    81  
    82  		config := testAccProvider.Meta().(*Config)
    83  
    84  		found, err := config.clientCompute.ForwardingRules.Get(
    85  			config.Project, config.Region, rs.Primary.ID).Do()
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		if found.Name != rs.Primary.ID {
    91  			return fmt.Errorf("ForwardingRule not found")
    92  		}
    93  
    94  		return nil
    95  	}
    96  }
    97  
    98  func testAccComputeForwardingRule_basic(poolName, ruleName string) string {
    99  	return fmt.Sprintf(`
   100  resource "google_compute_target_pool" "foobar-tp" {
   101    description = "Resource created for Terraform acceptance testing"
   102    instances   = ["us-central1-a/foo", "us-central1-b/bar"]
   103    name        = "%s"
   104  }
   105  resource "google_compute_forwarding_rule" "foobar" {
   106    description = "Resource created for Terraform acceptance testing"
   107    ip_protocol = "UDP"
   108    name        = "%s"
   109    port_range  = "80-81"
   110    target      = "${google_compute_target_pool.foobar-tp.self_link}"
   111  }
   112  `, poolName, ruleName)
   113  }
   114  
   115  func testAccComputeForwardingRule_ip(addrName, poolName, ruleName string) string {
   116  	return fmt.Sprintf(`
   117  resource "google_compute_address" "foo" {
   118    name = "%s"
   119  }
   120  resource "google_compute_target_pool" "foobar-tp" {
   121    description = "Resource created for Terraform acceptance testing"
   122    instances   = ["us-central1-a/foo", "us-central1-b/bar"]
   123    name        = "%s"
   124  }
   125  resource "google_compute_forwarding_rule" "foobar" {
   126    description = "Resource created for Terraform acceptance testing"
   127    ip_address  = "${google_compute_address.foo.address}"
   128    ip_protocol = "TCP"
   129    name        = "%s"
   130    port_range  = "80-81"
   131    target      = "${google_compute_target_pool.foobar-tp.self_link}"
   132  }
   133  `, addrName, poolName, ruleName)
   134  }