From 9b99fd1dbec9cb1cab391204aa4f592621c0f50f Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Thu, 17 Aug 2023 10:01:39 +0800 Subject: [PATCH 1/9] Add : Support outputting the result of 'verify' in YAML format --- commands/verify/verify.go | 34 +++++++++++++++++++++++--------- internal/config/globalConfig.go | 8 +++++--- internal/util/config.go | 1 + pkg/output/printer.go | 19 +++++++++++++++++- pkg/output/yaml.go | 35 +++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 pkg/output/yaml.go diff --git a/commands/verify/verify.go b/commands/verify/verify.go index b1e3296..3476020 100644 --- a/commands/verify/verify.go +++ b/commands/verify/verify.go @@ -33,16 +33,18 @@ import ( ) var ( - query string - actual string - expected string - printer output.Printer + query string + actual string + expected string + printer output.Printer + verifyResult output.VerifyResult ) 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().BoolVarP(&util.Yaml, "yaml", "y", false, "whether to output the verify result in YAML format. If it is true, the interactive operations and summary of verify will be disabled") } // Verify verifies that the actual data satisfies the expected data pattern. @@ -190,9 +192,15 @@ 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 Yaml is true, output the verifyResult in YAML + // also, if Yaml is true, avoid to output the summary of verify + if util.Yaml { + verifyResult.OutputVerifyResultInYAML() + } else { + _, errNum, _ := printer.PrintResult(res) + if errNum > 0 { + err = fmt.Errorf("failed to verify %d case(s)", errNum) + } } }() @@ -222,6 +230,8 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err } res[idx].Skip = false printer.Success(res[idx].Msg) + // add the passed cases to verifyResult + verifyResult.AddPassedCase(caseName(v)) break } else if current != verifyInfo.retryCount { if current == 0 { @@ -237,7 +247,13 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err printer.UpdateText(fmt.Sprintf("failed to verify %v, retry [%d/%d]", caseName(v), current, verifyInfo.retryCount)) printer.Warning(res[idx].Msg) printer.Fail(res[idx].Err.Error()) + // add the failed cases to verifyResult + verifyResult.AddFailedCase(caseName(v)) if verifyInfo.failFast { + // add the skipped cases to verifyResult + for j := idx + 1; j < len(verify.Cases); j++ { + verifyResult.AddSkippedCase(caseName(&verify.Cases[j])) + } return } } @@ -284,11 +300,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) return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo) } - printer = output.NewPrinter(util.BatchMode) + printer = output.NewPrinter(util.BatchMode, util.Yaml) return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo) } diff --git a/internal/config/globalConfig.go b/internal/config/globalConfig.go index c01b078..2828073 100644 --- a/internal/config/globalConfig.go +++ b/internal/config/globalConfig.go @@ -23,11 +23,11 @@ import ( "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. @@ -76,7 +76,9 @@ func ReadGlobalConfigFile() { } GlobalConfig.Error = nil - logger.Log.Info("load the e2e config successfully") + if !util.Yaml { + logger.Log.Info("load the e2e config successfully") + } } func convertVerify(verify *Verify) error { diff --git a/internal/util/config.go b/internal/util/config.go index 86d99b4..ee6cf20 100644 --- a/internal/util/config.go +++ b/internal/util/config.go @@ -30,6 +30,7 @@ var ( WorkDir string LogDir string BatchMode bool + Yaml bool ) // ResolveAbs resolves the relative path (relative to CfgFile) to an absolute file path. diff --git a/pkg/output/printer.go b/pkg/output/printer.go index fe23062..abd6ad6 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -42,11 +42,12 @@ type Printer interface { type printer struct { spinner *pterm.SpinnerPrinter batchOutput bool + yamlOutput bool } var _ Printer = &printer{} -func NewPrinter(batchOutput bool) Printer { +func NewPrinter(batchOutput bool, yamlOutput bool) Printer { spinner := pterm.DefaultSpinner.WithShowTimer(false) pterm.Error.Prefix = pterm.Prefix{ Text: "DETAILS", @@ -56,6 +57,7 @@ func NewPrinter(batchOutput bool) Printer { return &printer{ spinner: spinner, batchOutput: batchOutput, + yamlOutput: yamlOutput, } } @@ -63,6 +65,9 @@ func (p *printer) Start(msg ...string) { if p.batchOutput { return } + if p.yamlOutput { + return + } p.spinner, _ = p.spinner.Start(msg) } @@ -71,6 +76,9 @@ func (p *printer) Success(msg string) { if p.batchOutput { return } + if p.yamlOutput { + return + } p.spinner.Success(msg) } @@ -79,6 +87,9 @@ func (p *printer) Warning(msg string) { if p.batchOutput { return } + if p.yamlOutput { + return + } p.spinner.Warning(msg) } @@ -87,6 +98,9 @@ func (p *printer) Fail(msg string) { if p.batchOutput { return } + if p.yamlOutput { + return + } p.spinner.Fail(msg) } @@ -95,6 +109,9 @@ func (p *printer) UpdateText(text string) { if p.batchOutput { return } + if p.yamlOutput { + return + } p.spinner.UpdateText(text) } diff --git a/pkg/output/yaml.go b/pkg/output/yaml.go new file mode 100644 index 0000000..aeefd4a --- /dev/null +++ b/pkg/output/yaml.go @@ -0,0 +1,35 @@ +package output + +import ( + "fmt" + + "gopkg.in/yaml.v2" +) + +// VerifyResult stores the result of verify +type VerifyResult struct { + Passed []string + Failed []string + Skipped []string +} + +// OutputVerifyResultInYAML outputs the verifyResult in YAML +func (verifyResult *VerifyResult) OutputVerifyResultInYAML() { + yaml, _ := yaml.Marshal(verifyResult) + fmt.Print(string(yaml)) +} + +// AddPassedCase adds the passed cases to verifyResult +func (verifyResult *VerifyResult) AddPassedCase(caseName string) { + verifyResult.Passed = append(verifyResult.Passed, caseName) +} + +// AddFailedCase adds the failed cases to verifyResult +func (verifyResult *VerifyResult) AddFailedCase(caseName string) { + verifyResult.Failed = append(verifyResult.Failed, caseName) +} + +// AddSkippedCase adds the skipped cases to verifyResult +func (verifyResult *VerifyResult) AddSkippedCase(caseName string) { + verifyResult.Skipped = append(verifyResult.Skipped, caseName) +} From 3f2e62bf2f0a6e6763badfd05e751d981923ac77 Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Sat, 19 Aug 2023 23:37:20 +0800 Subject: [PATCH 2/9] Update : Add '-o --output' and '--simple-result' --- commands/verify/verify.go | 31 +++++++++++--------- internal/config/globalConfig.go | 3 +- internal/util/config.go | 1 - pkg/output/output.go | 51 +++++++++++++++++++++++++++++++++ pkg/output/printer.go | 46 +++++++++++++++++++---------- pkg/output/yaml.go | 35 ---------------------- 6 files changed, 101 insertions(+), 66 deletions(-) create mode 100644 pkg/output/output.go delete mode 100644 pkg/output/yaml.go diff --git a/commands/verify/verify.go b/commands/verify/verify.go index 3476020..d1998d5 100644 --- a/commands/verify/verify.go +++ b/commands/verify/verify.go @@ -36,15 +36,17 @@ var ( query string actual string expected string + simpleResult bool printer output.Printer - verifyResult output.VerifyResult + caseInfo output.CaseInfo ) 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().BoolVarP(&util.Yaml, "yaml", "y", false, "whether to output the verify result in YAML format. If it is true, the interactive operations and summary of verify will be disabled") + Verify.Flags().StringVarP(&output.Format, "output", "o", "", "output the verify summary in which format") + Verify.Flags().BoolVarP(&simpleResult, "simple-result", "", false, "") } // Verify verifies that the actual data satisfies the expected data pattern. @@ -192,10 +194,8 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err } defer func() { - // if Yaml is true, output the verifyResult in YAML - // also, if Yaml is true, avoid to output the summary of verify - if util.Yaml { - verifyResult.OutputVerifyResultInYAML() + if output.Format != "" { + output.PrintTheOutput(caseInfo) } else { _, errNum, _ := printer.PrintResult(res) if errNum > 0 { @@ -230,8 +230,8 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err } res[idx].Skip = false printer.Success(res[idx].Msg) - // add the passed cases to verifyResult - verifyResult.AddPassedCase(caseName(v)) + // add the passed cases to caseInfo + caseInfo.AddPassedCase(caseName(v)) break } else if current != verifyInfo.retryCount { if current == 0 { @@ -241,18 +241,18 @@ 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)) printer.Warning(res[idx].Msg) printer.Fail(res[idx].Err.Error()) // add the failed cases to verifyResult - verifyResult.AddFailedCase(caseName(v)) + caseInfo.AddFailedCase(caseName(v)) if verifyInfo.failFast { - // add the skipped cases to verifyResult + // add the skipped cases to caseInfo for j := idx + 1; j < len(verify.Cases); j++ { - verifyResult.AddSkippedCase(caseName(&verify.Cases[j])) + caseInfo.AddSkippedCase(caseName(&verify.Cases[j])) } return } @@ -275,6 +275,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 } @@ -300,11 +303,11 @@ func DoVerifyAccordingConfig() error { concurrency := e2eConfig.Verify.Concurrency if concurrency { // enable batch output mode when concurrency is enabled - printer = output.NewPrinter(true, false) + printer = output.NewPrinter(true, false, false) return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo) } - printer = output.NewPrinter(util.BatchMode, util.Yaml) + printer = output.NewPrinter(util.BatchMode, output.Format != "", simpleResult) return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo) } diff --git a/internal/config/globalConfig.go b/internal/config/globalConfig.go index 2828073..bc54698 100644 --- a/internal/config/globalConfig.go +++ b/internal/config/globalConfig.go @@ -20,6 +20,7 @@ package config import ( "fmt" + "github.com/apache/skywalking-infra-e2e/pkg/output" "os" "path/filepath" @@ -76,7 +77,7 @@ func ReadGlobalConfigFile() { } GlobalConfig.Error = nil - if !util.Yaml { + if output.Output == "" { logger.Log.Info("load the e2e config successfully") } } diff --git a/internal/util/config.go b/internal/util/config.go index ee6cf20..86d99b4 100644 --- a/internal/util/config.go +++ b/internal/util/config.go @@ -30,7 +30,6 @@ var ( WorkDir string LogDir string BatchMode bool - Yaml bool ) // ResolveAbs resolves the relative path (relative to CfgFile) to an absolute file path. diff --git a/pkg/output/output.go b/pkg/output/output.go new file mode 100644 index 0000000..acaab0d --- /dev/null +++ b/pkg/output/output.go @@ -0,0 +1,51 @@ +package output + +import ( + "fmt" + "gopkg.in/yaml.v2" +) + +var ( + Format string + Formats = []string{"yaml"} +) + +type CaseInfo struct { + Passed []string + Failed []string + Skipped []string +} + +func FormatIsNotExist() bool { + for _, format := range Formats { + if Format == format { + return false + } + } + + return true +} + +func PrintTheOutput(caseInfo CaseInfo) { + switch Format { + case "yaml": + caseInfo.PrintInYAML() + } +} + +func (caseInfo *CaseInfo) PrintInYAML() { + yaml, _ := yaml.Marshal(caseInfo) + fmt.Print(string(yaml)) +} + +func (caseInfo *CaseInfo) AddPassedCase(caseName string) { + caseInfo.Passed = append(caseInfo.Passed, caseName) +} + +func (caseInfo *CaseInfo) AddFailedCase(caseName string) { + caseInfo.Failed = append(caseInfo.Failed, caseName) +} + +func (caseInfo *CaseInfo) AddSkippedCase(caseName string) { + caseInfo.Skipped = append(caseInfo.Skipped, caseName) +} diff --git a/pkg/output/printer.go b/pkg/output/printer.go index abd6ad6..f38a4f5 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -19,7 +19,6 @@ package output import ( "fmt" - "github.com/pterm/pterm" ) @@ -40,14 +39,15 @@ type Printer interface { } type printer struct { - spinner *pterm.SpinnerPrinter - batchOutput bool - yamlOutput bool + spinner *pterm.SpinnerPrinter + batchOutput bool + outputInFormat bool + simpleResult bool } var _ Printer = &printer{} -func NewPrinter(batchOutput bool, yamlOutput bool) Printer { +func NewPrinter(batchOutput bool, outputInFormat bool, simpleResult bool) Printer { spinner := pterm.DefaultSpinner.WithShowTimer(false) pterm.Error.Prefix = pterm.Prefix{ Text: "DETAILS", @@ -55,9 +55,10 @@ func NewPrinter(batchOutput bool, yamlOutput bool) Printer { } return &printer{ - spinner: spinner, - batchOutput: batchOutput, - yamlOutput: yamlOutput, + spinner: spinner, + batchOutput: batchOutput, + outputInFormat: outputInFormat, + simpleResult: simpleResult, } } @@ -65,7 +66,8 @@ func (p *printer) Start(msg ...string) { if p.batchOutput { return } - if p.yamlOutput { + + if p.outputInFormat { return } @@ -73,10 +75,12 @@ func (p *printer) Start(msg ...string) { } func (p *printer) Success(msg string) { + if p.batchOutput { return } - if p.yamlOutput { + + if p.outputInFormat { return } @@ -87,7 +91,8 @@ func (p *printer) Warning(msg string) { if p.batchOutput { return } - if p.yamlOutput { + + if p.outputInFormat { return } @@ -98,18 +103,24 @@ func (p *printer) Fail(msg string) { if p.batchOutput { return } - if p.yamlOutput { + + if p.outputInFormat { return } - p.spinner.Fail(msg) + if !p.simpleResult { + p.spinner.Fail(msg) + } else { + println() + } } func (p *printer) UpdateText(text string) { if p.batchOutput { return } - if p.yamlOutput { + + if p.outputInFormat { return } @@ -132,7 +143,12 @@ func (p *printer) PrintResult(caseRes []*CaseResult) (passNum, failNum, skipNum failNum++ if p.batchOutput { p.spinner.Warning(cr.Msg) - p.spinner.Fail(cr.Err.Error()) + if !p.simpleResult { + p.spinner.Fail(cr.Err.Error()) + } else { + fmt.Println() + } + } } } else { diff --git a/pkg/output/yaml.go b/pkg/output/yaml.go deleted file mode 100644 index aeefd4a..0000000 --- a/pkg/output/yaml.go +++ /dev/null @@ -1,35 +0,0 @@ -package output - -import ( - "fmt" - - "gopkg.in/yaml.v2" -) - -// VerifyResult stores the result of verify -type VerifyResult struct { - Passed []string - Failed []string - Skipped []string -} - -// OutputVerifyResultInYAML outputs the verifyResult in YAML -func (verifyResult *VerifyResult) OutputVerifyResultInYAML() { - yaml, _ := yaml.Marshal(verifyResult) - fmt.Print(string(yaml)) -} - -// AddPassedCase adds the passed cases to verifyResult -func (verifyResult *VerifyResult) AddPassedCase(caseName string) { - verifyResult.Passed = append(verifyResult.Passed, caseName) -} - -// AddFailedCase adds the failed cases to verifyResult -func (verifyResult *VerifyResult) AddFailedCase(caseName string) { - verifyResult.Failed = append(verifyResult.Failed, caseName) -} - -// AddSkippedCase adds the skipped cases to verifyResult -func (verifyResult *VerifyResult) AddSkippedCase(caseName string) { - verifyResult.Skipped = append(verifyResult.Skipped, caseName) -} From baed40212a1e3e1ea2ed0705e18102f4933e2361 Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Sun, 20 Aug 2023 15:40:31 +0800 Subject: [PATCH 3/9] Update : the detail information is in PR title and descriptions --- commands/verify/verify.go | 16 ++++++++-------- internal/config/globalConfig.go | 2 +- pkg/output/printer.go | 10 +++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/commands/verify/verify.go b/commands/verify/verify.go index d1998d5..c41e0c2 100644 --- a/commands/verify/verify.go +++ b/commands/verify/verify.go @@ -33,12 +33,12 @@ import ( ) var ( - query string - actual string - expected string - simpleResult bool - printer output.Printer - caseInfo output.CaseInfo + query string + actual string + expected string + outputDiff bool + printer output.Printer + caseInfo output.CaseInfo ) func init() { @@ -46,7 +46,7 @@ func init() { 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") - Verify.Flags().BoolVarP(&simpleResult, "simple-result", "", false, "") + Verify.Flags().BoolVarP(&outputDiff, "no-diff", "", false, "") } // Verify verifies that the actual data satisfies the expected data pattern. @@ -307,7 +307,7 @@ func DoVerifyAccordingConfig() error { return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo) } - printer = output.NewPrinter(util.BatchMode, output.Format != "", simpleResult) + printer = output.NewPrinter(util.BatchMode, output.Format != "", outputDiff) return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo) } diff --git a/internal/config/globalConfig.go b/internal/config/globalConfig.go index bc54698..4c2f406 100644 --- a/internal/config/globalConfig.go +++ b/internal/config/globalConfig.go @@ -77,7 +77,7 @@ func ReadGlobalConfigFile() { } GlobalConfig.Error = nil - if output.Output == "" { + if output.Format == "" { logger.Log.Info("load the e2e config successfully") } } diff --git a/pkg/output/printer.go b/pkg/output/printer.go index f38a4f5..3b8d461 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -42,12 +42,12 @@ type printer struct { spinner *pterm.SpinnerPrinter batchOutput bool outputInFormat bool - simpleResult bool + outputDiff bool } var _ Printer = &printer{} -func NewPrinter(batchOutput bool, outputInFormat bool, simpleResult bool) Printer { +func NewPrinter(batchOutput bool, outputInFormat bool, outputDiff bool) Printer { spinner := pterm.DefaultSpinner.WithShowTimer(false) pterm.Error.Prefix = pterm.Prefix{ Text: "DETAILS", @@ -58,7 +58,7 @@ func NewPrinter(batchOutput bool, outputInFormat bool, simpleResult bool) Printe spinner: spinner, batchOutput: batchOutput, outputInFormat: outputInFormat, - simpleResult: simpleResult, + outputDiff: outputDiff, } } @@ -108,7 +108,7 @@ func (p *printer) Fail(msg string) { return } - if !p.simpleResult { + if !p.outputDiff { p.spinner.Fail(msg) } else { println() @@ -143,7 +143,7 @@ func (p *printer) PrintResult(caseRes []*CaseResult) (passNum, failNum, skipNum failNum++ if p.batchOutput { p.spinner.Warning(cr.Msg) - if !p.simpleResult { + if !p.outputDiff { p.spinner.Fail(cr.Err.Error()) } else { fmt.Println() From 73a825e3339b7fe98eae801df113e216b5abac35 Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Sun, 20 Aug 2023 17:02:05 +0800 Subject: [PATCH 4/9] update : add '--summary-only' --- commands/verify/verify.go | 26 +++++++----------- pkg/output/output.go | 35 +++++++++++++------------ pkg/output/printer.go | 55 ++++++++++----------------------------- 3 files changed, 41 insertions(+), 75 deletions(-) diff --git a/commands/verify/verify.go b/commands/verify/verify.go index c41e0c2..eb97a45 100644 --- a/commands/verify/verify.go +++ b/commands/verify/verify.go @@ -33,12 +33,11 @@ import ( ) var ( - query string - actual string - expected string - outputDiff bool - printer output.Printer - caseInfo output.CaseInfo + query string + actual string + expected string + summaryOnly bool + printer output.Printer ) func init() { @@ -46,7 +45,7 @@ func init() { 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") - Verify.Flags().BoolVarP(&outputDiff, "no-diff", "", false, "") + Verify.Flags().BoolVarP(&summaryOnly, "summary-only", "", false, "") } // Verify verifies that the actual data satisfies the expected data pattern. @@ -195,7 +194,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err defer func() { if output.Format != "" { - output.PrintTheOutput(caseInfo) + output.PrintResult(res) } else { _, errNum, _ := printer.PrintResult(res) if errNum > 0 { @@ -207,6 +206,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err for idx := range verify.Cases { printer.Start() v := &verify.Cases[idx] + res[idx].CaseName = caseName(v) if v.GetExpected() == "" { res[idx].Skip = false @@ -230,8 +230,6 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err } res[idx].Skip = false printer.Success(res[idx].Msg) - // add the passed cases to caseInfo - caseInfo.AddPassedCase(caseName(v)) break } else if current != verifyInfo.retryCount { if current == 0 { @@ -247,13 +245,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err printer.UpdateText(fmt.Sprintf("failed to verify %v, retry [%d/%d]", caseName(v), current, verifyInfo.retryCount)) printer.Warning(res[idx].Msg) printer.Fail(res[idx].Err.Error()) - // add the failed cases to verifyResult - caseInfo.AddFailedCase(caseName(v)) if verifyInfo.failFast { - // add the skipped cases to caseInfo - for j := idx + 1; j < len(verify.Cases); j++ { - caseInfo.AddSkippedCase(caseName(&verify.Cases[j])) - } return } } @@ -307,7 +299,7 @@ func DoVerifyAccordingConfig() error { return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo) } - printer = output.NewPrinter(util.BatchMode, output.Format != "", outputDiff) + printer = output.NewPrinter(util.BatchMode, output.Format != "", summaryOnly) return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo) } diff --git a/pkg/output/output.go b/pkg/output/output.go index acaab0d..e1b5ba5 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -10,7 +10,7 @@ var ( Formats = []string{"yaml"} ) -type CaseInfo struct { +type YamlCaseResult struct { Passed []string Failed []string Skipped []string @@ -26,26 +26,27 @@ func FormatIsNotExist() bool { return true } -func PrintTheOutput(caseInfo CaseInfo) { +func PrintResult(caseRes []*CaseResult) { switch Format { case "yaml": - caseInfo.PrintInYAML() + PrintResultInYAML(caseRes) } } -func (caseInfo *CaseInfo) PrintInYAML() { - yaml, _ := yaml.Marshal(caseInfo) - fmt.Print(string(yaml)) -} - -func (caseInfo *CaseInfo) AddPassedCase(caseName string) { - caseInfo.Passed = append(caseInfo.Passed, caseName) -} - -func (caseInfo *CaseInfo) AddFailedCase(caseName string) { - caseInfo.Failed = append(caseInfo.Failed, caseName) -} +func PrintResultInYAML(caseRes []*CaseResult) { + 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) + } + } -func (caseInfo *CaseInfo) AddSkippedCase(caseName string) { - caseInfo.Skipped = append(caseInfo.Skipped, caseName) + yaml, _ := yaml.Marshal(yamlCaseResult) + fmt.Print(string(yaml)) } diff --git a/pkg/output/printer.go b/pkg/output/printer.go index 3b8d461..5224b58 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -24,9 +24,10 @@ import ( // CaseResult represents the result of a verification case. type CaseResult struct { - Msg string - Err error - Skip bool + CaseName string + Msg string + Err error + Skip bool } type Printer interface { @@ -42,12 +43,12 @@ type printer struct { spinner *pterm.SpinnerPrinter batchOutput bool outputInFormat bool - outputDiff bool + summaryOnly bool } var _ Printer = &printer{} -func NewPrinter(batchOutput bool, outputInFormat bool, outputDiff bool) Printer { +func NewPrinter(batchOutput bool, outputInFormat bool, summaryOnly bool) Printer { spinner := pterm.DefaultSpinner.WithShowTimer(false) pterm.Error.Prefix = pterm.Prefix{ Text: "DETAILS", @@ -58,16 +59,12 @@ func NewPrinter(batchOutput bool, outputInFormat bool, outputDiff bool) Printer spinner: spinner, batchOutput: batchOutput, outputInFormat: outputInFormat, - outputDiff: outputDiff, + summaryOnly: summaryOnly, } } func (p *printer) Start(msg ...string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -76,11 +73,7 @@ func (p *printer) Start(msg ...string) { func (p *printer) Success(msg string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -88,11 +81,7 @@ func (p *printer) Success(msg string) { } func (p *printer) Warning(msg string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -100,27 +89,15 @@ func (p *printer) Warning(msg string) { } func (p *printer) Fail(msg string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } - if !p.outputDiff { - p.spinner.Fail(msg) - } else { - println() - } + p.spinner.Fail(msg) } func (p *printer) UpdateText(text string) { - if p.batchOutput { - return - } - - if p.outputInFormat { + if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -143,11 +120,7 @@ func (p *printer) PrintResult(caseRes []*CaseResult) (passNum, failNum, skipNum failNum++ if p.batchOutput { p.spinner.Warning(cr.Msg) - if !p.outputDiff { - p.spinner.Fail(cr.Err.Error()) - } else { - fmt.Println() - } + p.spinner.Fail(cr.Err.Error()) } } From c181506f932778bec42fdb40df8f2d91808b0554 Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Sun, 20 Aug 2023 17:26:16 +0800 Subject: [PATCH 5/9] fix : add License to output.go --- pkg/output/output.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/output/output.go b/pkg/output/output.go index e1b5ba5..d763565 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -1,7 +1,26 @@ +// +// 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" ) From 27961f0e1cfac1a1a7ce495c30f14232156eec4f Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Mon, 21 Aug 2023 17:50:32 +0800 Subject: [PATCH 6/9] fix some nits --- commands/verify/verify.go | 13 +++++----- internal/config/globalConfig.go | 2 +- pkg/output/output.go | 28 +++++++++------------ pkg/output/printer.go | 43 +++++++++++++++++++++++++-------- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/commands/verify/verify.go b/commands/verify/verify.go index eb97a45..ce78f84 100644 --- a/commands/verify/verify.go +++ b/commands/verify/verify.go @@ -44,8 +44,8 @@ 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") - Verify.Flags().BoolVarP(&summaryOnly, "summary-only", "", false, "") + Verify.Flags().StringVarP(&output.Format, "output", "o", "", "output the verify summary in which format. Currently, only 'yaml' is supported. ") + Verify.Flags().BoolVarP(&summaryOnly, "summary-only", "", false, "if true, only 'SUMMARY' part of the verify result will be outputted") } // Verify verifies that the actual data satisfies the expected data pattern. @@ -206,7 +206,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err for idx := range verify.Cases { printer.Start() v := &verify.Cases[idx] - res[idx].CaseName = caseName(v) + res[idx].Name = caseName(v) if v.GetExpected() == "" { res[idx].Skip = false @@ -267,7 +267,7 @@ func caseName(v *config.VerifyCase) string { // DoVerifyAccordingConfig reads cases from the config file and verifies them. func DoVerifyAccordingConfig() error { - if output.Format != "" && output.FormatIsNotExist() { + if output.Format != "" && !output.HasFormat() { return fmt.Errorf(" '%s' format doesn't exist", output.Format) } if config.GlobalConfig.Error != nil { @@ -295,11 +295,10 @@ func DoVerifyAccordingConfig() error { concurrency := e2eConfig.Verify.Concurrency if concurrency { // enable batch output mode when concurrency is enabled - printer = output.NewPrinter(true, false, false) + printer = output.NewPrinter(output.WithBatchOutput(true)) return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo) } - - printer = output.NewPrinter(util.BatchMode, output.Format != "", summaryOnly) + printer = output.NewPrinter(output.WithBatchOutput(util.BatchMode), output.WithOutputInFormat(output.Format != ""), output.WithSummaryOnly(summaryOnly)) return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo) } diff --git a/internal/config/globalConfig.go b/internal/config/globalConfig.go index 4c2f406..0f59063 100644 --- a/internal/config/globalConfig.go +++ b/internal/config/globalConfig.go @@ -20,7 +20,6 @@ package config import ( "fmt" - "github.com/apache/skywalking-infra-e2e/pkg/output" "os" "path/filepath" @@ -29,6 +28,7 @@ import ( "github.com/apache/skywalking-infra-e2e/internal/constant" "github.com/apache/skywalking-infra-e2e/internal/logger" "github.com/apache/skywalking-infra-e2e/internal/util" + "github.com/apache/skywalking-infra-e2e/pkg/output" ) // GlobalE2EConfig stores E2EConfig which can be used globally. diff --git a/pkg/output/output.go b/pkg/output/output.go index d763565..f4723c3 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -26,7 +26,9 @@ import ( var ( Format string - Formats = []string{"yaml"} + Formats = map[string]struct{}{ + "yaml": {}, + } ) type YamlCaseResult struct { @@ -35,19 +37,13 @@ type YamlCaseResult struct { Skipped []string } -func FormatIsNotExist() bool { - for _, format := range Formats { - if Format == format { - return false - } - } - - return true +func HasFormat() bool { + _, ok := Formats[Format] + return ok } func PrintResult(caseRes []*CaseResult) { - switch Format { - case "yaml": + if Format == "yaml" { PrintResultInYAML(caseRes) } } @@ -57,15 +53,15 @@ func PrintResultInYAML(caseRes []*CaseResult) { for _, cr := range caseRes { if !cr.Skip { if cr.Err == nil { - yamlCaseResult.Passed = append(yamlCaseResult.Passed, cr.CaseName) + yamlCaseResult.Passed = append(yamlCaseResult.Passed, cr.Name) } else { - yamlCaseResult.Failed = append(yamlCaseResult.Failed, cr.CaseName) + yamlCaseResult.Failed = append(yamlCaseResult.Failed, cr.Name) } } else { - yamlCaseResult.Skipped = append(yamlCaseResult.Skipped, cr.CaseName) + yamlCaseResult.Skipped = append(yamlCaseResult.Skipped, cr.Name) } } - yaml, _ := yaml.Marshal(yamlCaseResult) - fmt.Print(string(yaml)) + yamlData, _ := yaml.Marshal(yamlCaseResult) + fmt.Print(string(yamlData)) } diff --git a/pkg/output/printer.go b/pkg/output/printer.go index 5224b58..095309b 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -24,10 +24,10 @@ import ( // CaseResult represents the result of a verification case. type CaseResult struct { - CaseName string - Msg string - Err error - Skip bool + Name string + Msg string + Err error + Skip bool } type Printer interface { @@ -48,18 +48,41 @@ type printer struct { var _ Printer = &printer{} -func NewPrinter(batchOutput bool, outputInFormat bool, summaryOnly bool) Printer { +func NewPrinter(options ...Option) Printer { spinner := pterm.DefaultSpinner.WithShowTimer(false) pterm.Error.Prefix = pterm.Prefix{ Text: "DETAILS", Style: &pterm.ThemeDefault.ErrorPrefixStyle, } - return &printer{ - spinner: spinner, - batchOutput: batchOutput, - outputInFormat: outputInFormat, - summaryOnly: summaryOnly, + p := &printer{ + spinner: spinner, + } + + for _, option := range options { + option(p) + } + + return p +} + +type Option func(*printer) + +func WithBatchOutput(batchOutput bool) Option { + return func(p *printer) { + p.batchOutput = batchOutput + } +} + +func WithOutputInFormat(outputInFormat bool) Option { + return func(p *printer) { + p.outputInFormat = outputInFormat + } +} + +func WithSummaryOnly(summaryOnly bool) Option { + return func(p *printer) { + p.summaryOnly = summaryOnly } } From f2f0acd2e53d26946c80d1abd6d86336ac2b03cf Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Mon, 21 Aug 2023 18:11:49 +0800 Subject: [PATCH 7/9] fix some go-lint errors --- commands/verify/verify.go | 4 ++-- pkg/output/printer.go | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/commands/verify/verify.go b/commands/verify/verify.go index ce78f84..c7ee18a 100644 --- a/commands/verify/verify.go +++ b/commands/verify/verify.go @@ -295,10 +295,10 @@ func DoVerifyAccordingConfig() error { concurrency := e2eConfig.Verify.Concurrency if concurrency { // enable batch output mode when concurrency is enabled - printer = output.NewPrinter(output.WithBatchOutput(true)) + printer = output.NewPrinter(output.WithBatchMod(true)) return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo) } - printer = output.NewPrinter(output.WithBatchOutput(util.BatchMode), output.WithOutputInFormat(output.Format != ""), output.WithSummaryOnly(summaryOnly)) + printer = output.NewPrinter(output.WithBatchMod(util.BatchMode), output.WithFormat(output.Format != ""), output.WithSummaryOnly(summaryOnly)) return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo) } diff --git a/pkg/output/printer.go b/pkg/output/printer.go index 095309b..172732c 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -68,13 +68,13 @@ func NewPrinter(options ...Option) Printer { type Option func(*printer) -func WithBatchOutput(batchOutput bool) Option { +func WithBatchMod(batchOutput bool) Option { return func(p *printer) { p.batchOutput = batchOutput } } -func WithOutputInFormat(outputInFormat bool) Option { +func WithFormat(outputInFormat bool) Option { return func(p *printer) { p.outputInFormat = outputInFormat } @@ -95,7 +95,6 @@ func (p *printer) Start(msg ...string) { } func (p *printer) Success(msg string) { - if p.batchOutput || p.outputInFormat || p.summaryOnly { return } @@ -144,7 +143,6 @@ func (p *printer) PrintResult(caseRes []*CaseResult) (passNum, failNum, skipNum if p.batchOutput { p.spinner.Warning(cr.Msg) p.spinner.Fail(cr.Err.Error()) - } } } else { From 9a2de94d87a8a06cb2fe18413301830997761f1e Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Mon, 21 Aug 2023 18:23:49 +0800 Subject: [PATCH 8/9] fix go-lint error (local go-lint test passed) --- pkg/output/printer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/output/printer.go b/pkg/output/printer.go index 172732c..b880941 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -19,6 +19,7 @@ package output import ( "fmt" + "github.com/pterm/pterm" ) From 41e7372f76b0037e77c6132b3e1da6256cdbdb1c Mon Sep 17 00:00:00 2001 From: wang <1572839226@qq.com> Date: Wed, 23 Aug 2023 20:12:57 +0800 Subject: [PATCH 9/9] fix some nits --- commands/verify/verify.go | 21 ++++++++++----------- internal/config/globalConfig.go | 2 +- pkg/output/output.go | 13 +++++++------ pkg/output/printer.go | 25 +++++++++---------------- 4 files changed, 27 insertions(+), 34 deletions(-) diff --git a/commands/verify/verify.go b/commands/verify/verify.go index c7ee18a..2411c89 100644 --- a/commands/verify/verify.go +++ b/commands/verify/verify.go @@ -33,19 +33,18 @@ import ( ) var ( - query string - actual string - expected string - summaryOnly bool - printer output.Printer + query string + actual string + expected string + 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. Currently, only 'yaml' is supported. ") - Verify.Flags().BoolVarP(&summaryOnly, "summary-only", "", false, "if true, only 'SUMMARY' part of the verify result will be outputted") + Verify.Flags().StringVarP(&output.Format, "output", "o", "yaml", "output the verify summary in which format. Currently, only 'yaml' is supported. ") + Verify.Flags().BoolVarP(&output.SummaryOnly, "summary-only", "", false, "if true, only 'SUMMARY' part of the verify result will be outputted") } // Verify verifies that the actual data satisfies the expected data pattern. @@ -193,7 +192,7 @@ func verifyCasesSerially(verify *config.Verify, verifyInfo *verifyInfo) (err err } defer func() { - if output.Format != "" { + if output.SummaryOnly { output.PrintResult(res) } else { _, errNum, _ := printer.PrintResult(res) @@ -267,7 +266,7 @@ func caseName(v *config.VerifyCase) string { // DoVerifyAccordingConfig reads cases from the config file and verifies them. func DoVerifyAccordingConfig() error { - if output.Format != "" && !output.HasFormat() { + if output.SummaryOnly && !output.HasFormat() && output.Format != "yaml" { return fmt.Errorf(" '%s' format doesn't exist", output.Format) } if config.GlobalConfig.Error != nil { @@ -295,10 +294,10 @@ func DoVerifyAccordingConfig() error { concurrency := e2eConfig.Verify.Concurrency if concurrency { // enable batch output mode when concurrency is enabled - printer = output.NewPrinter(output.WithBatchMod(true)) + printer = output.NewPrinter(output.WithBatchOutput(true)) return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo) } - printer = output.NewPrinter(output.WithBatchMod(util.BatchMode), output.WithFormat(output.Format != ""), output.WithSummaryOnly(summaryOnly)) + printer = output.NewPrinter(output.WithBatchOutput(util.BatchMode), output.WithSummaryOnly(output.SummaryOnly)) return verifyCasesSerially(&e2eConfig.Verify, &VerifyInfo) } diff --git a/internal/config/globalConfig.go b/internal/config/globalConfig.go index 0f59063..4119d92 100644 --- a/internal/config/globalConfig.go +++ b/internal/config/globalConfig.go @@ -77,7 +77,7 @@ func ReadGlobalConfigFile() { } GlobalConfig.Error = nil - if output.Format == "" { + if !output.SummaryOnly { logger.Log.Info("load the e2e config successfully") } } diff --git a/pkg/output/output.go b/pkg/output/output.go index f4723c3..ba5afad 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -25,8 +25,9 @@ import ( ) var ( - Format string - Formats = map[string]struct{}{ + SummaryOnly bool + Format string + SupportedFormats = map[string]struct{}{ "yaml": {}, } ) @@ -38,17 +39,17 @@ type YamlCaseResult struct { } func HasFormat() bool { - _, ok := Formats[Format] + _, ok := SupportedFormats[Format] return ok } func PrintResult(caseRes []*CaseResult) { if Format == "yaml" { - PrintResultInYAML(caseRes) + printResultInYAML(caseRes) } } -func PrintResultInYAML(caseRes []*CaseResult) { +func printResultInYAML(caseRes []*CaseResult) { var yamlCaseResult YamlCaseResult for _, cr := range caseRes { if !cr.Skip { @@ -63,5 +64,5 @@ func PrintResultInYAML(caseRes []*CaseResult) { } yamlData, _ := yaml.Marshal(yamlCaseResult) - fmt.Print(string(yamlData)) + fmt.Println(string(yamlData)) } diff --git a/pkg/output/printer.go b/pkg/output/printer.go index b880941..e398287 100644 --- a/pkg/output/printer.go +++ b/pkg/output/printer.go @@ -41,10 +41,9 @@ type Printer interface { } type printer struct { - spinner *pterm.SpinnerPrinter - batchOutput bool - outputInFormat bool - summaryOnly bool + spinner *pterm.SpinnerPrinter + batchOutput bool + summaryOnly bool } var _ Printer = &printer{} @@ -69,18 +68,12 @@ func NewPrinter(options ...Option) Printer { type Option func(*printer) -func WithBatchMod(batchOutput bool) Option { +func WithBatchOutput(batchOutput bool) Option { return func(p *printer) { p.batchOutput = batchOutput } } -func WithFormat(outputInFormat bool) Option { - return func(p *printer) { - p.outputInFormat = outputInFormat - } -} - func WithSummaryOnly(summaryOnly bool) Option { return func(p *printer) { p.summaryOnly = summaryOnly @@ -88,7 +81,7 @@ func WithSummaryOnly(summaryOnly bool) Option { } func (p *printer) Start(msg ...string) { - if p.batchOutput || p.outputInFormat || p.summaryOnly { + if p.batchOutput || p.summaryOnly { return } @@ -96,7 +89,7 @@ func (p *printer) Start(msg ...string) { } func (p *printer) Success(msg string) { - if p.batchOutput || p.outputInFormat || p.summaryOnly { + if p.batchOutput || p.summaryOnly { return } @@ -104,7 +97,7 @@ func (p *printer) Success(msg string) { } func (p *printer) Warning(msg string) { - if p.batchOutput || p.outputInFormat || p.summaryOnly { + if p.batchOutput || p.summaryOnly { return } @@ -112,7 +105,7 @@ func (p *printer) Warning(msg string) { } func (p *printer) Fail(msg string) { - if p.batchOutput || p.outputInFormat || p.summaryOnly { + if p.batchOutput || p.summaryOnly { return } @@ -120,7 +113,7 @@ func (p *printer) Fail(msg string) { } func (p *printer) UpdateText(text string) { - if p.batchOutput || p.outputInFormat || p.summaryOnly { + if p.batchOutput || p.summaryOnly { return }