Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: display information about the CNFCERT run #1690

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cli

import (
"fmt"
)

const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Purple = "\033[35m"
Cyan = "\033[36m"
Gray = "\033[37m"
White = "\033[97m"
)

// ASCII art generated on http://www.patorjk.com/software/taag/ with
// the font DOOM by Frans P. de Vries <[email protected]> 18 Jun 1996.
// All backticks (`) were removed for string literal compatibility.
const banner = `
jmontesi marked this conversation as resolved.
Show resolved Hide resolved

_____ _ _ ______ _____ _____ ______ _____ __ _____ _____ __
/ __ \| \ | || ___|/ __ \| ___|| ___ \|_ _| / / | ___| | _ |\ \
| / \/| \| || |_ | / \/| |__ | |_/ / | | | | __ __|___ \ | |/' | | |
| | | . || _| | | | __| | / | | | | \ \ / / \ \ | /| | | |
| \__/\| |\ || | | \__/\| |___ | |\ \ | | | | \ V / /\__/ / _ \ |_/ / | |
\____/\_| \_/\_| \____/\____/ \_| \_| \_/ | | \_/ \____/ (_) \___/ | |
\_\ /_/


`

func PrintBanner() {
fmt.Print(banner)
}

func PrintResultsTable(results map[string][]int) {
fmt.Printf("\n")
fmt.Println("-----------------------------------------------------------")
fmt.Printf("| %-27s %-9s %-9s %s |\n", "SUITE", "PASSED", "FAILED", "SKIPPED")
fmt.Println("-----------------------------------------------------------")
for groupName, groupResults := range results {
fmt.Printf("| %-25s %8d %9d %10d |\n", groupName,
groupResults[0],
groupResults[1],
groupResults[2])
fmt.Println("-----------------------------------------------------------")
}
fmt.Printf("\n")
}
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/test-network-function/cnf-certification-test/cnf-certification-test/webserver"

"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/configuration"
Expand Down Expand Up @@ -106,7 +107,6 @@ func setLogLevel() {
logLevel = logrus.DebugLevel
}

logrus.Info("Log level set to: ", logLevel)
logrus.SetLevel(logLevel)
}

Expand Down Expand Up @@ -144,14 +144,23 @@ func main() {
loghelper.SetLogFormat()
setLogLevel()

logrusLogFile, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE, logFilePermissions)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create log file, err: %v", err)
os.Exit(1)
}
defer logrusLogFile.Close()

logrus.SetOutput(logrusLogFile)

// Set up logger
err = os.Remove(logFileName)
err = os.Remove("test_log") // TODO: use proper file when logrus is removed
if err != nil && !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "could not delete old log file, err: %v", err)
os.Exit(1)
os.Exit(1) //nolint:gocritic // the error will not happen after logrus is removed
}

logFile, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE, logFilePermissions)
logFile, err := os.OpenFile("test_log", os.O_RDWR|os.O_CREATE, logFilePermissions)
if err != nil {
fmt.Fprintf(os.Stderr, "could not create log file, err: %v", err)
os.Exit(1)
Expand All @@ -165,14 +174,23 @@ func main() {
logrus.Infof("Claim Format Version: %s", versions.ClaimFormatVersion)
logrus.Infof("Labels filter : %v", *labelsFlag)

cli.PrintBanner()

fmt.Printf("CNFCERT version: %s\n", versions.GitVersion())
fmt.Printf("Claim file version: %s\n", versions.ClaimFormatVersion)
fmt.Printf("Checks filter: %s\n", *labelsFlag)
fmt.Printf("Output folder: %s\n", *claimPath)
fmt.Printf("Log file: %s\n", logFileName)
fmt.Printf("\n")

_ = clientsholder.GetClientsHolder(getK8sClientsConfigFileNames()...)

certsuite.LoadChecksDB(*labelsFlag)

if *listFlag {
// ToDo: List all the available checks, filtered with --labels.
logrus.Errorf("Not implemented yet.")
os.Exit(1) //nolint:gocritic
os.Exit(1)
}

// Diagnostic functions will run when no labels are provided.
Expand Down
24 changes: 24 additions & 0 deletions pkg/checksdb/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/sirupsen/logrus"
"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/testhelper"
)
Expand All @@ -18,6 +19,10 @@ const (
CheckResultFailed = "failed"
CheckResultError = "error"
CheckResultAborted = "aborted"

CheckResultTagPass = "PASS"
CheckResultTagSkip = "SKIP"
CheckResultTagFail = "FAIL"
)

type skipMode int
Expand Down Expand Up @@ -246,6 +251,8 @@ func (check *Check) Run() error {
return fmt.Errorf("unable to run due to a previously existing error: %v", check.Error)
}

fmt.Printf("[ "+cli.Cyan+"RUNNING"+cli.Reset+" ] %s", check.ID)

check.StartTime = time.Now()
defer func() {
check.EndTime = time.Now()
Expand All @@ -268,5 +275,22 @@ func (check *Check) Run() error {
}
}

printCheckResult(check)

return nil
}

const nbCharsToAvoidLineAliasing = 20

//nolint:goconst
func printCheckResult(check *Check) {
checkID := check.ID + strings.Repeat(" ", nbCharsToAvoidLineAliasing)
switch check.Result {
case CheckResultPassed:
fmt.Printf("\r[ "+cli.Green+"%s"+cli.Reset+" ] %s\n", CheckResultTagPass, checkID)
case CheckResultFailed:
fmt.Printf("\r[ "+cli.Red+"%s"+cli.Reset+" ] %s\n", CheckResultTagFail, checkID)
case CheckResultSkipped:
fmt.Printf("\r[ "+cli.Yellow+"%s"+cli.Reset+" ] %s\n", CheckResultTagSkip, checkID)
}
}
56 changes: 56 additions & 0 deletions pkg/checksdb/checksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"unicode/utf8"

