Skip to content

Commit

Permalink
flags: allow filtered checks listing (#1827)
Browse files Browse the repository at this point in the history
* flags: allow filtered checks listing

Enable the flag -L/--list to list the checks.
  • Loading branch information
jmontesi authored Jan 22, 2024
1 parent 7385bb5 commit 4510ef3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
10 changes: 10 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ func PrintResultsTable(results map[string][]int) {
fmt.Printf("\n")
}

func PrintChecksList(checkIDs []string) {
fmt.Println("------------------------------------------------------------")
fmt.Println("| TEST CASE SELECTION |")
fmt.Println("------------------------------------------------------------")
for _, checkID := range checkIDs {
fmt.Printf("| %-56s |\n", checkID)
}
fmt.Println("------------------------------------------------------------")
}

func stopCheckLineGoroutine() {
if stopChan == nil {
// This may happen for checks that were skipped if no compliant nor non-compliant objects found.
Expand Down
9 changes: 1 addition & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ func main() {
fmt.Printf("Log file: %s\n", log.LogFileName)
fmt.Printf("\n")

if *flags.ListFlag {
// ToDo: List all the available checks, filtered with --labels.

fmt.Fprint(os.Stderr, "Checks listing is not implemented yet\n")
os.Exit(1) //nolint:gocritic
}

// Set clientsholder singleton with the filenames from the env vars.
log.Info("Output folder for the claim file: %s", *flags.OutputDir)
if *flags.ServerModeFlag {
Expand All @@ -113,7 +106,7 @@ func main() {
err = certsuite.Run(*flags.LabelsFlag, *flags.OutputDir)
if err != nil {
log.Error("Failed to run CNF Certification Suite: %v", err)
os.Exit(1)
os.Exit(1) //nolint:gocritic // exitAfterDefer
}
}
}
12 changes: 12 additions & 0 deletions pkg/certsuite/certsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/test-network-function/cnf-certification-test/cnf-certification-test/platform"
"github.com/test-network-function/cnf-certification-test/cnf-certification-test/preflight"
"github.com/test-network-function/cnf-certification-test/cnf-certification-test/results"
"github.com/test-network-function/cnf-certification-test/internal/cli"
"github.com/test-network-function/cnf-certification-test/internal/clientsholder"
"github.com/test-network-function/cnf-certification-test/internal/log"
"github.com/test-network-function/cnf-certification-test/pkg/checksdb"
Expand Down Expand Up @@ -92,6 +93,17 @@ func Run(labelsFilter, outputFolder string) error {
timeout := processFlags()
var returnErr bool

// If the list flag is passed, print the checks filtered with --labels and leave
if *flags.ListFlag {
checksIDs, err := checksdb.FilterCheckIDs(labelsFilter)
if err != nil {
return fmt.Errorf("could not list test cases, err: %v", err)
}
cli.PrintChecksList(checksIDs)

os.Exit(1)
}

fmt.Println("Running discovery of CNF target resources...")
fmt.Print("\n")
var env provider.TestEnvironment
Expand Down
18 changes: 18 additions & 0 deletions pkg/checksdb/checksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,21 @@ func GetTestsCountByState(state string) int {
}
return count
}

func FilterCheckIDs(labelsFilter string) ([]string, error) {
filteredCheckIDs := []string{}
labelsExprEvaluator, err := NewLabelsExprEvaluator(labelsFilter)
if err != nil {
return nil, fmt.Errorf("invalid labels expression: %v", err)
}

for _, group := range dbByGroup {
for _, check := range group.checks {
if labelsExprEvaluator.Eval(check.Labels) {
filteredCheckIDs = append(filteredCheckIDs, check.ID)
}
}
}

return filteredCheckIDs, nil
}

0 comments on commit 4510ef3

Please sign in to comment.