Skip to content

Commit

Permalink
Add test case for VM refresh panic
Browse files Browse the repository at this point in the history
This adds a test case for @mjrider's new network device polling logic
that fixes several issues resulting from the inconsistencies between
physical devices and IP information when the guest is shut down. The
test is similar to the VM test, but in a second step it powers down the
VM and does another plan. This effectively triggers the panic on the old
code.
  • Loading branch information
vancluever committed Aug 17, 2017
1 parent f08a7d8 commit 6d23f58
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion vsphere/resource_vsphere_virtual_machine_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package vsphere

import (
"errors"
"fmt"
"log"
"os"
"regexp"
"testing"
"time"

"path/filepath"

"context"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi"
Expand All @@ -17,7 +21,6 @@ import (
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
)

///////
Expand Down Expand Up @@ -294,6 +297,39 @@ func TestAccVSphereVirtualMachine_basic(t *testing.T) {
})
}

func TestAccVSphereVirtualMachine_noPanicShutdown(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)

resource.Test(t, resource.TestCase{
PreCheck: func() { testBasicPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereVirtualMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
TestFuncData{vm: vm, label: basic_vars.label}.testCheckFuncBasic(),
),
},
resource.TestStep{
PreConfig: func() {
if err := testPowerOffVM("terraform-test"); err != nil {
panic(err)
}
},
PlanOnly: true,
Config: config,
ExpectNonEmptyPlan: true,
},
},
})
}

const testAccCheckVSphereVirtualMachineConfig_debug = `
provider "vsphere" {
client_debug = true
Expand Down Expand Up @@ -1153,6 +1189,45 @@ func TestAccVSphereVirtualMachine_mac_address(t *testing.T) {
})
}

// testPowerOffVM does an immediate power-off of the virtual machine and is
// used to help set up a refresh scenario where a VM is powered off, which has
// been a source of panics.
func testPowerOffVM(name string) error {
client := testAccProvider.Meta().(*govmomi.Client)
finder := find.NewFinder(client.Client, true)

dc, err := getDatacenter(client, os.Getenv("VSPHERE_DATACENTER"))
if err != nil {
return fmt.Errorf("error fetching datacenter: %s", err)
}

finder.SetDatacenter(dc)
if err != nil {
return fmt.Errorf("error finding datacenter: %s", err)
}

vm, err := finder.VirtualMachine(context.TODO(), name)
if err != nil {
return err
}
if vm == nil {
return fmt.Errorf("VM not found: %s", name)
}
task, err := vm.PowerOff(context.TODO())
if err != nil {
return fmt.Errorf("error powering off VM: %s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
if err := task.Wait(ctx); err != nil {
return fmt.Errorf("error waiting for poweroff: %s", err)
}
if ctx.Err() == context.Canceled {
return errors.New("timeout waiting for VM to shutdown")
}
return nil
}

func testAccCheckVSphereVirtualMachineDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*govmomi.Client)
finder := find.NewFinder(client.Client, true)
Expand Down

0 comments on commit 6d23f58

Please sign in to comment.