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

Add : Support outputting the result of 'verify' in YAML format and only outputting the summary of the result of 'verify' #115

Merged
merged 9 commits into from
Aug 23, 2023
31 changes: 21 additions & 10 deletions commands/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ import (
)

var (
query string
actual string
expected string
printer output.Printer
query string
actual string
expected string
summaryOnly bool
printer output.Printer
)

func init() {
Verify.Flags().StringVarP(&query, "query", "q", "", "the query to get the actual data, the result of the query should in YAML format")
Verify.Flags().StringVarP(&actual, "actual", "a", "", "the actual data file, only YAML file format is supported")
Verify.Flags().StringVarP(&expected, "expected", "e", "", "the expected data file, only YAML file format is supported")
Verify.Flags().StringVarP(&output.Format, "output", "o", "", "output the verify summary in which format")
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
Verify.Flags().BoolVarP(&summaryOnly, "summary-only", "", false, "")
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
}

// Verify verifies that the actual data satisfies the expected data pattern.
Expand Down Expand Up @@ -190,15 +193,20 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err
}

defer func() {
_, errNum, _ := printer.PrintResult(res)
if errNum > 0 {
err = fmt.Errorf("failed to verify %d case(s)", errNum)
if output.Format != "" {
output.PrintResult(res)
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
} else {
_, errNum, _ := printer.PrintResult(res)
if errNum > 0 {
err = fmt.Errorf("failed to verify %d case(s)", errNum)
}
}
}()

for idx := range verify.Cases {
printer.Start()
v := &verify.Cases[idx]
res[idx].CaseName = caseName(v)

if v.GetExpected() == "" {
res[idx].Skip = false
Expand Down Expand Up @@ -231,7 +239,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err
}
time.Sleep(verifyInfo.interval)
} else {
res[idx].Msg = fmt.Sprintf("failed to verify %v, retried %d time(s):", caseName(v), current)
res[idx].Msg = fmt.Sprintf("failed to verify %v, retried %d time(s)", caseName(v), current)
res[idx].Err = e
res[idx].Skip = false
printer.UpdateText(fmt.Sprintf("failed to verify %v, retry [%d/%d]", caseName(v), current, verifyInfo.retryCount))
Expand Down Expand Up @@ -259,6 +267,9 @@ func caseName(v *config.VerifyCase) string {

// DoVerifyAccordingConfig reads cases from the config file and verifies them.
func DoVerifyAccordingConfig() error {
if output.Format != "" && output.FormatIsNotExist() {
return fmt.Errorf(" '%s' format doesn't exist", output.Format)
}
if config.GlobalConfig.Error != nil {
return config.GlobalConfig.Error
}
Expand All @@ -284,11 +295,11 @@ func DoVerifyAccordingConfig() error {
concurrency := e2eConfig.Verify.Concurrency
if concurrency {
// enable batch output mode when concurrency is enabled
printer = output.NewPrinter(true)
printer = output.NewPrinter(true, false, false)
return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo)
}

printer = output.NewPrinter(util.BatchMode)
printer = output.NewPrinter(util.BatchMode, output.Format != "", summaryOnly)
return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo)
}

Expand Down
9 changes: 6 additions & 3 deletions internal/config/globalConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ package config

import (
"fmt"
"github.com/apache/skywalking-infra-e2e/pkg/output"
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
"os"
"path/filepath"

"gopkg.in/yaml.v2"

"github.com/apache/skywalking-infra-e2e/internal/constant"
"github.com/apache/skywalking-infra-e2e/internal/logger"
"github.com/apache/skywalking-infra-e2e/internal/util"

"gopkg.in/yaml.v2"
)

// GlobalE2EConfig stores E2EConfig which can be used globally.
Expand Down Expand Up @@ -76,7 +77,9 @@ func ReadGlobalConfigFile() {
}

GlobalConfig.Error = nil
logger.Log.Info("load the e2e config successfully")
if output.Format == "" {
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
logger.Log.Info("load the e2e config successfully")
}
}

func convertVerify(verify *Verify) error {
Expand Down
71 changes: 71 additions & 0 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package output

import (
"fmt"

"gopkg.in/yaml.v2"
)

var (
Format string
Formats = []string{"yaml"}
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
)

type YamlCaseResult struct {
Passed []string
Failed []string
Skipped []string
}

func FormatIsNotExist() bool {
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
for _, format := range Formats {
if Format == format {
return false
}
}

return true
}

func PrintResult(caseRes []*CaseResult) {
switch Format {

Check failure on line 49 in pkg/output/output.go

View workflow job for this annotation

GitHub Actions / Build

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)

Check failure on line 49 in pkg/output/output.go

View workflow job for this annotation

GitHub Actions / Build

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case "yaml":
PrintResultInYAML(caseRes)
}
}

func PrintResultInYAML(caseRes []*CaseResult) {
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
var yamlCaseResult YamlCaseResult
for _, cr := range caseRes {
if !cr.Skip {
if cr.Err == nil {
yamlCaseResult.Passed = append(yamlCaseResult.Passed, cr.CaseName)
} else {
yamlCaseResult.Failed = append(yamlCaseResult.Failed, cr.CaseName)
}
} else {
yamlCaseResult.Skipped = append(yamlCaseResult.Skipped, cr.CaseName)
}
}

yaml, _ := yaml.Marshal(yamlCaseResult)

Check failure on line 69 in pkg/output/output.go

View workflow job for this annotation

GitHub Actions / Build

importShadow: shadow of imported from 'gopkg.in/yaml.v2' package 'yaml' (gocritic)

Check failure on line 69 in pkg/output/output.go

View workflow job for this annotation

GitHub Actions / Build

importShadow: shadow of imported from 'gopkg.in/yaml.v2' package 'yaml' (gocritic)
fmt.Print(string(yaml))
}
34 changes: 20 additions & 14 deletions pkg/output/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@

import (
"fmt"

"github.com/pterm/pterm"
)

// CaseResult represents the result of a verification case.
type CaseResult struct {
Msg string
Err error
Skip bool
CaseName string
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
Msg string
Err error
Skip bool
}

type Printer interface {
Expand All @@ -40,59 +40,64 @@
}

type printer struct {
spinner *pterm.SpinnerPrinter
batchOutput bool
spinner *pterm.SpinnerPrinter
batchOutput bool
outputInFormat bool
summaryOnly bool
}

var _ Printer = &printer{}

func NewPrinter(batchOutput bool) Printer {
func NewPrinter(batchOutput bool, outputInFormat bool, summaryOnly bool) Printer {

Check failure on line 51 in pkg/output/printer.go

View workflow job for this annotation

GitHub Actions / Build

paramTypeCombine: func(batchOutput bool, outputInFormat bool, summaryOnly bool) Printer could be replaced with func(batchOutput, outputInFormat, summaryOnly bool) Printer (gocritic)

Check failure on line 51 in pkg/output/printer.go

View workflow job for this annotation

GitHub Actions / Build

paramTypeCombine: func(batchOutput bool, outputInFormat bool, summaryOnly bool) Printer could be replaced with func(batchOutput, outputInFormat, summaryOnly bool) Printer (gocritic)
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
spinner := pterm.DefaultSpinner.WithShowTimer(false)
pterm.Error.Prefix = pterm.Prefix{
Text: "DETAILS",
Style: &pterm.ThemeDefault.ErrorPrefixStyle,
}

return &printer{
spinner: spinner,
batchOutput: batchOutput,
spinner: spinner,
batchOutput: batchOutput,
outputInFormat: outputInFormat,
summaryOnly: summaryOnly,
}
}

func (p *printer) Start(msg ...string) {
if p.batchOutput {
if p.batchOutput || p.outputInFormat || p.summaryOnly {
chunriyeqiongsaigao marked this conversation as resolved.
Show resolved Hide resolved
return
}

p.spinner, _ = p.spinner.Start(msg)
}

func (p *printer) Success(msg string) {
if p.batchOutput {

if p.batchOutput || p.outputInFormat || p.summaryOnly {
return
}

p.spinner.Success(msg)
}

func (p *printer) Warning(msg string) {
if p.batchOutput {
if p.batchOutput || p.outputInFormat || p.summaryOnly {
return
}

p.spinner.Warning(msg)
}

func (p *printer) Fail(msg string) {
if p.batchOutput {
if p.batchOutput || p.outputInFormat || p.summaryOnly {
return
}

p.spinner.Fail(msg)
}

func (p *printer) UpdateText(text string) {
if p.batchOutput {
if p.batchOutput || p.outputInFormat || p.summaryOnly {
return
}

Expand All @@ -116,6 +121,7 @@
if p.batchOutput {
p.spinner.Warning(cr.Msg)
p.spinner.Fail(cr.Err.Error())

}
}
} else {
Expand Down
Loading