Skip to content

Commit

Permalink
Add option to sanitize claim file result output (#2299)
Browse files Browse the repository at this point in the history
* Add option to sanitize claim file result output

test of claim result

* Make labelsEvaluator its own package
  • Loading branch information
sebrandon1 authored Jul 31, 2024
1 parent afb0f20 commit c3251fd
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cmd/certsuite/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/certsuite/certsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/checksdb/checksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,7 +24,7 @@ var (

resultsDB = map[string]claim.Result{}

labelsExprEvaluator LabelsExprEvaluator
labelsExprEvaluator labels.LabelsExprEvaluator
)

type AbortPanicMsg string
Expand Down Expand Up @@ -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)
}
Expand Down
39 changes: 34 additions & 5 deletions pkg/claimhelper/claimhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type TestParameters struct {
DaemonsetCPULim string
DaemonsetMemReq string
DaemonsetMemLim string
SanitizeClaim bool
TnfImageRepo string
TnfDebugImage string
NonIntrusiveOnly bool
Expand Down
4 changes: 2 additions & 2 deletions pkg/checksdb/labels.go → pkg/labels/labels.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checksdb
package labels

import (
"fmt"
Expand All @@ -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, ",", "||")

Expand Down
4 changes: 2 additions & 2 deletions pkg/checksdb/labels_test.go → pkg/labels/labels_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package checksdb
package labels

import (
"testing"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit c3251fd

Please sign in to comment.