github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_singlePort(t *testing.T) {
    33  	poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    34  	ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccComputeForwardingRule_singlePort(poolName, ruleName),
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckComputeForwardingRuleExists(
    45  						"google_compute_forwarding_rule.foobar"),
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func TestAccComputeForwardingRule_ip(t *testing.T) {
    53  	addrName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    54  	poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    55  	ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    56  
    57  	resource.Test(t, resource.TestCase{
    58  		PreCheck:     func() { testAccPreCheck(t) },
    59  		Providers:    testAccProviders,
    60  		CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
    61  		Steps: []resource.TestStep{
    62  			resource.TestStep{
    63  				Config: testAccComputeForwardingRule_ip(addrName, poolName, ruleName),
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckComputeForwardingRuleExists(
    66  						"google_compute_forwarding_rule.foobar"),
    67  				),
    68  			},
    69  		},
    70  	})
    71  }
    72  
    73  func TestAccComputeForwardingRule_internalLoadBalancing(t *testing.T) {
    74  	serviceName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    75  	checkName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    76  	ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    77  
    78  	resource.Test(t, resource.TestCase{
    79  		PreCheck:     func() { testAccPreCheck(t) },
    80  		Providers:    testAccProviders,
    81  		CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
    82  		Steps: []resource.TestStep{
    83  			resource.TestStep{
    84  				Config: testAccComputeForwardingRule_internalLoadBalancing(serviceName, checkName, ruleName),
    85  				Check: resource.ComposeTestCheckFunc(
    86  					testAccCheckComputeForwardingRuleExists(
    87  						"google_compute_forwarding_rule.foobar"),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func testAccCheckComputeForwardingRuleDestroy(s *terraform.State) error {
    95  	config := testAccProvider.Meta().(*Config)
    96  
    97  	for _, rs := range s.RootModule().Resources {
    98  		if rs.Type != "google_compute_forwarding_rule" {
    99  			continue
   100  		}
   101  
   102  		_, err := config.clientCompute.ForwardingRules.Get(
   103  			config.Project, config.Region, rs.Primary.ID).Do()
   104  		if err == nil {
   105  			return fmt.Errorf("ForwardingRule still exists")
   106  		}
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  func testAccCheckComputeForwardingRuleExists(n string) resource.TestCheckFunc {
   113  	return func(s *terraform.State) error {
   114  		rs, ok := s.RootModule().Resources[n]
   115  		if !ok {
   116  			return fmt.Errorf("Not found: %s", n)
   117  		}
   118  
   119  		if rs.Primary.ID == "" {
   120  			return fmt.Errorf("No ID is set")
   121  		}
   122  
   123  		config := testAccProvider.Meta().(*Config)
   124  
   125  		found, err := config.clientCompute.ForwardingRules.Get(
   126  			config.Project, config.Region, rs.Primary.ID).Do()
   127  		if err != nil {
   128  			return err
   129  		}
   130  
   131  		if found.Name != rs.Primary.ID {
   132  			return fmt.Errorf("ForwardingRule not found")
   133  		}
   134  
   135  		return nil
   136  	}
   137  }
   138  
   139  func testAccComputeForwardingRule_basic(poolName, ruleName string) string {
   140  	return fmt.Sprintf(`
   141  resource "google_compute_target_pool" "foobar-tp" {
   142    description = "Resource created for Terraform acceptance testing"
   143    instances   = ["us-central1-a/foo", "us-central1-b/bar"]
   144    name        = "%s"
   145  }
   146  resource "google_compute_forwarding_rule" "foobar" {
   147    description = "Resource created for Terraform acceptance testing"
   148    ip_protocol = "UDP"
   149    name        = "%s"
   150    port_range  = "80-81"
   151    target      = "${google_compute_target_pool.foobar-tp.self_link}"
   152  }
   153  `, poolName, ruleName)
   154  }
   155  
   156  func testAccComputeForwardingRule_singlePort(poolName, ruleName string) string {
   157  	return fmt.Sprintf(`
   158  resource "google_compute_target_pool" "foobar-tp" {
   159    description = "Resource created for Terraform acceptance testing"
   160    instances   = ["us-central1-a/foo", "us-central1-b/bar"]
   161    name        = "%s"
   162  }
   163  resource "google_compute_forwarding_rule" "foobar" {
   164    description = "Resource created for Terraform acceptance testing"
   165    ip_protocol = "UDP"
   166    name        = "%s"
   167    port_range  = "80"
   168    target      = "${google_compute_target_pool.foobar-tp.self_link}"
   169  }
   170  `, poolName, ruleName)
   171  }
   172  
   173  func testAccComputeForwardingRule_ip(addrName, poolName, ruleName string) string {
   174  	return fmt.Sprintf(`
   175  resource "google_compute_address" "foo" {
   176    name = "%s"
   177  }
   178  resource "google_compute_target_pool" "foobar-tp" {
   179    description = "Resource created for Terraform acceptance testing"
   180    instances   = ["us-central1-a/foo", "us-central1-b/bar"]
   181    name        = "%s"
   182  }
   183  resource "google_compute_forwarding_rule" "foobar" {
   184    description = "Resource created for Terraform acceptance testing"
   185    ip_address  = "${google_compute_address.foo.address}"
   186    ip_protocol = "TCP"
   187    name        = "%s"
   188    port_range  = "80-81"
   189    target      = "${google_compute_target_pool.foobar-tp.self_link}"
   190  }
   191  `, addrName, poolName, ruleName)
   192  }
   193  
   194  func testAccComputeForwardingRule_internalLoadBalancing(serviceName, checkName, ruleName string) string {
   195  	return fmt.Sprintf(`
   196  resource "google_compute_region_backend_service" "foobar-bs" {
   197    name                  = "%s"
   198    description           = "Resource created for Terraform acceptance testing"
   199    health_checks         = ["${google_compute_health_check.zero.self_link}"]
   200    region                = "us-central1"
   201  }
   202  resource "google_compute_health_check" "zero" {
   203    name               = "%s"
   204    description        = "Resource created for Terraform acceptance testing"
   205    check_interval_sec = 1
   206    timeout_sec        = 1
   207  
   208    tcp_health_check {
   209      port = "80"
   210    }
   211  }
   212  resource "google_compute_forwarding_rule" "foobar" {
   213    description           = "Resource created for Terraform acceptance testing"
   214    name                  = "%s"
   215    load_balancing_scheme = "INTERNAL"
   216    backend_service       = "${google_compute_region_backend_service.foobar-bs.self_link}"
   217    ports                 = ["80"]
   218  }
   219  `, serviceName, checkName, ruleName)
   220  }