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

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

commit 61fd0727f8d8ff8a8b4e0a493f983aae93eaee4c
Author: Hoshea <[email protected]>
AuthorDate: Wed Mar 3 20:38:42 2021 +0800

    Polish code and add tests
---
 commands/verify/verify.go                     | 35 ++++++++++++++++++---------
 e2e.yaml                                      |  6 ++---
 internal/components/verifier/verifier.go      | 29 +++-------------------
 internal/components/verifier/verifier_test.go |  4 +--
 internal/util/utils.go                        |  3 +--
 internal/util/utils_test.go                   | 31 ++++++++++++++++++++++++
 e2e.yaml => test/verify/3.expected.yaml       | 16 ++++--------
 7 files changed, 69 insertions(+), 55 deletions(-)

diff --git a/commands/verify/verify.go b/commands/verify/verify.go
index 3f53183..1cfc8be 100644
--- a/commands/verify/verify.go
+++ b/commands/verify/verify.go
@@ -18,6 +18,7 @@
 package verify
 
 import (
+       "fmt"
        "github.com/apache/skywalking-infra-e2e/internal/components/verifier"
        "github.com/apache/skywalking-infra-e2e/internal/config"
        "github.com/apache/skywalking-infra-e2e/internal/logger"
@@ -53,26 +54,36 @@ var Verify = &cobra.Command{
 func verifySingleCase(expectedFile, actualFile, query string) error {
        expectedData, err := util.ReadFileContent(expectedFile)
        if err != nil {
-               logger.Log.Error("failed to read the expected data file")
-               return err
+               return fmt.Errorf("failed to read the expected data file: %v", 
err)
        }
 
+       var actualData, sourceName string
        if actualFile != "" {
-               if err = verifier.VerifyDataFile(actualFile, expectedData); err 
!= nil {
-                       logger.Log.Warnf("failed to verify the output: %s\n", 
actualFile)
-               } else {
-                       logger.Log.Infof("verified the output: %s\n", 
actualFile)
+               sourceName = actualFile
+               actualData, err = util.ReadFileContent(actualFile)
+               if err != nil {
+                       return fmt.Errorf("failed to read the actual data file: 
%v", err)
                }
        } else if query != "" {
-               if err = verifier.VerifyQuery(query, expectedData); err != nil {
-                       logger.Log.Warnf("failed to verify the output: %s\n", 
query)
-               } else {
-                       logger.Log.Infof("verified the output: %s\n", query)
+               sourceName = query
+               actualData, err = util.ExecuteCommand(query)
+               if err != nil {
+                       return fmt.Errorf("failed to execute the query: %v", 
err)
+               }
+       }
+
+       if err = verifier.Verify(actualData, expectedData); err != nil {
+               logger.Log.Warnf("failed to verify the output: %s\n", 
sourceName)
+               if me, ok := err.(*verifier.MismatchError); ok {
+                       fmt.Println(me.Error())
                }
+       } else {
+               logger.Log.Infof("verified the output: %s\n", sourceName)
        }
        return nil
 }
 
+// verifyAccordingConfig reads cases from the config file and verifies them.
 func verifyAccordingConfig() error {
        if config.GlobalConfig.Error != nil {
                return config.GlobalConfig.Error
@@ -82,7 +93,9 @@ func verifyAccordingConfig() error {
 
        for _, v := range e2eConfig.Verify {
                if v.Expected != "" {
-                       verifySingleCase(v.Expected, v.Actual, v.Query)
+                       if err := verifySingleCase(v.Expected, v.Actual, 
v.Query); err != nil {
+                               logger.Log.Errorf("%v", err)
+                       }
                } else {
                        logger.Log.Error("the expected data file is not 
specified")
                }
diff --git a/e2e.yaml b/e2e.yaml
index 37281fa..66e865b 100644
--- a/e2e.yaml
+++ b/e2e.yaml
@@ -15,7 +15,7 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# This config file can be used to test.
+# This file is used to show how to write configuration files and can be used 
to test.
 
 verify:
   - actual: test/verify/1.actual.yaml
@@ -24,5 +24,5 @@ verify:
     expected: test/verify/2.expected.yaml
   - actual: test/verify/1.actual.yaml
     expected: test/verify/2.expected.yaml
-  - actual: test/verify/2.actual.yaml
-    expected: test/verify/1.expected.yaml
\ No newline at end of file
+  - query: swctl --display yaml service ls
+    expected: test/verify/3.expected.yaml
\ No newline at end of file
diff --git a/internal/components/verifier/verifier.go 
b/internal/components/verifier/verifier.go
index ed15361..866b063 100644
--- a/internal/components/verifier/verifier.go
+++ b/internal/components/verifier/verifier.go
@@ -21,14 +21,13 @@ import (
        "bytes"
        "fmt"
 
-       "github.com/apache/skywalking-infra-e2e/internal/util"
        "github.com/apache/skywalking-infra-e2e/third-party/go/template"
 
        "github.com/google/go-cmp/cmp"
        "gopkg.in/yaml.v2"
 )
 
-// MismatchError is the error type returned by the verify functions.
+// MismatchError is the error type returned by the Verify functions.
 // It contains the diff content.
 type MismatchError struct {
        Err  error
@@ -44,30 +43,8 @@ func (e *MismatchError) Error() string {
        return e.diff
 }
 
-// VerifyDataFile reads the actual data from the file and verifies.
-func VerifyDataFile(actualFile, expectedData string) error {
-       actualData, err := util.ReadFileContent(actualFile)
-       if err != nil {
-               return fmt.Errorf("failed to read the actual data file: %v", 
err)
-       }
-
-       return verify(actualData, expectedData)
-}
-
-// VerifyQuery gets the actual data from the query and then verifies.
-func VerifyQuery(query, expectedData string) error {
-       queryResult, err := util.ExecuteCommand(query)
-       if err != nil {
-               return fmt.Errorf("failed to execute the query: %v", err)
-       }
-
-       // TODO: ensure that the query result has the same format as expected 
data
-
-       return verify(queryResult, expectedData)
-}
-
-// verify checks if the actual data match the expected template.
-func verify(actualData, expectedTemplate string) error {
+// Verify checks if the actual data match the expected template.
+func Verify(actualData, expectedTemplate string) error {
        var actual interface{}
        if err := yaml.Unmarshal([]byte(actualData), &actual); err != nil {
                return fmt.Errorf("failed to unmarshal actual data: %v", err)
diff --git a/internal/components/verifier/verifier_test.go 
b/internal/components/verifier/verifier_test.go
index f7d073a..0d6b7e7 100644
--- a/internal/components/verifier/verifier_test.go
+++ b/internal/components/verifier/verifier_test.go
@@ -142,8 +142,8 @@ metrics:
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
-                       if err := verify(tt.args.actualData, 
tt.args.expectedTemplate); (err != nil) != tt.wantErr {
-                               t.Errorf("verify() error = %v, wantErr %v", 
err, tt.wantErr)
+                       if err := Verify(tt.args.actualData, 
tt.args.expectedTemplate); (err != nil) != tt.wantErr {
+                               t.Errorf("Verify() error = %v, wantErr %v", 
err, tt.wantErr)
                        }
                })
        }
diff --git a/internal/util/utils.go b/internal/util/utils.go
index ab97c3c..5559e11 100644
--- a/internal/util/utils.go
+++ b/internal/util/utils.go
@@ -55,9 +55,8 @@ func ReadFileContent(filename string) (string, error) {
 }
 
 // ExecuteCommand executes the given command and returns the result.
-// TODO: consider security issues.
 func ExecuteCommand(cmd string) (string, error) {
-       command := exec.Command(cmd)
+       command := exec.Command("bash", "-c", cmd)
        outinfo := bytes.Buffer{}
        command.Stdout = &outinfo
 
diff --git a/internal/util/utils_test.go b/internal/util/utils_test.go
new file mode 100644
index 0000000..b098b15
--- /dev/null
+++ b/internal/util/utils_test.go
@@ -0,0 +1,31 @@
+package util
+
+import "testing"
+
+func TestExecuteCommand(t *testing.T) {
+       tests := []struct {
+               name    string
+               cmd     string
+               wantErr bool
+       }{
+               {
+                       name:    "without args",
+                       cmd:     "swctl",
+                       wantErr: false,
+               },
+               {
+                       name:    "with args",
+                       cmd:     "swctl service ls",
+                       wantErr: false,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       _, err := ExecuteCommand(tt.cmd)
+                       if (err != nil) != tt.wantErr {
+                               t.Errorf("ExecuteCommand() error = %v, wantErr 
%v", err, tt.wantErr)
+                               return
+                       }
+               })
+       }
+}
diff --git a/e2e.yaml b/test/verify/3.expected.yaml
similarity index 68%
copy from e2e.yaml
copy to test/verify/3.expected.yaml
index 37281fa..40f5133 100644
--- a/e2e.yaml
+++ b/test/verify/3.expected.yaml
@@ -15,14 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# This config file can be used to test.
-
-verify:
-  - actual: test/verify/1.actual.yaml
-    expected: test/verify/1.expected.yaml
-  - actual: test/verify/2.actual.yaml
-    expected: test/verify/2.expected.yaml
-  - actual: test/verify/1.actual.yaml
-    expected: test/verify/2.expected.yaml
-  - actual: test/verify/2.actual.yaml
-    expected: test/verify/1.expected.yaml
\ No newline at end of file
+{{- contains . }}
+- id: {{ notEmpty .id }}
+  name: {{ notEmpty .name }}
+  group: ""
+{{- end }}

Reply via email to