Skip to content

Commit

Permalink
add ut with simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
emlin committed Nov 8, 2016
1 parent 85b3340 commit 2308708
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pkg/vsphere/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

"github.com/stretchr/testify/assert"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
Expand Down Expand Up @@ -444,3 +446,78 @@ func TestBfsSnapshotTree(t *testing.T) {
t.Errorf("Should not found snapshot")
}
}

// faultyVirtualMachine wrap simulator.VirtualMachine with fault injection
type faultyVirtualMachine struct {
simulator.VirtualMachine

fault types.BaseMethodFault
}

// Run implements simulator.TaskRunner and always returns vm.fault
func (vm *faultyVirtualMachine) Run(task *simulator.Task) (types.AnyType, types.BaseMethodFault) {
return nil, vm.fault
}

// TestSoapFaults covers the various soap fault checking paths
func TestProperties(t *testing.T) {
ctx := context.Background()

// Nothing VC specific in this test, so we use the simpler ESX model
model := simulator.ESX()
defer model.Remove()
err := model.Create()
if err != nil {
t.Fatal(err)
}

server := model.Service.NewServer()
defer server.Close()

client, err := govmomi.NewClient(ctx, server.URL, true)
if err != nil {
t.Fatal(err)
}

// Any VM will do
finder := find.NewFinder(client.Client, false)
vmo, err := finder.VirtualMachine(ctx, "/ha-datacenter/vm/*_VM0")
if err != nil {
t.Fatal(err)
}

config := &session.Config{
Service: server.URL.String(),
Insecure: true,
Keepalive: time.Duration(5) * time.Minute,
DatacenterPath: "",
DatastorePath: "/ha-datacenter/datastore/*",
HostPath: "/ha-datacenter/host/*/*",
PoolPath: "/ha-datacenter/host/*/Resources",
}

s, err := session.NewSession(config).Create(ctx)
if err != nil {
t.Fatal(err)
}
vmm := NewVirtualMachine(ctx, s, vmo.Reference())
// Test the success path
var o mo.VirtualMachine
err = vmm.Properties(ctx, vmo.Reference(), []string{"config"}, &o)
if err != nil {
t.Fatal(err)
}

// Wrap existing vm MO with faultyVirtualMachine
ref := simulator.Map.Get(vmo.Reference())
fvm := &faultyVirtualMachine{*ref.(*simulator.VirtualMachine), nil}
simulator.Map.Put(fvm)

// Inject invalid connection state to vm
fvm.Summary.Runtime.ConnectionState = types.VirtualMachineConnectionStateInvalid
err = vmm.Properties(ctx, vmo.Reference(), []string{"config"}, &o)
if err != nil {
t.Fatal(err)
}
assert.True(t, o.Summary.Runtime.ConnectionState != types.VirtualMachineConnectionStateInvalid, "vm state should be fixed")
}

0 comments on commit 2308708

Please sign in to comment.