Skip to content

Commit

Permalink
api: Sim vm config.changeVersion & config.modified
Browse files Browse the repository at this point in the history
This patch updates the simulator to update a VM's config.changeVersion
and config.modified if the VM is reconfigured.
  • Loading branch information
akutz committed Feb 5, 2024
1 parent 1b5ec02 commit 8e17e4c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
26 changes: 24 additions & 2 deletions simulator/virtual_machine.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2017-2023 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -545,7 +545,13 @@ func validateGuestID(id string) types.BaseMethodFault {
return &types.InvalidArgument{InvalidProperty: "configSpec.guestId"}
}

func (vm *VirtualMachine) configure(ctx *Context, spec *types.VirtualMachineConfigSpec) types.BaseMethodFault {
func (vm *VirtualMachine) configure(ctx *Context, spec *types.VirtualMachineConfigSpec) (result types.BaseMethodFault) {
defer func() {
if result == nil {
vm.updateLastModifiedAndChangeVersion(ctx)
}
}()

vm.apply(spec)

if spec.MemoryAllocation != nil {
Expand Down Expand Up @@ -2638,3 +2644,19 @@ func changeTrackingSupported(spec *types.VirtualMachineConfigSpec) bool {
}
return false
}

func (vm *VirtualMachine) updateLastModifiedAndChangeVersion(ctx *Context) {
modified := time.Now()
ctx.Map.Update(vm, []types.PropertyChange{
{
Name: "config.changeVersion",
Val: fmt.Sprintf("%d", modified.UnixNano()),
Op: types.PropertyChangeOpAssign,
},
{
Name: "config.modified",
Val: modified,
Op: types.PropertyChangeOpAssign,
},
})
}
57 changes: 55 additions & 2 deletions simulator/virtual_machine_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -2183,3 +2183,56 @@ func TestApplyExtraConfig(t *testing.T) {
applyAndAssertExtraConfigValue(ctx, vm, "", true)
})
}

func TestLastModifiedAndChangeVersionAreUpdated(t *testing.T) {
Test(func(ctx context.Context, c *vim25.Client) {
vm := object.NewVirtualMachine(c, Map.Any("VirtualMachine").Reference())
var vmMo mo.VirtualMachine
if err := vm.Properties(
ctx,
vm.Reference(),
[]string{"config.modified", "config.changeVersion"},
&vmMo); err != nil {

t.Fatalf("failed to fetch initial vm props: %v", err)
}

oldModified := vmMo.Config.Modified
oldChangeVersion := vmMo.Config.ChangeVersion

tsk, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{
ExtraConfig: []types.BaseOptionValue{
&types.OptionValue{
Key: "hello",
Value: "world",
},
},
})
if err != nil {
t.Fatalf("failed to call reconfigure api: %v", err)
}
if err := tsk.WaitEx(ctx); err != nil {
t.Fatalf("failed to reconfigure: %v", err)
}

if err := vm.Properties(
ctx,
vm.Reference(),
[]string{"config.modified", "config.changeVersion"},
&vmMo); err != nil {

t.Fatalf("failed to fetch vm props after reconfigure: %v", err)
}

newModified := vmMo.Config.Modified
newChangeVersion := vmMo.Config.ChangeVersion

if a, e := newModified, oldModified; a == e {
t.Errorf("config.modified was not updated: %v", a)
}

if a, e := newChangeVersion, oldChangeVersion; a == e {
t.Errorf("config.changeVersion was not updated: %v", a)
}
})
}

0 comments on commit 8e17e4c

Please sign in to comment.