github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_route_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 TestAccComputeRoute_basic(t *testing.T) {
    14  	var route compute.Route
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckComputeRouteDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccComputeRoute_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckComputeRouteExists(
    25  						"google_compute_route.foobar", &route),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccComputeRoute_defaultInternetGateway(t *testing.T) {
    33  	var route compute.Route
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckComputeRouteDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccComputeRoute_defaultInternetGateway,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckComputeRouteExists(
    44  						"google_compute_route.foobar", &route),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckComputeRouteDestroy(s *terraform.State) error {
    52  	config := testAccProvider.Meta().(*Config)
    53  
    54  	for _, rs := range s.RootModule().Resources {
    55  		if rs.Type != "google_compute_route" {
    56  			continue
    57  		}
    58  
    59  		_, err := config.clientCompute.Routes.Get(
    60  			config.Project, rs.Primary.ID).Do()
    61  		if err == nil {
    62  			return fmt.Errorf("Route still exists")
    63  		}
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func testAccCheckComputeRouteExists(n string, route *compute.Route) resource.TestCheckFunc {
    70  	return func(s *terraform.State) error {
    71  		rs, ok := s.RootModule().Resources[n]
    72  		if !ok {
    73  			return fmt.Errorf("Not found: %s", n)
    74  		}
    75  
    76  		if rs.Primary.ID == "" {
    77  			return fmt.Errorf("No ID is set")
    78  		}
    79  
    80  		config := testAccProvider.Meta().(*Config)
    81  
    82  		found, err := config.clientCompute.Routes.Get(
    83  			config.Project, rs.Primary.ID).Do()
    84  		if err != nil {
    85  			return err
    86  		}
    87  
    88  		if found.Name != rs.Primary.ID {
    89  			return fmt.Errorf("Route not found")
    90  		}
    91  
    92  		*route = *found
    93  
    94  		return nil
    95  	}
    96  }
    97  
    98  var testAccComputeRoute_basic = fmt.Sprintf(`
    99  resource "google_compute_network" "foobar" {
   100  	name = "route-test-%s"
   101  	ipv4_range = "10.0.0.0/16"
   102  }
   103  
   104  resource "google_compute_route" "foobar" {
   105  	name = "route-test-%s"
   106  	dest_range = "15.0.0.0/24"
   107  	network = "${google_compute_network.foobar.name}"
   108  	next_hop_ip = "10.0.1.5"
   109  	priority = 100
   110  }`, acctest.RandString(10), acctest.RandString(10))
   111  
   112  var testAccComputeRoute_defaultInternetGateway = fmt.Sprintf(`
   113  resource "google_compute_network" "foobar" {
   114  	name = "route-test-%s"
   115  	ipv4_range = "10.0.0.0/16"
   116  }
   117  
   118  resource "google_compute_route" "foobar" {
   119  	name = "route-test-%s"
   120  	dest_range = "0.0.0.0/0"
   121  	network = "${google_compute_network.foobar.name}"
   122  	next_hop_gateway = "default-internet-gateway"
   123  	priority = 100
   124  }`, acctest.RandString(10), acctest.RandString(10))