Merge pull request #3717 from rakutentech/improve-acceptance-test
provider/vsphere: Fix acceptance tests for using optional parameters
This commit is contained in:
commit
e85a618d67
|
@ -15,9 +15,21 @@ import (
|
||||||
|
|
||||||
func TestAccVSphereVirtualMachine_basic(t *testing.T) {
|
func TestAccVSphereVirtualMachine_basic(t *testing.T) {
|
||||||
var vm virtualMachine
|
var vm virtualMachine
|
||||||
datacenter := os.Getenv("VSPHERE_DATACENTER")
|
var locationOpt string
|
||||||
cluster := os.Getenv("VSPHERE_CLUSTER")
|
var datastoreOpt string
|
||||||
datastore := os.Getenv("VSPHERE_DATASTORE")
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
template := os.Getenv("VSPHERE_TEMPLATE")
|
template := os.Getenv("VSPHERE_TEMPLATE")
|
||||||
gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY")
|
gateway := os.Getenv("VSPHERE_NETWORK_GATEWAY")
|
||||||
label := os.Getenv("VSPHERE_NETWORK_LABEL")
|
label := os.Getenv("VSPHERE_NETWORK_LABEL")
|
||||||
|
@ -31,28 +43,23 @@ func TestAccVSphereVirtualMachine_basic(t *testing.T) {
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: fmt.Sprintf(
|
Config: fmt.Sprintf(
|
||||||
testAccCheckVSphereVirtualMachineConfig_basic,
|
testAccCheckVSphereVirtualMachineConfig_basic,
|
||||||
datacenter,
|
locationOpt,
|
||||||
cluster,
|
|
||||||
gateway,
|
gateway,
|
||||||
label,
|
label,
|
||||||
ip_address,
|
ip_address,
|
||||||
datastore,
|
datastoreOpt,
|
||||||
template,
|
template,
|
||||||
),
|
),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.foo", &vm),
|
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.foo", &vm),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.foo", "name", "terraform-test"),
|
"vsphere_virtual_machine.foo", "name", "terraform-test"),
|
||||||
resource.TestCheckResourceAttr(
|
|
||||||
"vsphere_virtual_machine.foo", "datacenter", datacenter),
|
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.foo", "vcpu", "2"),
|
"vsphere_virtual_machine.foo", "vcpu", "2"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.foo", "memory", "4096"),
|
"vsphere_virtual_machine.foo", "memory", "4096"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.foo", "disk.#", "2"),
|
"vsphere_virtual_machine.foo", "disk.#", "2"),
|
||||||
resource.TestCheckResourceAttr(
|
|
||||||
"vsphere_virtual_machine.foo", "disk.0.datastore", datastore),
|
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.foo", "disk.0.template", template),
|
"vsphere_virtual_machine.foo", "disk.0.template", template),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
@ -67,12 +74,23 @@ func TestAccVSphereVirtualMachine_basic(t *testing.T) {
|
||||||
|
|
||||||
func TestAccVSphereVirtualMachine_dhcp(t *testing.T) {
|
func TestAccVSphereVirtualMachine_dhcp(t *testing.T) {
|
||||||
var vm virtualMachine
|
var vm virtualMachine
|
||||||
datacenter := os.Getenv("VSPHERE_DATACENTER")
|
var locationOpt string
|
||||||
cluster := os.Getenv("VSPHERE_CLUSTER")
|
var datastoreOpt string
|
||||||
datastore := os.Getenv("VSPHERE_DATASTORE")
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
template := os.Getenv("VSPHERE_TEMPLATE")
|
template := os.Getenv("VSPHERE_TEMPLATE")
|
||||||
label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
|
label := os.Getenv("VSPHERE_NETWORK_LABEL_DHCP")
|
||||||
password := os.Getenv("VSPHERE_VM_PASSWORD")
|
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
@ -82,27 +100,21 @@ func TestAccVSphereVirtualMachine_dhcp(t *testing.T) {
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: fmt.Sprintf(
|
Config: fmt.Sprintf(
|
||||||
testAccCheckVSphereVirtualMachineConfig_dhcp,
|
testAccCheckVSphereVirtualMachineConfig_dhcp,
|
||||||
datacenter,
|
locationOpt,
|
||||||
cluster,
|
|
||||||
label,
|
label,
|
||||||
datastore,
|
datastoreOpt,
|
||||||
template,
|
template,
|
||||||
password,
|
|
||||||
),
|
),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm),
|
testAccCheckVSphereVirtualMachineExists("vsphere_virtual_machine.bar", &vm),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.bar", "name", "terraform-test"),
|
"vsphere_virtual_machine.bar", "name", "terraform-test"),
|
||||||
resource.TestCheckResourceAttr(
|
|
||||||
"vsphere_virtual_machine.bar", "datacenter", datacenter),
|
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.bar", "vcpu", "2"),
|
"vsphere_virtual_machine.bar", "vcpu", "2"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.bar", "memory", "4096"),
|
"vsphere_virtual_machine.bar", "memory", "4096"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.bar", "disk.#", "1"),
|
"vsphere_virtual_machine.bar", "disk.#", "1"),
|
||||||
resource.TestCheckResourceAttr(
|
|
||||||
"vsphere_virtual_machine.bar", "disk.0.datastore", datastore),
|
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"vsphere_virtual_machine.bar", "disk.0.template", template),
|
"vsphere_virtual_machine.bar", "disk.0.template", template),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
@ -168,20 +180,6 @@ func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resou
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
|
_, err = object.NewSearchIndex(client.Client).FindChild(context.TODO(), dcFolders.VmFolder, rs.Primary.Attributes["name"])
|
||||||
/*
|
|
||||||
vmRef, err := client.SearchIndex().FindChild(dcFolders.VmFolder, rs.Primary.Attributes["name"])
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
found := govmomi.NewVirtualMachine(client, vmRef.Reference())
|
|
||||||
fmt.Printf("%v", found)
|
|
||||||
|
|
||||||
if found.Name != rs.Primary.ID {
|
|
||||||
return fmt.Errorf("Instance not found")
|
|
||||||
}
|
|
||||||
*instance = *found
|
|
||||||
*/
|
|
||||||
|
|
||||||
*vm = virtualMachine{
|
*vm = virtualMachine{
|
||||||
name: rs.Primary.ID,
|
name: rs.Primary.ID,
|
||||||
|
@ -194,8 +192,7 @@ func testAccCheckVSphereVirtualMachineExists(n string, vm *virtualMachine) resou
|
||||||
const testAccCheckVSphereVirtualMachineConfig_basic = `
|
const testAccCheckVSphereVirtualMachineConfig_basic = `
|
||||||
resource "vsphere_virtual_machine" "foo" {
|
resource "vsphere_virtual_machine" "foo" {
|
||||||
name = "terraform-test"
|
name = "terraform-test"
|
||||||
datacenter = "%s"
|
%s
|
||||||
cluster = "%s"
|
|
||||||
vcpu = 2
|
vcpu = 2
|
||||||
memory = 4096
|
memory = 4096
|
||||||
gateway = "%s"
|
gateway = "%s"
|
||||||
|
@ -205,7 +202,7 @@ resource "vsphere_virtual_machine" "foo" {
|
||||||
subnet_mask = "255.255.255.0"
|
subnet_mask = "255.255.255.0"
|
||||||
}
|
}
|
||||||
disk {
|
disk {
|
||||||
datastore = "%s"
|
%s
|
||||||
template = "%s"
|
template = "%s"
|
||||||
iops = 500
|
iops = 500
|
||||||
}
|
}
|
||||||
|
@ -219,22 +216,15 @@ resource "vsphere_virtual_machine" "foo" {
|
||||||
const testAccCheckVSphereVirtualMachineConfig_dhcp = `
|
const testAccCheckVSphereVirtualMachineConfig_dhcp = `
|
||||||
resource "vsphere_virtual_machine" "bar" {
|
resource "vsphere_virtual_machine" "bar" {
|
||||||
name = "terraform-test"
|
name = "terraform-test"
|
||||||
datacenter = "%s"
|
%s
|
||||||
cluster = "%s"
|
|
||||||
vcpu = 2
|
vcpu = 2
|
||||||
memory = 4096
|
memory = 4096
|
||||||
network_interface {
|
network_interface {
|
||||||
label = "%s"
|
label = "%s"
|
||||||
}
|
}
|
||||||
disk {
|
disk {
|
||||||
datastore = "%s"
|
%s
|
||||||
template = "%s"
|
template = "%s"
|
||||||
}
|
}
|
||||||
|
|
||||||
connection {
|
|
||||||
host = "${self.network_interface.0.ip_address}"
|
|
||||||
user = "root"
|
|
||||||
password = "%s"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
|
@ -64,15 +64,19 @@ configuration fields to be set using the documented environment variables.
|
||||||
|
|
||||||
In addition, the following environment variables are used in tests, and must be set to valid values for your vSphere environment:
|
In addition, the following environment variables are used in tests, and must be set to valid values for your vSphere environment:
|
||||||
|
|
||||||
* VSPHERE\_CLUSTER
|
|
||||||
* VSPHERE\_DATACENTER
|
|
||||||
* VSPHERE\_DATASTORE
|
|
||||||
* VSPHERE\_NETWORK\_GATEWAY
|
* VSPHERE\_NETWORK\_GATEWAY
|
||||||
* VSPHERE\_NETWORK\_IP\_ADDRESS
|
* VSPHERE\_NETWORK\_IP\_ADDRESS
|
||||||
* VSPHERE\_NETWORK\_LABEL
|
* VSPHERE\_NETWORK\_LABEL
|
||||||
* VSPHERE\_NETWORK\_LABEL\_DHCP
|
* VSPHERE\_NETWORK\_LABEL\_DHCP
|
||||||
* VSPHERE\_TEMPLATE
|
* VSPHERE\_TEMPLATE
|
||||||
* VSPHERE\_VM\_PASSWORD
|
|
||||||
|
The following environment variables depend on your vSphere environment:
|
||||||
|
|
||||||
|
* VSPHERE\_DATACENTER
|
||||||
|
* VSPHERE\_CLUSTER
|
||||||
|
* VSPHERE\_RESOURCE\_POOL
|
||||||
|
* VSPHERE\_DATASTORE
|
||||||
|
|
||||||
|
|
||||||
These are used to set and verify attributes on the `vsphere_virtual_machine`
|
These are used to set and verify attributes on the `vsphere_virtual_machine`
|
||||||
resource in tests.
|
resource in tests.
|
||||||
|
|
Loading…
Reference in New Issue