|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/resource" |
| 10 | + "github.com/hashicorp/terraform/terraform" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccComputeRegions_basic(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + resource.Test(t, resource.TestCase{ |
| 17 | + PreCheck: func() { testAccPreCheck(t) }, |
| 18 | + Providers: testAccProviders, |
| 19 | + Steps: []resource.TestStep{ |
| 20 | + { |
| 21 | + Config: testAccCheckGoogleComputeRegionsConfig, |
| 22 | + Check: resource.ComposeTestCheckFunc( |
| 23 | + testAccCheckGoogleComputeRegionsMeta("data.google_compute_regions.available"), |
| 24 | + ), |
| 25 | + }, |
| 26 | + }, |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +func testAccCheckGoogleComputeRegionsMeta(n string) resource.TestCheckFunc { |
| 31 | + return func(s *terraform.State) error { |
| 32 | + rs, ok := s.RootModule().Resources[n] |
| 33 | + if !ok { |
| 34 | + return fmt.Errorf("Can't find regions data source: %s", n) |
| 35 | + } |
| 36 | + |
| 37 | + if rs.Primary.ID == "" { |
| 38 | + return errors.New("regions data source ID not set.") |
| 39 | + } |
| 40 | + |
| 41 | + count, ok := rs.Primary.Attributes["names.#"] |
| 42 | + if !ok { |
| 43 | + return errors.New("can't find 'names' attribute") |
| 44 | + } |
| 45 | + |
| 46 | + noOfNames, err := strconv.Atoi(count) |
| 47 | + if err != nil { |
| 48 | + return errors.New("failed to read number of regions") |
| 49 | + } |
| 50 | + if noOfNames < 2 { |
| 51 | + return fmt.Errorf("expected at least 2 regions, received %d, this is most likely a bug", |
| 52 | + noOfNames) |
| 53 | + } |
| 54 | + |
| 55 | + for i := 0; i < noOfNames; i++ { |
| 56 | + idx := "names." + strconv.Itoa(i) |
| 57 | + v, ok := rs.Primary.Attributes[idx] |
| 58 | + if !ok { |
| 59 | + return fmt.Errorf("region list is corrupt (%q not found), this is definitely a bug", idx) |
| 60 | + } |
| 61 | + if len(v) < 1 { |
| 62 | + return fmt.Errorf("Empty region name (%q), this is definitely a bug", idx) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +var testAccCheckGoogleComputeRegionsConfig = ` |
| 71 | +data "google_compute_regions" "available" {} |
| 72 | +` |
0 commit comments