2015-10-06 05:53:07 +02:00
|
|
|
package vsphere
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-02 21:15:01 +02:00
|
|
|
"log"
|
2015-10-06 05:53:07 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2016-06-09 10:19:10 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/vmware/govmomi"
|
|
|
|
"github.com/vmware/govmomi/find"
|
|
|
|
"github.com/vmware/govmomi/object"
|
2015-11-25 05:35:40 +01:00
|
|
|
"github.com/vmware/govmomi/property"
|
|
|
|
"github.com/vmware/govmomi/vim25/mo"
|
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
2015-10-06 05:53:07 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
///////
|
|
|
|
// Various ENV vars are used to setup these tests. Look for `os.Getenv`
|
|
|
|
///////
|
|
|
|
|
|
|
|
// Base setup function to check that a template, and nic information is set
|
|
|
|
// TODO needs some TLC - determine exactly how we want to do this
|
|
|
|
func testBasicPreCheck(t *testing.T) {
|
|
|
|
|
|
|
|
testAccPreCheck(t)
|
|
|
|
|
|
|
|
if v := os.Getenv("VSPHERE_TEMPLATE"); v == "" {
|
|
|
|
t.Fatal("env variable VSPHERE_TEMPLATE must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := os.Getenv("VSPHERE_IPV4_GATEWAY"); v == "" {
|
|
|
|
t.Fatal("env variable VSPHERE_IPV4_GATEWAY must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := os.Getenv("VSPHERE_IPV4_ADDRESS"); v == "" {
|
|
|
|
t.Fatal("env variable VSPHERE_IPV4_ADDRESS must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := os.Getenv("VSPHERE_NETWORK_LABEL"); v == "" {
|
|
|
|
t.Fatal("env variable VSPHERE_NETWORK_LABEL must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
// Collects optional env vars used in the tests
|
|
|
|
////
|
|
|
|
func setupBaseVars() (string, string) {
|
2015-10-30 10:18:25 +01:00
|
|
|
var locationOpt string
|
|
|
|
var datastoreOpt string
|
|
|
|
|
|
|
|
if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
|
|
|
|
locationOpt += fmt.Sprintf(" datacenter = \"%s\"\n", v)
|
|
|
|
}
|
|
|
|
if v := os.Getenv("VSPHERE_CLUSTER"); v != "" {
|
|
|
|
locationOpt += fmt.Sprintf(" cluster = \"%s\"\n", v)
|
|
|
|
}
|
|
|
|
if v := os.Getenv("VSPHERE_RESOURCE_POOL"); v != "" {
|
|
|
|
locationOpt += fmt.Sprintf(" resource_pool = \"%s\"\n", v)
|
|
|
|
}
|
|
|
|
if v := os.Getenv("VSPHERE_DATASTORE"); v != "" {
|
|
|
|
datastoreOpt = fmt.Sprintf(" datastore = \"%s\"\n", v)
|
|
|
|
}
|
2016-06-02 21:15:01 +02:00
|
|
|
|
|
|
|
return locationOpt, datastoreOpt
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
// Structs and funcs used with DHCP data template
|
|
|
|
////
|
|
|
|
type TestDHCPBodyData struct {
|
|
|
|
template string
|
|
|
|
locationOpt string
|
|
|
|
datastoreOpt string
|
|
|
|
label string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (body TestDHCPBodyData) parseDHCPTemplateConfigWithTemplate(template string) string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
template,
|
|
|
|
body.locationOpt,
|
|
|
|
body.label,
|
|
|
|
body.datastoreOpt,
|
|
|
|
body.template,
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckVSphereTemplate_dhcp = `
|
|
|
|
%s
|
|
|
|
vcpu = 2
|
|
|
|
memory = 1024
|
|
|
|
network_interface {
|
|
|
|
label = "%s"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
%s
|
|
|
|
template = "%s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
// replaces data in the above template
|
|
|
|
func (body TestDHCPBodyData) parseDHCPTemplateConfig() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
testAccCheckVSphereTemplate_dhcp,
|
|
|
|
body.locationOpt,
|
|
|
|
body.label,
|
|
|
|
body.datastoreOpt,
|
|
|
|
body.template,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (body TestDHCPBodyData) testSprintfDHCPTemplateBodySecondArgDynamic(template string, arg string) string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
template,
|
|
|
|
body.locationOpt,
|
|
|
|
arg,
|
|
|
|
body.label,
|
|
|
|
body.datastoreOpt,
|
|
|
|
body.template,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns variables that are used in DHCP tests
|
|
|
|
func setupTemplateFuncDHCPData() TestDHCPBodyData {
|
|
|
|
|
|
|
|
locationOpt, datastoreOpt := setupBaseVars()
|
|
|
|
data := TestDHCPBodyData{
|
|
|
|
template: os.Getenv("VSPHERE_TEMPLATE"),
|
|
|
|
label: os.Getenv("VSPHERE_NETWORK_LABEL_DHCP"),
|
|
|
|
locationOpt: locationOpt,
|
|
|
|
datastoreOpt: datastoreOpt,
|
|
|
|
}
|
|
|
|
// log.Printf("[DEBUG] basic vars= %v", data)
|
|
|
|
return data
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
// Structs and funcs used with static ip data templates
|
|
|
|
////
|
|
|
|
type TemplateBasicBodyVars struct {
|
|
|
|
locationOpt string
|
|
|
|
label string
|
|
|
|
ipv4IpAddress string
|
|
|
|
ipv4Prefix string
|
|
|
|
ipv4Gateway string
|
|
|
|
datastoreOpt string
|
|
|
|
template string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a base template that has seven "%s" values in it, used by most fixed ip
|
|
|
|
// tests
|
|
|
|
func (body TemplateBasicBodyVars) testSprintfTemplateBody(template string) string {
|
|
|
|
|
|
|
|
return fmt.Sprintf(
|
|
|
|
template,
|
|
|
|
body.locationOpt,
|
|
|
|
body.label,
|
|
|
|
body.ipv4IpAddress,
|
|
|
|
body.ipv4Prefix,
|
|
|
|
body.ipv4Gateway,
|
|
|
|
body.datastoreOpt,
|
|
|
|
body.template,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setups variables used by fixed ip tests
|
|
|
|
func setupTemplateBasicBodyVars() TemplateBasicBodyVars {
|
|
|
|
|
|
|
|
locationOpt, datastoreOpt := setupBaseVars()
|
|
|
|
prefix := os.Getenv("VSPHERE_IPV4_PREFIX")
|
|
|
|
if prefix == "" {
|
|
|
|
prefix = "24"
|
|
|
|
}
|
|
|
|
data := TemplateBasicBodyVars{
|
|
|
|
template: os.Getenv("VSPHERE_TEMPLATE"),
|
|
|
|
ipv4Gateway: os.Getenv("VSPHERE_IPV4_GATEWAY"),
|
|
|
|
label: os.Getenv("VSPHERE_NETWORK_LABEL"),
|
|
|
|
ipv4IpAddress: os.Getenv("VSPHERE_IPV4_ADDRESS"),
|
|
|
|
ipv4Prefix: prefix,
|
|
|
|
locationOpt: locationOpt,
|
|
|
|
datastoreOpt: datastoreOpt,
|
|
|
|
}
|
|
|
|
// log.Printf("[DEBUG] basic vars= %v", data)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
// Basic data to create series of testing functions
|
|
|
|
////
|
|
|
|
type TestFuncData struct {
|
|
|
|
vm virtualMachine
|
|
|
|
label string
|
|
|
|
vmName string
|
|
|
|
vmResource string
|
|
|
|
numDisks string
|
|
|
|
numCPU string
|
|
|
|
mem string
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns TestCheckFunc's that are used in many of our tests
|
|
|
|
// mem defaults to 1024
|
|
|
|
// cpu defaults to 2
|
|
|
|
// disks defatuls to 1
|
|
|
|
// vmResource defaults to "terraform-test"
|
|
|
|
// vmName defaults to "vsphere_virtual_machine.foo
|
|
|
|
func (test TestFuncData) testCheckFuncBasic() (
|
|
|
|
resource.TestCheckFunc, resource.TestCheckFunc, resource.TestCheckFunc, resource.TestCheckFunc,
|
|
|
|
resource.TestCheckFunc, resource.TestCheckFunc, resource.TestCheckFunc) {
|
|
|
|
// log.Printf("[DEBUG] data= %v", test)
|
|
|
|
mem := test.mem
|
|
|
|
if mem == "" {
|
|
|
|
mem = "1024"
|
|
|
|
}
|
|
|
|
cpu := test.numCPU
|
|
|
|
if cpu == "" {
|
|
|
|
cpu = "2"
|
|
|
|
}
|
|
|
|
disks := test.numDisks
|
|
|
|
if disks == "" {
|
|
|
|
disks = "1"
|
|
|
|
}
|
|
|
|
res := test.vmResource
|
|
|
|
if res == "" {
|
|
|
|
res = "terraform-test"
|
|
|
|
}
|
|
|
|
vmName := test.vmName
|
|
|
|
if vmName == "" {
|
|
|
|
vmName = "vsphere_virtual_machine.foo"
|
|
|
|
}
|
|
|
|
return testAccCheckVSphereVirtualMachineExists(vmName, &test.vm),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "name", res),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "vcpu", cpu),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "memory", mem),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "disk.#", disks),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "network_interface.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "network_interface.0.label", test.label)
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckVSphereVirtualMachineConfig_really_basic = `
|
|
|
|
resource "vsphere_virtual_machine" "foo" {
|
|
|
|
name = "terraform-test"
|
|
|
|
` + testAccTemplateBasicBodyWithEnd
|
|
|
|
|
|
|
|
// WARNING this is one of the base templates. You change this and you will
|
|
|
|
// be impacting multiple tests
|
|
|
|
const testAccTemplateBasicBody = `
|
|
|
|
%s
|
|
|
|
vcpu = 2
|
|
|
|
memory = 1024
|
|
|
|
network_interface {
|
|
|
|
label = "%s"
|
|
|
|
ipv4_address = "%s"
|
|
|
|
ipv4_prefix_length = %s
|
|
|
|
ipv4_gateway = "%s"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
%s
|
|
|
|
template = "%s"
|
|
|
|
iops = 500
|
|
|
|
}
|
|
|
|
`
|
|
|
|
const testAccTemplateBasicBodyWithEnd = testAccTemplateBasicBody + `
|
|
|
|
}`
|
|
|
|
|
|
|
|
func TestAccVSphereVirtualMachine_basic(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
|
|
|
basic_vars := setupTemplateBasicBodyVars()
|
|
|
|
config := basic_vars.testSprintfTemplateBody(testAccCheckVSphereVirtualMachineConfig_really_basic)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_really_basic)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
2015-10-06 05:53:07 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-06-02 21:15:01 +02:00
|
|
|
PreCheck: func() { testBasicPreCheck(t) },
|
2015-10-06 05:53:07 +02:00
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2015-10-06 05:53:07 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
TestFuncData{vm: vm, label: basic_vars.label}.testCheckFuncBasic(),
|
2015-10-06 05:53:07 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_debug = `
|
|
|
|
provider "vsphere" {
|
|
|
|
client_debug = true
|
|
|
|
}
|
|
|
|
|
|
|
|
` + testAccCheckVSphereVirtualMachineConfig_really_basic
|
|
|
|
|
|
|
|
func TestAccVSphereVirtualMachine_client_debug(t *testing.T) {
|
2015-12-12 09:33:01 +01:00
|
|
|
var vm virtualMachine
|
2016-06-02 21:15:01 +02:00
|
|
|
basic_vars := setupTemplateBasicBodyVars()
|
|
|
|
config := basic_vars.testSprintfTemplateBody(testAccCheckVSphereVirtualMachineConfig_debug)
|
2015-12-12 09:33:01 +01:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_debug)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
|
|
|
|
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label :=
|
|
|
|
TestFuncData{vm: vm, label: basic_vars.label}.testCheckFuncBasic()
|
2015-12-12 09:33:01 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-06-02 21:15:01 +02:00
|
|
|
PreCheck: func() { testBasicPreCheck(t) },
|
2015-12-12 09:33:01 +01:00
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2015-12-12 09:33:01 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label,
|
|
|
|
testAccCheckDebugExists(),
|
2015-12-12 09:33:01 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_initType = `
|
|
|
|
resource "vsphere_virtual_machine" "thin" {
|
|
|
|
name = "terraform-test"
|
|
|
|
` + testAccTemplateBasicBody + `
|
|
|
|
disk {
|
|
|
|
size = 1
|
|
|
|
iops = 500
|
|
|
|
controller_type = "scsi"
|
|
|
|
name = "one"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
size = 1
|
|
|
|
controller_type = "ide"
|
|
|
|
type = "eager_zeroed"
|
|
|
|
name = "two"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestAccVSphereVirtualMachine_diskInitType(t *testing.T) {
|
2015-10-06 05:53:07 +02:00
|
|
|
var vm virtualMachine
|
2016-06-02 21:15:01 +02:00
|
|
|
basic_vars := setupTemplateBasicBodyVars()
|
|
|
|
config := basic_vars.testSprintfTemplateBody(testAccCheckVSphereVirtualMachineConfig_initType)
|
2015-10-30 10:18:25 +01:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
vmName := "vsphere_virtual_machine.thin"
|
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label :=
|
|
|
|
TestFuncData{vm: vm, label: basic_vars.label, vmName: vmName, numDisks: "3"}.testCheckFuncBasic()
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_initType)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
2015-10-06 05:53:07 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2015-10-06 05:53:07 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label,
|
|
|
|
// FIXME dynmically calculate the hashes
|
|
|
|
resource.TestCheckResourceAttr(vmName, "disk.294918912.type", "eager_zeroed"),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "disk.294918912.controller_type", "ide"),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "disk.1380467090.controller_type", "scsi"),
|
2015-10-06 05:53:07 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_dhcp = `
|
|
|
|
resource "vsphere_virtual_machine" "bar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
`
|
2016-06-02 01:38:56 +02:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
func TestAccVSphereVirtualMachine_dhcp(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
|
|
|
data := setupTemplateFuncDHCPData()
|
|
|
|
config := testAccCheckVSphereVirtualMachineConfig_dhcp + data.parseDHCPTemplateConfigWithTemplate(testAccCheckVSphereTemplate_dhcp)
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_dhcp+testAccCheckVSphereTemplate_dhcp)
|
|
|
|
log.Printf("[DEBUG] config= %s", config)
|
2016-06-02 01:38:56 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2016-06-02 01:38:56 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: "vsphere_virtual_machine.bar"}.testCheckFuncBasic(),
|
2016-06-02 01:38:56 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_custom_configs = `
|
|
|
|
resource "vsphere_virtual_machine" "car" {
|
|
|
|
name = "terraform-test-custom"
|
|
|
|
custom_configuration_parameters {
|
|
|
|
"foo" = "bar"
|
|
|
|
"car" = "ferrari"
|
|
|
|
"num" = 42
|
|
|
|
}
|
2016-06-09 10:19:10 +02:00
|
|
|
enable_disk_uuid = true
|
2016-06-02 21:15:01 +02:00
|
|
|
`
|
|
|
|
|
2015-11-09 02:49:49 +01:00
|
|
|
func TestAccVSphereVirtualMachine_custom_configs(t *testing.T) {
|
2016-06-02 21:15:01 +02:00
|
|
|
|
2015-11-09 02:49:49 +01:00
|
|
|
var vm virtualMachine
|
2016-06-02 21:15:01 +02:00
|
|
|
data := setupTemplateFuncDHCPData()
|
|
|
|
config := testAccCheckVSphereVirtualMachineConfig_custom_configs + data.parseDHCPTemplateConfigWithTemplate(testAccCheckVSphereTemplate_dhcp)
|
|
|
|
vmName := "vsphere_virtual_machine.car"
|
|
|
|
res := "terraform-test-custom"
|
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label :=
|
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: vmName, vmResource: res}.testCheckFuncBasic()
|
2015-11-09 02:49:49 +01:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_custom_configs+testAccCheckVSphereTemplate_dhcp)
|
|
|
|
log.Printf("[DEBUG] config= %s", config)
|
2015-11-09 02:49:49 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2015-11-09 02:49:49 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label,
|
|
|
|
testAccCheckVSphereVirtualMachineExistsHasCustomConfig(vmName, &vm),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "custom_configuration_parameters.foo", "bar"),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "custom_configuration_parameters.car", "ferrari"),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "custom_configuration_parameters.num", "42"),
|
2016-06-09 10:19:10 +02:00
|
|
|
resource.TestCheckResourceAttr(vmName, "enable_disk_uuid", "true"),
|
2015-11-09 02:49:49 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_createInFolder = `
|
|
|
|
resource "vsphere_virtual_machine" "folder" {
|
|
|
|
name = "terraform-test-folder"
|
|
|
|
folder = "%s"
|
|
|
|
`
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
func TestAccVSphereVirtualMachine_createInExistingFolder(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
2016-06-02 21:15:01 +02:00
|
|
|
datacenter := os.Getenv("VSPHERE_DATACENTER")
|
2015-11-16 19:44:39 +01:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
folder := "tf_test_cpureateInExistingFolder"
|
2015-11-16 19:44:39 +01:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
data := setupTemplateFuncDHCPData()
|
|
|
|
config := fmt.Sprintf(testAccCheckVSphereVirtualMachineConfig_createInFolder,
|
|
|
|
folder,
|
|
|
|
) + data.parseDHCPTemplateConfig()
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_createInFolder)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
2015-11-16 19:44:39 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2015-12-17 18:24:24 +01:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-11-16 19:44:39 +01:00
|
|
|
CheckDestroy: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
removeVSphereFolder(datacenter, folder, ""),
|
|
|
|
),
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
PreConfig: func() { createVSphereFolder(datacenter, folder) },
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2015-11-16 19:44:39 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: "vsphere_virtual_machine.folder", vmResource: "terraform-test-folder"}.testCheckFuncBasic(),
|
2015-11-16 19:44:39 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_createWithFolder = `
|
|
|
|
resource "vsphere_folder" "with_folder" {
|
|
|
|
path = "%s"
|
|
|
|
%s
|
|
|
|
}
|
|
|
|
resource "vsphere_virtual_machine" "with_folder" {
|
|
|
|
name = "terraform-test-with-folder"
|
|
|
|
folder = "${vsphere_folder.with_folder.path}"
|
|
|
|
`
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
func TestAccVSphereVirtualMachine_createWithFolder(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
|
|
|
var folderLocationOpt string
|
2016-06-02 21:15:01 +02:00
|
|
|
var f folder
|
2015-11-16 19:44:39 +01:00
|
|
|
|
|
|
|
if v := os.Getenv("VSPHERE_DATACENTER"); v != "" {
|
|
|
|
folderLocationOpt = fmt.Sprintf(" datacenter = \"%s\"\n", v)
|
|
|
|
}
|
2016-06-02 21:15:01 +02:00
|
|
|
|
|
|
|
folder := "tf_test_cpureateWithFolder"
|
|
|
|
|
|
|
|
data := setupTemplateFuncDHCPData()
|
|
|
|
vmName := "vsphere_virtual_machine.with_folder"
|
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label :=
|
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: vmName, vmResource: "terraform-test-with-folder"}.testCheckFuncBasic()
|
|
|
|
|
|
|
|
config := fmt.Sprintf(testAccCheckVSphereVirtualMachineConfig_createWithFolder,
|
|
|
|
folder,
|
|
|
|
folderLocationOpt,
|
|
|
|
) + data.parseDHCPTemplateConfig()
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_createWithFolder+testAccCheckVSphereTemplate_dhcp)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
2015-11-16 19:44:39 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2015-12-17 18:24:24 +01:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-11-16 19:44:39 +01:00
|
|
|
CheckDestroy: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
testAccCheckVSphereFolderDestroy,
|
|
|
|
),
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2015-11-16 19:44:39 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label,
|
|
|
|
testAccCheckVSphereFolderExists(vmName, &f),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "folder", folder),
|
2015-11-16 19:44:39 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVsphereVirtualMachineConfig_cdrom = `
|
|
|
|
resource "vsphere_virtual_machine" "with_cdrom" {
|
|
|
|
name = "terraform-test-with-cdrom"
|
|
|
|
cdrom {
|
|
|
|
datastore = "%s"
|
|
|
|
path = "%s"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2015-11-25 15:35:37 +01:00
|
|
|
func TestAccVSphereVirtualMachine_createWithCdrom(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
// FIXME check that these exist
|
2015-11-25 15:35:37 +01:00
|
|
|
cdromDatastore := os.Getenv("VSPHERE_CDROM_DATASTORE")
|
|
|
|
cdromPath := os.Getenv("VSPHERE_CDROM_PATH")
|
2016-06-02 21:15:01 +02:00
|
|
|
vmName := "vsphere_virtual_machine.with_cdrom"
|
|
|
|
|
|
|
|
data := setupTemplateFuncDHCPData()
|
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label :=
|
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: vmName, vmResource: "terraform-test-with-cdrom"}.testCheckFuncBasic()
|
|
|
|
|
|
|
|
config := fmt.Sprintf(
|
|
|
|
testAccCheckVsphereVirtualMachineConfig_cdrom,
|
|
|
|
cdromDatastore,
|
|
|
|
cdromPath,
|
|
|
|
) + data.parseDHCPTemplateConfig()
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVsphereVirtualMachineConfig_cdrom)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
2015-11-25 15:35:37 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2015-11-25 15:35:37 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label,
|
|
|
|
//resource.TestCheckResourceAttr(
|
|
|
|
// "vsphere_virtual_machine.with_cdrom", "disk.4088143748.template", template),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "cdrom.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "cdrom.0.datastore", cdromDatastore),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "cdrom.0.path", cdromPath),
|
2015-11-25 15:35:37 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_withExistingVmdk = `
|
|
|
|
resource "vsphere_virtual_machine" "with_existing_vmdk" {
|
|
|
|
name = "terraform-test-with-existing-vmdk"
|
|
|
|
%s
|
|
|
|
vcpu = 2
|
|
|
|
memory = 1024
|
|
|
|
network_interface {
|
|
|
|
label = "%s"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
%s
|
|
|
|
vmdk = "%s"
|
|
|
|
bootable = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestAccVSphereVirtualMachine_createWithExistingVmdk(t *testing.T) {
|
2016-04-27 18:22:22 +02:00
|
|
|
var vm virtualMachine
|
2016-06-02 21:15:01 +02:00
|
|
|
vmdk_path := os.Getenv("VSPHERE_VMDK_PATH")
|
2016-04-27 18:22:22 +02:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
data := setupTemplateFuncDHCPData()
|
|
|
|
config := fmt.Sprintf(
|
|
|
|
testAccCheckVSphereVirtualMachineConfig_withExistingVmdk,
|
|
|
|
data.locationOpt,
|
|
|
|
data.label,
|
|
|
|
data.datastoreOpt,
|
|
|
|
vmdk_path,
|
|
|
|
)
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_withExistingVmdk)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
2016-04-27 18:22:22 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2016-04-27 18:22:22 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: "vsphere_virtual_machine.with_existing_vmdk",
|
|
|
|
vmResource: "terraform-test-with-existing-vmdk"}.testCheckFuncBasic(),
|
|
|
|
//resource.TestCheckResourceAttr(
|
|
|
|
// "vsphere_virtual_machine.with_existing_vmdk", "disk.2393891804.vmdk", vmdk_path),
|
|
|
|
//resource.TestCheckResourceAttr(
|
|
|
|
// "vsphere_virtual_machine.with_existing_vmdk", "disk.2393891804.bootable", "true"),
|
2016-04-27 18:22:22 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_updateMemory = `
|
|
|
|
resource "vsphere_virtual_machine" "bar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
%s
|
|
|
|
vcpu = 2
|
|
|
|
memory = %s
|
|
|
|
network_interface {
|
|
|
|
label = "%s"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
%s
|
|
|
|
template = "%s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-05-03 17:58:33 +02:00
|
|
|
func TestAccVSphereVirtualMachine_updateMemory(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
2016-06-02 21:15:01 +02:00
|
|
|
data := setupTemplateFuncDHCPData()
|
2016-05-03 17:58:33 +02:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_updateMemory)
|
|
|
|
|
|
|
|
config := data.testSprintfDHCPTemplateBodySecondArgDynamic(testAccCheckVSphereVirtualMachineConfig_updateMemory, "1024")
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
|
|
|
|
|
|
|
configUpdate := data.testSprintfDHCPTemplateBodySecondArgDynamic(testAccCheckVSphereVirtualMachineConfig_updateMemory, "2048")
|
|
|
|
log.Printf("[DEBUG] template configUpdate= %s", configUpdate)
|
2016-05-03 17:58:33 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2016-05-03 17:58:33 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: "vsphere_virtual_machine.bar"}.testCheckFuncBasic(),
|
2016-05-03 17:58:33 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: configUpdate,
|
2016-05-03 17:58:33 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
TestFuncData{vm: vm, label: data.label, mem: "2048", vmName: "vsphere_virtual_machine.bar"}.testCheckFuncBasic(),
|
2016-05-03 17:58:33 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_updateVcpu = `
|
|
|
|
resource "vsphere_virtual_machine" "bar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
%s
|
|
|
|
vcpu = %s
|
|
|
|
memory = 1024
|
|
|
|
network_interface {
|
|
|
|
label = "%s"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
%s
|
|
|
|
template = "%s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-05-03 17:58:33 +02:00
|
|
|
func TestAccVSphereVirtualMachine_updateVcpu(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
2016-06-02 21:15:01 +02:00
|
|
|
data := setupTemplateFuncDHCPData()
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_updateVcpu)
|
2016-05-03 17:58:33 +02:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
config := data.testSprintfDHCPTemplateBodySecondArgDynamic(testAccCheckVSphereVirtualMachineConfig_updateVcpu, "2")
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
|
|
|
|
|
|
|
configUpdate := data.testSprintfDHCPTemplateBodySecondArgDynamic(testAccCheckVSphereVirtualMachineConfig_updateVcpu, "4")
|
|
|
|
log.Printf("[DEBUG] template configUpdate= %s", configUpdate)
|
2016-05-03 17:58:33 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config,
|
2016-05-03 17:58:33 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: "vsphere_virtual_machine.bar"}.testCheckFuncBasic(),
|
2016-05-03 17:58:33 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: configUpdate,
|
2016-05-03 17:58:33 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: "vsphere_virtual_machine.bar", numCPU: "4"}.testCheckFuncBasic(),
|
2016-05-03 17:58:33 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
const testAccCheckVSphereVirtualMachineConfig_ipv4Andipv6 = `
|
|
|
|
resource "vsphere_virtual_machine" "ipv4ipv6" {
|
|
|
|
name = "terraform-test-ipv4-ipv6"
|
|
|
|
%s
|
|
|
|
vcpu = 2
|
|
|
|
memory = 1024
|
|
|
|
network_interface {
|
|
|
|
label = "%s"
|
|
|
|
ipv4_address = "%s"
|
|
|
|
ipv4_prefix_length = %s
|
|
|
|
ipv4_gateway = "%s"
|
|
|
|
ipv6_address = "%s"
|
|
|
|
ipv6_prefix_length = 64
|
|
|
|
ipv6_gateway = "%s"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
%s
|
|
|
|
template = "%s"
|
|
|
|
iops = 500
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
size = 1
|
|
|
|
iops = 500
|
|
|
|
name = "one"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-05-03 18:27:24 +02:00
|
|
|
func TestAccVSphereVirtualMachine_ipv4Andipv6(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
2016-06-02 21:15:01 +02:00
|
|
|
data := setupTemplateBasicBodyVars()
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_ipv4Andipv6)
|
2016-05-03 18:27:24 +02:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
vmName := "vsphere_virtual_machine.ipv4ipv6"
|
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label :=
|
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: vmName, numDisks: "2", vmResource: "terraform-test-ipv4-ipv6"}.testCheckFuncBasic()
|
|
|
|
|
|
|
|
// FIXME test for this or warn??
|
2016-05-03 18:27:24 +02:00
|
|
|
ipv6Address := os.Getenv("VSPHERE_IPV6_ADDRESS")
|
|
|
|
ipv6Gateway := os.Getenv("VSPHERE_IPV6_GATEWAY")
|
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
config := fmt.Sprintf(
|
|
|
|
testAccCheckVSphereVirtualMachineConfig_ipv4Andipv6,
|
|
|
|
data.locationOpt,
|
|
|
|
data.label,
|
|
|
|
data.ipv4IpAddress,
|
|
|
|
data.ipv4Prefix,
|
|
|
|
data.ipv4Gateway,
|
|
|
|
ipv6Address,
|
|
|
|
ipv6Gateway,
|
|
|
|
data.datastoreOpt,
|
|
|
|
data.template,
|
|
|
|
)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label,
|
|
|
|
resource.TestCheckResourceAttr(vmName, "network_interface.0.ipv4_address", data.ipv4IpAddress),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "network_interface.0.ipv4_gateway", data.ipv4Gateway),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "network_interface.0.ipv6_address", ipv6Address),
|
|
|
|
resource.TestCheckResourceAttr(vmName, "network_interface.0.ipv6_gateway", ipv6Gateway),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckVSphereVirtualMachineConfig_updateAddDisks = `
|
|
|
|
resource "vsphere_virtual_machine" "foo" {
|
|
|
|
name = "terraform-test"
|
|
|
|
` + testAccTemplateBasicBody + `
|
|
|
|
disk {
|
|
|
|
size = 1
|
|
|
|
iops = 500
|
|
|
|
name = "one"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
size = 1
|
|
|
|
iops = 500
|
|
|
|
name = "two"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
size = 1
|
|
|
|
iops = 500
|
|
|
|
name = "three"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
const testAccCheckVSphereVirtualMachineConfig_basic = `
|
|
|
|
resource "vsphere_virtual_machine" "foo" {
|
|
|
|
name = "terraform-test"
|
|
|
|
` + testAccTemplateBasicBody + `
|
|
|
|
disk {
|
|
|
|
size = 1
|
|
|
|
iops = 500
|
|
|
|
name = "one"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestAccVSphereVirtualMachine_updateDisks(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
|
|
|
basic_vars := setupTemplateBasicBodyVars()
|
|
|
|
config_basic := basic_vars.testSprintfTemplateBody(testAccCheckVSphereVirtualMachineConfig_basic)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_basic)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config_basic)
|
|
|
|
|
|
|
|
config_add := basic_vars.testSprintfTemplateBody(testAccCheckVSphereVirtualMachineConfig_updateAddDisks)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_basic)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config_add)
|
|
|
|
|
|
|
|
config_del := basic_vars.testSprintfTemplateBody(testAccCheckVSphereVirtualMachineConfig_really_basic)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_really_basic)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config_del)
|
|
|
|
|
2016-05-03 18:27:24 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-06-02 21:15:01 +02:00
|
|
|
Config: config_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
TestFuncData{vm: vm, label: basic_vars.label, numDisks: "2"}.testCheckFuncBasic(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config_add,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
TestFuncData{vm: vm, label: basic_vars.label, numDisks: "4"}.testCheckFuncBasic(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config_del,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
TestFuncData{vm: vm, label: basic_vars.label, numDisks: "1"}.testCheckFuncBasic(),
|
2016-05-03 18:27:24 +02:00
|
|
|
),
|
2016-06-02 21:15:01 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckVSphereVirtualMachineConfig_mac_address = `
|
|
|
|
resource "vsphere_virtual_machine" "mac_address" {
|
|
|
|
name = "terraform-mac-address"
|
|
|
|
%s
|
|
|
|
vcpu = 2
|
|
|
|
memory = 1024
|
|
|
|
network_interface {
|
|
|
|
label = "%s"
|
|
|
|
mac_address = "%s"
|
|
|
|
}
|
|
|
|
disk {
|
|
|
|
%s
|
|
|
|
template = "%s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
// VSPHERE_NETWORK_MAC_ADDRESS needs to be set to run TestAccVSphereVirtualMachine_mac_address
|
|
|
|
// use a basic NIC MAC address like 6:5c:89:2b:a0:64
|
|
|
|
func testMacPreCheck(t *testing.T) {
|
|
|
|
|
|
|
|
testBasicPreCheck(t)
|
|
|
|
|
|
|
|
// TODO should start do parse values to ensure they are correct
|
|
|
|
// for instance
|
|
|
|
// func ParseMAC(s string) (hw HardwareAddr, err error)
|
|
|
|
if v := os.Getenv("VSPHERE_NETWORK_MAC_ADDRESS"); v == "" {
|
|
|
|
t.Fatal("env variable VSPHERE_NETWORK_MAC_ADDRESS must be set for this acceptance test")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test new mac address feature
|
|
|
|
func TestAccVSphereVirtualMachine_mac_address(t *testing.T) {
|
|
|
|
var vm virtualMachine
|
|
|
|
data := setupTemplateFuncDHCPData()
|
|
|
|
vmName := "vsphere_virtual_machine.mac_address"
|
|
|
|
|
|
|
|
macAddress := os.Getenv("VSPHERE_NETWORK_MAC_ADDRESS")
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] template= %s", testAccCheckVSphereVirtualMachineConfig_mac_address)
|
|
|
|
config := fmt.Sprintf(
|
|
|
|
testAccCheckVSphereVirtualMachineConfig_mac_address,
|
|
|
|
data.locationOpt,
|
|
|
|
data.label,
|
|
|
|
macAddress,
|
|
|
|
data.datastoreOpt,
|
|
|
|
data.template,
|
|
|
|
)
|
|
|
|
log.Printf("[DEBUG] template config= %s", config)
|
|
|
|
|
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label :=
|
|
|
|
TestFuncData{vm: vm, label: data.label, vmName: vmName, numDisks: "1", vmResource: "terraform-mac-address"}.testCheckFuncBasic()
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testMacPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: config,
|
2016-05-03 18:27:24 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-06-02 21:15:01 +02:00
|
|
|
test_exists, test_name, test_cpu, test_mem, test_num_disk, test_num_of_nic, test_nic_label,
|
|
|
|
resource.TestCheckResourceAttr(vmName, "network_interface.0.mac_address", macAddress),
|
2016-05-03 18:27:24 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
|
|
|
|
client := testAccProvider.Meta().(*govmomi.Client)
|
|
|
|
finder := find.NewFinder(client.Client, true)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "vsphere_virtual_machine" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dcFolders, err := dc.Folders(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
folder := dcFolders.VmFolder
|
|
|
|
if len(rs.Primary.Attributes["folder"]) > 0 {
|
|
|
|
si := object.NewSearchIndex(client.Client)
|
|
|
|
folderRef, err := si.FindByInventoryPath(
|
|
|
|
context.TODO(), fmt.Sprintf("%v/vm/%v", rs.Primary.Attributes["datacenter"], rs.Primary.Attributes["folder"]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if folderRef != nil {
|
|
|
|
folder = folderRef.(*object.Folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 09:33:01 +01:00
|
|
|
v, err := object.NewSearchIndex(client.Client).FindChild(context.TODO(), folder, rs.Primary.Attributes["name"])
|
2015-11-16 19:44:39 +01:00
|
|
|
|
2015-12-12 09:33:01 +01:00
|
|
|
if v != nil {
|
2015-10-06 05:53:07 +02:00
|
|
|
return fmt.Errorf("Record still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-25 06:41:01 +01:00
|
|
|
func testAccCheckVSphereVirtualMachineExistsHasCustomConfig(n string, vm *virtualMachine) resource.TestCheckFunc {
|
2015-11-25 05:35:40 +01:00
|
|
|
return func(s *terraform.State) error {
|
2015-11-25 06:41:01 +01:00
|
|
|
|
2015-11-25 05:35:40 +01:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*govmomi.Client)
|
|
|
|
finder := find.NewFinder(client.Client, true)
|
|
|
|
|
|
|
|
dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dcFolders, err := dc.Folders(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
|
2015-11-25 06:41:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
2015-11-25 05:35:40 +01:00
|
|
|
|
2015-11-25 06:41:01 +01:00
|
|
|
finder = finder.SetDatacenter(dc)
|
2015-11-25 05:35:40 +01:00
|
|
|
instance, err := finder.VirtualMachine(context.TODO(), rs.Primary.Attributes["name"])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var mvm mo.VirtualMachine
|
|
|
|
|
|
|
|
collector := property.DefaultCollector(client.Client)
|
|
|
|
|
|
|
|
if err := collector.RetrieveOne(context.TODO(), instance.Reference(), []string{"config.extraConfig"}, &mvm); err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var configMap = make(map[string]types.AnyType)
|
|
|
|
if mvm.Config != nil && mvm.Config.ExtraConfig != nil && len(mvm.Config.ExtraConfig) > 0 {
|
|
|
|
for _, v := range mvm.Config.ExtraConfig {
|
|
|
|
value := v.GetOptionValue()
|
|
|
|
configMap[value.Key] = value.Value
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("error no ExtraConfig")
|
|
|
|
}
|
|
|
|
|
|
|
|
if configMap["foo"] == nil {
|
|
|
|
return fmt.Errorf("error no ExtraConfig for 'foo'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if configMap["foo"] != "bar" {
|
|
|
|
return fmt.Errorf("error ExtraConfig 'foo' != bar")
|
|
|
|
}
|
|
|
|
|
|
|
|
if configMap["car"] == nil {
|
|
|
|
return fmt.Errorf("error no ExtraConfig for 'car'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if configMap["car"] != "ferrari" {
|
|
|
|
return fmt.Errorf("error ExtraConfig 'car' != ferrari")
|
|
|
|
}
|
|
|
|
|
2015-11-25 06:41:01 +01:00
|
|
|
if configMap["num"] == nil {
|
|
|
|
return fmt.Errorf("error no ExtraConfig for 'num'")
|
2015-11-25 05:35:40 +01:00
|
|
|
}
|
|
|
|
|
2015-11-25 06:41:01 +01:00
|
|
|
// todo this should be an int, getting back a string
|
|
|
|
if configMap["num"] != "42" {
|
|
|
|
return fmt.Errorf("error ExtraConfig 'num' != 42")
|
2015-11-25 05:35:40 +01:00
|
|
|
}
|
|
|
|
*vm = virtualMachine{
|
|
|
|
name: rs.Primary.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-11-16 19:44:39 +01:00
|
|
|
|
2016-06-02 21:15:01 +02:00
|
|
|
func testAccCheckDebugExists() resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if _, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".govmomi")); os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("Debug logs not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2016-06-02 21:15:01 +02:00
|
|
|
if n == "" {
|
|
|
|
return fmt.Errorf("No vm name passed in")
|
|
|
|
}
|
|
|
|
if vm == nil {
|
|
|
|
return fmt.Errorf("No vm obj passed in")
|
|
|
|
}
|
2015-10-06 05:53:07 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*govmomi.Client)
|
|
|
|
finder := find.NewFinder(client.Client, true)
|
|
|
|
|
|
|
|
dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dcFolders, err := dc.Folders(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error %s", err)
|
|
|
|
}
|
|
|
|
|
2015-11-16 19:44:39 +01:00
|
|
|
folder := dcFolders.VmFolder
|
|
|
|
if len(rs.Primary.Attributes["folder"]) > 0 {
|
|
|
|
si := object.NewSearchIndex(client.Client)
|
|
|
|
folderRef, err := si.FindByInventoryPath(
|
|
|
|
context.TODO(), fmt.Sprintf("%v/vm/%v", rs.Primary.Attributes["datacenter"], rs.Primary.Attributes["folder"]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if folderRef != nil {
|
|
|
|
folder = folderRef.(*object.Folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), folder, rs.Primary.Attributes["name"])
|
|
|
|
|
2015-10-06 05:53:07 +02:00
|
|
|
*vm = virtualMachine{
|
|
|
|
name: rs.Primary.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|