github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/data_source_google_compute_zones_test.go (about)

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