Add Equal func for Status struct

This commit is contained in:
drfish
2021-01-27 22:29:20 +08:00
parent 1119a505ac
commit d15dacb57d
3 changed files with 101 additions and 0 deletions

View File

@@ -25,6 +25,8 @@ import (
"strings"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/informers"
@@ -180,6 +182,21 @@ func (s *Status) AsError() error {
return errors.New(s.Message())
}
// Equal checks equality of two statuses. This is useful for testing with
// cmp.Equal.
func (s *Status) Equal(x *Status) bool {
if s == nil || x == nil {
return s.IsSuccess() && x.IsSuccess()
}
if s.code != x.code {
return false
}
if s.code == Error {
return cmp.Equal(s.err, x.err, cmpopts.EquateErrors())
}
return cmp.Equal(s.reasons, x.reasons)
}
// NewStatus makes a Status out of the given arguments and returns its pointer.
func NewStatus(code Code, reasons ...string) *Status {
s := &Status{