github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/google/data_source_google_container_engine_versions_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 TestAccGoogleContainerEngineVersions_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: testAccCheckGoogleContainerEngineVersionsConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckGoogleContainerEngineVersionsMeta("data.google_container_engine_versions.versions"), 22 ), 23 }, 24 }, 25 }) 26 } 27 28 func testAccCheckGoogleContainerEngineVersionsMeta(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 versions data source: %s", n) 33 } 34 35 if rs.Primary.ID == "" { 36 return errors.New("versions data source ID not set.") 37 } 38 39 nodeCount, ok := rs.Primary.Attributes["valid_node_versions.#"] 40 if !ok { 41 return errors.New("can't find 'valid_node_versions' attribute") 42 } 43 44 noOfNodes, err := strconv.Atoi(nodeCount) 45 if err != nil { 46 return errors.New("failed to read number of valid node versions") 47 } 48 if noOfNodes < 2 { 49 return fmt.Errorf("expected at least 2 valid node versions, received %d, this is most likely a bug", 50 noOfNodes) 51 } 52 53 for i := 0; i < noOfNodes; i++ { 54 idx := "valid_node_versions." + strconv.Itoa(i) 55 v, ok := rs.Primary.Attributes[idx] 56 if !ok { 57 return fmt.Errorf("valid node versions list is corrupt (%q not found), this is definitely a bug", idx) 58 } 59 if len(v) < 1 { 60 return fmt.Errorf("Empty node version (%q), this is definitely a bug", idx) 61 } 62 } 63 64 masterCount, ok := rs.Primary.Attributes["valid_master_versions.#"] 65 if !ok { 66 return errors.New("can't find 'valid_master_versions' attribute") 67 } 68 69 noOfMasters, err := strconv.Atoi(masterCount) 70 if err != nil { 71 return errors.New("failed to read number of valid master versions") 72 } 73 if noOfMasters < 2 { 74 return fmt.Errorf("expected at least 2 valid master versions, received %d, this is most likely a bug", 75 noOfMasters) 76 } 77 78 for i := 0; i < noOfMasters; i++ { 79 idx := "valid_master_versions." + strconv.Itoa(i) 80 v, ok := rs.Primary.Attributes[idx] 81 if !ok { 82 return fmt.Errorf("valid master versions list is corrupt (%q not found), this is definitely a bug", idx) 83 } 84 if len(v) < 1 { 85 return fmt.Errorf("Empty master version (%q), this is definitely a bug", idx) 86 } 87 } 88 89 return nil 90 } 91 } 92 93 var testAccCheckGoogleContainerEngineVersionsConfig = ` 94 data "google_container_engine_versions" "versions" { 95 zone = "us-central1-b" 96 } 97 `