github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 testAccCheckComputeRouteDestroy(s *terraform.State) error {
    33  	config := testAccProvider.Meta().(*Config)
    34  
    35  	for _, rs := range s.RootModule().Resources {
    36  		if rs.Type != "google_compute_route" {
    37  			continue
    38  		}
    39  
    40  		_, err := config.clientCompute.Routes.Get(
    41  			config.Project, rs.Primary.ID).Do()
    42  		if err == nil {
    43  			return fmt.Errorf("Route still exists")
    44  		}
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func testAccCheckComputeRouteExists(n string, route *compute.Route) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		rs, ok := s.RootModule().Resources[n]
    53  		if !ok {
    54  			return fmt.Errorf("Not found: %s", n)
    55  		}
    56  
    57  		if rs.Primary.ID == "" {
    58  			return fmt.Errorf("No ID is set")
    59  		}
    60  
    61  		config := testAccProvider.Meta().(*Config)
    62  
    63  		found, err := config.clientCompute.Routes.Get(
    64  			config.Project, rs.Primary.ID).Do()
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		if found.Name != rs.Primary.ID {
    70  			return fmt.Errorf("Route not found")
    71  		}
    72  
    73  		*route = *found
    74  
    75  		return nil
    76  	}
    77  }
    78  
    79  var testAccComputeRoute_basic = fmt.Sprintf(`
    80  resource "google_compute_network" "foobar" {
    81  	name = "route-test-%s"
    82  	ipv4_range = "10.0.0.0/16"
    83  }
    84  
    85  resource "google_compute_route" "foobar" {
    86  	name = "route-test-%s"
    87  	dest_range = "15.0.0.0/24"
    88  	network = "${google_compute_network.foobar.name}"
    89  	next_hop_ip = "10.0.1.5"
    90  	priority = 100
    91  }`, acctest.RandString(10), acctest.RandString(10))