Skip to content

Commit f12361c

Browse files
authored
Add tests for types of meta fault (#3824)
This adds compound tests for whether a fault is a transient error, or whether it's an error due to an attempt to power off an already powered off VM. The former is widely reimplemented by clients, and often missing some types. The latter is extremely useful to have when mapping desired state to imperative. Signed-off-by: George Hicken <[email protected]>
1 parent 7111a7f commit f12361c

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

fault/meta_types.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package fault
6+
7+
import "github.com/vmware/govmomi/vim25/types"
8+
9+
// IsAlreadyPoweredOffError verifies that the error is an InvalidPowerState
10+
// error and returns true if the existing state from the error is powered off
11+
func IsAlreadyPoweredOffError(err any) bool {
12+
var existingState types.VirtualMachinePowerState
13+
14+
In(err, func(
15+
fault types.BaseMethodFault,
16+
localizedMessage string,
17+
localizableMessages []types.LocalizableMessage) bool {
18+
if invalidPowerState, ok := fault.(*types.InvalidPowerState); ok {
19+
existingState = invalidPowerState.ExistingState
20+
21+
return true
22+
}
23+
24+
return false
25+
})
26+
27+
return existingState == types.VirtualMachinePowerStatePoweredOff
28+
}
29+
30+
// IsTransientError checks whether the error type indicates an error that is
31+
// likely to resolve without explicit action in calling logic.
32+
// Some of those are highly transient, such as TaskInProgress. Others are
33+
// potentially longer term, such as HostCommunication; they are inherently
34+
// transient but may not resolve in a short time frame.
35+
func IsTransientError(err any) bool {
36+
return Is(err, &types.TaskInProgress{}) ||
37+
Is(err, &types.NetworkDisruptedAndConfigRolledBack{}) ||
38+
Is(err, &types.VAppTaskInProgress{}) ||
39+
Is(err, &types.FailToLockFaultToleranceVMs{}) ||
40+
Is(err, &types.HostCommunication{})
41+
}

fault/meta_types_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package fault_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/vmware/govmomi/fault"
13+
"github.com/vmware/govmomi/task"
14+
"github.com/vmware/govmomi/vim25/types"
15+
)
16+
17+
func TestAlreadyPoweredOff(t *testing.T) {
18+
var (
19+
errFalse any
20+
errTrue any
21+
)
22+
23+
errFalse = task.Error{
24+
LocalizedMethodFault: &types.LocalizedMethodFault{
25+
Fault: &types.InvalidPowerState{
26+
ExistingState: types.VirtualMachinePowerStateSuspended,
27+
RequestedState: types.VirtualMachinePowerStatePoweredOff,
28+
},
29+
LocalizedMessage: "vm must be powered on to power off",
30+
},
31+
}
32+
33+
errTrue = task.Error{
34+
LocalizedMethodFault: &types.LocalizedMethodFault{
35+
Fault: &types.InvalidPowerState{
36+
ExistingState: types.VirtualMachinePowerStatePoweredOff,
37+
RequestedState: types.VirtualMachinePowerStatePoweredOff,
38+
},
39+
LocalizedMessage: "vm must be powered on to power off",
40+
},
41+
}
42+
43+
assert.False(t, fault.IsAlreadyPoweredOffError(errFalse))
44+
assert.True(t, fault.IsAlreadyPoweredOffError(errTrue))
45+
}

0 commit comments

Comments
 (0)