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