From 8e17e4c1a73b460f73535a275468dcb9d319e640 Mon Sep 17 00:00:00 2001 From: akutz Date: Mon, 5 Feb 2024 14:38:02 -0600 Subject: [PATCH] api: Sim vm config.changeVersion & config.modified This patch updates the simulator to update a VM's config.changeVersion and config.modified if the VM is reconfigured. --- simulator/virtual_machine.go | 26 ++++++++++++-- simulator/virtual_machine_test.go | 57 +++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 4 deletions(-) diff --git a/simulator/virtual_machine.go b/simulator/virtual_machine.go index 98af0865c..c619fa207 100644 --- a/simulator/virtual_machine.go +++ b/simulator/virtual_machine.go @@ -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. @@ -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 { @@ -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, + }, + }) +} diff --git a/simulator/virtual_machine_test.go b/simulator/virtual_machine_test.go index d6b868965..43b782d2c 100644 --- a/simulator/virtual_machine_test.go +++ b/simulator/virtual_machine_test.go @@ -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, @@ -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) + } + }) +}