You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2021/08/02 03:28:55 UTC

[servicecomb-kie] branch master updated: unified unit test (#191)

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

littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git


The following commit(s) were added to refs/heads/master by this push:
     new d8a6eb7  unified unit test (#191)
d8a6eb7 is described below

commit d8a6eb77f6cc2f8134f9897e4cf4d38ca0a05388
Author: Shawn <xi...@gmail.com>
AuthorDate: Mon Aug 2 11:28:49 2021 +0800

    unified unit test (#191)
---
 .github/workflows/build.yml                        | 15 ++++
 .github/workflows/golangci-lint.yml                | 24 +++++-
 .github/workflows/mongo_storage.yml                | 23 ++++++
 .github/workflows/static_check.yml                 | 48 ------------
 server/datasource/dao.go                           |  2 +-
 .../service_test.go => history_dao_test.go}        | 47 ++++++------
 .../kv/kv_service_test.go => kv_dao_test.go}       | 87 ++++++++++------------
 server/datasource/mongo/session/session.go         |  7 +-
 .../revision_test.go => revision_dao_test.go}      | 19 +----
 server/handler/track_handler.go                    |  3 +-
 server/resource/v1/kv_resource_test.go             |  4 -
 test/README.md                                     |  6 ++
 test/init.go                                       | 15 +++-
 13 files changed, 150 insertions(+), 150 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
new file mode 100644
index 0000000..c881b52
--- /dev/null
+++ b/.github/workflows/build.yml
@@ -0,0 +1,15 @@
+name: Merge check
+on: [push, pull_request]
+jobs:
+  build:
+    runs-on: ubuntu-latest
+    steps:
+    - name: Set up Go
+      uses: actions/setup-go@v1
+      with:
+        go-version: 1.16
+      id: go
+    - name: Check out code into the Go module directory
+      uses: actions/checkout@v1
+    - name: Build
+      run: go build -v github.com/apache/servicecomb-kie/cmd/kieserver
diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml
index 080f489..63aef9d 100644
--- a/.github/workflows/golangci-lint.yml
+++ b/.github/workflows/golangci-lint.yml
@@ -16,4 +16,26 @@ jobs:
         uses: golangci/golangci-lint-action@v2
         with:
           version: v1.29
