github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/google/data_source_google_compute_network_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 ) 11 12 func TestAccDataSourceGoogleNetwork(t *testing.T) { 13 networkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccDataSourceGoogleNetworkConfig(networkName), 20 Check: resource.ComposeTestCheckFunc( 21 testAccDataSourceGoogleNetworkCheck("data.google_compute_network.my_network", "google_compute_network.foobar"), 22 ), 23 }, 24 }, 25 }) 26 } 27 28 func testAccDataSourceGoogleNetworkCheck(data_source_name string, resource_name string) resource.TestCheckFunc { 29 return func(s *terraform.State) error { 30 ds, ok := s.RootModule().Resources[data_source_name] 31 if !ok { 32 return fmt.Errorf("root module has no resource called %s", data_source_name) 33 } 34 35 rs, ok := s.RootModule().Resources[resource_name] 36 if !ok { 37 return fmt.Errorf("can't find %s in state", resource_name) 38 } 39 40 ds_attr := ds.Primary.Attributes 41 rs_attr := rs.Primary.Attributes 42 network_attrs_to_test := []string{ 43 "id", 44 "self_link", 45 "name", 46 "description", 47 } 48 49 for _, attr_to_check := range network_attrs_to_test { 50 if ds_attr[attr_to_check] != rs_attr[attr_to_check] { 51 return fmt.Errorf( 52 "%s is %s; want %s", 53 attr_to_check, 54 ds_attr[attr_to_check], 55 rs_attr[attr_to_check], 56 ) 57 } 58 } 59 return nil 60 } 61 } 62 63 func testAccDataSourceGoogleNetworkConfig(name string) string { 64 return fmt.Sprintf(` 65 resource "google_compute_network" "foobar" { 66 name = "%s" 67 description = "my-description" 68 } 69 70 data "google_compute_network" "my_network" { 71 name = "${google_compute_network.foobar.name}" 72 }`, name) 73 }