You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ho...@apache.org on 2021/03/03 12:41:59 UTC

[skywalking-infra-e2e] branch verifier/query updated (7f00f7f -> b30814c)

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

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


    from 7f00f7f  Implement the verification of query
     new 61fd072  Polish code and add tests
     new b30814c  Fix lint

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 commands/verify/verify.go                     | 36 ++++++++++++++-------
 e2e.yaml                                      |  6 ++--
 internal/components/verifier/verifier.go      | 29 ++---------------
 internal/components/verifier/verifier_test.go |  4 +--
 internal/config/globalConfig.go               |  6 ++--
 internal/util/utils.go                        |  6 ++--
 internal/util/{waitSet.go => utils_test.go}   | 45 ++++++++++++++++-----------
 .gitignore => test/verify/3.expected.yaml     | 15 ++++-----
 8 files changed, 72 insertions(+), 75 deletions(-)
 copy internal/util/{waitSet.go => utils_test.go} (61%)
 copy .gitignore => test/verify/3.expected.yaml (88%)


[skywalking-infra-e2e] 02/02: Fix lint

Posted by ho...@apache.org.
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 b30814ce80905dfb2d4694771c1492575ed1e495
Author: Hoshea <fg...@gmail.com>
AuthorDate: Wed Mar 3 20:41:41 2021 +0800

    Fix lint
---
 commands/verify/verify.go       |  1 +
 internal/config/globalConfig.go |  6 +++---
 internal/util/utils.go          |  3 +--
 internal/util/utils_test.go     | 17 +++++++++++++++++
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/commands/verify/verify.go b/commands/verify/verify.go
index 1cfc8be..05ccbe0 100644
--- a/commands/verify/verify.go
+++ b/commands/verify/verify.go
@@ -19,6 +19,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"
diff --git a/internal/config/globalConfig.go b/internal/config/globalConfig.go
index c2bf184..c796962 100644
--- a/internal/config/globalConfig.go
+++ b/internal/config/globalConfig.go
@@ -20,12 +20,12 @@ package config
 
 import (
 	"fmt"
-	"github.com/apache/skywalking-infra-e2e/internal/logger"
 	"io/ioutil"
 
-	"gopkg.in/yaml.v2"
-
+	"github.com/apache/skywalking-infra-e2e/internal/logger"
 	"github.com/apache/skywalking-infra-e2e/internal/util"
+
+	"gopkg.in/yaml.v2"
 )
 
 // GlobalE2EConfig store E2EConfig which can be used globally.
diff --git a/internal/util/utils.go b/internal/util/utils.go
index 5559e11..fc5bda4 100644
--- a/internal/util/utils.go
+++ b/internal/util/utils.go
@@ -65,7 +65,6 @@ func ExecuteCommand(cmd string) (string, error) {
 	}
 	if err := command.Wait(); err != nil {
 		return "", err
-	} else {
-		return outinfo.String(), nil
 	}
+	return outinfo.String(), nil
 }
diff --git a/internal/util/utils_test.go b/internal/util/utils_test.go
index b098b15..1242516 100644
--- a/internal/util/utils_test.go
+++ b/internal/util/utils_test.go
@@ -1,3 +1,20 @@
+// 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 util
 
 import "testing"


[skywalking-infra-e2e] 01/02: Polish code and add tests

Posted by ho...@apache.org.
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 <fg...@gmail.com>
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 }}