|
| 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 TestAccDataSourceComputeBackendService_basic(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) |
| 16 | + checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) |
| 17 | + |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { testAccPreCheck(t) }, |
| 20 | + Providers: testAccProviders, |
| 21 | + CheckDestroy: testAccCheckComputeBackendServiceDestroy, |
| 22 | + Steps: []resource.TestStep{ |
| 23 | + resource.TestStep{ |
| 24 | + Config: testAccDataSourceComputeBackendService_basic(serviceName, checkName), |
| 25 | + Check: testAccDataSourceComputeBackendServiceCheck("data.google_compute_backend_service.baz", "google_compute_backend_service.foobar"), |
| 26 | + }, |
| 27 | + }, |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +func testAccDataSourceComputeBackendServiceCheck(dsName, rsName string) resource.TestCheckFunc { |
| 32 | + return func(s *terraform.State) error { |
| 33 | + rs, ok := s.RootModule().Resources[rsName] |
| 34 | + if !ok { |
| 35 | + return fmt.Errorf("can't find resource called %s in state", rsName) |
| 36 | + } |
| 37 | + |
| 38 | + ds, ok := s.RootModule().Resources[dsName] |
| 39 | + if !ok { |
| 40 | + return fmt.Errorf("can't find data source called %s in state", dsName) |
| 41 | + } |
| 42 | + |
| 43 | + dsAttr := ds.Primary.Attributes |
| 44 | + rsAttr := rs.Primary.Attributes |
| 45 | + |
| 46 | + attrsToTest := []string{ |
| 47 | + "id", |
| 48 | + "name", |
| 49 | + "description", |
| 50 | + "self_link", |
| 51 | + "fingerprint", |
| 52 | + "port_name", |
| 53 | + "protocol", |
| 54 | + } |
| 55 | + |
| 56 | + for _, attrToTest := range attrsToTest { |
| 57 | + if dsAttr[attrToTest] != rsAttr[attrToTest] { |
| 58 | + return fmt.Errorf("%s is %s; want %s", attrToTest, dsAttr[attrToTest], rsAttr[attrToTest]) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func testAccDataSourceComputeBackendService_basic(serviceName, checkName string) string { |
| 67 | + return fmt.Sprintf(` |
| 68 | +resource "google_compute_backend_service" "foobar" { |
| 69 | + name = "%s" |
| 70 | + description = "foobar backend service" |
| 71 | + health_checks = ["${google_compute_http_health_check.zero.self_link}"] |
| 72 | +} |
| 73 | +
|
| 74 | +resource "google_compute_http_health_check" "zero" { |
| 75 | + name = "%s" |
| 76 | + request_path = "/" |
| 77 | + check_interval_sec = 1 |
| 78 | + timeout_sec = 1 |
| 79 | +} |
| 80 | +
|
| 81 | +data "google_compute_backend_service" "baz" { |
| 82 | + name = "${google_compute_backend_service.foobar.name}" |
| 83 | +} |
| 84 | +`, serviceName, checkName) |
| 85 | +} |
0 commit comments