-          args: --skip-dirs=examples,test --skip-files=.*_test.go$
\ No newline at end of file
+          args: --skip-dirs=examples,test --skip-files=.*_test.go$
+  static-checks:
+    runs-on: ubuntu-latest
+    env:
+      GO111MODULE: on
+    steps:
+      - name: Checkout Source
+        uses: actions/checkout@v2
+      - name: Fmt
+        run: |
+          bash scripts/ci/formatChecker.sh
+      - name: Misspell
+        run: |
+          go get -u github.com/client9/misspell/cmd/misspell
+          bash scripts/ci/formatChecker.sh
+      - name: Lint
+        run: |
+          go get -u golang.org/x/lint/golint
+          bash scripts/ci/goLintChecker.sh
+      - name: Cyclo
+        run: |
+          go get github.com/fzipp/gocyclo
+          bash scripts/ci/goCycloChecker.sh
\ No newline at end of file
diff --git a/.github/workflows/mongo_storage.yml b/.github/workflows/mongo_storage.yml
new file mode 100644
index 0000000..a6a3bbc
--- /dev/null
+++ b/.github/workflows/mongo_storage.yml
@@ -0,0 +1,23 @@
+name: Merge check
+on: [push, pull_request]
+jobs:
+  mongo-storage:
+    runs-on: ubuntu-latest
+    steps:
+    - name: Set up Go
+      uses: actions/setup-go@v1
+      with:
+        go-version: 1.16
+      id: go
+    - name: Check out code into the Go module directory
+      uses: actions/checkout@v1
+    - name: UT
+      run: |
+        cd build
+        bash build_docker.sh
+        cd ../
+        sudo docker-compose -f ./deployments/docker/docker-compose.yaml up -d
+        sleep 20
+        export TEST_DB_KIND=mongo
+        export TEST_DB_URI=mongodb://kie:123@127.0.0.1:27017/kie
+        go test $(go list ./... |  grep -v third_party | grep -v examples) -cover -covermode atomic -coverprofile coverage.out
\ No newline at end of file
diff --git a/.github/workflows/static_check.yml b/.github/workflows/static_check.yml
deleted file mode 100644
index 1b5c360..0000000
--- a/.github/workflows/static_check.yml
+++ /dev/null
@@ -1,48 +0,0 @@
-name: Merge check
-on: [push, pull_request]
-jobs:
-  build:
-    runs-on: ubuntu-latest
-    steps:
-    - name: Set up Go 1.13
-      uses: actions/setup-go@v1
-      with:
-        go-version: 1.13
-      id: go
-
-    - name: Check out code into the Go module directory
-      uses: actions/checkout@v1
-
-    - name: Build
-      run: go build -v github.com/apache/servicecomb-kie/cmd/kieserver
-
-    - name: UT
-      run: |
-        cd build
-        bash build_docker.sh
-        cd ../
-        sudo docker-compose -f ./deployments/docker/docker-compose.yaml up -d
-        sleep 30
-        go test $(go list ./... |  grep -v third_party | grep -v examples) -cover -covermode atomic -coverprofile coverage.out
-  tests:
-    runs-on: ubuntu-latest
-    env:
-      GO111MODULE: on
-    steps:
-      - name: Checkout Source
-        uses: actions/checkout@v2
-      - name: Fmt
-        run: |
-          bash scripts/ci/formatChecker.sh
-      - name: Misspell
-        run: |
-          go get -u github.com/client9/misspell/cmd/misspell
-          bash scripts/ci/formatChecker.sh
-      - name: Lint
-        run: |
-          go get -u golang.org/x/lint/golint
-          bash scripts/ci/goLintChecker.sh
-      - name: Cyclo
-        run: |
-          go get github.com/fzipp/gocyclo
-          bash scripts/ci/goCycloChecker.sh
\ No newline at end of file
diff --git a/server/datasource/dao.go b/server/datasource/dao.go
index d4fab83..e0e238a 100644
--- a/server/datasource/dao.go
+++ b/server/datasource/dao.go
@@ -87,7 +87,7 @@ type HistoryDao interface {
 	GetHistory(ctx context.Context, keyID string, options ...FindOption) (*model.KVResponse, error)
 }
 
