github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/data_source_google_compute_network_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/resource"
     6  	"github.com/hashicorp/terraform/terraform"
     7  	"testing"
     8  )
     9  
    10  func TestAccDataSourceGoogleNetwork(t *testing.T) {
    11  	resource.Test(t, resource.TestCase{
    12  		PreCheck:  func() { testAccPreCheck(t) },
    13  		Providers: testAccProviders,
    14  		Steps: []resource.TestStep{
    15  			resource.TestStep{
    16  				Config: TestAccDataSourceGoogleNetworkConfig,
    17  				Check: resource.ComposeTestCheckFunc(
    18  					testAccDataSourceGoogleNetworkCheck("data.google_compute_network.my_network", "google_compute_network.foobar"),
    19  				),
    20  			},
    21  		},
    22  	})
    23  }
    24  
    25  func testAccDataSourceGoogleNetworkCheck(data_source_name string, resource_name string) resource.TestCheckFunc {
    26  	return func(s *terraform.State) error {
    27  		ds, ok := s.RootModule().Resources[data_source_name]
    28  		if !ok {
    29  			return fmt.Errorf("root module has no resource called %s", data_source_name)
    30  		}
    31  
    32  		rs, ok := s.RootModule().Resources[resource_name]
    33  		if !ok {
    34  			return fmt.Errorf("can't find %s in state", resource_name)
    35  		}
    36  
    37  		ds_attr := ds.Primary.Attributes
    38  		rs_attr := rs.Primary.Attributes
    39  		network_attrs_to_test := []string{
    40  			"id",
    41  			"self_link",
    42  			"name",
    43  			"description",
    44  		}
    45  
    46  		for _, attr_to_check := range network_attrs_to_test {
    47  			if ds_attr[attr_to_check] != rs_attr[attr_to_check] {
    48  				return fmt.Errorf(
    49  					"%s is %s; want %s",
    50  					attr_to_check,
    51  					ds_attr[attr_to_check],
    52  					rs_attr[attr_to_check],
    53  				)
    54  			}
    55  		}
    56  		return nil
    57  	}
    58  }
    59  
    60  var TestAccDataSourceGoogleNetworkConfig = `
    61  resource "google_compute_network" "foobar" {
    62  	name = "network-test"
    63  	description = "my-description"
    64  }
    65  
    66  data "google_compute_network" "my_network" {
    67  	name = "${google_compute_network.foobar.name}"
    68  }`