github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/builtin/providers/google/resource_container_cluster_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 TestAccContainerCluster_basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 CheckDestroy: testAccCheckContainerClusterDestroy, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccContainerCluster_basic, 20 Check: resource.ComposeTestCheckFunc( 21 testAccCheckContainerClusterExists( 22 "google_container_cluster.primary"), 23 ), 24 }, 25 }, 26 }) 27 } 28 29 func TestAccContainerCluster_withNodeConfig(t *testing.T) { 30 resource.Test(t, resource.TestCase{ 31 PreCheck: func() { testAccPreCheck(t) }, 32 Providers: testAccProviders, 33 CheckDestroy: testAccCheckContainerClusterDestroy, 34 Steps: []resource.TestStep{ 35 resource.TestStep{ 36 Config: testAccContainerCluster_withNodeConfig, 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckContainerClusterExists( 39 "google_container_cluster.with_node_config"), 40 ), 41 }, 42 }, 43 }) 44 } 45 46 func testAccCheckContainerClusterDestroy(s *terraform.State) error { 47 config := testAccProvider.Meta().(*Config) 48 49 for _, rs := range s.RootModule().Resources { 50 if rs.Type != "google_container_cluster" { 51 continue 52 } 53 54 attributes := rs.Primary.Attributes 55 _, err := config.clientContainer.Projects.Zones.Clusters.Get( 56 config.Project, attributes["zone"], attributes["name"]).Do() 57 if err == nil { 58 return fmt.Errorf("Cluster still exists") 59 } 60 } 61 62 return nil 63 } 64 65 func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 rs, ok := s.RootModule().Resources[n] 68 if !ok { 69 return fmt.Errorf("Not found: %s", n) 70 } 71 72 if rs.Primary.ID == "" { 73 return fmt.Errorf("No ID is set") 74 } 75 76 config := testAccProvider.Meta().(*Config) 77 78 attributes := rs.Primary.Attributes 79 found, err := config.clientContainer.Projects.Zones.Clusters.Get( 80 config.Project, attributes["zone"], attributes["name"]).Do() 81 if err != nil { 82 return err 83 } 84 85 if found.Name != attributes["name"] { 86 return fmt.Errorf("Cluster not found") 87 } 88 89 return nil 90 } 91 } 92 93 var testAccContainerCluster_basic = fmt.Sprintf(` 94 resource "google_container_cluster" "primary" { 95 name = "cluster-test-%s" 96 zone = "us-central1-a" 97 initial_node_count = 3 98 99 master_auth { 100 username = "mr.yoda" 101 password = "adoy.rm" 102 } 103 }`, acctest.RandString(10)) 104 105 var testAccContainerCluster_withNodeConfig = fmt.Sprintf(` 106 resource "google_container_cluster" "with_node_config" { 107 name = "cluster-test-%s" 108 zone = "us-central1-f" 109 initial_node_count = 1 110 111 master_auth { 112 username = "mr.yoda" 113 password = "adoy.rm" 114 } 115 116 node_config { 117 machine_type = "g1-small" 118 disk_size_gb = 15 119 oauth_scopes = [ 120 "https://www.googleapis.com/auth/compute", 121 "https://www.googleapis.com/auth/devstorage.read_only", 122 "https://www.googleapis.com/auth/logging.write", 123 "https://www.googleapis.com/auth/monitoring" 124 ] 125 } 126 }`, acctest.RandString(10))