Skip to content

Commit bc0c8a0

Browse files
authored
Merge pull request #3544 from akutz/feature/fault
api: Fault helpers
2 parents 0a94649 + 476704a commit bc0c8a0

File tree

6 files changed

+1579
-58
lines changed

6 files changed

+1579
-58
lines changed

eam/simulator/simulator_test.go

+4-58
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/vmware/govmomi/eam"
3030
"github.com/vmware/govmomi/eam/object"
3131
"github.com/vmware/govmomi/eam/types"
32+
"github.com/vmware/govmomi/fault"
3233
"github.com/vmware/govmomi/find"
3334
vimobject "github.com/vmware/govmomi/object"
3435
"github.com/vmware/govmomi/session"
@@ -348,53 +349,6 @@ func TestSimulator(t *testing.T) {
348349
func TestNotAuthenticated(t *testing.T) {
349350
vcsim.Test(func(ctx context.Context, vimClient *vim25.Client) {
350351

351-
isNotAuthenticatedFault := func(fault vim.AnyType) bool {
352-
switch fault.(type) {
353-
case vim.NotAuthenticated, *vim.NotAuthenticated:
354-
return true
355-
default:
356-
return false
357-
}
358-
}
359-
360-
isEamInvalidLoginFault := func(fault vim.AnyType) bool {
361-
switch fault.(type) {
362-
case types.EamInvalidLogin, *types.EamInvalidLogin:
363-
return true
364-
default:
365-
return false
366-
}
367-
}
368-
369-
validateFault := func(
370-
err error,
371-
faultType string,
372-
faultIsTypeFn func(vim.AnyType) bool) error {
373-
374-
if err == nil {
375-
return fmt.Errorf("%s expected", faultType)
376-
}
377-
378-
validateVimFault := func(vimFault vim.AnyType) error {
379-
if !faultIsTypeFn(vimFault) {
380-
return fmt.Errorf(
381-
"err is not %[1]s: %[2]T %[2]v", faultType, vimFault)
382-
}
383-
return nil
384-
}
385-
386-
if soap.IsSoapFault(err) {
387-
return validateVimFault(soap.ToSoapFault(err).VimFault())
388-
}
389-
390-
if soap.IsVimFault(err) {
391-
return validateVimFault(soap.ToVimFault(err))
392-
}
393-
394-
return fmt.Errorf(
395-
"err is not a Soap or Vim fault: %[1]T %[1]v", err)
396-
}
397-
398352
t.Run("TerminateSession", func(t *testing.T) {
399353
// Terminate the session.
400354
sessionManager := session.NewManager(vimClient)
@@ -415,11 +369,7 @@ func TestNotAuthenticated(t *testing.T) {
415369
// error.
416370
_, err := finder.DefaultDatacenter(ctx)
417371

418-
if err := validateFault(
419-
err,
420-
"NotAuthenticated",
421-
isNotAuthenticatedFault); err != nil {
422-
372+
if !fault.Is(err, &vim.NotAuthenticated{}) {
423373
t.Fatal(err)
424374
}
425375
})
@@ -436,12 +386,8 @@ func TestNotAuthenticated(t *testing.T) {
436386
// Try to list the agencies, but receive an EamInvalidLogin error.
437387
_, err := mgr.Agencies(ctx)
438388

439-
if err := validateFault(
440-
err,
441-
"EamInvalidLogin",
442-
isEamInvalidLoginFault); err != nil {
443-
444-
t.Fatal(err)
389+
if !fault.Is(err, &types.EamInvalidLogin{}) {
390+
t.Fatalf("err=%[1]T %+[1]v", err)
445391
}
446392
})
447393
})

0 commit comments

Comments
 (0)