This is an automated email from the ASF dual-hosted git repository.

hoshea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-infra-e2e.git


The following commit(s) were added to refs/heads/main by this push:
     new 0f31b43  Add : Support outputting the result of 'verify' in YAML 
format and only outputting the summary of the result of 'verify' (#115)
0f31b43 is described below

commit 0f31b43812f2db482ddb689309d1aa9f37540579
Author: Prince-Chen <1572839...@qq.com>
AuthorDate: Wed Aug 23 22:48:55 2023 +0800

    Add : Support outputting the result of 'verify' in YAML format and only 
outputting the summary of the result of 'verify' (#115)
---
 commands/verify/verify.go       | 23 +++++++++-----
 internal/config/globalConfig.go |  9 ++++--
 pkg/output/output.go            | 68 +++++++++++++++++++++++++++++++++++++++++
 pkg/output/printer.go           | 39 +++++++++++++++++------
 4 files changed, 120 insertions(+), 19 deletions(-)

diff --git a/commands/verify/verify.go b/commands/verify/verify.go
index b1e3296..2411c89 100644
--- a/commands/verify/verify.go
+++ b/commands/verify/verify.go
@@ -43,6 +43,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", "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.
@@ -190,15 +192,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.SummaryOnly {
+                       output.PrintResult(res)
+               } 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].Name = caseName(v)
 
                if v.GetExpected() == "" {
                        res[idx].Skip = false
@@ -231,7 +238,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))
@@ -259,6 +266,9 @@ func caseName(v *config.VerifyCase) string {
 
 // DoVerifyAccordingConfig reads cases from the config file and verifies them.
 func DoVerifyAccordingConfig() error {
+       if output.SummaryOnly && !output.HasFormat() && output.Format != "yaml" 
{
+               return fmt.Errorf(" '%s' format doesn't exist", output.Format)
+       }
        if config.GlobalConfig.Error != nil {
                return config.GlobalConfig.Error
        }
@@ -284,11 +294,10 @@ func DoVerifyAccordingConfig() error {
        concurrency := e2eConfig.Verify.Concurrency
        if concurrency {
                // enable batch output mode when concurrency is enabled
-               printer = output.NewPrinter(true)
+               printer = output.NewPrinter(output.WithBatchOutput(true))
                return verifyCasesConcurrently(&e2eConfig.Verify, &VerifyInfo)
        }
-
-       printer = output.NewPrinter(util.BatchMode)
+       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 c01b078..4119d92 100644
--- a/internal/config/globalConfig.go
+++ b/internal/config/globalConfig.go
@@ -23,11 +23,12 @@ 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"
+       "github.com/apache/skywalking-infra-e2e/pkg/output"
 )
 
 // GlobalE2EConfig stores E2EConfig which can be used globally.
@@ -76,7 +77,9 @@ func ReadGlobalConfigFile() {
        }
 
        GlobalConfig.Error = nil
-       logger.Log.Info("load the e2e config successfully")
+       if !output.SummaryOnly {
+               logger.Log.Info("load the e2e config successfully")
+       }
 }
 
 func convertVerify(verify *Verify) error {
diff --git a/pkg/output/output.go b/pkg/output/output.go
new file mode 100644
index 0000000..ba5afad
--- /dev/null
+++ b/pkg/output/output.go
@@ -0,0 +1,68 @@
+//
+// 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 (
+       SummaryOnly      bool
+       Format           string
+       SupportedFormats = map[string]struct{}{
+               "yaml": {},
+       }
+)
+
+type YamlCaseResult struct {
+       Passed  []string
+       Failed  []string
+       Skipped []string
+}
+
+func HasFormat() bool {
+       _, ok := SupportedFormats[Format]
+       return ok
+}
+
+func PrintResult(caseRes []*CaseResult) {
+       if Format == "yaml" {
+               printResultInYAML(caseRes)
+       }
+}
+
+func printResultInYAML(caseRes []*CaseResult) {
+       var yamlCaseResult YamlCaseResult
+       for _, cr := range caseRes {
+               if !cr.Skip {
+                       if cr.Err == nil {
+                               yamlCaseResult.Passed = 
append(yamlCaseResult.Passed, cr.Name)
+                       } else {
+                               yamlCaseResult.Failed = 
append(yamlCaseResult.Failed, cr.Name)
+                       }
+               } else {
+                       yamlCaseResult.Skipped = append(yamlCaseResult.Skipped, 
cr.Name)
+               }
+       }
+
+       yamlData, _ := yaml.Marshal(yamlCaseResult)
+       fmt.Println(string(yamlData))
+}
diff --git a/pkg/output/printer.go b/pkg/output/printer.go
index fe23062..e398287 100644
--- a/pkg/output/printer.go
+++ b/pkg/output/printer.go
@@ -25,6 +25,7 @@ import (
 
 // CaseResult represents the result of a verification case.
 type CaseResult struct {
+       Name string
        Msg  string
        Err  error
        Skip bool
@@ -42,25 +43,45 @@ type Printer interface {
 type printer struct {
        spinner     *pterm.SpinnerPrinter
        batchOutput bool
+       summaryOnly bool
 }
 
 var _ Printer = &printer{}
 
-func NewPrinter(batchOutput 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,
+       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 WithSummaryOnly(summaryOnly bool) Option {
+       return func(p *printer) {
+               p.summaryOnly = summaryOnly
        }
 }
 
 func (p *printer) Start(msg ...string) {
-       if p.batchOutput {
+       if p.batchOutput || p.summaryOnly {
                return
        }
 
@@ -68,7 +89,7 @@ func (p *printer) Start(msg ...string) {
 }
 
 func (p *printer) Success(msg string) {
-       if p.batchOutput {
+       if p.batchOutput || p.summaryOnly {
                return
        }
 
@@ -76,7 +97,7 @@ func (p *printer) Success(msg string) {
 }
 
 func (p *printer) Warning(msg string) {
-       if p.batchOutput {
+       if p.batchOutput || p.summaryOnly {
                return
        }
 
@@ -84,7 +105,7 @@ func (p *printer) Warning(msg string) {
 }
 
 func (p *printer) Fail(msg string) {
-       if p.batchOutput {
+       if p.batchOutput || p.summaryOnly {
                return
        }
 
@@ -92,7 +113,7 @@ func (p *printer) Fail(msg string) {
 }
 
 func (p *printer) UpdateText(text string) {
-       if p.batchOutput {
+       if p.batchOutput || p.summaryOnly {
                return
        }
 

Reply via email to