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 2021/02/02 06:10:31 UTC

[servicecomb-service-center] branch master updated: [SCB-2094] move role_test to datasource (#842)

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 081088d  [SCB-2094] move role_test to datasource (#842)
081088d is described below

commit 081088d55e1a25a6a01f85f61ee65cd3692a1a92
Author: panqian <pq...@163.com>
AuthorDate: Tue Feb 2 14:10:22 2021 +0800

    [SCB-2094] move role_test to datasource (#842)
---
 datasource/datasource_test.go      | 24 ++++++++++
 datasource/manager_test.go         | 74 -------------------------------
 datasource/mongo/ms_test.go        |  8 ----
 datasource/mongo/role_test.go      | 90 --------------------------------------
 datasource/mongo/system_test.go    |  5 ---
 datasource/{etcd => }/role_test.go | 11 ++---
 scripts/ut_file.sh                 | 30 +++++++++++++
 scripts/ut_test_in_docker.sh       | 10 ++++-
 8 files changed, 67 insertions(+), 185 deletions(-)

diff --git a/datasource/datasource_test.go b/datasource/datasource_test.go
new file mode 100644
index 0000000..f5fcef1
--- /dev/null
+++ b/datasource/datasource_test.go
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the 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.
+ * The 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 datasource_test
+
+import _ "github.com/apache/servicecomb-service-center/test"
+
+// Run The UT-TEST By Set The TEST_MODE ENV
+// Mongo: TEST_MODE=mongo.
+// ETCD: TEST_MODE=etcd.
diff --git a/datasource/manager_test.go b/datasource/manager_test.go
deleted file mode 100644
index 5c1f041..0000000
--- a/datasource/manager_test.go
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Licensed to the 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.
- * The 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 datasource_test
-
-import (
-	"strconv"
-	"strings"
-	"testing"
-
-	"github.com/apache/servicecomb-service-center/datasource"
-	"github.com/apache/servicecomb-service-center/datasource/etcd"
-	"github.com/apache/servicecomb-service-center/pkg/log"
-	_ "github.com/apache/servicecomb-service-center/test"
-	"github.com/go-chassis/go-archaius"
-	"github.com/stretchr/testify/assert"
-)
-
-func TestInit(t *testing.T) {
-	_ = archaius.Init(archaius.WithMemorySource())
-	_ = archaius.Set("servicecomb.datasource.name", "etcd")
-	_ = archaius.Set("servicecomb.instance.TTL", 1000)
-	_ = archaius.Set("servicecomb.instance.editable", "true")
-	t.Run("init microservice data source plugin, should not pass", func(t *testing.T) {
-		schemaEditableConfig := strings.ToLower(archaius.GetString("servicecomb.schema.editable", "true"))
-		schemaEditable := strings.Compare(schemaEditableConfig, "true") == 0
-		pluginName := datasource.ImplName("unknown")
-		TTL, err := strconv.ParseInt(archaius.GetString("servicecomb.instance.TTL", "1000"), 10, 0)
-		if err != nil {
-			log.Error("microservice etcd implement failed for INSTANCE_TTL config: %v", err)
-		}
-
-		err = datasource.Init(datasource.Options{
-			PluginImplName: pluginName,
-			InstanceTTL:    TTL,
-			SchemaEditable: schemaEditable,
-		})
-		assert.Error(t, err)
-	})
-	t.Run("install and init microservice data source plugin, should pass", func(t *testing.T) {
-		datasource.Install("etcd", func(opts datasource.Options) (datasource.DataSource, error) {
-			return etcd.NewDataSource(opts)
-		})
-
-		schemaEditableConfig := strings.ToLower(archaius.GetString("servicecomb.schema.editable", "true"))
-		schemaEditable := strings.Compare(schemaEditableConfig, "true") == 0
-		pluginName := datasource.ImplName(archaius.GetString("servicecomb.datasource.name", "etcd"))
-		TTL, err := strconv.ParseInt(archaius.GetString("servicecomb.instance.TTL", "1000"), 10, 0)
-		if err != nil {
-			log.Error("microservice etcd implement failed for INSTANCE_TTL config: %v", err)
-		}
-
-		err = datasource.Init(datasource.Options{
-			PluginImplName: pluginName,
-			InstanceTTL:    TTL,
-			SchemaEditable: schemaEditable,
-		})
-		assert.NoError(t, err)
-	})
-}
diff --git a/datasource/mongo/ms_test.go b/datasource/mongo/ms_test.go
index b296c7d..210a4a5 100644
--- a/datasource/mongo/ms_test.go
+++ b/datasource/mongo/ms_test.go
@@ -25,7 +25,6 @@ import (
 	"time"
 
 	pb "github.com/go-chassis/cari/discovery"
-	"github.com/go-chassis/go-chassis/v2/storage"
 	"github.com/stretchr/testify/assert"
 	"go.mongodb.org/mongo-driver/bson"
 
@@ -40,13 +39,6 @@ import (
 	"github.com/apache/servicecomb-service-center/server/service"
 )
 
-func init() {
-	config := storage.Options{
-		URI: "mongodb://localhost:27017",
-	}
-	client.NewMongoClient(config)
-}
-
 func TestService_Register(t *testing.T) {
 	t.Run("Register service after init & install, should pass", func(t *testing.T) {
 		size := quota.DefaultSchemaQuota + 1
diff --git a/datasource/mongo/role_test.go b/datasource/mongo/role_test.go
deleted file mode 100644
index c77940e..0000000
--- a/datasource/mongo/role_test.go
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Licensed to the 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.
- * The 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 mongo_test
-
-import (
-	"context"
-	"testing"
-
-	"github.com/astaxie/beego"
-	"github.com/stretchr/testify/assert"
-
-	"github.com/apache/servicecomb-service-center/datasource"
-	"github.com/apache/servicecomb-service-center/pkg/rbacframe"
-)
-
-var (
-	r1 = rbacframe.Role{
-		ID:    "11111-22222-33333",
-		Name:  "test-role1",
-		Perms: nil,
-	}
-
-	r2 = rbacframe.Role{
-		ID:    "11111-22222-33333-44444",
-		Name:  "test-role2",
-		Perms: nil,
-	}
-)
-
-func init() {
-	beego.AppConfig.Set("registry_plugin", "mongo")
-}
-
-func TestRole(t *testing.T) {
-	t.Run("create role should success", func(t *testing.T) {
-		err := datasource.Instance().CreateRole(context.Background(), &r1)
-		assert.NoError(t, err)
-		r, err := datasource.Instance().GetRole(context.Background(), "test-role1")
-		assert.NoError(t, err)
-		assert.Equal(t, r1, *r)
-	})
-	t.Run("role should exist", func(t *testing.T) {
-		exist, err := datasource.Instance().RoleExist(context.Background(), "test-role1")
-		assert.NoError(t, err)
-		assert.True(t, exist)
-	})
-
-	t.Run("repeated create role should failed", func(t *testing.T) {
-		err := datasource.Instance().CreateRole(context.Background(), &r1)
-		assert.Error(t, err)
-	})
-
-	t.Run("update role should success", func(t *testing.T) {
-		r1.ID = "11111-22222-33333-4"
-		err := datasource.Instance().UpdateRole(context.Background(), "test-role1", &r1)
-		assert.NoError(t, err)
-	})
-	t.Run("add new role should success", func(t *testing.T) {
-		err := datasource.Instance().CreateRole(context.Background(), &r2)
-		assert.NoError(t, err)
-		_, n, err := datasource.Instance().ListRole(context.Background())
-		assert.NoError(t, err)
-		assert.Equal(t, int64(2), n)
-	})
-
-	t.Run("delete role should success", func(t *testing.T) {
-		_, err := datasource.Instance().DeleteRole(context.Background(), "test-role1")
-		assert.NoError(t, err)
-		_, err = datasource.Instance().DeleteRole(context.Background(), "test-role2")
-		assert.NoError(t, err)
-		_, n, err := datasource.Instance().ListRole(context.Background())
-		assert.NoError(t, err)
-		assert.Equal(t, int64(0), n)
-	})
-}
diff --git a/datasource/mongo/system_test.go b/datasource/mongo/system_test.go
index ea377ec..887c933 100644
--- a/datasource/mongo/system_test.go
+++ b/datasource/mongo/system_test.go
@@ -25,16 +25,11 @@ import (
 	"github.com/apache/servicecomb-service-center/datasource/mongo/client"
 	"github.com/apache/servicecomb-service-center/datasource/mongo/sd"
 	pb "github.com/go-chassis/cari/discovery"
-	"github.com/go-chassis/go-chassis/v2/storage"
 	"github.com/stretchr/testify/assert"
 	"go.mongodb.org/mongo-driver/bson"
 )
 
 func init() {
-	config := storage.Options{
-		URI: "mongodb://localhost:27017",
-	}
-	client.NewMongoClient(config)
 	// clean the mongodb
 	client.GetMongoClient().Delete(getContext(), mongo.CollectionInstance, bson.M{})
 	client.GetMongoClient().Delete(getContext(), mongo.CollectionService, bson.M{})
diff --git a/datasource/etcd/role_test.go b/datasource/role_test.go
similarity index 96%
rename from datasource/etcd/role_test.go
rename to datasource/role_test.go
index 550dc7b..dc81a8a 100644
--- a/datasource/etcd/role_test.go
+++ b/datasource/role_test.go
@@ -15,16 +15,16 @@
  * limitations under the License.
  */
 
-package etcd_test
+package datasource_test
 
 import (
 	"context"
 	"testing"
 
+	"github.com/stretchr/testify/assert"
+
 	"github.com/apache/servicecomb-service-center/datasource"
 	"github.com/apache/servicecomb-service-center/pkg/rbacframe"
-	"github.com/astaxie/beego"
-	"github.com/stretchr/testify/assert"
 )
 
 var (
@@ -41,10 +41,6 @@ var (
 	}
 )
 
-func init() {
-	beego.AppConfig.Set("registry_plugin", "etcd")
-}
-
 func TestRole(t *testing.T) {
 	t.Run("create role should success", func(t *testing.T) {
 		err := datasource.Instance().CreateRole(context.Background(), &r1)
@@ -65,6 +61,7 @@ func TestRole(t *testing.T) {
 	})
 
 	t.Run("update role should success", func(t *testing.T) {
+		r1.ID = "11111-22222-33333-4"
 		err := datasource.Instance().UpdateRole(context.Background(), "test-role1", &r1)
 		assert.NoError(t, err)
 	})
diff --git a/scripts/ut_file.sh b/scripts/ut_file.sh
new file mode 100644
index 0000000..f801334
--- /dev/null
+++ b/scripts/ut_file.sh
@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+
+# Licensed to the 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.
+# The 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.
+
+set -e
+CURRENT_PATH=$(cd $(dirname $0);pwd)
+ROOT_PATH=$(dirname $CURRENT_PATH)
+
+export COVERAGE_PATH=$(pwd)
+cd $1
+if [ $(ls | grep _test.go | wc -l) -gt 0 ]; then
+    go test -cover -covermode atomic -coverprofile coverage.out
+    if [ -f coverage.out ]; then
+        sed '1d;$d' coverage.out >> $ROOT_PATH/coverage.txt
+    fi
+fi
+
diff --git a/scripts/ut_test_in_docker.sh b/scripts/ut_test_in_docker.sh
index 9924966..207f031 100755
--- a/scripts/ut_test_in_docker.sh
+++ b/scripts/ut_test_in_docker.sh
@@ -25,10 +25,16 @@ set -e
 
 ut_for_dir() {
     local name=$1
-    echo "${green}Running UT for Service-Center $name${reset}"
+    echo "${green}Running  UT for Service-Center $name${reset}"
     bash -x ./scripts/ut.sh $name
 }
 
+ut_for_file() {
+    local name=$1
+    echo "${green}Running UT for Service-Center $name${reset}"
+    bash -x ./scripts/ut_file.sh $name
+}
+
 echo "${green}Starting Unit Testing for Service Center${reset}"
 
 echo "${green}Starting etcd in docker${reset}"
@@ -50,6 +56,7 @@ echo "${green}Preparing the env for UT....${reset}"
 ./scripts/prepare_env_ut.sh
 
 export TEST_MODE=etcd
+[ $? == 0 ] && ut_for_file datasource
 [ $? == 0 ] && ut_for_dir datasource/etcd
 [ $? == 0 ] && ut_for_dir pkg
 [ $? == 0 ] && ut_for_dir server
@@ -66,6 +73,7 @@ else
 fi
 
 export TEST_MODE=mongo
+[ $? == 0 ] && ut_for_file datasource
 [ $? == 0 ] && ut_for_dir datasource/mongo
 [ $? == 0 ] && ut_for_dir syncer
 ret=$?