You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2020/11/23 01:21:18 UTC

[servicecomb-service-center] branch master updated: SCB-2094 Adjust Mongo suite test (#751)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 169cf16  SCB-2094 Adjust Mongo suite test (#751)
169cf16 is described below

commit 169cf16484529eeda6fe3a4c94311255e874a504
Author: robotLJW <79...@qq.com>
AuthorDate: Mon Nov 23 09:21:11 2020 +0800

    SCB-2094 Adjust Mongo suite test (#751)
---
 datasource/mongo/account_test.go                   | 57 ++++++++++------------
 .../mongo/bootstrap/bootstrap.go                   | 21 ++------
 datasource/mongo/mongo.go                          | 12 +++++
 datasource/mongo/{mongo.go => mongo_suite_test.go} | 51 ++++++++++---------
 scripts/ut_test_in_docker.sh                       | 20 +++++++-
 server/bootstrap/bootstrap.go                      |  3 ++
 test/test.go                                       | 22 ++++++---
 7 files changed, 106 insertions(+), 80 deletions(-)

diff --git a/datasource/mongo/account_test.go b/datasource/mongo/account_test.go
index 5f7d8a0..ced8fc2 100644
--- a/datasource/mongo/account_test.go
+++ b/datasource/mongo/account_test.go
@@ -28,14 +28,11 @@ import (
 	"testing"
 )
 
-var instance datasource.DataSource
-
 func init() {
 	config := storage.DB{
 		URI: "mongodb://localhost:27017",
 	}
 	client.NewMongoClient(config, []string{mongo.CollectionAccount})
-	instance, _ = mongo.NewDataSource(datasource.Options{})
 }
 
 func TestCreateAccount(t *testing.T) {
@@ -48,19 +45,19 @@ func TestCreateAccount(t *testing.T) {
 		CurrentPassword:     "tnuocca-tset1",
 	}
 	t.Run("create account: should be able to create account", func(t *testing.T) {
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
-		err := instance.CreateAccount(context.Background(), &account)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
+		err := datasource.Instance().CreateAccount(context.Background(), &account)
 		assert.Nil(t, err)
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
 	})
 
 	t.Run("create account: should not be able to create two same account", func(t *testing.T) {
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
-		err := instance.CreateAccount(context.Background(), &account)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
+		err := datasource.Instance().CreateAccount(context.Background(), &account)
 		assert.Nil(t, err)
-		err = instance.CreateAccount(context.Background(), &account)
+		err = datasource.Instance().CreateAccount(context.Background(), &account)
 		assert.NotNil(t, err)
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
 	})
 }
 
@@ -74,17 +71,17 @@ func TestGetAccount(t *testing.T) {
 		CurrentPassword:     "tnuocca-tset1",
 	}
 	t.Run("get account: if the account exists, it should be able to get the account", func(t *testing.T) {
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
-		err := instance.CreateAccount(context.Background(), &account)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
+		err := datasource.Instance().CreateAccount(context.Background(), &account)
 		assert.Nil(t, err)
-		result, err := instance.GetAccount(context.Background(), account.Name)
+		result, err := datasource.Instance().GetAccount(context.Background(), account.Name)
 		assert.Nil(t, err)
 		assert.Equal(t, &account, result)
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
 	})
 
 	t.Run("get account: if the account not exists, it should not be able to get the account", func(t *testing.T) {
-		_, err := instance.GetAccount(context.Background(), account.Name)
+		_, err := datasource.Instance().GetAccount(context.Background(), account.Name)
 		assert.NotNil(t, err)
 	})
 }
@@ -107,19 +104,19 @@ func TestListAccount(t *testing.T) {
 		CurrentPassword:     "tnuocca-tset2",
 	}
 	t.Run("list account: if there are multiple accounts exist, it should be able to query multiple accounts", func(t *testing.T) {
-		_ = instance.CreateAccount(context.Background(), &account1)
-		_ = instance.CreateAccount(context.Background(), &account2)
-		_, count, err := instance.ListAccount(context.Background(), "test-account")
+		_ = datasource.Instance().CreateAccount(context.Background(), &account1)
+		_ = datasource.Instance().CreateAccount(context.Background(), &account2)
+		_, count, err := datasource.Instance().ListAccount(context.Background(), "test-account")
 		assert.Equal(t, int64(2), count)
 		assert.Nil(t, err)
-		_, _ = instance.DeleteAccount(context.Background(), account1.Name)
-		_, _ = instance.DeleteAccount(context.Background(), account2.Name)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account1.Name)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account2.Name)
 	})
 }
 
 func TestDeleteAccount(t *testing.T) {
 	t.Run("delete account, if the account does not exist,it should not be deleted", func(t *testing.T) {
-		flag, _ := instance.DeleteAccount(context.Background(), "not_exist")
+		flag, _ := datasource.Instance().DeleteAccount(context.Background(), "not_exist")
 		assert.Equal(t, false, flag)
 	})
 
@@ -132,10 +129,10 @@ func TestDeleteAccount(t *testing.T) {
 			TokenExpirationTime: "2020-12-30",
 			CurrentPassword:     "tnuocca-tset1",
 		}
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
-		err := instance.CreateAccount(context.Background(), &account)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
+		err := datasource.Instance().CreateAccount(context.Background(), &account)
 		assert.Nil(t, err)
-		flag, err := instance.DeleteAccount(context.Background(), account.Name)
+		flag, err := datasource.Instance().DeleteAccount(context.Background(), account.Name)
 		assert.Equal(t, true, flag)
 		assert.Nil(t, err)
 	})
