github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_autoscaler_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 "google.golang.org/api/compute/v1" 11 ) 12 13 func TestAccAutoscaler_basic(t *testing.T) { 14 var ascaler compute.Autoscaler 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAutoscalerDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAutoscaler_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAutoscalerExists( 25 "google_compute_autoscaler.foobar", &ascaler), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccAutoscaler_update(t *testing.T) { 33 var ascaler compute.Autoscaler 34 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckAutoscalerDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccAutoscaler_basic, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckAutoscalerExists( 44 "google_compute_autoscaler.foobar", &ascaler), 45 ), 46 }, 47 resource.TestStep{ 48 Config: testAccAutoscaler_update, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckAutoscalerExists( 51 "google_compute_autoscaler.foobar", &ascaler), 52 testAccCheckAutoscalerUpdated( 53 "google_compute_autoscaler.foobar", 10), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func testAccCheckAutoscalerDestroy(s *terraform.State) error { 61 config := testAccProvider.Meta().(*Config) 62 63 for _, rs := range s.RootModule().Resources { 64 if rs.Type != "google_compute_autoscaler" { 65 continue 66 } 67 68 _, err := config.clientCompute.Autoscalers.Get( 69 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 70 if err == nil { 71 return fmt.Errorf("Autoscaler still exists") 72 } 73 } 74 75 return nil 76 } 77 78 func testAccCheckAutoscalerExists(n string, ascaler *compute.Autoscaler) resource.TestCheckFunc { 79 return func(s *terraform.State) error { 80 rs, ok := s.RootModule().Resources[n] 81 if !ok { 82 return fmt.Errorf("Not found: %s", n) 83 } 84 85 if rs.Primary.ID == "" { 86 return fmt.Errorf("No ID is set") 87 } 88 89 config := testAccProvider.Meta().(*Config) 90 91 found, err := config.clientCompute.Autoscalers.Get( 92 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 93 if err != nil { 94 return err 95 } 96 97 if found.Name != rs.Primary.ID { 98 return fmt.Errorf("Autoscaler not found") 99 } 100 101 *ascaler = *found 102 103 return nil 104 } 105 } 106 107 func testAccCheckAutoscalerUpdated(n string, max int64) resource.TestCheckFunc { 108 return func(s *terraform.State) error { 109 rs, ok := s.RootModule().Resources[n] 110 if !ok { 111 return fmt.Errorf("Not found: %s", n) 112 } 113 114 if rs.Primary.ID == "" { 115 return fmt.Errorf("No ID is set") 116 } 117 118 config := testAccProvider.Meta().(*Config) 119 120 ascaler, err := config.clientCompute.Autoscalers.Get( 121 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 122 if err != nil { 123 return err 124 } 125 126 if ascaler.AutoscalingPolicy.MaxNumReplicas != max { 127 return fmt.Errorf("maximum replicas incorrect") 128 } 129 130 return nil 131 } 132 } 133 134 var testAccAutoscaler_basic = fmt.Sprintf(` 135 resource "google_compute_instance_template" "foobar" { 136 name = "ascaler-test-%s" 137 machine_type = "n1-standard-1" 138 can_ip_forward = false 139 tags = ["foo", "bar"] 140 141 disk { 142 source_image = "debian-cloud/debian-8-jessie-v20160803" 143 auto_delete = true 144 boot = true 145 } 146 147 network_interface { 148 network = "default" 149 } 150 151 metadata { 152 foo = "bar" 153 } 154 155 service_account { 156 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 157 } 158 } 159 160 resource "google_compute_target_pool" "foobar" { 161 description = "Resource created for Terraform acceptance testing" 162 name = "ascaler-test-%s" 163 session_affinity = "CLIENT_IP_PROTO" 164 } 165 166 resource "google_compute_instance_group_manager" "foobar" { 167 description = "Terraform test instance group manager" 168 name = "ascaler-test-%s" 169 instance_template = "${google_compute_instance_template.foobar.self_link}" 170 target_pools = ["${google_compute_target_pool.foobar.self_link}"] 171 base_instance_name = "foobar" 172 zone = "us-central1-a" 173 } 174 175 resource "google_compute_autoscaler" "foobar" { 176 description = "Resource created for Terraform acceptance testing" 177 name = "ascaler-test-%s" 178 zone = "us-central1-a" 179 target = "${google_compute_instance_group_manager.foobar.self_link}" 180 autoscaling_policy = { 181 max_replicas = 5 182 min_replicas = 1 183 cooldown_period = 60 184 cpu_utilization = { 185 target = 0.5 186 } 187 } 188 189 }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10)) 190 191 var testAccAutoscaler_update = fmt.Sprintf(` 192 resource "google_compute_instance_template" "foobar" { 193 name = "ascaler-test-%s" 194 machine_type = "n1-standard-1" 195 can_ip_forward = false 196 tags = ["foo", "bar"] 197 198 disk { 199 source_image = "debian-cloud/debian-8-jessie-v20160803" 200 auto_delete = true 201 boot = true 202 } 203 204 network_interface { 205 network = "default" 206 } 207 208 metadata { 209 foo = "bar" 210 } 211 212 service_account { 213 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 214 } 215 } 216 217 resource "google_compute_target_pool" "foobar" { 218 description = "Resource created for Terraform acceptance testing" 219 name = "ascaler-test-%s" 220 session_affinity = "CLIENT_IP_PROTO" 221 } 222 223 resource "google_compute_instance_group_manager" "foobar" { 224 description = "Terraform test instance group manager" 225 name = "ascaler-test-%s" 226 instance_template = "${google_compute_instance_template.foobar.self_link}" 227 target_pools = ["${google_compute_target_pool.foobar.self_link}"] 228 base_instance_name = "foobar" 229 zone = "us-central1-a" 230 } 231 232 resource "google_compute_autoscaler" "foobar" { 233 description = "Resource created for Terraform acceptance testing" 234 name = "ascaler-test-%s" 235 zone = "us-central1-a" 236 target = "${google_compute_instance_group_manager.foobar.self_link}" 237 autoscaling_policy = { 238 max_replicas = 10 239 min_replicas = 1 240 cooldown_period = 60 241 cpu_utilization = { 242 target = 0.5 243 } 244 } 245 246 }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))