github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/vsphere/resource_vsphere_virtual_machine_test.go (about) 1 package vsphere 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/vmware/govmomi" 11 "github.com/vmware/govmomi/find" 12 "github.com/vmware/govmomi/object" 13 "github.com/vmware/govmomi/property" 14 "github.com/vmware/govmomi/vim25/mo" 15 "github.com/vmware/govmomi/vim25/types" 16 "golang.org/x/net/context" 17 ) 18 19 func TestAccVSphereVirtualMachine_basic(t *testing.T) { 20 var vm virtualMachine 21 var locationOpt string 22 var datastoreOpt string 23 24 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 25 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 26 } 27 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 28 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 29 } 30 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 31 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 32 } 33 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 34 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 35 } 36 template := os.Getenv("VSPHERE_TEMPLATE") 37 gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY") 38 label := os.Getenv("VSPHERE_NETWORK_LABEL") 39 ip_address := os.Getenv("VSPHERE_NETWORK_IP_ADDRESS") 40 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 45 Steps: []resource.TestStep{ 46 resource.TestStep{ 47 Config: fmt.Sprintf( 48 testAccCheckVSphereVirtualMachineConfig_basic, 49 locationOpt, 50 gateway, 51 label, 52 ip_address, 53 datastoreOpt, 54 template, 55 ), 56 Check: resource.ComposeTestCheckFunc( 57 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.foo", &vm), 58 resource.TestCheckResourceAttr( 59 "vsphere_virtual_machine.foo", "name", "terraform-test"), 60 resource.TestCheckResourceAttr( 61 "vsphere_virtual_machine.foo", "vcpu", "2"), 62 resource.TestCheckResourceAttr( 63 "vsphere_virtual_machine.foo", "memory", "4096"), 64 resource.TestCheckResourceAttr( 65 "vsphere_virtual_machine.foo", "disk.#", "2"), 66 resource.TestCheckResourceAttr( 67 "vsphere_virtual_machine.foo", "disk.0.template", template), 68 resource.TestCheckResourceAttr( 69 "vsphere_virtual_machine.foo", "network_interface.#", "1"), 70 resource.TestCheckResourceAttr( 71 "vsphere_virtual_machine.foo", "network_interface.0.label", label), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func TestAccVSphereVirtualMachine_dhcp(t *testing.T) { 79 var vm virtualMachine 80 var locationOpt string 81 var datastoreOpt string 82 83 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 84 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 85 } 86 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 87 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 88 } 89 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 90 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 91 } 92 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 93 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 94 } 95 template := os.Getenv("VSPHERE_TEMPLATE") 96 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 97 98 resource.Test(t, resource.TestCase{ 99 PreCheck: func() { testAccPreCheck(t) }, 100 Providers: testAccProviders, 101 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 102 Steps: []resource.TestStep{ 103 resource.TestStep{ 104 Config: fmt.Sprintf( 105 testAccCheckVSphereVirtualMachineConfig_dhcp, 106 locationOpt, 107 label, 108 datastoreOpt, 109 template, 110 ), 111 Check: resource.ComposeTestCheckFunc( 112 testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm), 113 resource.TestCheckResourceAttr( 114 "vsphere_virtual_machine.bar", "name", "terraform-test"), 115 resource.TestCheckResourceAttr( 116 "vsphere_virtual_machine.bar", "vcpu", "2"), 117 resource.TestCheckResourceAttr( 118 "vsphere_virtual_machine.bar", "memory", "4096"), 119 resource.TestCheckResourceAttr( 120 "vsphere_virtual_machine.bar", "disk.#", "1"), 121 resource.TestCheckResourceAttr( 122 "vsphere_virtual_machine.bar", "disk.0.template", template), 123 resource.TestCheckResourceAttr( 124 "vsphere_virtual_machine.bar", "network_interface.#", "1"), 125 resource.TestCheckResourceAttr( 126 "vsphere_virtual_machine.bar", "network_interface.0.label", label), 127 ), 128 }, 129 }, 130 }) 131 } 132 133 func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) { 134 var vm virtualMachine 135 var locationOpt string 136 var datastoreOpt string 137 138 if v := os.Getenv("VSPHERE_DATACENTER"); v != "" { 139 locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v) 140 } 141 if v := os.Getenv("VSPHERE_CLUSTER"); v != "" { 142 locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v) 143 } 144 if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" { 145 locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v) 146 } 147 if v := os.Getenv("VSPHERE_DATASTORE"); v != "" { 148 datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v) 149 } 150 template := os.Getenv("VSPHERE_TEMPLATE") 151 label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP") 152 153 resource.Test(t, resource.TestCase{ 154 PreCheck: func() { testAccPreCheck(t) }, 155 Providers: testAccProviders, 156 CheckDestroy: testAccCheckVSphereVirtualMachineDestroy, 157 Steps: []resource.TestStep{ 158 resource.TestStep{ 159 Config: fmt.Sprintf( 160 testAccCheckVSphereVirtualMachineConfig_custom_configs, 161 locationOpt, 162 label, 163 datastoreOpt, 164 template, 165 ), 166 Check: resource.ComposeTestCheckFunc( 167 testAccCheckVSphereVirtualMachineExistsHasCustomConfig("vsphere_virtual_machine.car", &vm), 168 resource.TestCheckResourceAttr( 169 "vsphere_virtual_machine.car", "name", "terraform-test-custom"), 170 resource.TestCheckResourceAttr( 171 "vsphere_virtual_machine.car", "vcpu", "2"), 172 resource.TestCheckResourceAttr( 173 "vsphere_virtual_machine.car", "memory", "4096"), 174 resource.TestCheckResourceAttr( 175 "vsphere_virtual_machine.car", "disk.#", "1"), 176 resource.TestCheckResourceAttr( 177 "vsphere_virtual_machine.car", "disk.0.template", template), 178 resource.TestCheckResourceAttr( 179 "vsphere_virtual_machine.car", "network_interface.#", "1"), 180 resource.TestCheckResourceAttr( 181 "vsphere_virtual_machine.car", "custom_configuration_parameters.foo", "bar"), 182 resource.TestCheckResourceAttr( 183 "vsphere_virtual_machine.car", "custom_configuration_parameters.car", "ferrari"), 184 resource.TestCheckResourceAttr( 185 "vsphere_virtual_machine.car", "custom_configuration_parameters.num", "42"), 186 resource.TestCheckResourceAttr( 187 "vsphere_virtual_machine.car", "network_interface.0.label", label), 188 ), 189 }, 190 }, 191 }) 192 } 193 194 func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error { 195 client := testAccProvider.Meta().(*govmomi.Client) 196 finder := find.NewFinder(client.Client, true) 197 198 for _, rs := range s.RootModule().Resources { 199 if rs.Type != "vsphere_virtual_machine" { 200 continue 201 } 202 203 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 204 if err != nil { 205 return fmt.Errorf("error %s", err) 206 } 207 208 dcFolders, err := dc.Folders(context.TODO()) 209 if err != nil { 210 return fmt.Errorf("error %s", err) 211 } 212 213 _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"]) 214 if err == nil { 215 return fmt.Errorf("Record still exists") 216 } 217 } 218 219 return nil 220 } 221 222 func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtualMachine) resource.TestCheckFunc { 223 return func(s *terraform.State) error { 224 225 226 rs, ok := s.RootModule().Resources[n] 227 if !ok { 228 return fmt.Errorf("Not found: %s", n) 229 } 230 231 if rs.Primary.ID == "" { 232 return fmt.Errorf("No ID is set") 233 } 234 235 client := testAccProvider.Meta().(*govmomi.Client) 236 finder := find.NewFinder(client.Client, true) 237 238 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 239 if err != nil { 240 return fmt.Errorf("error %s", err) 241 } 242 243 dcFolders, err := dc.Folders(context.TODO()) 244 if err != nil { 245 return fmt.Errorf("error %s", err) 246 } 247 248 249 _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"]) 250 if err != nil { 251 return fmt.Errorf("error %s", err) 252 } 253 254 finder = finder.SetDatacenter(dc) 255 instance, err := finder.VirtualMachine(context.TODO(), rs.Primary.Attributes["name"]) 256 if err != nil { 257 return fmt.Errorf("error %s", err) 258 } 259 260 var mvm mo.VirtualMachine 261 262 collector := property.DefaultCollector(client.Client) 263 264 if err := collector.RetrieveOne(context.TODO(), instance.Reference(), []string{"config.extraConfig"}, &mvm); err != nil { 265 return fmt.Errorf("error %s", err) 266 } 267 268 var configMap = make(map[string]types.AnyType) 269 if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 { 270 for _, v := range mvm.Config.ExtraConfig { 271 value := v.GetOptionValue() 272 configMap[value.Key] = value.Value 273 } 274 } else { 275 return fmt.Errorf("error no ExtraConfig") 276 } 277 278 if configMap["foo"] == nil { 279 return fmt.Errorf("error no ExtraConfig for 'foo'") 280 } 281 282 if configMap["foo"] != "bar" { 283 return fmt.Errorf("error ExtraConfig 'foo' != bar") 284 } 285 286 if configMap["car"] == nil { 287 return fmt.Errorf("error no ExtraConfig for 'car'") 288 } 289 290 if configMap["car"] != "ferrari" { 291 return fmt.Errorf("error ExtraConfig 'car' != ferrari") 292 } 293 294 if configMap["num"] == nil { 295 return fmt.Errorf("error no ExtraConfig for 'num'") 296 } 297 298 // todo this should be an int, getting back a string 299 if configMap["num"] != "42" { 300 return fmt.Errorf("error ExtraConfig 'num' != 42") 301 } 302 *vm = virtualMachine{ 303 name: rs.Primary.ID, 304 } 305 306 return nil 307 } 308 } 309 func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resource.TestCheckFunc { 310 return func(s *terraform.State) error { 311 312 rs, ok := s.RootModule().Resources[n] 313 if !ok { 314 return fmt.Errorf("Not found: %s", n) 315 } 316 317 if rs.Primary.ID == "" { 318 return fmt.Errorf("No ID is set") 319 } 320 321 client := testAccProvider.Meta().(*govmomi.Client) 322 finder := find.NewFinder(client.Client, true) 323 324 dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"]) 325 if err != nil { 326 return fmt.Errorf("error %s", err) 327 } 328 329 dcFolders, err := dc.Folders(context.TODO()) 330 if err != nil { 331 return fmt.Errorf("error %s", err) 332 } 333 334 _, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"]) 335 336 *vm = virtualMachine{ 337 name: rs.Primary.ID, 338 } 339 340 return nil 341 342 } 343 } 344 345 const testAccCheckVSphereVirtualMachineConfig_basic = ` 346 resource "vsphere_virtual_machine" "foo" { 347 name = "terraform-test" 348 %s 349 vcpu = 2 350 memory = 4096 351 gateway = "%s" 352 network_interface { 353 label = "%s" 354 ip_address = "%s" 355 subnet_mask = "255.255.255.0" 356 } 357 disk { 358 %s 359 template = "%s" 360 iops = 500 361 } 362 disk { 363 size = 1 364 iops = 500 365 } 366 } 367 ` 368 const testAccCheckVSphereVirtualMachineConfig_dhcp = ` 369 resource "vsphere_virtual_machine" "bar" { 370 name = "terraform-test" 371 %s 372 vcpu = 2 373 memory = 4096 374 network_interface { 375 label = "%s" 376 } 377 disk { 378 %s 379 template = "%s" 380 } 381 } 382 ` 383 384 const testAccCheckVSphereVirtualMachineConfig_custom_configs = ` 385 resource "vsphere_virtual_machine" "car" { 386 name = "terraform-test-custom" 387 %s 388 vcpu = 2 389 memory = 4096 390 network_interface { 391 label = "%s" 392 } 393 custom_configuration_parameters { 394 "foo" = "bar" 395 "car" = "ferrari" 396 "num" = 42 397 } 398 disk { 399 %s 400 template = "%s" 401 } 402 } 403 `