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/01 09:03:51 UTC

[skywalking-infra-e2e] branch verifier/action updated: Polish code and add a unit test

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

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


The following commit(s) were added to refs/heads/verifier/action by this push:
     new 33edd4a  Polish code and add a unit test
33edd4a is described below

commit 33edd4a648c34927d2dc85f99293338397cfa1fd
Author: Hoshea <fg...@gmail.com>
AuthorDate: Mon Mar 1 17:03:06 2021 +0800

    Polish code and add a unit test
---
 internal/components/verifier/verifier_test.go | 134 ++++++++++++++++++++++++++
 test/verify/2.actual.yaml                     |   4 +-
 test/verify/2.expected.yaml                   |   2 +-
 third-party/go/template/exec.go               |  25 ++---
 4 files changed, 151 insertions(+), 14 deletions(-)

diff --git a/internal/components/verifier/verifier_test.go b/internal/components/verifier/verifier_test.go
new file mode 100644
index 0000000..544b969
--- /dev/null
+++ b/internal/components/verifier/verifier_test.go
@@ -0,0 +1,134 @@
+package verifier
+
+import "testing"
+
+func TestVerify(t *testing.T) {
+	type args struct {
+		actualData       string
+		expectedTemplate string
+	}
+	tests := []struct {
+		name    string
+		args    args
+		wantErr bool
+		err     error
+	}{
+		{
+			name: "should contain two elements",
+			args: args{
+				actualData: `
+metrics:
+  - name: business-zone::projectA
+    id: YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1
+    value: 1
+  - name: system::load balancer1
+    id: c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1
+    value: 0
+  - name: system::load balancer2
+    id: WW91cl9BcHBsaWNhdGlvbk5hbWU=.1
+    value: 2
+`,
+				expectedTemplate: `
+metrics:
+{{- contains .metrics }}
+  - name: {{ notEmpty .name }}
+    id: {{ notEmpty .id }}
+    value: {{ gt .value 0 }}
+  - name: {{ notEmpty .name }}
+    id: {{ notEmpty .id }}
+    value: {{ gt .value 1 }}
+{{- end }}
+`,
+			},
+			wantErr: false,
+		},
+		{
+			name: "fail to contain two elements",
+			args: args{
+				actualData: `
+metrics:
+  - name: business-zone::projectA
+    id: YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1
+    value: 1
+  - name: system::load balancer1
+    id: c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1
+    value: 0
+  - name: system::load balancer2
+    id: WW91cl9BcHBsaWNhdGlvbk5hbWU=.1
+    value: 1
+`,
+				expectedTemplate: `
+metrics:
+{{- contains .metrics }}
+  - name: {{ notEmpty .name }}
+    id: {{ notEmpty .id }}
+    value: {{ gt .value 0 }}
+  - name: {{ notEmpty .name }}
+    id: {{ notEmpty .id }}
+    value: {{ gt .value 1 }}
+{{- end }}
+`,
+			},
+			wantErr: true,
+		},
+		{
+			name: "should contain one element",
+			args: args{
+				actualData: `
+metrics:
+  - name: business-zone::projectA
+    id: YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1
+    value: 1
+  - name: system::load balancer1
+    id: c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1
+    value: 0
+  - name: system::load balancer2
+    id: WW91cl9BcHBsaWNhdGlvbk5hbWU=.1
+    value: 2
+`,
+				expectedTemplate: `
+metrics:
+{{- contains .metrics }}
+  - name: {{ notEmpty .name }}
+    id: {{ notEmpty .id }}
+    value: {{ gt .value 1 }}
+{{- end }}
+`,
+			},
+			wantErr: false,
+		},
+		{
+			name: "fail to contain one element",
+			args: args{
+				actualData: `
+metrics:
+  - name: business-zone::projectA
+    id: YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1
+    value: 1
+  - name: system::load balancer1
+    id: c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1
+    value: 0
+  - name: system::load balancer2
+    id: WW91cl9BcHBsaWNhdGlvbk5hbWU=.1
+    value: 2
+`,
+				expectedTemplate: `
+metrics:
+{{- contains .metrics }}
+  - name: {{ notEmpty .name }}
+    id: {{ notEmpty .id }}
+    value: {{ gt .value 3 }}
+{{- end }}
+`,
+			},
+			wantErr: true,
+		},
+	}
+	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)
+			}
+		})
+	}
+}
diff --git a/test/verify/2.actual.yaml b/test/verify/2.actual.yaml
index cf3357d..bbe0eb1 100644
--- a/test/verify/2.actual.yaml
+++ b/test/verify/2.actual.yaml
@@ -6,5 +6,5 @@ metrics:
     id: c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1
     value: 0
   - name: system::load balancer2
-    id: c3lzdGVtOjpsb2FkIGJhbGFuY2VyMg==.1
-    value: 1
+    id: WW91cl9BcHBsaWNhdGlvbk5hbWU=.1
+    value: 2
diff --git a/test/verify/2.expected.yaml b/test/verify/2.expected.yaml
index a296c04..6fdf7f8 100644
--- a/test/verify/2.expected.yaml
+++ b/test/verify/2.expected.yaml
@@ -5,5 +5,5 @@ metrics:
     value: {{ gt .value 0 }}
   - name: {{ notEmpty .name }}
     id: {{ notEmpty .id }}
-    value: {{ gt .value 2 }}
+    value: {{ gt .value 1 }}
 {{- end }}
diff --git a/third-party/go/template/exec.go b/third-party/go/template/exec.go
index b6b2067..0d88f70 100644
--- a/third-party/go/template/exec.go
+++ b/third-party/go/template/exec.go
@@ -437,24 +437,27 @@ func (s *state) walkContains(dot reflect.Value, r *parse.ContainsNode) {
 		if val.Len() == 0 {
 			break
 		}
-		match := make(map[int]int) // the matched pair of indices <actual index>:<expected index>
-		actualSize := 0
+		expectedSize := 0
+		// matched stores the matched pair of indices <expected index>: <actual index>
+		matched := make(map[int]int)
 		output := make([]interface{}, val.Len())
 		for i := 0; i < val.Len(); i++ {
-			actualArr := oneIteration(reflect.ValueOf(i), val.Index(i))
-			actualSize = len(actualArr)
-			for j, actual := range actualArr {
-				value, _ := printableValue(val.Index(i))
-				if fmt.Sprint(value) == fmt.Sprint(actual) {
-					match[j] = i
-					output[i] = value // if rule matches
-				} else {
+			expectedArr := oneIteration(reflect.ValueOf(i), val.Index(i))
+			// expectedSize is the number of elements that the actual array should contain.
+			expectedSize = len(expectedArr)
+			actual, _ := printableValue(val.Index(i))
+			for j, expected := range expectedArr {
+				if fmt.Sprint(actual) == fmt.Sprint(expected) {
+					matched[j] = i
 					output[i] = actual
+				} else {
+					output[i] = expected
 				}
 			}
 		}
+
 		var marshal []byte
-		if len(match) == actualSize {
+		if len(matched) == expectedSize {
 			value, _ := printableValue(val)
 			marshal, _ = yaml.Marshal(value)
 		} else {