blob: 717521157861293ac84816020cc5e77da51a8a53 [file] [log] [blame]
package validators
import (
"reflect"
"github.com/lrills/helm-unittest/unittest/common"
"github.com/lrills/helm-unittest/unittest/valueutils"
)
// EqualValidator validate whether the value of Path equal to Value
type EqualValidator struct {
Path string
Value interface{}
}
func (a EqualValidator) failInfo(actual interface{}, index int, not bool) []string {
var notAnnotation string
if not {
notAnnotation = " NOT to equal"
}
failFormat := `
Path:%s
Expected` + notAnnotation + `:
%s`
expectedYAML := common.TrustedMarshalYAML(a.Value)
if not {
return splitInfof(failFormat, index, a.Path, expectedYAML)
}
actualYAML := common.TrustedMarshalYAML(actual)
return splitInfof(
failFormat+`
Actual:
%s
Diff:
%s
`,
index,
a.Path,
expectedYAML,
actualYAML,
diff(expectedYAML, actualYAML),
)
}
// Validate implement Validatable
func (a EqualValidator) Validate(context *ValidateContext) (bool, []string) {
manifests, err := context.getManifests()
if err != nil {
return false, splitInfof(errorFormat, -1, err.Error())
}
validateSuccess := true
validateErrors := make([]string, 0)
for idx, manifest := range manifests {
actual, err := valueutils.GetValueOfSetPath(manifest, a.Path)
if err != nil {
validateSuccess = false
errorMessage := splitInfof(errorFormat, idx, err.Error())
validateErrors = append(validateErrors, errorMessage...)
continue
}
if reflect.DeepEqual(a.Value, actual) == context.Negative {
validateSuccess = false
errorMessage := a.failInfo(actual, idx, context.Negative)
validateErrors = append(validateErrors, errorMessage...)
continue
}
validateSuccess = determineSuccess(validateSuccess, true)
}
return validateSuccess, validateErrors
}