Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vcsim: add autostart option to power on VMs #906

Merged
merged 1 commit into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion govc/test/vm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ load test_helper
}

@test "vm.register vcsim" {
vcsim_env
vcsim_env -autostart=false

host=$GOVC_HOST
pool=$GOVC_RESOURCE_POOL
Expand Down
13 changes: 12 additions & 1 deletion simulator/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Model struct {
ServiceContent types.ServiceContent
RootFolder mo.Folder

// Autostart will power on Model created VMs when true
Autostart bool

// Datacenter specifies the number of Datacenter entities to create
Datacenter int

Expand Down Expand Up @@ -87,6 +90,7 @@ func ESX() *Model {
return &Model{
ServiceContent: esx.ServiceContent,
RootFolder: esx.RootFolder,
Autostart: true,
Datastore: 1,
Machine: 2,
}
Expand All @@ -97,6 +101,7 @@ func VPX() *Model {
return &Model{
ServiceContent: vpx.ServiceContent,
RootFolder: vpx.RootFolder,
Autostart: true,
Datacenter: 1,
Portgroup: 1,
Host: 1,
Expand Down Expand Up @@ -238,10 +243,16 @@ func (m *Model) Create() error {
return err
}

err = task.Wait(ctx)
info, err := task.WaitForResult(ctx, nil)
if err != nil {
return err
}

vm := object.NewVirtualMachine(client, info.Result.(types.ManagedObjectReference))

if m.Autostart {
_, _ = vm.PowerOn(ctx)
}
}

return nil
Expand Down
55 changes: 15 additions & 40 deletions simulator/virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,52 +412,27 @@ func TestShutdownGuest(t *testing.T) {
t.Fatal(err)
}

finder := find.NewFinder(c.Client, false)

dc, err := finder.DefaultDatacenter(ctx)
vm := object.NewVirtualMachine(c.Client, Map.Any("VirtualMachine").Reference())
// shutdown the vm
err = vm.ShutdownGuest(ctx)
if err != nil {
t.Fatal(err)
}
// state should be poweroff
state, err := vm.PowerState(ctx)
if err != nil {
t.Fatal(err)
}

finder.SetDatacenter(dc)

vms, err := finder.VirtualMachineList(ctx, "*")
// use the default first vm for test
if len(vms) > 0 {
vmm := vms[0]
// powon first
task, err := vmm.PowerOn(ctx)
if err != nil {
t.Fatal(err)
}

err = task.Wait(ctx)
if err != nil {
t.Fatal(err)
}

// shutdown the vm
err = vmm.ShutdownGuest(ctx)
if err != nil {
t.Fatal(err)
}
// state should be poweroff
state, err := vmm.PowerState(ctx)
if err != nil {
t.Fatal(err)
}

if state != types.VirtualMachinePowerStatePoweredOff {
t.Errorf("state=%s", state)
}

// shutdown a poweroff vm should fail
err = vmm.ShutdownGuest(ctx)
if err == nil {
t.Error("expected error: InvalidPowerState")
}
if state != types.VirtualMachinePowerStatePoweredOff {
t.Errorf("state=%s", state)
}

// shutdown a poweroff vm should fail
err = vm.ShutdownGuest(ctx)
if err == nil {
t.Error("expected error: InvalidPowerState")
}
}
}

Expand Down
1 change: 1 addition & 0 deletions vcsim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
flag.IntVar(&model.Pod, "pod", model.Pod, "Number of storage pods per datacenter")
flag.IntVar(&model.Portgroup, "pg", model.Portgroup, "Number of port groups")
flag.IntVar(&model.Folder, "folder", model.Folder, "Number of folders")
flag.BoolVar(&model.Autostart, "autostart", model.Autostart, "Autostart model created VMs")

isESX := flag.Bool("esx", false, "Simulate standalone ESX")
isTLS := flag.Bool("tls", true, "Enable TLS")
Expand Down