github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 TestAccComputeForwardingRule_internalLoadBalancing(t *testing.T) {
    54  	serviceName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    55  	checkName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    56  	ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
    62  		Steps: []resource.TestStep{
    63  			resource.TestStep{
    64  				Config: testAccComputeForwardingRule_internalLoadBalancing(serviceName, checkName, ruleName),
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckComputeForwardingRuleExists(
    67  						"google_compute_forwarding_rule.foobar"),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func testAccCheckComputeForwardingRuleDestroy(s *terraform.State) error {
    75  	config := testAccProvider.Meta().(*Config)
    76  
    77  	for _, rs := range s.RootModule().Resources {
    78  		if rs.Type != "google_compute_forwarding_rule" {
    79  			continue
    80  		}
    81  
    82  		_, err := config.clientCompute.ForwardingRules.Get(
    83  			config.Project, config.Region, rs.Primary.ID).Do()
    84  		if err == nil {
    85  			return fmt.Errorf("ForwardingRule still exists")
    86  		}
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func testAccCheckComputeForwardingRuleExists(n string) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		rs, ok := s.RootModule().Resources[n]
    95  		if !ok {
    96  			return fmt.Errorf("Not found: %s", n)
    97  		}
    98  
    99  		if rs.Primary.ID == "" {
   100  			return fmt.Errorf("No ID is set")
   101  		}
   102  
   103  		config := testAccProvider.Meta().(*Config)
   104  
   105  		found, err := config.clientCompute.ForwardingRules.Get(
   106  			config.Project, config.Region, rs.Primary.ID).Do()
   107  		if err != nil {
   108  			return err
   109  		}
   110  
   111  		if found.Name != rs.Primary.ID {
   112  			return fmt.Errorf("ForwardingRule not found")
   113  		}
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  func testAccComputeForwardingRule_basic(poolName, ruleName string) string {
   120  	return fmt.Sprintf(`
   121  resource "google_compute_target_pool" "foobar-tp" {
   122    description = "Resource created for Terraform acceptance testing"
   123    instances   = ["us-central1-a/foo", "us-central1-b/bar"]
   124    name        = "%s"
   125  }
   126  resource "google_compute_forwarding_rule" "foobar" {
   127    description = "Resource created for Terraform acceptance testing"
   128    ip_protocol = "UDP"
   129    name        = "%s"
   130    port_range  = "80-81"
   131    target      = "${google_compute_target_pool.foobar-tp.self_link}"
   132  }
   133  `, poolName, ruleName)
   134  }
   135  
   136  func testAccComputeForwardingRule_ip(addrName, poolName, ruleName string) string {
   137  	return fmt.Sprintf(`
   138  resource "google_compute_address" "foo" {
   139    name = "%s"
   140  }
   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_address  = "${google_compute_address.foo.address}"
   149    ip_protocol = "TCP"
   150    name        = "%s"
   151    port_range  = "80-81"
   152    target      = "${google_compute_target_pool.foobar-tp.self_link}"
   153  }
   154  `, addrName, poolName, ruleName)
   155  }
   156  
   157  func testAccComputeForwardingRule_internalLoadBalancing(serviceName, checkName, ruleName string) string {
   158  	return fmt.Sprintf(`
   159  resource "google_compute_region_backend_service" "foobar-bs" {
   160    name                  = "%s"
   161    description           = "Resource created for Terraform acceptance testing"
   162    health_checks         = ["${google_compute_health_check.zero.self_link}"]
   163    region                = "us-central1"
   164  }
   165  resource "google_compute_health_check" "zero" {
   166    name               = "%s"
   167    description        = "Resource created for Terraform acceptance testing"
   168    check_interval_sec = 1
   169    timeout_sec        = 1
   170  
   171    tcp_health_check {
   172      port = "80"
   173    }
   174  }
   175  resource "google_compute_forwarding_rule" "foobar" {
   176    description           = "Resource created for Terraform acceptance testing"
   177    name                  = "%s"
   178    load_balancing_scheme = "INTERNAL"
   179    backend_service       = "${google_compute_region_backend_service.foobar-bs.self_link}"
   180    ports                 = ["80"]
   181  }
   182  `, serviceName, checkName, ruleName)
   183  }