@@ -151,8 +148,8 @@ func TestUpdateAccount(t *testing.T) {
 			TokenExpirationTime: "2020-12-30",
 			CurrentPassword:     "tnuocca-tset1",
 		}
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
-		err := instance.UpdateAccount(context.Background(), account.Name, &account)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
+		err := datasource.Instance().UpdateAccount(context.Background(), account.Name, &account)
 		assert.NotNil(t, err)
 	})
 
@@ -165,13 +162,13 @@ func TestUpdateAccount(t *testing.T) {
 			TokenExpirationTime: "2020-12-30",
 			CurrentPassword:     "tnuocca-tset1",
 		}
-		_ = instance.CreateAccount(context.Background(), &account)
+		_ = datasource.Instance().CreateAccount(context.Background(), &account)
 		account.ID = "11111-22222-33333-44444"
-		err := instance.UpdateAccount(context.Background(), account.Name, &account)
+		err := datasource.Instance().UpdateAccount(context.Background(), account.Name, &account)
 		assert.Nil(t, err)
-		result, err := instance.GetAccount(context.Background(), account.Name)
+		result, err := datasource.Instance().GetAccount(context.Background(), account.Name)
 		assert.Nil(t, err)
 		assert.Equal(t, account.ID, result.ID)
-		_, _ = instance.DeleteAccount(context.Background(), account.Name)
+		_, _ = datasource.Instance().DeleteAccount(context.Background(), account.Name)
 	})
 }
diff --git a/test/test.go b/datasource/mongo/bootstrap/bootstrap.go
similarity index 53%
copy from test/test.go
copy to datasource/mongo/bootstrap/bootstrap.go
index 35e86f4..9bb2471 100644
--- a/test/test.go
+++ b/datasource/mongo/bootstrap/bootstrap.go
@@ -15,22 +15,9 @@
  * limitations under the License.
  */
 
-//Package test prepare service center required module before UT
-package test
+package bootstrap
 
-import _ "github.com/apache/servicecomb-service-center/server/init"
-import _ "github.com/apache/servicecomb-service-center/server/bootstrap"
-import (
-	"github.com/apache/servicecomb-service-center/datasource"
-	"github.com/apache/servicecomb-service-center/server/core"
-	"github.com/apache/servicecomb-service-center/server/service"
-	"github.com/go-chassis/go-archaius"
-)
+import _ "github.com/apache/servicecomb-service-center/datasource/mongo"
 