-//TrackDao provide api of TrackDao entity
+//TrackDao provide api of Track entity
 type TrackDao interface {
 	CreateOrUpdate(ctx context.Context, detail *model.PollingDetail) (*model.PollingDetail, error)
 	GetPollingDetail(ctx context.Context, detail *model.PollingDetail) ([]*model.PollingDetail, error)
diff --git a/server/datasource/mongo/history/service_test.go b/server/datasource/history_dao_test.go
similarity index 62%
rename from server/datasource/mongo/history/service_test.go
rename to server/datasource/history_dao_test.go
index 280ef52..b8f4f1b 100644
--- a/server/datasource/mongo/history/service_test.go
+++ b/server/datasource/history_dao_test.go
@@ -15,43 +15,38 @@
  * limitations under the License.
  */
 
-package history_test
+package datasource_test
 
 import (
 	"context"
-	"time"
+	"testing"
 
+	common2 "github.com/apache/servicecomb-kie/pkg/common"
+	"github.com/apache/servicecomb-kie/pkg/model"
 	"github.com/apache/servicecomb-kie/server/datasource"
 
-	"testing"
-
-	"github.com/apache/servicecomb-kie/server/datasource/mongo/session"
 	_ "github.com/apache/servicecomb-kie/test"
 	"github.com/stretchr/testify/assert"
-	"go.mongodb.org/mongo-driver/bson"
 )
 
-func init() {
-	session.Init(&datasource.Config{
-		URI:     "mongodb://kie:123@127.0.0.1:27017/kie",
-		Timeout: 10 * time.Second,
+func TestGetHistory(t *testing.T) {
+	kv, err := kvdao.Create(context.TODO(), &model.KVDoc{
+		Key:    "history",
+		Value:  "2s",
+		Status: common2.StatusEnabled,
+		Labels: map[string]string{
+			"app":     "mall",
+			"service": "cart",
+		},
+		Domain:  domain,
+		Project: project,
 	})
-}
-
-func TestAddHistory(t *testing.T) {
-	ctx := context.Background()
-	coll := session.GetDB().Collection("label_revision")
-	cur, err := coll.Find(
-		context.Background(),
-		bson.M{
-			"label_format": "5dbc079183ff1a09242376e7",
-			"data.key":     "lb",
-		})
 	assert.NoError(t, err)
-	for cur.Next(ctx) {
-		var elem interface{}
-		err := cur.Decode(&elem)
+	assert.NotEmpty(t, kv.ID)
+	t.Run("after create kv, should has history", func(t *testing.T) {
+		h, err := datasource.GetBroker().GetHistoryDao().GetHistory(context.TODO(), kv.ID)
 		assert.NoError(t, err)
-		t.Log(elem)
-	}
+		assert.GreaterOrEqual(t, h.Total, 1)
+	})
+
 }
diff --git a/server/datasource/mongo/kv/kv_service_test.go b/server/datasource/kv_dao_test.go
similarity index 67%
rename from server/datasource/mongo/kv/kv_service_test.go
rename to server/datasource/kv_dao_test.go
index 7ad9c71..ec9d135 100644
--- a/server/datasource/mongo/kv/kv_service_test.go
+++ b/server/datasource/kv_dao_test.go
@@ -15,11 +15,9 @@
  * limitations under the License.
  */
 
-package kv_test
+package datasource_test
 
 import (
-	"time"
-
 	"github.com/apache/servicecomb-kie/server/datasource"
 	_ "github.com/apache/servicecomb-kie/test"
 
@@ -28,14 +26,15 @@ import (
 
 	common2 "github.com/apache/servicecomb-kie/pkg/common"
 	"github.com/apache/servicecomb-kie/pkg/model"
-	"github.com/apache/servicecomb-kie/server/datasource/mongo/kv"
-	"github.com/apache/servicecomb-kie/server/datasource/mongo/session"
 	"github.com/go-chassis/openlog"
 	log "github.com/go-chassis/seclog"
 	"github.com/stretchr/testify/assert"
 )
 
+var project = "kv-test"
+var domain = "default"
 var id string
+var kvdao = datasource.GetBroker().GetKVDao()
 
 func init() {
 	log.Init(log.Config{
@@ -48,15 +47,8 @@ func init() {
 }
 
 func TestService_CreateOrUpdate(t *testing.T) {
-	var err error
-	err = session.Init(&datasource.Config{
-		URI:     "mongodb://kie:123@127.0.0.1:27017/kie",
-		Timeout: 10 * time.Second,
-	})
-	assert.NoError(t, err)
-	kvsvc := &kv.Service{}
 	t.Run("put kv timeout,with labels app and service", func(t *testing.T) {
-		kv, err := kvsvc.Create(context.TODO(), &model.KVDoc{
+		kv, err := kvdao.Create(context.TODO(), &model.KVDoc{
 			Key:    "timeout",
 			Value:  "2s",
 			Status: common2.StatusEnabled,
@@ -64,14 +56,14 @@ func TestService_CreateOrUpdate(t *testing.T) {
 				"app":     "mall",
 				"service": "cart",
 			},
-			Domain:  "default",
-			Project: "kv-test",
+			Domain:  domain,
+			Project: project,
 		})
 		assert.NoError(t, err)
 		assert.NotEmpty(t, kv.ID)
 	})
 	t.Run("put kv timeout,with labels app, service and version", func(t *testing.T) {
-		kv, err := kvsvc.Create(context.TODO(), &model.KVDoc{
+		kv, err := kvdao.Create(context.TODO(), &model.KVDoc{
 			Key:    "timeout",
 			Value:  "2s",
 			Status: common2.StatusEnabled,
@@ -80,50 +72,51 @@ func TestService_CreateOrUpdate(t *testing.T) {
 				"service": "cart",
 				"version": "1.0.0",
 			},
-			Domain:  "default",
-			Project: "kv-test",
+			Domain:  domain,
+			Project: project,
+		})
+		oid, err := kvdao.Get(context.TODO(), &model.GetKVRequest{
+			Domain:  domain,
+			Project: project,
+			ID:      kv.ID,
 		})
-		oid, err := kvsvc.Exist(context.TODO(), "default", "timeout", "kv-test", datasource.WithLabels(map[string]string{
-			"app":     "mall",
-			"service": "cart",
-			"version": "1.0.0",
-		}))
 		assert.NoError(t, err)
 		assert.NotEmpty(t, kv.ID)
 		assert.NoError(t, err)
 		assert.NotEmpty(t, oid)
 	})
 	t.Run("put kv timeout,with labels app,and update value", func(t *testing.T) {
-		beforeKV, err := kvsvc.Create(context.Background(), &model.KVDoc{
+		beforeKV, err := kvdao.Create(context.Background(), &model.KVDoc{
 			Key:    "timeout",
 			Value:  "1s",
 			Status: common2.StatusEnabled,
 			Labels: map[string]string{
 				"app": "mall",
 			},
-			Domain:  "default",
-			Project: "kv-test",
+			Domain:  domain,
+			Project: project,
 		})
 		assert.NoError(t, err)
-		afterKV, err := kvsvc.Update(context.Background(), &model.UpdateKVRequest{
+		afterKV, err := kvdao.Update(context.Background(), &model.UpdateKVRequest{
 			ID:      beforeKV.ID,
 			Value:   "3s",
-			Domain:  "default",
-			Project: "kv-test",
+			Domain:  domain,
+			Project: project,
 		})
 		assert.Equal(t, "3s", afterKV.Value)
-		savedKV, err := kvsvc.Exist(context.Background(), "default", "timeout", "kv-test", datasource.WithLabels(map[string]string{
-			"app": "mall",
-		}))
+		savedKV, err := kvdao.Get(context.TODO(), &model.GetKVRequest{
+			Domain:  domain,
+			Project: project,
+			ID:      afterKV.ID,
+		})
 		assert.NoError(t, err)
 		assert.Equal(t, afterKV.Value, savedKV.Value)
 	})
 }
 
 func TestService_Create(t *testing.T) {
-	kvsvc := &kv.Service{}
 	t.Run("create kv timeout,with labels app and service", func(t *testing.T) {
-		result, err := kvsvc.Create(context.TODO(), &model.KVDoc{
+		result, err := kvdao.Create(context.TODO(), &model.KVDoc{
 			Key:    "timeout",
 			Value:  "2s",
 			Status: common2.StatusEnabled,
@@ -131,8 +124,8 @@ func TestService_Create(t *testing.T) {
 				"app":     "mall",
 				"service": "utCart",
 			},
-			Domain:  "default",
-			Project: "kv-test",
+			Domain:  domain,
+			Project: project,
 		})
 		assert.NoError(t, err)
 		assert.NotEmpty(t, result.ID)
@@ -140,7 +133,7 @@ func TestService_Create(t *testing.T) {
 		id = result.ID
 	})
 	t.Run("create the same kv", func(t *testing.T) {
-		_, err := kvsvc.Create(context.TODO(), &model.KVDoc{
+		_, err := kvdao.Create(context.TODO(), &model.KVDoc{
 			Key:    "timeout",
 			Value:  "2s",
 			Status: common2.StatusEnabled,
@@ -148,29 +141,30 @@ func TestService_Create(t *testing.T) {
 				"app":     "mall",
 				"service": "utCart",
 			},
-			Domain:  "default",
-			Project: "kv-test",
+			Domain:  domain,
+			Project: project,
 		})
 		assert.EqualError(t, err, datasource.ErrKVAlreadyExists.Error())
 	})
 	t.Run("list the kv", func(t *testing.T) {
-		res, err := kvsvc.List(context.TODO(), "default", "kv-test", datasource.WithKey("wildcard(time*1)"))
+		res, err := kvdao.List(context.TODO(), domain, project,
+			datasource.WithKey("wildcard(time*1)"))
 		assert.NoError(t, err)
 		assert.Equal(t, 0, len(res.Data))
-		res, err = kvsvc.List(context.TODO(), "default", "kv-test", datasource.WithKey("wildcard(time*t)"))
+		res, err = kvdao.List(context.TODO(), domain, project,
+			datasource.WithKey("wildcard(time*t)"))
 		assert.NoError(t, err)
 		assert.NotEqual(t, 0, len(res.Data))
 	})
 }
 
 func TestService_Update(t *testing.T) {
-	kvsvc := &kv.Service{}
 	t.Run("update kv by kvID", func(t *testing.T) {
-		result, err := kvsvc.Update(context.TODO(), &model.UpdateKVRequest{
+		result, err := kvdao.Update(context.TODO(), &model.UpdateKVRequest{
 			ID:      id,
 			Value:   "3s",
-			Domain:  "default",
-			Project: "kv-test",
+			Domain:  domain,
+			Project: project,
 		})
 		assert.NoError(t, err)
 		assert.Equal(t, "3s", result.Value)
@@ -178,9 +172,8 @@ func TestService_Update(t *testing.T) {
 }
 
 func TestService_Delete(t *testing.T) {
-	kvsvc := &kv.Service{}
 	t.Run("delete kv by kvID", func(t *testing.T) {
-		_, err := kvsvc.FindOneAndDelete(context.TODO(), id, "default", "kv-test")
+		_, err := kvdao.FindOneAndDelete(context.TODO(), id, domain, project)
 		assert.NoError(t, err)
 	})
 }
diff --git a/server/datasource/mongo/session/session.go b/server/datasource/mongo/session/session.go
index b0e4bf9..d5ff413 100644
--- a/server/datasource/mongo/session/session.go
+++ b/server/datasource/mongo/session/session.go
@@ -62,10 +62,9 @@ var (
 
 	ErrKeyMustNotEmpty = errors.New("must supply key if you want to get exact one result")
 
-	ErrIDIsNil         = errors.New("id is empty")
-	ErrKeyIsNil        = errors.New("key must not be empty")
-	ErrRootCAMissing   = errors.New("rootCAFile is empty in config file")
-	ErrKVAlreadyExists = errors.New("kv already exists")
+	ErrIDIsNil       = errors.New("id is empty")
+	ErrKeyIsNil      = errors.New("key must not be empty")
+	ErrRootCAMissing = errors.New("rootCAFile is empty in config file")
 
 	ErrViewCreation = errors.New("can not create view")
 	ErrViewUpdate   = errors.New("can not update view")
diff --git a/server/datasource/mongo/counter/revision_test.go b/server/datasource/revision_dao_test.go
similarity index 68%
rename from server/datasource/mongo/counter/revision_test.go
rename to server/datasource/revision_dao_test.go
index 7ff2c78..e4e38d3 100644
--- a/server/datasource/mongo/counter/revision_test.go
+++ b/server/datasource/revision_dao_test.go
@@ -15,33 +15,20 @@
  * limitations under the License.
  */
 
-package counter_test
+package datasource_test
 
 import (
-	"time"
-
 	"github.com/apache/servicecomb-kie/server/datasource"
 	_ "github.com/apache/servicecomb-kie/test"
 
 	"context"
 	"testing"
 
-	"github.com/apache/servicecomb-kie/server/datasource/mongo/counter"
-	"github.com/apache/servicecomb-kie/server/datasource/mongo/session"
 	"github.com/stretchr/testify/assert"
 )
 
 func TestIncreaseAndGetRevision(t *testing.T) {
-	var err error
-	err = session.Init(&datasource.Config{
-		URI:     "mongodb://kie:123@127.0.0.1:27017/kie",
-		Timeout: 10 * time.Second,
-	})
+	dao := datasource.GetBroker().GetRevisionDao()
+	_, err := dao.GetRevision(context.TODO(), domain)
 	assert.NoError(t, err)
-	s := &counter.Service{}
-	n, _ := s.GetRevision(context.TODO(), "default")
-	t.Log(n)
-
-	next, _ := counter.ApplyRevision(context.TODO(), "default")
-	assert.Equal(t, n+1, next)
 }
diff --git a/server/handler/track_handler.go b/server/handler/track_handler.go
index e4fd5ae..364a76b 100644
--- a/server/handler/track_handler.go
+++ b/server/handler/track_handler.go
@@ -18,11 +18,12 @@
 package handler
 
 import (
-	v1 "github.com/apache/servicecomb-kie/server/resource/v1"
 	"net/http"
 	"strings"
 	"time"
 
+	v1 "github.com/apache/servicecomb-kie/server/resource/v1"
+
 	"github.com/apache/servicecomb-kie/pkg/common"
 	"github.com/apache/servicecomb-kie/pkg/iputil"
 	"github.com/apache/servicecomb-kie/pkg/model"
diff --git a/server/resource/v1/kv_resource_test.go b/server/resource/v1/kv_resource_test.go
index 2562ee3..7e9a32a 100644
--- a/server/resource/v1/kv_resource_test.go
+++ b/server/resource/v1/kv_resource_test.go
@@ -70,10 +70,6 @@ func init() {
 	if err != nil {
 		panic(err)
 	}
-	err = archaius.Init(archaius.WithENVSource(), archaius.WithMemorySource())
-	if err != nil {
-		panic(err)
-	}
 }
 func TestKVResource_Post(t *testing.T) {
 	t.Run("post kv, label is invalid, should return err", func(t *testing.T) {
diff --git a/test/README.md b/test/README.md
new file mode 100644
index 0000000..f028e4a
--- /dev/null
+++ b/test/README.md
@@ -0,0 +1,6 @@
+go to root folder of project and execute
+```shell
+export TEST_DB_KIND=mongo
+export TEST_DB_URI=mongodb://kie:123@127.0.0.1:27017/kie
+go test ./...
+```
diff --git a/test/init.go b/test/init.go
index afe24ff..4eaad8c 100644
--- a/test/init.go
+++ b/test/init.go
@@ -28,14 +28,25 @@ import (
 	_ "github.com/apache/servicecomb-kie/server/datasource/mongo"
 )
 
+var uri string
+var kind string
+
 func init() {
+	err := archaius.Init(archaius.WithENVSource(),
+		archaius.WithMemorySource())
+	if err != nil {
+		panic(err)
+	}
+	kind := archaius.GetString("TEST_DB_KIND", "mongo")
+	uri := archaius.GetString("TEST_DB_URI", "mongodb://kie:123@127.0.0.1:27017/kie")
 	archaius.Init(archaius.WithMemorySource())
 	archaius.Set("servicecomb.cipher.plugin", "default")
 	cipher.Init()
 	validator.Init()
-	err := datasource.Init(config.DB{
-		URI:     "mongodb://kie:123@127.0.0.1:27017/kie",
+	err = datasource.Init(config.DB{
+		URI:     uri,
 		Timeout: "10s",
+		Kind:    kind,
 	})
 	if err != nil {
 		panic(err)