From c3251fdd9a73f7af02805ac002b2f785b1adbd3d Mon Sep 17 00:00:00 2001 From: Brandon Palm Date: Wed, 31 Jul 2024 16:13:38 -0500 Subject: [PATCH] Add option to sanitize claim file result output (#2299) * Add option to sanitize claim file result output test of claim result * Make labelsEvaluator its own package --- cmd/certsuite/run/run.go | 2 ++ pkg/certsuite/certsuite.go | 8 +++++ pkg/checksdb/checksdb.go | 5 ++-- pkg/claimhelper/claimhelper.go | 39 +++++++++++++++++++++---- pkg/configuration/configuration.go | 1 + pkg/{checksdb => labels}/labels.go | 4 +-- pkg/{checksdb => labels}/labels_test.go | 4 +-- 7 files changed, 52 insertions(+), 11 deletions(-) rename pkg/{checksdb => labels}/labels.go (95%) rename pkg/{checksdb => labels}/labels_test.go (96%) diff --git a/cmd/certsuite/run/run.go b/cmd/certsuite/run/run.go index df48898d9..71e6fed2d 100644 --- a/cmd/certsuite/run/run.go +++ b/cmd/certsuite/run/run.go @@ -45,6 +45,7 @@ func NewCommand() *cobra.Command { runCmd.PersistentFlags().String("daemonset-cpu-lim", "100m", "CPU limit for the debug DaemonSet container") runCmd.PersistentFlags().String("daemonset-mem-req", "100M", "Memory request for the debug DaemonSet container") runCmd.PersistentFlags().String("daemonset-mem-lim", "100M", "Memory limit for the debug DaemonSet container") + runCmd.PersistentFlags().Bool("sanitize-claim", false, "Sanitize the claim.json file before sending it to the collector") return runCmd } @@ -73,6 +74,7 @@ func initTestParamsFromFlags(cmd *cobra.Command) error { testParams.DaemonsetCPULim, _ = cmd.Flags().GetString("daemonset-cpu-lim") testParams.DaemonsetMemReq, _ = cmd.Flags().GetString("daemonset-mem-req") testParams.DaemonsetMemLim, _ = cmd.Flags().GetString("daemonset-mem-lim") + testParams.SanitizeClaim, _ = cmd.Flags().GetBool("sanitize-claim") timeoutStr, _ := cmd.Flags().GetString("timeout") // Check if the output directory exists and, if not, create it diff --git a/pkg/certsuite/certsuite.go b/pkg/certsuite/certsuite.go index a0a09a9b8..ff468f0a8 100644 --- a/pkg/certsuite/certsuite.go +++ b/pkg/certsuite/certsuite.go @@ -167,11 +167,19 @@ func Run(labelsFilter, outputFolder string) error { claimBuilder.ToJUnitXML(junitOutputFileName, startTime, endTime) } + if configuration.GetTestParameters().SanitizeClaim { + claimOutputFile, err = claimhelper.SanitizeClaimFile(claimOutputFile, configuration.GetTestParameters().LabelsFilter) + if err != nil { + log.Error("Failed to sanitize claim file: %v", err) + } + } + // Send claim file to the collector if specified by env var if configuration.GetTestParameters().EnableDataCollection { if env.CollectorAppEndpoint == "" { env.CollectorAppEndpoint = collectorAppURL } + err = collector.SendClaimFileToCollector(env.CollectorAppEndpoint, claimOutputFile, env.ExecutedBy, env.PartnerName, env.CollectorAppPassword) if err != nil { log.Error("Failed to send post request to the collector: %v", err) diff --git a/pkg/checksdb/checksdb.go b/pkg/checksdb/checksdb.go index b7db83391..e390c8993 100644 --- a/pkg/checksdb/checksdb.go +++ b/pkg/checksdb/checksdb.go @@ -12,6 +12,7 @@ import ( "github.com/test-network-function/cnf-certification-test/internal/cli" "github.com/test-network-function/cnf-certification-test/internal/log" + "github.com/test-network-function/cnf-certification-test/pkg/labels" "github.com/test-network-function/cnf-certification-test/pkg/stringhelper" "github.com/test-network-function/cnf-certification-test/tests/identifiers" "github.com/test-network-function/test-network-function-claim/pkg/claim" @@ -23,7 +24,7 @@ var ( resultsDB = map[string]claim.Result{} - labelsExprEvaluator LabelsExprEvaluator + labelsExprEvaluator labels.LabelsExprEvaluator ) type AbortPanicMsg string @@ -252,7 +253,7 @@ func InitLabelsExprEvaluator(labelsFilter string) error { labelsFilter = strings.Join(allTags, ",") } - eval, err := newLabelsExprEvaluator(labelsFilter) + eval, err := labels.NewLabelsExprEvaluator(labelsFilter) if err != nil { return fmt.Errorf("could not create a label evaluator, err: %v", err) } diff --git a/pkg/claimhelper/claimhelper.go b/pkg/claimhelper/claimhelper.go index 9270e9baa..1a6d7e2d3 100644 --- a/pkg/claimhelper/claimhelper.go +++ b/pkg/claimhelper/claimhelper.go @@ -28,9 +28,11 @@ import ( "time" "github.com/test-network-function/cnf-certification-test/internal/log" + "github.com/test-network-function/cnf-certification-test/tests/identifiers" "github.com/test-network-function/cnf-certification-test/pkg/checksdb" "github.com/test-network-function/cnf-certification-test/pkg/diagnostics" + "github.com/test-network-function/cnf-certification-test/pkg/labels" "github.com/test-network-function/cnf-certification-test/pkg/provider" "github.com/test-network-function/cnf-certification-test/pkg/versions" "github.com/test-network-function/test-network-function-claim/pkg/claim" @@ -302,11 +304,7 @@ func ReadClaimFile(claimFileName string) (data []byte, err error) { if err != nil { log.Error("ReadFile failed with err: %v", err) } - path, err := os.Getwd() - if err != nil { - log.Error("Getwd failed with err: %v", err) - } - log.Info("Reading claim file at path: %s", path) + log.Info("Reading claim file at path: %s", claimFileName) return data, nil } @@ -340,6 +338,7 @@ func MarshalClaimOutput(claimRoot *claim.Root) []byte { // WriteClaimOutput writes the output payload to the claim file. In the event of an error, this method fatally fails. func WriteClaimOutput(claimOutputFile string, payload []byte) { + log.Info("Writing claim data to %s", claimOutputFile) err := os.WriteFile(claimOutputFile, payload, claimFilePermissions) if err != nil { log.Fatal("Error writing claim data:\n%s", string(payload)) @@ -374,3 +373,33 @@ func CreateClaimRoot() *claim.Root { }, } } + +func SanitizeClaimFile(claimFileName, labelsFilter string) (string, error) { + log.Info("Sanitizing claim file %s", claimFileName) + data, err := ReadClaimFile(claimFileName) + if err != nil { + log.Error("ReadClaimFile failed with err: %v", err) + return "", err + } + var aRoot claim.Root + UnmarshalClaim(data, &aRoot) + + // Remove the results that do not match the labels filter + for testID := range aRoot.Claim.Results { + evaluator, err := labels.NewLabelsExprEvaluator(labelsFilter) + if err != nil { + log.Error("Failed to create labels expression evaluator: %v", err) + return "", err + } + + _, gatheredLabels := identifiers.GetTestIDAndLabels(*aRoot.Claim.Results[testID].TestID) + + if !evaluator.Eval(gatheredLabels) { + log.Info("Removing test ID: %s from the claim", testID) + delete(aRoot.Claim.Results, testID) + } + } + + WriteClaimOutput(claimFileName, MarshalClaimOutput(&aRoot)) + return claimFileName, nil +} diff --git a/pkg/configuration/configuration.go b/pkg/configuration/configuration.go index aca1e6cc8..f2198a800 100644 --- a/pkg/configuration/configuration.go +++ b/pkg/configuration/configuration.go @@ -106,6 +106,7 @@ type TestParameters struct { DaemonsetCPULim string DaemonsetMemReq string DaemonsetMemLim string + SanitizeClaim bool TnfImageRepo string TnfDebugImage string NonIntrusiveOnly bool diff --git a/pkg/checksdb/labels.go b/pkg/labels/labels.go similarity index 95% rename from pkg/checksdb/labels.go rename to pkg/labels/labels.go index 8e50be581..2b53ae954 100644 --- a/pkg/checksdb/labels.go +++ b/pkg/labels/labels.go @@ -1,4 +1,4 @@ -package checksdb +package labels import ( "fmt" @@ -18,7 +18,7 @@ type labelsExprParser struct { astRootNode ast.Expr } -func newLabelsExprEvaluator(labelsExpr string) (LabelsExprEvaluator, error) { +func NewLabelsExprEvaluator(labelsExpr string) (LabelsExprEvaluator, error) { goLikeExpr := strings.ReplaceAll(labelsExpr, "-", "_") goLikeExpr = strings.ReplaceAll(goLikeExpr, ",", "||") diff --git a/pkg/checksdb/labels_test.go b/pkg/labels/labels_test.go similarity index 96% rename from pkg/checksdb/labels_test.go rename to pkg/labels/labels_test.go index 5795337a0..5cc2d3787 100644 --- a/pkg/checksdb/labels_test.go +++ b/pkg/labels/labels_test.go @@ -1,4 +1,4 @@ -package checksdb +package labels import ( "testing" @@ -124,7 +124,7 @@ func TestIsWordInExpr(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Get labels expr evaluator - labelsExprEvaluator, err := newLabelsExprEvaluator(tt.args.expr) + labelsExprEvaluator, err := NewLabelsExprEvaluator(tt.args.expr) assert.NotNil(t, labelsExprEvaluator) assert.Nil(t, err)