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

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