Skip to content

Commit 30750a6

Browse files
committed
vcsim: Add CreateSnapshotEx for simulator
Signed-off-by: Sreyas Natarajan <[email protected]>
1 parent a534f39 commit 30750a6

File tree

3 files changed

+167
-5
lines changed

3 files changed

+167
-5
lines changed

object/virtual_machine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ func (v VirtualMachine) CreateSnapshot(ctx context.Context, name string, descrip
649649
}
650650

651651
// CreateSnapshotEx creates a new snapshot of a virtual machine.
652-
func (v VirtualMachine) CreateSnapshotEx(ctx context.Context, name string, description string, memory bool, quiesceSpec *types.VirtualMachineGuestQuiesceSpec) (*Task, error) {
652+
func (v VirtualMachine) CreateSnapshotEx(ctx context.Context, name string, description string, memory bool, quiesceSpec types.BaseVirtualMachineGuestQuiesceSpec) (*Task, error) {
653653
req := types.CreateSnapshotEx_Task{
654654
This: v.Reference(),
655655
Name: name,

simulator/virtual_machine.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,7 +2687,33 @@ func (vm *VirtualMachine) CustomizeVMTask(ctx *Context, req *types.CustomizeVM_T
26872687
}
26882688

26892689
func (vm *VirtualMachine) CreateSnapshotTask(ctx *Context, req *types.CreateSnapshot_Task) soap.HasFault {
2690-
task := CreateTask(vm, "createSnapshot", func(t *Task) (types.AnyType, types.BaseMethodFault) {
2690+
body := &methods.CreateSnapshot_TaskBody{}
2691+
2692+
r := &types.CreateSnapshotEx_Task{
2693+
Name: req.Name,
2694+
Description: req.Description,
2695+
Memory: req.Memory,
2696+
}
2697+
2698+
if req.Quiesce {
2699+
r.QuiesceSpec = &types.VirtualMachineGuestQuiesceSpec{}
2700+
}
2701+
2702+
res := vm.CreateSnapshotExTask(ctx, r)
2703+
2704+
if res.Fault() != nil {
2705+
body.Fault_ = res.Fault()
2706+
} else {
2707+
body.Res = &types.CreateSnapshot_TaskResponse{
2708+
Returnval: res.(*methods.CreateSnapshotEx_TaskBody).Res.Returnval,
2709+
}
2710+
}
2711+
2712+
return body
2713+
}
2714+
2715+
func (vm *VirtualMachine) CreateSnapshotExTask(ctx *Context, req *types.CreateSnapshotEx_Task) soap.HasFault {
2716+
task := CreateTask(vm, "createSnapshotEx", func(t *Task) (types.AnyType, types.BaseMethodFault) {
26912717
var changes []types.PropertyChange
26922718

26932719
if vm.Snapshot == nil {
@@ -2701,6 +2727,11 @@ func (vm *VirtualMachine) CreateSnapshotTask(ctx *Context, req *types.CreateSnap
27012727

27022728
ctx.Map.Put(snapshot)
27032729

2730+
quiesced := false
2731+
if req.QuiesceSpec != nil {
2732+
quiesced = true
2733+
}
2734+
27042735
treeItem := types.VirtualMachineSnapshotTree{
27052736
Snapshot: snapshot.Self,
27062737
Vm: snapshot.Vm,
@@ -2709,7 +2740,7 @@ func (vm *VirtualMachine) CreateSnapshotTask(ctx *Context, req *types.CreateSnap
27092740
Id: atomic.AddInt32(&vm.sid, 1),
27102741
CreateTime: time.Now(),
27112742
State: vm.Runtime.PowerState,
2712-
Quiesced: req.Quiesce,
2743+
Quiesced: quiesced,
27132744
BackupManifest: "",
27142745
ReplaySupported: types.NewBool(false),
27152746
}
@@ -2740,8 +2771,8 @@ func (vm *VirtualMachine) CreateSnapshotTask(ctx *Context, req *types.CreateSnap
27402771
return snapshot.Self, nil
27412772
})
27422773

2743-
return &methods.CreateSnapshot_TaskBody{
2744-
Res: &types.CreateSnapshot_TaskResponse{
2774+
return &methods.CreateSnapshotEx_TaskBody{
2775+
Res: &types.CreateSnapshotEx_TaskResponse{
27452776
Returnval: task.Run(ctx),
27462777
},
27472778
}

simulator/virtual_machine_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,137 @@ func TestVmSnapshot(t *testing.T) {
18201820
}
18211821
}
18221822

1823+
func TestVmSnapshotEx(t *testing.T) {
1824+
esx := ESX()
1825+
1826+
Test(func(ctx context.Context, c *vim25.Client) {
1827+
simVm := esx.Map().Any("VirtualMachine")
1828+
vm := object.NewVirtualMachine(c, simVm.Reference())
1829+
1830+
_, err := fieldValue(reflect.ValueOf(simVm), "snapshot")
1831+
if err != errEmptyField {
1832+
t.Fatal("snapshot property should be 'nil' if there are no snapshots")
1833+
}
1834+
1835+
quiesceSpec := &types.VirtualMachineGuestQuiesceSpec{
1836+
Timeout: 100,
1837+
}
1838+
1839+
task, err := vm.CreateSnapshotEx(ctx, "root", "description", true, quiesceSpec)
1840+
if err != nil {
1841+
t.Fatal(err)
1842+
}
1843+
1844+
info, err := task.WaitForResult(ctx)
1845+
if err != nil {
1846+
t.Fatal(err)
1847+
}
1848+
1849+
snapRef, ok := info.Result.(types.ManagedObjectReference)
1850+
if !ok {
1851+
t.Fatal("expected ManagedObjectRefrence result for CreateSnapshot")
1852+
}
1853+
1854+
_, err = vm.FindSnapshot(ctx, snapRef.Value)
1855+
if err != nil {
1856+
t.Fatal(err, "snapshot should be found by result reference")
1857+
}
1858+
1859+
_, err = fieldValue(reflect.ValueOf(simVm), "snapshot")
1860+
if err == errEmptyField {
1861+
t.Fatal("snapshot property should not be 'nil' if there are snapshots")
1862+
}
1863+
// NOTE: fieldValue cannot be used for nil check
1864+
if len(simVm.(*VirtualMachine).RootSnapshot) == 0 {
1865+
t.Fatal("rootSnapshot property should have elements if there are snapshots")
1866+
}
1867+
1868+
task, err = vm.CreateSnapshotEx(ctx, "child", "description", true, quiesceSpec)
1869+
if err != nil {
1870+
t.Fatal(err)
1871+
}
1872+
1873+
err = task.Wait(ctx)
1874+
if err != nil {
1875+
t.Fatal(err)
1876+
}
1877+
1878+
_, err = vm.FindSnapshot(ctx, "child")
1879+
if err != nil {
1880+
t.Fatal(err)
1881+
}
1882+
1883+
task, err = vm.RevertToCurrentSnapshot(ctx, true)
1884+
if err != nil {
1885+
t.Fatal(err)
1886+
}
1887+
1888+
err = task.Wait(ctx)
1889+
if err != nil {
1890+
t.Fatal(err)
1891+
}
1892+
1893+
task, err = vm.RevertToSnapshot(ctx, "root", true)
1894+
if err != nil {
1895+
t.Fatal(err)
1896+
}
1897+
1898+
err = task.Wait(ctx)
1899+
if err != nil {
1900+
t.Fatal(err)
1901+
}
1902+
1903+
task, err = vm.RemoveSnapshot(ctx, "child", false, nil)
1904+
if err != nil {
1905+
t.Fatal(err)
1906+
}
1907+
1908+
err = task.Wait(ctx)
1909+
if err != nil {
1910+
t.Fatal(err)
1911+
}
1912+
1913+
_, err = fieldValue(reflect.ValueOf(simVm), "snapshot")
1914+
if err == errEmptyField {
1915+
t.Fatal("snapshot property should not be 'nil' if there are snapshots")
1916+
}
1917+
// NOTE: fieldValue cannot be used for nil check
1918+
if len(simVm.(*VirtualMachine).RootSnapshot) == 0 {
1919+
t.Fatal("rootSnapshot property should have elements if there are snapshots")
1920+
}
1921+
1922+
_, err = vm.FindSnapshot(ctx, "child")
1923+
if err == nil {
1924+
t.Fatal("child should be removed")
1925+
}
1926+
1927+
task, err = vm.RemoveAllSnapshot(ctx, nil)
1928+
if err != nil {
1929+
t.Fatal(err)
1930+
}
1931+
1932+
err = task.Wait(ctx)
1933+
if err != nil {
1934+
t.Fatal(err)
1935+
}
1936+
1937+
_, err = fieldValue(reflect.ValueOf(simVm), "snapshot")
1938+
if err != errEmptyField {
1939+
t.Fatal("snapshot property should be 'nil' if there are no snapshots")
1940+
}
1941+
// NOTE: fieldValue cannot be used for nil check
1942+
if len(simVm.(*VirtualMachine).RootSnapshot) != 0 {
1943+
t.Fatal("rootSnapshot property should not have elements if there are no snapshots")
1944+
}
1945+
1946+
_, err = vm.FindSnapshot(ctx, "root")
1947+
if err == nil {
1948+
t.Fatal("all snapshots should be removed")
1949+
}
1950+
1951+
}, esx)
1952+
}
1953+
18231954
func TestVmMarkAsTemplate(t *testing.T) {
18241955
ctx := context.Background()
18251956

0 commit comments

Comments
 (0)