-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds new yamlCompare and jsonCompare analyzers (#598)
* feat: adds new yamlCompare analyzer * feat: adds new jsonCompare analyzer * outcome when for yamlCompare and jsonCompare
- Loading branch information
Craig O'Donnell
authored
Jun 17, 2022
1 parent
1de5a79
commit 354a996
Showing
8 changed files
with
1,415 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package analyzer | ||
|
||
import ( | ||
"encoding/json" | ||
"path/filepath" | ||
"reflect" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" | ||
iutils "github.com/replicatedhq/troubleshoot/pkg/interfaceutils" | ||
) | ||
|
||
func analyzeJsonCompare(analyzer *troubleshootv1beta2.JsonCompare, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { | ||
fullPath := filepath.Join(analyzer.CollectorName, analyzer.FileName) | ||
collected, err := getCollectedFileContents(fullPath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to read collected file name: %s", fullPath) | ||
} | ||
|
||
var actual interface{} | ||
err = json.Unmarshal(collected, &actual) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to parse collected data as json") | ||
} | ||
|
||
if analyzer.Path != "" { | ||
actual, err = iutils.GetAtPath(actual, analyzer.Path) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to get object at path: %s", analyzer.Path) | ||
} | ||
} | ||
|
||
var expected interface{} | ||
err = json.Unmarshal([]byte(analyzer.Value), &expected) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to parse expected value as json") | ||
} | ||
|
||
title := analyzer.CheckName | ||
if title == "" { | ||
title = analyzer.CollectorName | ||
} | ||
|
||
result := &AnalyzeResult{ | ||
Title: title, | ||
IconKey: "kubernetes_text_analyze", | ||
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", | ||
} | ||
|
||
equal := reflect.DeepEqual(actual, expected) | ||
|
||
for _, outcome := range analyzer.Outcomes { | ||
if outcome.Fail != nil { | ||
when := false | ||
if outcome.Fail.When != "" { | ||
when, err = strconv.ParseBool(outcome.Fail.When) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Fail.When) | ||
} | ||
} | ||
|
||
if when == equal { | ||
result.IsFail = true | ||
result.Message = outcome.Fail.Message | ||
result.URI = outcome.Fail.URI | ||
|
||
return result, nil | ||
} | ||
} else if outcome.Warn != nil { | ||
when := false | ||
if outcome.Warn.When != "" { | ||
when, err = strconv.ParseBool(outcome.Warn.When) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Warn.When) | ||
} | ||
} | ||
|
||
if when == equal { | ||
result.IsWarn = true | ||
result.Message = outcome.Warn.Message | ||
result.URI = outcome.Warn.URI | ||
|
||
return result, nil | ||
} | ||
} else if outcome.Pass != nil { | ||
when := true // default to passing when values are equal | ||
if outcome.Pass.When != "" { | ||
when, err = strconv.ParseBool(outcome.Pass.When) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to process when statement: %s", outcome.Pass.When) | ||
} | ||
} | ||
|
||
if when == equal { | ||
result.IsPass = true | ||
result.Message = outcome.Pass.Message | ||
result.URI = outcome.Pass.URI | ||
|
||
return result, nil | ||
} | ||
} | ||
} | ||
|
||
return &AnalyzeResult{ | ||
Title: title, | ||
IconKey: "kubernetes_text_analyze", | ||
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg", | ||
IsFail: true, | ||
Message: "Invalid analyzer", | ||
}, nil | ||
} |
Oops, something went wrong.