"github.com/sirupsen/logrus"
"github.com/test-network-function/cnf-certification-test/cnf-certification-test/identifiers"
"github.com/test-network-function/cnf-certification-test/internal/cli"
"github.com/test-network-function/test-network-function-claim/pkg/claim"
)

Expand All @@ -25,6 +28,7 @@ func AddCheck(check *Check) {
db = append(db, check)
}

//nolint:funlen
func RunChecks(labelsExpr string, timeout time.Duration) error {
dbLock.Lock()
defer dbLock.Unlock()
Expand Down Expand Up @@ -79,6 +83,10 @@ func RunChecks(labelsExpr string, timeout time.Duration) error {
group.RecordChecksResults()
}

// Print the results in the CLI
cli.PrintResultsTable(getResultsSummary())
printFailedChecksLog()

if len(errs) > 0 {
logrus.Errorf("RunChecks errors: %v", errs)
return fmt.Errorf("%d errors found in checks/groups", len(errs))
Expand Down Expand Up @@ -132,3 +140,51 @@ func GetReconciledResults() map[string]interface{} {
}
return resultMap
}

const (
PASSED = 0
FAILED = 1
SKIPPED = 2
)

func getResultsSummary() map[string][]int {
results := make(map[string][]int)
for groupName, group := range dbByGroup {
groupResults := []int{0, 0, 0}
for _, check := range group.checks {
switch check.Result {
case CheckResultPassed:
groupResults[PASSED]++
case CheckResultFailed:
groupResults[FAILED]++
case CheckResultSkipped:
groupResults[SKIPPED]++
}
}
results[groupName] = groupResults
}
return results
}

const nbColorSymbols = 9

func printFailedChecksLog() {
for _, group := range dbByGroup {
for _, check := range group.checks {
if check.Result != CheckResultFailed {
continue
}
logHeader := fmt.Sprintf("| "+cli.Cyan+"LOG (%s)"+cli.Reset+" |", check.ID)
nbSymbols := utf8.RuneCountInString(logHeader) - nbColorSymbols
fmt.Println(strings.Repeat("-", nbSymbols))
fmt.Println(logHeader)
fmt.Println(strings.Repeat("-", nbSymbols))
log := check.GetLogs()
if log == "" {
fmt.Println("Empty log output")
} else {
fmt.Println(log)
}
}
}
}
4 changes: 4 additions & 0 deletions pkg/checksdb/checksgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/sirupsen/logrus"
"github.com/test-network-function/cnf-certification-test/internal/cli"
)

type ChecksGroup struct {
Expand Down Expand Up @@ -76,6 +77,8 @@ func (group *ChecksGroup) Add(check *Check) {
func skipCheck(check *Check, reason string) {
logrus.Infof("Skipping check %s, reason: %s", check.ID, reason)

fmt.Printf("[ "+cli.Yellow+"SKIP"+cli.Reset+" ] %s\n", check.ID)

check.SetResultSkipped(reason)
}

Expand Down Expand Up @@ -284,6 +287,7 @@ func runCheck(check *Check, group *ChecksGroup, remainingChecks []*Check) (err e
//nolint:funlen
func (group *ChecksGroup) RunChecks(labelsExpr string, stopChan <-chan bool) (errs []error) {
logrus.Infof("Running group %q checks.", group.name)
fmt.Printf("Running suite %s\n", strings.ToUpper(group.name))

labelsExprEvaluator, err := NewLabelsExprEvaluator(labelsExpr)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/configuration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ func LoadConfiguration(filePath string) (TestConfiguration, error) {
}

func LoadEnvironmentVariables() error {
log.Info("Saving environment variables & parameters.")
err := envconfig.Process("tnf", &parameters)
if err != nil {
return fmt.Errorf("could not process the environment variables values, error: %v", err)
}
log.Infof("Environment: %+v", parameters)

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ func GitVersion() string {
GitDisplayRelease = GitRelease
}

return GitDisplayRelease + " ( " + GitCommit + " )"
return GitDisplayRelease + " (" + GitCommit + ")"
}
10 changes: 5 additions & 5 deletions run-cnf-suites.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# [debug] uncomment line below to print out the statements as they are being
# executed.
set -x
# set -x

# defaults
export OUTPUT_LOC="$PWD/cnf-certification-test"
Expand Down Expand Up @@ -94,14 +94,14 @@ if [ "$SERVER_RUN" = "true" ]; then
EXTRA_ARGS="$EXTRA_ARGS -serverMode"
fi

echo "Label: $LABEL"
# echo "Label: $LABEL"
if [[ $LABEL == "all" ]]; then
LABEL='common,extended,faredge,telco'
fi

echo "Running with label filter '$LABEL'"
echo "Report will be output to '$OUTPUT_LOC'"
echo "Extra arguments '${EXTRA_ARGS}'"
# echo "Running with label filter '$LABEL'"
# echo "Report will be output to '$OUTPUT_LOC'"
# echo "Extra arguments '${EXTRA_ARGS}'"
LABEL_STRING=''

if [ -z "$LABEL" ] && { [ -z "$SERVER_RUN" ] || [ "$SERVER_RUN" == "false" ]; }; then
Expand Down
Loading