From 6d1e6a353eff7650e3038f20351d1650fc8580d4 Mon Sep 17 00:00:00 2001 From: Leandro Poroli Date: Tue, 2 Jan 2024 13:44:13 -0300 Subject: [PATCH] checking if failures for package exist in map --- catalog-validator/main.go | 5 ++++- .../validation/rules/invalid_rule_result.go | 15 +++++++++++---- .../validation/validator/validator.go | 11 ++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/catalog-validator/main.go b/catalog-validator/main.go index 49249ca..d4375ce 100644 --- a/catalog-validator/main.go +++ b/catalog-validator/main.go @@ -48,7 +48,10 @@ func main() { exitFailure(err) } validatorObj := validator.NewValidator(packageCatalog, rulesToValidate) - validatorResult := validatorObj.Validate(ctx) + validatorResult, err := validatorObj.Validate(ctx) + if err != nil { + exitFailure(err) + } if !validatorResult.IsValidCatalog() { logrus.Errorf("THE VALIDATOR REPORT FAILURES IN THE FOLLOWING RULES") diff --git a/catalog-validator/validation/rules/invalid_rule_result.go b/catalog-validator/validation/rules/invalid_rule_result.go index 59c42c3..832cd77 100644 --- a/catalog-validator/validation/rules/invalid_rule_result.go +++ b/catalog-validator/validation/rules/invalid_rule_result.go @@ -1,10 +1,13 @@ package rules -import "github.com/kurtosis-tech/kurtosis-package-indexer/server/types" +import ( + "github.com/kurtosis-tech/kurtosis-package-indexer/server/types" + "github.com/kurtosis-tech/stacktrace" +) type CheckResult struct { ruleName RuleName - wasValidated bool //TODO look for a better name + wasValidated bool failures map[types.PackageName][]string } @@ -24,6 +27,10 @@ func (ruleReport *CheckResult) GetFailures() map[types.PackageName][]string { return ruleReport.failures } -func (ruleReport *CheckResult) GetFailuresForPackage(packageName types.PackageName) []string { - return ruleReport.failures[packageName] // TODO check if found +func (ruleReport *CheckResult) GetFailuresForPackage(packageName types.PackageName) ([]string, error) { + failures, found := ruleReport.failures[packageName] + if !found { + return nil, stacktrace.NewError("Expected to find failures for package '%s' but nothing was found, this is a bug in the catalog") + } + return failures, nil } diff --git a/catalog-validator/validation/validator/validator.go b/catalog-validator/validation/validator/validator.go index dddee48..e61df6d 100644 --- a/catalog-validator/validation/validator/validator.go +++ b/catalog-validator/validation/validator/validator.go @@ -5,6 +5,7 @@ import ( "github.com/kurtosis-tech/kurtosis-package-catalog/catalog-validator/validation/rules" "github.com/kurtosis-tech/kurtosis-package-indexer/server/catalog" "github.com/kurtosis-tech/kurtosis-package-indexer/server/types" + "github.com/kurtosis-tech/stacktrace" "github.com/sirupsen/logrus" ) @@ -17,7 +18,7 @@ func NewValidator(catalog catalog.PackageCatalog, rules []rules.Rule) *Validator return &Validator{catalog: catalog, rules: rules} } -func (validator *Validator) Validate(ctx context.Context) *result { +func (validator *Validator) Validate(ctx context.Context) (*result, error) { isValidCatalog := true rulesResult := map[rules.RuleName]map[types.PackageName][]string{} @@ -35,7 +36,11 @@ func (validator *Validator) Validate(ctx context.Context) *result { ruleName := checkResult.GetRuleName() failuresByPackageForRule, found := rulesResult[ruleName] if found { - failuresByPackageForRule[packageName] = checkResult.GetFailuresForPackage(packageName) + packageFailures, err := checkResult.GetFailuresForPackage(packageName) + if err != nil { + return nil, stacktrace.Propagate(err, "an error occurred getting failures for package '%s'", packageName) + } + failuresByPackageForRule[packageName] = packageFailures } else { rulesResult[ruleName] = checkResult.GetFailures() } @@ -48,5 +53,5 @@ func (validator *Validator) Validate(ctx context.Context) *result { resultObj := newResult(isValidCatalog, rulesResult) - return resultObj + return resultObj, nil }