github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 "private_ip_google_access", 49 } 50 51 for _, attr_to_check := range subnetwork_attrs_to_test { 52 if ds_attr[attr_to_check] != rs_attr[attr_to_check] { 53 return fmt.Errorf( 54 "%s is %s; want %s", 55 attr_to_check, 56 ds_attr[attr_to_check], 57 rs_attr[attr_to_check], 58 ) 59 } 60 } 61 62 return nil 63 } 64 } 65 66 var TestAccDataSourceGoogleSubnetworkConfig = ` 67 68 resource "google_compute_network" "foobar" { 69 name = "network-test" 70 description = "my-description" 71 } 72 resource "google_compute_subnetwork" "foobar" { 73 name = "subnetwork-test" 74 description = "my-description" 75 ip_cidr_range = "10.0.0.0/24" 76 network = "${google_compute_network.foobar.self_link}" 77 private_ip_google_access = true 78 } 79 80 data "google_compute_subnetwork" "my_subnetwork" { 81 name = "${google_compute_subnetwork.foobar.name}" 82 } 83 `