github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/google/resource_compute_instance_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "code.google.com/p/google-api-go-client/compute/v1" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccComputeInstance_basic(t *testing.T) { 14 var instance compute.Instance 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckComputeInstanceDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccComputeInstance_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckComputeInstanceExists( 25 "google_compute_instance.foobar", &instance), 26 testAccCheckComputeInstanceTag(&instance, "foo"), 27 testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"), 28 testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func TestAccComputeInstance_IP(t *testing.T) { 36 var instance compute.Instance 37 38 resource.Test(t, resource.TestCase{ 39 PreCheck: func() { testAccPreCheck(t) }, 40 Providers: testAccProviders, 41 CheckDestroy: testAccCheckComputeInstanceDestroy, 42 Steps: []resource.TestStep{ 43 resource.TestStep{ 44 Config: testAccComputeInstance_ip, 45 Check: resource.ComposeTestCheckFunc( 46 testAccCheckComputeInstanceExists( 47 "google_compute_instance.foobar", &instance), 48 testAccCheckComputeInstanceNetwork(&instance), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func TestAccComputeInstance_disks(t *testing.T) { 56 var instance compute.Instance 57 58 resource.Test(t, resource.TestCase{ 59 PreCheck: func() { testAccPreCheck(t) }, 60 Providers: testAccProviders, 61 CheckDestroy: testAccCheckComputeInstanceDestroy, 62 Steps: []resource.TestStep{ 63 resource.TestStep{ 64 Config: testAccComputeInstance_disks, 65 Check: resource.ComposeTestCheckFunc( 66 testAccCheckComputeInstanceExists( 67 "google_compute_instance.foobar", &instance), 68 testAccCheckComputeInstanceDisk(&instance, "terraform-test", true, true), 69 testAccCheckComputeInstanceDisk(&instance, "terraform-test-disk", false, false), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func TestAccComputeInstance_update(t *testing.T) { 77 var instance compute.Instance 78 79 resource.Test(t, resource.TestCase{ 80 PreCheck: func() { testAccPreCheck(t) }, 81 Providers: testAccProviders, 82 CheckDestroy: testAccCheckComputeInstanceDestroy, 83 Steps: []resource.TestStep{ 84 resource.TestStep{ 85 Config: testAccComputeInstance_basic, 86 Check: resource.ComposeTestCheckFunc( 87 testAccCheckComputeInstanceExists( 88 "google_compute_instance.foobar", &instance), 89 ), 90 }, 91 resource.TestStep{ 92 Config: testAccComputeInstance_update, 93 Check: resource.ComposeTestCheckFunc( 94 testAccCheckComputeInstanceExists( 95 "google_compute_instance.foobar", &instance), 96 testAccCheckComputeInstanceMetadata( 97 &instance, "bar", "baz"), 98 testAccCheckComputeInstanceTag(&instance, "baz"), 99 ), 100 }, 101 }, 102 }) 103 } 104 105 func testAccCheckComputeInstanceDestroy(s *terraform.State) error { 106 config := testAccProvider.Meta().(*Config) 107 108 for _, rs := range s.RootModule().Resources { 109 if rs.Type != "google_compute_instance" { 110 continue 111 } 112 113 _, err := config.clientCompute.Instances.Get( 114 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 115 if err == nil { 116 return fmt.Errorf("Instance still exists") 117 } 118 } 119 120 return nil 121 } 122 123 func testAccCheckComputeInstanceExists(n string, instance *compute.Instance) resource.TestCheckFunc { 124 return func(s *terraform.State) error { 125 rs, ok := s.RootModule().Resources[n] 126 if !ok { 127 return fmt.Errorf("Not found: %s", n) 128 } 129 130 if rs.Primary.ID == "" { 131 return fmt.Errorf("No ID is set") 132 } 133 134 config := testAccProvider.Meta().(*Config) 135 136 found, err := config.clientCompute.Instances.Get( 137 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 138 if err != nil { 139 return err 140 } 141 142 if found.Name != rs.Primary.ID { 143 return fmt.Errorf("Instance not found") 144 } 145 146 *instance = *found 147 148 return nil 149 } 150 } 151 152 func testAccCheckComputeInstanceMetadata( 153 instance *compute.Instance, 154 k string, v string) resource.TestCheckFunc { 155 return func(s *terraform.State) error { 156 if instance.Metadata == nil { 157 return fmt.Errorf("no metadata") 158 } 159 160 for _, item := range instance.Metadata.Items { 161 if k != item.Key { 162 continue 163 } 164 165 if v == item.Value { 166 return nil 167 } 168 169 return fmt.Errorf("bad value for %s: %s", k, item.Value) 170 } 171 172 return fmt.Errorf("metadata not found: %s", k) 173 } 174 } 175 176 func testAccCheckComputeInstanceNetwork(instance *compute.Instance) resource.TestCheckFunc { 177 return func(s *terraform.State) error { 178 for _, i := range instance.NetworkInterfaces { 179 for _, c := range i.AccessConfigs { 180 if c.NatIP == "" { 181 return fmt.Errorf("no NAT IP") 182 } 183 } 184 } 185 186 return nil 187 } 188 } 189 190 func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, delete bool, boot bool) resource.TestCheckFunc { 191 return func(s *terraform.State) error { 192 if instance.Disks == nil { 193 return fmt.Errorf("no disks") 194 } 195 196 for _, disk := range instance.Disks { 197 if strings.LastIndex(disk.Source, "/"+source) == (len(disk.Source)-len(source)-1) && disk.AutoDelete == delete && disk.Boot == boot { 198 return nil 199 } 200 } 201 202 return fmt.Errorf("Disk not found: %s", source) 203 } 204 } 205 206 func testAccCheckComputeInstanceTag(instance *compute.Instance, n string) resource.TestCheckFunc { 207 return func(s *terraform.State) error { 208 if instance.Tags == nil { 209 return fmt.Errorf("no tags") 210 } 211 212 for _, k := range instance.Tags.Items { 213 if k == n { 214 return nil 215 } 216 } 217 218 return fmt.Errorf("tag not found: %s", n) 219 } 220 } 221 222 const testAccComputeInstance_basic = ` 223 resource "google_compute_instance" "foobar" { 224 name = "terraform-test" 225 machine_type = "n1-standard-1" 226 zone = "us-central1-a" 227 can_ip_forward = false 228 tags = ["foo", "bar"] 229 230 disk { 231 image = "debian-7-wheezy-v20140814" 232 } 233 234 network { 235 source = "default" 236 } 237 238 metadata { 239 foo = "bar" 240 } 241 }` 242 243 const testAccComputeInstance_update = ` 244 resource "google_compute_instance" "foobar" { 245 name = "terraform-test" 246 machine_type = "n1-standard-1" 247 zone = "us-central1-a" 248 tags = ["baz"] 249 250 disk { 251 image = "debian-7-wheezy-v20140814" 252 } 253 254 network { 255 source = "default" 256 } 257 258 metadata { 259 bar = "baz" 260 } 261 }` 262 263 const testAccComputeInstance_ip = ` 264 resource "google_compute_address" "foo" { 265 name = "foo" 266 } 267 268 resource "google_compute_instance" "foobar" { 269 name = "terraform-test" 270 machine_type = "n1-standard-1" 271 zone = "us-central1-a" 272 tags = ["foo", "bar"] 273 274 disk { 275 image = "debian-7-wheezy-v20140814" 276 } 277 278 network { 279 source = "default" 280 address = "${google_compute_address.foo.address}" 281 } 282 283 metadata { 284 foo = "bar" 285 } 286 }` 287 288 const testAccComputeInstance_disks = ` 289 resource "google_compute_disk" "foobar" { 290 name = "terraform-test-disk" 291 size = 10 292 type = "pd-ssd" 293 zone = "us-central1-a" 294 } 295 296 resource "google_compute_instance" "foobar" { 297 name = "terraform-test" 298 machine_type = "n1-standard-1" 299 zone = "us-central1-a" 300 301 disk { 302 image = "debian-7-wheezy-v20140814" 303 } 304 305 disk { 306 disk = "${google_compute_disk.foobar.name}" 307 auto_delete = false 308 } 309 310 network { 311 source = "default" 312 } 313 314 metadata { 315 foo = "bar" 316 } 317 }`