-func init() {
-	archaius.Set("registry.cache.mode", 0)
-	archaius.Set("discovery.kind", "etcd")
-	archaius.Set("registry.kind", "etcd")
-	datasource.Init(datasource.Options{PluginImplName: "etcd"})
-	core.ServiceAPI, core.InstanceAPI = service.AssembleResources()
-}
+// heartbeat
+import _ "github.com/apache/servicecomb-service-center/datasource/mongo/heartbeat/heartbeatchecker"
diff --git a/datasource/mongo/mongo.go b/datasource/mongo/mongo.go
index 8371f66..1e93c17 100644
--- a/datasource/mongo/mongo.go
+++ b/datasource/mongo/mongo.go
@@ -19,7 +19,9 @@ package mongo
 
 import (
 	"github.com/apache/servicecomb-service-center/datasource"
+	"github.com/apache/servicecomb-service-center/datasource/mongo/heartbeat"
 	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/server/config"
 )
 
 func init() {
@@ -49,5 +51,15 @@ func NewDataSource(opts datasource.Options) (datasource.DataSource, error) {
 }
 
 func (ds *DataSource) initialize() error {
+	// init heartbeat plugins
+	ds.initPlugins()
 	return nil
 }
+
+func (ds *DataSource) initPlugins() {
+	kind := config.GetString("registry.heartbeat.kind", "")
+	err := heartbeat.Init(heartbeat.Options{PluginImplName: heartbeat.ImplName(kind)})
+	if err != nil {
+		log.Fatalf(err, "heartbeat init failed")
+	}
+}
diff --git a/datasource/mongo/mongo.go b/datasource/mongo/mongo_suite_test.go
similarity index 51%
copy from datasource/mongo/mongo.go
copy to datasource/mongo/mongo_suite_test.go
index 8371f66..015c0f0 100644
--- a/datasource/mongo/mongo.go
+++ b/datasource/mongo/mongo_suite_test.go
@@ -15,39 +15,38 @@
  * limitations under the License.
  */
 
-package mongo
+package mongo_test
+
+// initialize
+import _ "github.com/apache/servicecomb-service-center/test"
 
 import (
+	"context"
 	"github.com/apache/servicecomb-service-center/datasource"
-	"github.com/apache/servicecomb-service-center/pkg/log"
+	"github.com/apache/servicecomb-service-center/pkg/util"
+	. "github.com/onsi/ginkgo"
+	"github.com/onsi/ginkgo/reporters"
+	. "github.com/onsi/gomega"
+	"testing"
+	"time"
 )
 
-func init() {
-	datasource.Install("mongo", NewDataSource)
-}
-
-type DataSource struct {
-	// SchemaEditable determines whether schema modification is allowed for
-	SchemaEditable bool
-	// TTL options
-	ttlFromEnv int64
-}
+var timeLimit = 2 * time.Second
 
-func NewDataSource(opts datasource.Options) (datasource.DataSource, error) {
-	// TODO: construct a reasonable DataSource instance
-	log.Warnf("dependency data source enable etcd mode")
+var _ = BeforeSuite(func() {
+	//clear service created in last test
+	time.Sleep(timeLimit)
+	_ = datasource.Instance().ClearNoInstanceServices(context.Background(), timeLimit)
+})
 
-	inst := &DataSource{
-		SchemaEditable: opts.SchemaEditable,
-		ttlFromEnv:     opts.InstanceTTL,
-	}
-	// TODO: deal with exception
-	if err := inst.initialize(); err != nil {
-		return nil, err
-	}
-	return inst, nil
+func getContext() context.Context {
+	return util.SetContext(
+		util.SetDomainProject(context.Background(), "default", "default"),
+		util.CtxNocache, "1")
 }
 
-func (ds *DataSource) initialize() error {
-	return nil
+func TestMongo(t *testing.T) {
+	RegisterFailHandler(Fail)
+	junitReporter := reporters.NewJUnitReporter("mongo.junit.xml")
+	RunSpecsWithDefaultAndCustomReporters(t, "mongo Suite", []Reporter{junitReporter})
 }
diff --git a/scripts/ut_test_in_docker.sh b/scripts/ut_test_in_docker.sh
index b43b590..5d73a59 100755
--- a/scripts/ut_test_in_docker.sh
+++ b/scripts/ut_test_in_docker.sh
@@ -49,7 +49,8 @@ echo "${green}mongodb is running......${reset}"
 echo "${green}Preparing the env for UT....${reset}"
 ./scripts/prepare_env_ut.sh
 
-[ $? == 0 ] && ut_for_dir datasource
+export TEST_MODE=etcd
+[ $? == 0 ] && ut_for_dir datasource/etcd
 [ $? == 0 ] && ut_for_dir pkg
 [ $? == 0 ] && ut_for_dir server
 [ $? == 0 ] && ut_for_dir scctl
@@ -64,6 +65,23 @@ else
 	exit 1
 fi
 
+export TEST_MODE=mongo
+[ $? == 0 ] && ut_for_dir datasource/mongo
+# 由於mongo接口未全部實現先注釋
+#[ $? == 0 ] && ut_for_dir pkg
+#[ $? == 0 ] && ut_for_dir server
+#[ $? == 0 ] && ut_for_dir scctl
+#[ $? == 0 ] && ut_for_dir syncer
+ret=$?
+
+if [ ${ret} == 0 ]; then
+	echo "${green}All the unit test passed..${reset}"
+	echo "${green}Coverage is created in the file ./coverage.txt${reset}"
+else
+	echo "${red}Some or all the unit test failed..please check the logs for more details.${reset}"
+	exit 1
+fi
+
 echo "${green}Service-Center finished${reset}"
 
 echo "${green}Cleaning up the etcd docker container${reset}"
diff --git a/server/bootstrap/bootstrap.go b/server/bootstrap/bootstrap.go
index c98e43a..3828551 100644
--- a/server/bootstrap/bootstrap.go
+++ b/server/bootstrap/bootstrap.go
@@ -19,6 +19,9 @@ package bootstrap
 //etcd
 import _ "github.com/apache/servicecomb-service-center/datasource/etcd/bootstrap"
 
+//mongo
+import _ "github.com/apache/servicecomb-service-center/datasource/mongo/bootstrap"
+
 //rest v3 api
 import _ "github.com/apache/servicecomb-service-center/server/rest/controller/v3"
 
diff --git a/test/test.go b/test/test.go
index 35e86f4..0110c1c 100644
--- a/test/test.go
+++ b/test/test.go
@@ -18,19 +18,29 @@
 //Package test prepare service center required module before UT
 package test
 
-import _ "github.com/apache/servicecomb-service-center/server/init"
-import _ "github.com/apache/servicecomb-service-center/server/bootstrap"
 import (
 	"github.com/apache/servicecomb-service-center/datasource"
+	_ "github.com/apache/servicecomb-service-center/server/init"
+)
+import _ "github.com/apache/servicecomb-service-center/server/bootstrap"
+import (
 	"github.com/apache/servicecomb-service-center/server/core"
 	"github.com/apache/servicecomb-service-center/server/service"
 	"github.com/go-chassis/go-archaius"
 )
 
 func init() {
-	archaius.Set("registry.cache.mode", 0)
-	archaius.Set("discovery.kind", "etcd")
-	archaius.Set("registry.kind", "etcd")
-	datasource.Init(datasource.Options{PluginImplName: "etcd"})
+	t := archaius.Get("TEST_MODE")
+	if t == nil {
+		t = "etcd"
+	}
+	if t == "etcd" {
+		archaius.Set("registry.cache.mode", 0)
+		archaius.Set("discovery.kind", "etcd")
+		archaius.Set("registry.kind", "etcd")
+	} else {
+		archaius.Set("registry.heartbeat.kind", "heartbeatchecker")
+	}
+	datasource.Init(datasource.Options{PluginImplName: datasource.ImplName(t.(string))})
 	core.ServiceAPI, core.InstanceAPI = service.AssembleResources()
 }