github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_compute_instance_group_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "google.golang.org/api/compute/v1" 8 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccComputeInstanceGroup_basic(t *testing.T) { 15 var instanceGroup compute.InstanceGroup 16 var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10)) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccComputeInstanceGroup_destroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccComputeInstanceGroup_basic(instanceName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccComputeInstanceGroup_exists( 27 "google_compute_instance_group.basic", &instanceGroup), 28 testAccComputeInstanceGroup_exists( 29 "google_compute_instance_group.empty", &instanceGroup), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func TestAccComputeInstanceGroup_update(t *testing.T) { 37 var instanceGroup compute.InstanceGroup 38 var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10)) 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccComputeInstanceGroup_destroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: testAccComputeInstanceGroup_update(instanceName), 47 Check: resource.ComposeTestCheckFunc( 48 testAccComputeInstanceGroup_exists( 49 "google_compute_instance_group.update", &instanceGroup), 50 testAccComputeInstanceGroup_named_ports( 51 "google_compute_instance_group.update", 52 map[string]int64{"http": 8080, "https": 8443}, 53 &instanceGroup), 54 ), 55 }, 56 resource.TestStep{ 57 Config: testAccComputeInstanceGroup_update2(instanceName), 58 Check: resource.ComposeTestCheckFunc( 59 testAccComputeInstanceGroup_exists( 60 "google_compute_instance_group.update", &instanceGroup), 61 testAccComputeInstanceGroup_updated( 62 "google_compute_instance_group.update", 3, &instanceGroup), 63 testAccComputeInstanceGroup_named_ports( 64 "google_compute_instance_group.update", 65 map[string]int64{"http": 8081, "test": 8444}, 66 &instanceGroup), 67 ), 68 }, 69 }, 70 }) 71 } 72 73 func testAccComputeInstanceGroup_destroy(s *terraform.State) error { 74 config := testAccProvider.Meta().(*Config) 75 76 for _, rs := range s.RootModule().Resources { 77 if rs.Type != "google_compute_instance_group" { 78 continue 79 } 80 _, err := config.clientCompute.InstanceGroups.Get( 81 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 82 if err == nil { 83 return fmt.Errorf("InstanceGroup still exists") 84 } 85 } 86 87 return nil 88 } 89 90 func testAccComputeInstanceGroup_exists(n string, instanceGroup *compute.InstanceGroup) resource.TestCheckFunc { 91 return func(s *terraform.State) error { 92 rs, ok := s.RootModule().Resources[n] 93 if !ok { 94 return fmt.Errorf("Not found: %s", n) 95 } 96 97 if rs.Primary.ID == "" { 98 return fmt.Errorf("No ID is set") 99 } 100 101 config := testAccProvider.Meta().(*Config) 102 103 found, err := config.clientCompute.InstanceGroups.Get( 104 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 105 if err != nil { 106 return err 107 } 108 109 if found.Name != rs.Primary.ID { 110 return fmt.Errorf("InstanceGroup not found") 111 } 112 113 *instanceGroup = *found 114 115 return nil 116 } 117 } 118 119 func testAccComputeInstanceGroup_updated(n string, size int64, instanceGroup *compute.InstanceGroup) resource.TestCheckFunc { 120 return func(s *terraform.State) error { 121 rs, ok := s.RootModule().Resources[n] 122 if !ok { 123 return fmt.Errorf("Not found: %s", n) 124 } 125 126 if rs.Primary.ID == "" { 127 return fmt.Errorf("No ID is set") 128 } 129 130 config := testAccProvider.Meta().(*Config) 131 132 instanceGroup, err := config.clientCompute.InstanceGroups.Get( 133 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 134 if err != nil { 135 return err 136 } 137 138 // Cannot check the target pool as the instance creation is asynchronous. However, can 139 // check the target_size. 140 if instanceGroup.Size != size { 141 return fmt.Errorf("instance count incorrect") 142 } 143 144 return nil 145 } 146 } 147 148 func testAccComputeInstanceGroup_named_ports(n string, np map[string]int64, instanceGroup *compute.InstanceGroup) resource.TestCheckFunc { 149 return func(s *terraform.State) error { 150 rs, ok := s.RootModule().Resources[n] 151 if !ok { 152 return fmt.Errorf("Not found: %s", n) 153 } 154 155 if rs.Primary.ID == "" { 156 return fmt.Errorf("No ID is set") 157 } 158 159 config := testAccProvider.Meta().(*Config) 160 161 instanceGroup, err := config.clientCompute.InstanceGroups.Get( 162 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 163 if err != nil { 164 return err 165 } 166 167 var found bool 168 for _, namedPort := range instanceGroup.NamedPorts { 169 found = false 170 for name, port := range np { 171 if namedPort.Name == name && namedPort.Port == port { 172 found = true 173 } 174 } 175 if !found { 176 return fmt.Errorf("named port incorrect") 177 } 178 } 179 180 return nil 181 } 182 } 183 184 func testAccComputeInstanceGroup_basic(instance string) string { 185 return fmt.Sprintf(` 186 resource "google_compute_instance" "ig_instance" { 187 name = "%s" 188 machine_type = "n1-standard-1" 189 can_ip_forward = false 190 zone = "us-central1-c" 191 192 disk { 193 image = "debian-8-jessie-v20160803" 194 } 195 196 network_interface { 197 network = "default" 198 } 199 } 200 201 resource "google_compute_instance_group" "basic" { 202 description = "Terraform test instance group" 203 name = "%s" 204 zone = "us-central1-c" 205 instances = [ "${google_compute_instance.ig_instance.self_link}" ] 206 named_port { 207 name = "http" 208 port = "8080" 209 } 210 named_port { 211 name = "https" 212 port = "8443" 213 } 214 } 215 216 resource "google_compute_instance_group" "empty" { 217 description = "Terraform test instance group empty" 218 name = "%s-empty" 219 zone = "us-central1-c" 220 named_port { 221 name = "http" 222 port = "8080" 223 } 224 named_port { 225 name = "https" 226 port = "8443" 227 } 228 }`, instance, instance, instance) 229 } 230 231 func testAccComputeInstanceGroup_update(instance string) string { 232 return fmt.Sprintf(` 233 resource "google_compute_instance" "ig_instance" { 234 name = "%s-${count.index}" 235 machine_type = "n1-standard-1" 236 can_ip_forward = false 237 zone = "us-central1-c" 238 count = 1 239 240 disk { 241 image = "debian-8-jessie-v20160803" 242 } 243 244 network_interface { 245 network = "default" 246 } 247 } 248 249 resource "google_compute_instance_group" "update" { 250 description = "Terraform test instance group" 251 name = "%s" 252 zone = "us-central1-c" 253 instances = [ "${google_compute_instance.ig_instance.self_link}" ] 254 named_port { 255 name = "http" 256 port = "8080" 257 } 258 named_port { 259 name = "https" 260 port = "8443" 261 } 262 }`, instance, instance) 263 } 264 265 // Change IGM's instance template and target size 266 func testAccComputeInstanceGroup_update2(instance string) string { 267 return fmt.Sprintf(` 268 resource "google_compute_instance" "ig_instance" { 269 name = "%s-${count.index}" 270 machine_type = "n1-standard-1" 271 can_ip_forward = false 272 zone = "us-central1-c" 273 count = 3 274 275 disk { 276 image = "debian-8-jessie-v20160803" 277 } 278 279 network_interface { 280 network = "default" 281 } 282 } 283 284 resource "google_compute_instance_group" "update" { 285 description = "Terraform test instance group" 286 name = "%s" 287 zone = "us-central1-c" 288 instances = [ "${google_compute_instance.ig_instance.*.self_link}" ] 289 290 named_port { 291 name = "http" 292 port = "8081" 293 } 294 named_port { 295 name = "test" 296 port = "8444" 297 } 298 }`, instance, instance) 299 }