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 1e58aa0 [feat] add task and tombstone ut in eventbase (#1185) 1e58aa0 is described below commit 1e58aa0464c6f4ac7ddd08c6c2a52635900449b1 Author: robotljw <790504...@qq.com> AuthorDate: Tue Dec 28 17:23:01 2021 +0800 [feat] add task and tombstone ut in eventbase (#1185) --- .github/workflows/eventbase-ci.yml | 45 +++++++ eventbase/README.md | 37 ++++++ eventbase/datasource/etcd/task/task_dao.go | 8 +- eventbase/datasource/etcd/task/task_dao_test.go | 146 --------------------- .../datasource/etcd/tombstone/tombstone_dao.go | 4 +- .../etcd/tombstone/tombstone_dao_test.go | 113 ---------------- eventbase/datasource/mongo/model/types.go | 3 +- eventbase/datasource/mongo/mongo.go | 4 +- eventbase/datasource/mongo/task/task_dao.go | 10 +- eventbase/datasource/mongo/task/task_dao_test.go | 145 -------------------- .../datasource/mongo/tombstone/tombstone_dao.go | 7 +- .../mongo/tombstone/tombstone_dao_test.go | 112 ---------------- eventbase/datasource/options.go | 12 +- eventbase/datasource/tlsutil/tlsutil_test.go | 73 ----------- eventbase/datasource/tombstone.go | 4 +- eventbase/go.mod | 3 +- eventbase/go.sum | 6 +- .../tombstone_request.go => model/request.go} | 10 +- eventbase/service/task/task_svc.go | 10 +- eventbase/service/task/task_svc_test.go | 118 +++++++++++++++++ eventbase/service/tombstone/tombstone_svc.go | 6 +- eventbase/service/tombstone/tombstone_svc_test.go | 100 ++++++++++++++ eventbase/test/test.go | 56 +++++++- scripts/ut_test_in_docker.sh | 4 +- 24 files changed, 398 insertions(+), 638 deletions(-) diff --git a/.github/workflows/eventbase-ci.yml b/.github/workflows/eventbase-ci.yml new file mode 100644 index 0000000..a2a0f57 --- /dev/null +++ b/.github/workflows/eventbase-ci.yml @@ -0,0 +1,45 @@ +name: eventbase 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: Start MongoDB + uses: supercharge/mongodb-github-action@1.7.0 + with: + mongodb-version: 4.2 + mongodb-replica-set: test-rs + mongodb-port: 27017 + - name: Check out source code + uses: actions/checkout@v1 + - name: UT test + run: | + export TEST_DB_MODE=mongo + export TEST_DB_URI=mongodb://127.0.0.1:27017 + cd eventbase + go test -short -covermode=atomic $(go list ./... | grep -v etcd | grep -v third_party | grep -v examples) + etcd-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 for etcd + run: | + time docker run -d -p 2379:2379 --name etcd quay.io/coreos/etcd etcd -name etcd --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379 + while ! nc -z 127.0.0.1 2379; do + sleep 1 + done + export TEST_DB_MODE=etcd + export TEST_DB_URI=http://127.0.0.1:2379 + cd eventbase + time go test -short -covermode=atomic $(go list ./... | grep -v mongo | grep -v third_party | grep -v examples) \ No newline at end of file diff --git a/eventbase/README.md b/eventbase/README.md new file mode 100644 index 0000000..93ddf88 --- /dev/null +++ b/eventbase/README.md @@ -0,0 +1,37 @@ +# eventbase + +eventbase provides the crud interface of task and tombstone. + +### package + +**bootstrap**:used to start initial loading. + +**datasource**: realize the dao operation of etcd and mongo on task and tombstone. + +**domain**: task and tombstone request. + +**service**: Interfaces exposed by task and tombstone. + +**test**: test parameters. + +### how to use + +```go +import ( + _ "github.com/apache/servicecomb-service-center/eventbase/bootstrap" + "github.com/apache/servicecomb-service-center/eventbase/datasource" + ) + +func Init(){ + dbCfg := db.Config{ + Kind: "etcd", + URI: "http://127.0.0.1:2379", + Timeout: 10 * time.Second, + } + err := datasource.Init(dbCfg) + ... + datasource.GetDataSource().TaskDao() + datasource.GetDataSource().TombstoneDao() + ... +} +``` \ No newline at end of file diff --git a/eventbase/datasource/etcd/task/task_dao.go b/eventbase/datasource/etcd/task/task_dao.go index bc7a68e..c997752 100644 --- a/eventbase/datasource/etcd/task/task_dao.go +++ b/eventbase/datasource/etcd/task/task_dao.go @@ -38,7 +38,7 @@ func (d *Dao) Create(ctx context.Context, task *sync.Task) (*sync.Task, error) { openlog.Error("fail to marshal task") return nil, err } - ok, err := etcdadpt.InsertBytes(ctx, key.TaskKey(task.Domain, task.Project, task.TaskID, task.Timestamp), taskBytes) + ok, err := etcdadpt.InsertBytes(ctx, key.TaskKey(task.Domain, task.Project, task.ID, task.Timestamp), taskBytes) if err != nil { openlog.Error("fail to create task" + err.Error()) return nil, err @@ -51,7 +51,7 @@ func (d *Dao) Create(ctx context.Context, task *sync.Task) (*sync.Task, error) { } func (d *Dao) Update(ctx context.Context, task *sync.Task) error { - keyTask := key.TaskKey(task.Domain, task.Project, task.TaskID, task.Timestamp) + keyTask := key.TaskKey(task.Domain, task.Project, task.ID, task.Timestamp) resp, err := etcdadpt.Get(ctx, keyTask) if err != nil { openlog.Error("fail to get task" + err.Error()) @@ -79,7 +79,7 @@ func (d *Dao) Update(ctx context.Context, task *sync.Task) error { func (d *Dao) Delete(ctx context.Context, tasks ...*sync.Task) error { delOptions := make([]etcdadpt.OpOptions, len(tasks)) for i, task := range tasks { - delOptions[i] = etcdadpt.OpDel(etcdadpt.WithStrKey(key.TaskKey(task.Domain, task.Project, task.TaskID, task.Timestamp))) + delOptions[i] = etcdadpt.OpDel(etcdadpt.WithStrKey(key.TaskKey(task.Domain, task.Project, task.ID, task.Timestamp))) } err := etcdadpt.Txn(ctx, delOptions) if err != nil { @@ -119,7 +119,7 @@ func filterMatch(task *sync.Task, options datasource.TaskFindOptions) bool { if options.Action != "" && task.Action != options.Action { return false } - if options.DataType != "" && task.DataType != options.DataType { + if options.ResourceType != "" && task.ResourceType != options.ResourceType { return false } if options.Status != "" && task.Status != options.Status { diff --git a/eventbase/datasource/etcd/task/task_dao_test.go b/eventbase/datasource/etcd/task/task_dao_test.go deleted file mode 100644 index 97ac43c..0000000 --- a/eventbase/datasource/etcd/task/task_dao_test.go +++ /dev/null @@ -1,146 +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 task_test - -import ( - "context" - "testing" - - "github.com/go-chassis/cari/db" - "github.com/go-chassis/cari/sync" - "github.com/stretchr/testify/assert" - // support embedded etcd - _ "github.com/little-cui/etcdadpt/embedded" - _ "github.com/little-cui/etcdadpt/remote" - - "github.com/apache/servicecomb-service-center/eventbase/datasource" - "github.com/apache/servicecomb-service-center/eventbase/datasource/etcd" - "github.com/apache/servicecomb-service-center/eventbase/test" -) - -var ds datasource.DataSource - -func init() { - cfg := &db.Config{ - Kind: test.Etcd, - URI: test.EtcdURI, - } - ds, _ = etcd.NewDatasource(cfg) -} - -func TestTask(t *testing.T) { - var ( - task = sync.Task{ - TaskID: "30b93187-2a38-49e3-ae99-1961b28329b0", - Action: "create", - DataType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171566, - Status: "pending"} - taskTwo = sync.Task{ - TaskID: "40b93187-2a38-49e3-ae99-1961b28329b0", - Action: "update", - DataType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171567, - Status: "done"} - taskThree = sync.Task{ - TaskID: "50b93187-2a38-49e3-ae99-1961b28329b0", - Action: "update", - DataType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171568, - Status: "pending"} - ) - - t.Run("create task", func(t *testing.T) { - t.Run("create a task should pass", func(t *testing.T) { - _, err := ds.TaskDao().Create(context.Background(), &task) - assert.NoError(t, err) - }) - - t.Run("create a same task should fail", func(t *testing.T) { - _, err := ds.TaskDao().Create(context.Background(), &task) - assert.NotNil(t, err) - }) - - t.Run("create taskTwo and taskThree should pass", func(t *testing.T) { - _, err := ds.TaskDao().Create(context.Background(), &taskTwo) - assert.NoError(t, err) - _, err = ds.TaskDao().Create(context.Background(), &taskThree) - assert.NoError(t, err) - }) - }) - - t.Run("update task", func(t *testing.T) { - t.Run("update a existing task should pass", func(t *testing.T) { - task.Status = "done" - err := ds.TaskDao().Update(context.Background(), &task) - assert.NoError(t, err) - }) - - t.Run("update a not existing task should fail", func(t *testing.T) { - notExistTask := sync.Task{ - TaskID: "not-exist", - Action: "create", - DataType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171568, - Status: "pending", - } - err := ds.TaskDao().Update(context.Background(), ¬ExistTask) - assert.NotNil(t, err) - }) - }) - - t.Run("list task", func(t *testing.T) { - t.Run("list task with domain, project, action ,dataType and status should pass", func(t *testing.T) { - opts := []datasource.TaskFindOption{ - datasource.WithDomain(task.Domain), - datasource.WithProject(task.Project), - datasource.WithAction(task.Action), - datasource.WithDataType(task.DataType), - datasource.WithStatus(task.Status), - } - tasks, err := ds.TaskDao().List(context.Background(), opts...) - assert.NoError(t, err) - assert.Equal(t, 1, len(tasks)) - }) - - t.Run("list task without action ,dataType and status should pass", func(t *testing.T) { - tasks, err := ds.TaskDao().List(context.Background()) - assert.NoError(t, err) - assert.Equal(t, 3, len(tasks)) - assert.Equal(t, tasks[0].Timestamp, task.Timestamp) - assert.Equal(t, tasks[1].Timestamp, taskTwo.Timestamp) - assert.Equal(t, tasks[2].Timestamp, taskThree.Timestamp) - }) - - }) - - t.Run("delete task", func(t *testing.T) { - t.Run("delete tasks should pass", func(t *testing.T) { - err := ds.TaskDao().Delete(context.Background(), []*sync.Task{&task, &taskTwo, &taskThree}...) - assert.NoError(t, err) - }) - }) -} diff --git a/eventbase/datasource/etcd/tombstone/tombstone_dao.go b/eventbase/datasource/etcd/tombstone/tombstone_dao.go index 26467c5..d42a76f 100644 --- a/eventbase/datasource/etcd/tombstone/tombstone_dao.go +++ b/eventbase/datasource/etcd/tombstone/tombstone_dao.go @@ -27,13 +27,13 @@ import ( "github.com/apache/servicecomb-service-center/eventbase/datasource" "github.com/apache/servicecomb-service-center/eventbase/datasource/etcd/key" - "github.com/apache/servicecomb-service-center/eventbase/request" + "github.com/apache/servicecomb-service-center/eventbase/model" ) type Dao struct { } -func (d *Dao) Get(ctx context.Context, req *request.GetTombstoneRequest) (*sync.Tombstone, error) { +func (d *Dao) Get(ctx context.Context, req *model.GetTombstoneRequest) (*sync.Tombstone, error) { tombstoneKey := key.TombstoneKey(req.Domain, req.Project, req.ResourceType, req.ResourceID) kv, err := etcdadpt.Get(ctx, tombstoneKey) if err != nil { diff --git a/eventbase/datasource/etcd/tombstone/tombstone_dao_test.go b/eventbase/datasource/etcd/tombstone/tombstone_dao_test.go deleted file mode 100644 index aedd0f0..0000000 --- a/eventbase/datasource/etcd/tombstone/tombstone_dao_test.go +++ /dev/null @@ -1,113 +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 tombstone_test - -import ( - "context" - "testing" - - "github.com/go-chassis/cari/db" - "github.com/go-chassis/cari/sync" - "github.com/stretchr/testify/assert" - // support embedded etcd - _ "github.com/little-cui/etcdadpt/embedded" - _ "github.com/little-cui/etcdadpt/remote" - - "github.com/apache/servicecomb-service-center/eventbase/datasource" - "github.com/apache/servicecomb-service-center/eventbase/datasource/etcd" - "github.com/apache/servicecomb-service-center/eventbase/request" - "github.com/apache/servicecomb-service-center/eventbase/test" -) - -var ds datasource.DataSource - -func init() { - cfg := &db.Config{ - Kind: test.Etcd, - URI: test.EtcdURI, - } - ds, _ = etcd.NewDatasource(cfg) -} - -func TestTombstone(t *testing.T) { - var ( - tombstoneOne = sync.Tombstone{ - ResourceID: "app/test", - ResourceType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171566, - } - tombstoneTwo = sync.Tombstone{ - ResourceID: "property/test", - ResourceType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171567, - } - ) - - t.Run("create tombstone", func(t *testing.T) { - t.Run("create two tombstone should pass", func(t *testing.T) { - tombstone, err := ds.TombstoneDao().Create(context.Background(), &tombstoneOne) - assert.NoError(t, err) - assert.NotNil(t, tombstone) - tombstone, err = ds.TombstoneDao().Create(context.Background(), &tombstoneTwo) - assert.NoError(t, err) - assert.NotNil(t, tombstone) - }) - }) - - t.Run("get tombstone", func(t *testing.T) { - t.Run("get one tombstone should pass", func(t *testing.T) { - req := request.GetTombstoneRequest{ - Domain: tombstoneOne.Domain, - Project: tombstoneOne.Project, - ResourceType: tombstoneOne.ResourceType, - ResourceID: tombstoneOne.ResourceID, - } - tombstone, err := ds.TombstoneDao().Get(context.Background(), &req) - assert.NoError(t, err) - assert.Equal(t, tombstone.Timestamp, tombstoneOne.Timestamp) - }) - }) - - t.Run("list tombstone", func(t *testing.T) { - t.Run("list tombstone with Domain, Project ,ResourceType and BeforeTimestamp should pass", func(t *testing.T) { - opts := []datasource.TombstoneFindOption{ - datasource.WithTombstoneDomain("default"), - datasource.WithTombstoneDomain("default"), - datasource.WithResourceType(tombstoneOne.ResourceType), - datasource.WithBeforeTimestamp(1638171600), - } - tombstones, err := ds.TombstoneDao().List(context.Background(), opts...) - assert.NoError(t, err) - assert.Equal(t, 2, len(tombstones)) - assert.Equal(t, tombstones[0].Timestamp, tombstoneOne.Timestamp) - assert.Equal(t, tombstones[1].Timestamp, tombstoneTwo.Timestamp) - }) - }) - - t.Run("delete tombstone", func(t *testing.T) { - t.Run("delete two tombstone should pass", func(t *testing.T) { - err := ds.TombstoneDao().Delete(context.Background(), []*sync.Tombstone{&tombstoneOne, &tombstoneTwo}...) - assert.NoError(t, err) - }) - }) - -} diff --git a/eventbase/datasource/mongo/model/types.go b/eventbase/datasource/mongo/model/types.go index 04d4421..c53efb1 100644 --- a/eventbase/datasource/mongo/model/types.go +++ b/eventbase/datasource/mongo/model/types.go @@ -24,11 +24,10 @@ const ( CollectionTombstone = "tombstone" ColumnDomain = "domain" ColumnProject = "project" - ColumnTaskID = "task_id" + ColumnID = "id" ColumnTimestamp = "timestamp" ColumnResourceID = "resource_id" ColumnResourceType = "resource_type" ColumnStatus = "status" ColumnAction = "action" - ColumnDataType = "data_type" ) diff --git a/eventbase/datasource/mongo/mongo.go b/eventbase/datasource/mongo/mongo.go index 580a6f9..72f6099 100644 --- a/eventbase/datasource/mongo/mongo.go +++ b/eventbase/datasource/mongo/mongo.go @@ -113,14 +113,14 @@ func wrapError(err error, skipMsg ...string) { func ensureTask(session *mgo.Session) { c := session.DB(model.DBName).C(model.CollectionTask) err := c.Create(&mgo.CollectionInfo{Validator: bson.M{ - model.ColumnTaskID: bson.M{"$exists": true}, + model.ColumnID: bson.M{"$exists": true}, model.ColumnDomain: bson.M{"$exists": true}, model.ColumnProject: bson.M{"$exists": true}, model.ColumnTimestamp: bson.M{"$exists": true}, }}) wrapError(err) err = c.EnsureIndex(mgo.Index{ - Key: []string{model.ColumnDomain, model.ColumnProject, model.ColumnTaskID, model.ColumnTimestamp}, + Key: []string{model.ColumnDomain, model.ColumnProject, model.ColumnID, model.ColumnTimestamp}, Unique: true, }) wrapError(err) diff --git a/eventbase/datasource/mongo/task/task_dao.go b/eventbase/datasource/mongo/task/task_dao.go index c647a51..a012871 100644 --- a/eventbase/datasource/mongo/task/task_dao.go +++ b/eventbase/datasource/mongo/task/task_dao.go @@ -47,7 +47,7 @@ func (d *Dao) Create(ctx context.Context, task *sync.Task) (*sync.Task, error) { func (d *Dao) Update(ctx context.Context, task *sync.Task) error { collection := client.GetMongoClient().GetDB().Collection(model.CollectionTask) result, err := collection.UpdateOne(ctx, - bson.M{model.ColumnTaskID: task.TaskID, model.ColumnDomain: task.Domain, model.ColumnProject: task.Project, model.ColumnTimestamp: task.Timestamp}, + bson.M{model.ColumnID: task.ID, model.ColumnDomain: task.Domain, model.ColumnProject: task.Project, model.ColumnTimestamp: task.Timestamp}, bson.D{{Key: "$set", Value: bson.D{ {Key: model.ColumnStatus, Value: task.Status}}}, }) @@ -66,11 +66,11 @@ func (d *Dao) Delete(ctx context.Context, tasks ...*sync.Task) error { tasksIDs := make([]string, len(tasks)) filter := bson.A{} for i, task := range tasks { - tasksIDs[i] = task.TaskID + tasksIDs[i] = task.ID dFilter := bson.D{ {model.ColumnDomain, task.Domain}, {model.ColumnProject, task.Project}, - {model.ColumnTaskID, task.TaskID}, + {model.ColumnID, task.ID}, {model.ColumnTimestamp, task.Timestamp}, } filter = append(filter, dFilter) @@ -103,8 +103,8 @@ func (d *Dao) List(ctx context.Context, options ...datasource.TaskFindOption) ([ if opts.Action != "" { filter[model.ColumnAction] = opts.Action } - if opts.DataType != "" { - filter[model.ColumnDataType] = opts.DataType + if opts.ResourceType != "" { + filter[model.ColumnResourceType] = opts.ResourceType } if opts.Status != "" { filter[model.ColumnStatus] = opts.Status diff --git a/eventbase/datasource/mongo/task/task_dao_test.go b/eventbase/datasource/mongo/task/task_dao_test.go deleted file mode 100644 index 47c2d7b..0000000 --- a/eventbase/datasource/mongo/task/task_dao_test.go +++ /dev/null @@ -1,145 +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 task_test - -import ( - "context" - "testing" - "time" - - "github.com/go-chassis/cari/db" - "github.com/go-chassis/cari/sync" - "github.com/stretchr/testify/assert" - - "github.com/apache/servicecomb-service-center/eventbase/datasource" - "github.com/apache/servicecomb-service-center/eventbase/datasource/mongo" - "github.com/apache/servicecomb-service-center/eventbase/test" -) - -var ds datasource.DataSource - -func init() { - cfg := &db.Config{ - Kind: test.Mongo, - URI: test.MongoURI, - Timeout: 10 * time.Second, - } - ds, _ = mongo.NewDatasource(cfg) -} - -func TestTask(t *testing.T) { - var ( - task = sync.Task{ - TaskID: "30b93187-2a38-49e3-ae99-1961b28329b0", - Action: "create", - DataType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171566, - Status: "pending"} - taskTwo = sync.Task{ - TaskID: "40b93187-2a38-49e3-ae99-1961b28329b0", - Action: "update", - DataType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171567, - Status: "done"} - taskThree = sync.Task{ - TaskID: "50b93187-2a38-49e3-ae99-1961b28329b0", - Action: "update", - DataType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171568, - Status: "pending"} - ) - - t.Run("create task", func(t *testing.T) { - t.Run("create a task should pass", func(t *testing.T) { - _, err := ds.TaskDao().Create(context.Background(), &task) - assert.NoError(t, err) - }) - - t.Run("create a same task should fail", func(t *testing.T) { - _, err := ds.TaskDao().Create(context.Background(), &task) - assert.NotNil(t, err) - }) - - t.Run("create taskTwo and taskThree should pass", func(t *testing.T) { - _, err := ds.TaskDao().Create(context.Background(), &taskTwo) - assert.NoError(t, err) - _, err = ds.TaskDao().Create(context.Background(), &taskThree) - assert.NoError(t, err) - }) - }) - - t.Run("update task", func(t *testing.T) { - t.Run("update a existing task should pass", func(t *testing.T) { - task.Status = "done" - err := ds.TaskDao().Update(context.Background(), &task) - assert.NoError(t, err) - }) - - t.Run("update a not existing task should fail", func(t *testing.T) { - notExistTask := sync.Task{ - TaskID: "not-exist", - Action: "create", - DataType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171568, - Status: "pending", - } - err := ds.TaskDao().Update(context.Background(), ¬ExistTask) - assert.NotNil(t, err) - }) - }) - - t.Run("list task", func(t *testing.T) { - t.Run("list task with domain, project, action ,dataType and status should pass", func(t *testing.T) { - opts := []datasource.TaskFindOption{ - datasource.WithDomain(task.Domain), - datasource.WithProject(task.Project), - datasource.WithAction(task.Action), - datasource.WithDataType(task.DataType), - datasource.WithStatus(task.Status), - } - tasks, err := ds.TaskDao().List(context.Background(), opts...) - assert.NoError(t, err) - assert.Equal(t, 1, len(tasks)) - }) - - t.Run("list task without action ,dataType and status should pass", func(t *testing.T) { - tasks, err := ds.TaskDao().List(context.Background()) - assert.NoError(t, err) - assert.Equal(t, 3, len(tasks)) - assert.Equal(t, tasks[0].Timestamp, task.Timestamp) - assert.Equal(t, tasks[1].Timestamp, taskTwo.Timestamp) - assert.Equal(t, tasks[2].Timestamp, taskThree.Timestamp) - }) - - }) - - t.Run("delete task", func(t *testing.T) { - t.Run("delete tasks should pass", func(t *testing.T) { - err := ds.TaskDao().Delete(context.Background(), []*sync.Task{&task, &taskTwo, &taskThree}...) - assert.NoError(t, err) - }) - }) -} diff --git a/eventbase/datasource/mongo/tombstone/tombstone_dao.go b/eventbase/datasource/mongo/tombstone/tombstone_dao.go index f83b3eb..0bc038b 100644 --- a/eventbase/datasource/mongo/tombstone/tombstone_dao.go +++ b/eventbase/datasource/mongo/tombstone/tombstone_dao.go @@ -28,15 +28,16 @@ import ( "github.com/apache/servicecomb-service-center/eventbase/datasource" "github.com/apache/servicecomb-service-center/eventbase/datasource/mongo/client" "github.com/apache/servicecomb-service-center/eventbase/datasource/mongo/model" - "github.com/apache/servicecomb-service-center/eventbase/request" + emodel "github.com/apache/servicecomb-service-center/eventbase/model" ) type Dao struct { } -func (d *Dao) Get(ctx context.Context, req *request.GetTombstoneRequest) (*sync.Tombstone, error) { +func (d *Dao) Get(ctx context.Context, req *emodel.GetTombstoneRequest) (*sync.Tombstone, error) { collection := client.GetMongoClient().GetDB().Collection(model.CollectionTombstone) - filter := bson.M{model.ColumnDomain: req.Domain, model.ColumnProject: req.Project, model.ColumnResourceType: req.ResourceType, model.ColumnResourceID: req.ResourceID} + filter := bson.M{model.ColumnDomain: req.Domain, model.ColumnProject: req.Project, + model.ColumnResourceType: req.ResourceType, model.ColumnResourceID: req.ResourceID} result := collection.FindOne(ctx, filter) if result != nil && result.Err() != nil { openlog.Error("fail to get tombstone" + result.Err().Error()) diff --git a/eventbase/datasource/mongo/tombstone/tombstone_dao_test.go b/eventbase/datasource/mongo/tombstone/tombstone_dao_test.go deleted file mode 100644 index dccb1da..0000000 --- a/eventbase/datasource/mongo/tombstone/tombstone_dao_test.go +++ /dev/null @@ -1,112 +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 tombstone_test - -import ( - "context" - "testing" - "time" - - "github.com/go-chassis/cari/db" - "github.com/go-chassis/cari/sync" - "github.com/stretchr/testify/assert" - - "github.com/apache/servicecomb-service-center/eventbase/datasource" - "github.com/apache/servicecomb-service-center/eventbase/datasource/mongo" - "github.com/apache/servicecomb-service-center/eventbase/request" - "github.com/apache/servicecomb-service-center/eventbase/test" -) - -var ds datasource.DataSource - -func init() { - cfg := &db.Config{ - Kind: test.Mongo, - URI: test.MongoURI, - Timeout: 10 * time.Second, - } - ds, _ = mongo.NewDatasource(cfg) -} - -func TestTombstone(t *testing.T) { - var ( - tombstoneOne = sync.Tombstone{ - ResourceID: "app/test", - ResourceType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171566, - } - tombstoneTwo = sync.Tombstone{ - ResourceID: "property/test", - ResourceType: "config", - Domain: "default", - Project: "default", - Timestamp: 1638171567, - } - ) - - t.Run("create tombstone", func(t *testing.T) { - t.Run("create two tombstone should pass", func(t *testing.T) { - tombstone, err := ds.TombstoneDao().Create(context.Background(), &tombstoneOne) - assert.NoError(t, err) - assert.NotNil(t, tombstone) - tombstone, err = ds.TombstoneDao().Create(context.Background(), &tombstoneTwo) - assert.NoError(t, err) - assert.NotNil(t, tombstone) - }) - }) - - t.Run("get tombstone", func(t *testing.T) { - t.Run("get one tombstone should pass", func(t *testing.T) { - req := request.GetTombstoneRequest{ - Domain: tombstoneOne.Domain, - Project: tombstoneOne.Project, - ResourceType: tombstoneOne.ResourceType, - ResourceID: tombstoneOne.ResourceID, - } - tombstone, err := ds.TombstoneDao().Get(context.Background(), &req) - assert.NoError(t, err) - assert.Equal(t, tombstone.Timestamp, tombstoneOne.Timestamp) - }) - }) - - t.Run("list tombstone", func(t *testing.T) { - t.Run("list tombstone with ResourceType and BeforeTimestamp should pass", func(t *testing.T) { - opts := []datasource.TombstoneFindOption{ - datasource.WithTombstoneDomain("default"), - datasource.WithTombstoneProject("default"), - datasource.WithResourceType(tombstoneOne.ResourceType), - datasource.WithBeforeTimestamp(1638171600), - } - tombstones, err := ds.TombstoneDao().List(context.Background(), opts...) - assert.NoError(t, err) - assert.Equal(t, 2, len(tombstones)) - assert.Equal(t, tombstones[0].Timestamp, tombstoneOne.Timestamp) - assert.Equal(t, tombstones[1].Timestamp, tombstoneTwo.Timestamp) - }) - }) - - t.Run("delete tombstone", func(t *testing.T) { - t.Run("delete two tombstone should pass", func(t *testing.T) { - err := ds.TombstoneDao().Delete(context.Background(), []*sync.Tombstone{&tombstoneOne, &tombstoneTwo}...) - assert.NoError(t, err) - }) - }) - -} diff --git a/eventbase/datasource/options.go b/eventbase/datasource/options.go index 6c13236..1aee3e7 100644 --- a/eventbase/datasource/options.go +++ b/eventbase/datasource/options.go @@ -18,11 +18,11 @@ package datasource type TaskFindOptions struct { - Domain string - Project string - Action string - Status string - DataType string + Domain string + Project string + Action string + Status string + ResourceType string } type TombstoneFindOptions struct { @@ -75,7 +75,7 @@ func WithStatus(status string) TaskFindOption { // WithDataType find task with dataType func WithDataType(dataType string) TaskFindOption { return func(options *TaskFindOptions) { - options.DataType = dataType + options.ResourceType = dataType } } diff --git a/eventbase/datasource/tlsutil/tlsutil_test.go b/eventbase/datasource/tlsutil/tlsutil_test.go deleted file mode 100644 index fd99c05..0000000 --- a/eventbase/datasource/tlsutil/tlsutil_test.go +++ /dev/null @@ -1,73 +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 tlsutil_test - -import ( - "testing" - - "github.com/go-chassis/cari/db" - "github.com/go-chassis/go-archaius" - "github.com/go-chassis/go-chassis/v2/security/cipher" - _ "github.com/go-chassis/go-chassis/v2/security/cipher/plugins/plain" - "github.com/stretchr/testify/assert" - - "github.com/apache/servicecomb-service-center/eventbase/datasource/tlsutil" -) - -const sslRoot = "./../../../examples/service_center/ssl/" - -func init() { - err := archaius.Init() - if err != nil { - panic(err) - } - err = cipher.Init() - if err != nil { - panic(err) - } -} - -func TestConfig(t *testing.T) { - t.Run("normal scene, should return ok", func(t *testing.T) { - cfg, err := tlsutil.Config(&db.Config{ - RootCA: sslRoot + "trust.cer", - CertFile: sslRoot + "server.cer", - KeyFile: sslRoot + "server_key.pem", - CertPwdFile: sslRoot + "cert_pwd", - VerifyPeer: false, - }) - assert.NoError(t, err) - assert.NotNil(t, cfg) - }) - t.Run("without ca file, should return false", func(t *testing.T) { - cfg, err := tlsutil.Config(&db.Config{}) - assert.ErrorIs(t, tlsutil.ErrRootCAMissing, err) - assert.Nil(t, cfg) - }) - t.Run("set not exist pwd file, should return false", func(t *testing.T) { - cfg, err := tlsutil.Config(&db.Config{ - RootCA: sslRoot + "trust.cer", - CertFile: sslRoot + "server.cer", - KeyFile: sslRoot + "server_key.pem", - CertPwdFile: sslRoot + "xxx", - VerifyPeer: false, - }) - assert.Error(t, err) - assert.Nil(t, cfg) - }) -} diff --git a/eventbase/datasource/tombstone.go b/eventbase/datasource/tombstone.go index ee1e924..6afa856 100644 --- a/eventbase/datasource/tombstone.go +++ b/eventbase/datasource/tombstone.go @@ -22,12 +22,12 @@ import ( "github.com/go-chassis/cari/sync" - "github.com/apache/servicecomb-service-center/eventbase/request" + "github.com/apache/servicecomb-service-center/eventbase/model" ) // TombstoneDao provide api of Tombstone entity type TombstoneDao interface { - Get(ctx context.Context, req *request.GetTombstoneRequest) (*sync.Tombstone, error) + Get(ctx context.Context, req *model.GetTombstoneRequest) (*sync.Tombstone, error) // Create func is used for ut Create(ctx context.Context, tombstone *sync.Tombstone) (*sync.Tombstone, error) Delete(ctx context.Context, tombstones ...*sync.Tombstone) error diff --git a/eventbase/go.mod b/eventbase/go.mod index 4a659fe..1604c3d 100644 --- a/eventbase/go.mod +++ b/eventbase/go.mod @@ -1,7 +1,7 @@ module github.com/apache/servicecomb-service-center/eventbase require ( - github.com/go-chassis/cari v0.5.1-0.20211208092532-78a52aa9d52e + github.com/go-chassis/cari v0.5.1-0.20211227133501-53aa20cf7a44 github.com/go-chassis/foundation v0.4.0 github.com/go-chassis/go-archaius v1.5.1 github.com/go-chassis/go-chassis/v2 v2.3.0 @@ -25,6 +25,7 @@ require ( github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect github.com/fsnotify/fsnotify v1.4.7 // indirect github.com/go-stack/stack v1.8.0 // indirect + github.com/gofrs/uuid v4.0.0+incompatible // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.1 // indirect diff --git a/eventbase/go.sum b/eventbase/go.sum index 4ed735a..baec718 100644 --- a/eventbase/go.sum +++ b/eventbase/go.sum @@ -112,8 +112,8 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/ github.com/go-chassis/cari v0.0.0-20201210041921-7b6fbef2df11/go.mod h1:MgtsEI0AM4Ush6Lyw27z9Gk4nQ/8GWTSXrFzupawWDM= github.com/go-chassis/cari v0.4.0/go.mod h1:av/19fqwEP4eOC8unL/z67AAbFDwXUCko6SKa4Avrd8= github.com/go-chassis/cari v0.5.0/go.mod h1:av/19fqwEP4eOC8unL/z67AAbFDwXUCko6SKa4Avrd8= -github.com/go-chassis/cari v0.5.1-0.20211208092532-78a52aa9d52e h1:6z88U255Sm/Ds10uT7ZqYomKLanDzTWxseDBITONFhk= -github.com/go-chassis/cari v0.5.1-0.20211208092532-78a52aa9d52e/go.mod h1:av/19fqwEP4eOC8unL/z67AAbFDwXUCko6SKa4Avrd8= +github.com/go-chassis/cari v0.5.1-0.20211227133501-53aa20cf7a44 h1:2JThhCkuZ5mneXFy0qRvKS7HG1/omq+Hc6I4yNhOZkI= +github.com/go-chassis/cari v0.5.1-0.20211227133501-53aa20cf7a44/go.mod h1:HG0Olv4sy/4e/3e9S0pofO0pzchaDjJ0hMweyFU7d5Q= github.com/go-chassis/foundation v0.2.2-0.20201210043510-9f6d3de40234/go.mod h1:2PjwqpVwYEVaAldl5A58a08viH8p27pNeYaiE3ZxOBA= github.com/go-chassis/foundation v0.2.2/go.mod h1:2PjwqpVwYEVaAldl5A58a08viH8p27pNeYaiE3ZxOBA= github.com/go-chassis/foundation v0.3.0/go.mod h1:2PjwqpVwYEVaAldl5A58a08viH8p27pNeYaiE3ZxOBA= @@ -174,6 +174,8 @@ github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGt github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0= github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= +github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= diff --git a/eventbase/request/tombstone_request.go b/eventbase/model/request.go similarity index 87% rename from eventbase/request/tombstone_request.go rename to eventbase/model/request.go index 63d1cb0..5f2b368 100644 --- a/eventbase/request/tombstone_request.go +++ b/eventbase/model/request.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package request +package model // GetTombstoneRequest contains tombstone get request params type GetTombstoneRequest struct { @@ -29,9 +29,9 @@ type GetTombstoneRequest struct { type ListTaskRequest struct { Domain string `json:"domain,omitempty" yaml:"domain,omitempty"` Project string `json:"project,omitempty" yaml:"project,omitempty"` - TaskAction string `json:"task_action,omitempty" yaml:"task_action,omitempty"` - TaskStatus string `json:"task_status,omitempty" yaml:"task_status,omitempty"` - TaskDataType string `json:"task_data_type,omitempty" yaml:"task_data_type,omitempty"` + Action string `json:"action,omitempty" yaml:"action,omitempty"` + Status string `json:"status,omitempty" yaml:"status,omitempty"` + ResourceType string `json:"resource_type,omitempty" yaml:"resource_type,omitempty"` } // ListTombstoneRequest contains tombstone list request params @@ -40,4 +40,4 @@ type ListTombstoneRequest struct { Project string `json:"project,omitempty" yaml:"project,omitempty"` ResourceType string `json:"resource_type,omitempty" yaml:"resource_type,omitempty"` BeforeTimestamp int64 `json:"before_timestamp,omitempty" yaml:"before_timestamp,omitempty"` -} \ No newline at end of file +} diff --git a/eventbase/service/task/task_svc.go b/eventbase/service/task/task_svc.go index d35bfbf..ee5b436 100644 --- a/eventbase/service/task/task_svc.go +++ b/eventbase/service/task/task_svc.go @@ -23,7 +23,7 @@ import ( "github.com/go-chassis/cari/sync" "github.com/apache/servicecomb-service-center/eventbase/datasource" - "github.com/apache/servicecomb-service-center/eventbase/request" + "github.com/apache/servicecomb-service-center/eventbase/model" ) func Delete(ctx context.Context, tasks ...*sync.Task) error { @@ -34,13 +34,13 @@ func Update(ctx context.Context, task *sync.Task) error { return datasource.GetTaskDao().Update(ctx, task) } -func List(ctx context.Context, request *request.ListTaskRequest) ([]*sync.Task, error) { +func List(ctx context.Context, request *model.ListTaskRequest) ([]*sync.Task, error) { opts := []datasource.TaskFindOption{ datasource.WithDomain(request.Domain), datasource.WithProject(request.Project), - datasource.WithAction(request.TaskAction), - datasource.WithDataType(request.TaskDataType), - datasource.WithStatus(request.TaskStatus), + datasource.WithAction(request.Action), + datasource.WithDataType(request.ResourceType), + datasource.WithStatus(request.Status), } tasks, err := datasource.GetTaskDao().List(ctx, opts...) if err != nil { diff --git a/eventbase/service/task/task_svc_test.go b/eventbase/service/task/task_svc_test.go new file mode 100644 index 0000000..304741e --- /dev/null +++ b/eventbase/service/task/task_svc_test.go @@ -0,0 +1,118 @@ +/* + * 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 task_test + +import ( + "context" + "testing" + + "github.com/go-chassis/cari/discovery" + "github.com/go-chassis/cari/sync" + "github.com/stretchr/testify/assert" + + "github.com/apache/servicecomb-service-center/eventbase/datasource" + "github.com/apache/servicecomb-service-center/eventbase/model" + "github.com/apache/servicecomb-service-center/eventbase/service/task" + "github.com/apache/servicecomb-service-center/eventbase/test" +) + +func init() { + err := datasource.Init(test.DbCfg) + if err != nil { + panic(err) + } +} + +func TestTaskService(t *testing.T) { + taskOne, _ := sync.NewTask("task", "task", sync.CreateAction, "service", + discovery.MicroService{ + ServiceId: "123", + AppId: "appId1", + ServiceName: "svc1", + Version: "1.0", + }) + taskTwo, _ := sync.NewTask("task", "task", sync.UpdateAction, "service", + discovery.MicroService{ + ServiceId: "456", + AppId: "appId2", + ServiceName: "svc2", + Version: "1.0", + }) + taskThree, _ := sync.NewTask("task", "task", sync.DeleteAction, "service", + discovery.MicroService{ + ServiceId: "789", + AppId: "appId3", + ServiceName: "svc3", + Version: "1.0", + }) + t.Run("to create three tasks for next delete update and list operations, should pass", func(t *testing.T) { + _, err := datasource.GetDataSource().TaskDao().Create(context.Background(), taskOne) + assert.Nil(t, err) + _, err = datasource.GetDataSource().TaskDao().Create(context.Background(), taskTwo) + assert.Nil(t, err) + _, err = datasource.GetDataSource().TaskDao().Create(context.Background(), taskThree) + assert.Nil(t, err) + }) + + t.Run("list task service", func(t *testing.T) { + t.Run("list task with default domain and default project should pass", func(t *testing.T) { + listReq := model.ListTaskRequest{ + Domain: "task", + Project: "task", + } + tasks, err := task.List(context.Background(), &listReq) + assert.Nil(t, err) + assert.Equal(t, 3, len(tasks)) + }) + }) + + t.Run("update task service", func(t *testing.T) { + t.Run("set the status of the taskOne to done should pass", func(t *testing.T) { + taskOne.Status = sync.DoneStatus + err := task.Update(context.Background(), taskOne) + assert.Nil(t, err) + listReq := model.ListTaskRequest{ + Domain: taskOne.Domain, + Project: taskOne.Project, + Action: taskOne.Action, + ResourceType: taskOne.ResourceType, + Status: taskOne.Status, + } + tasks, err := task.List(context.Background(), &listReq) + assert.Nil(t, err) + assert.Equal(t, 1, len(tasks)) + }) + }) + + t.Run("delete task service", func(t *testing.T) { + t.Run("delete all tasks in default domain and default project should pass", func(t *testing.T) { + listReq := model.ListTaskRequest{ + Domain: "task", + Project: "task", + } + tasks, err := task.List(context.Background(), &listReq) + assert.Nil(t, err) + assert.Equal(t, 3, len(tasks)) + err = task.Delete(context.Background(), tasks...) + assert.Nil(t, err) + dTasks, err := task.List(context.Background(), &listReq) + assert.Nil(t, err) + assert.Equal(t, 0, len(dTasks)) + }) + }) +} diff --git a/eventbase/service/tombstone/tombstone_svc.go b/eventbase/service/tombstone/tombstone_svc.go index 17e7bbb..dfb64e4 100644 --- a/eventbase/service/tombstone/tombstone_svc.go +++ b/eventbase/service/tombstone/tombstone_svc.go @@ -23,10 +23,10 @@ import ( "github.com/go-chassis/cari/sync" "github.com/apache/servicecomb-service-center/eventbase/datasource" - "github.com/apache/servicecomb-service-center/eventbase/request" + "github.com/apache/servicecomb-service-center/eventbase/model" ) -func Get(ctx context.Context, req *request.GetTombstoneRequest) (*sync.Tombstone, error) { +func Get(ctx context.Context, req *model.GetTombstoneRequest) (*sync.Tombstone, error) { return datasource.GetTombstoneDao().Get(ctx, req) } @@ -34,7 +34,7 @@ func Delete(ctx context.Context, tombstones ...*sync.Tombstone) error { return datasource.GetTombstoneDao().Delete(ctx, tombstones...) } -func List(ctx context.Context, request *request.ListTombstoneRequest) ([]*sync.Tombstone, error) { +func List(ctx context.Context, request *model.ListTombstoneRequest) ([]*sync.Tombstone, error) { opts := []datasource.TombstoneFindOption{ datasource.WithTombstoneDomain(request.Domain), datasource.WithTombstoneProject(request.Project), diff --git a/eventbase/service/tombstone/tombstone_svc_test.go b/eventbase/service/tombstone/tombstone_svc_test.go new file mode 100644 index 0000000..1330104 --- /dev/null +++ b/eventbase/service/tombstone/tombstone_svc_test.go @@ -0,0 +1,100 @@ +/* + * 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 tombstone_test + +import ( + "context" + "testing" + + "github.com/go-chassis/cari/sync" + "github.com/stretchr/testify/assert" + + "github.com/apache/servicecomb-service-center/eventbase/datasource" + "github.com/apache/servicecomb-service-center/eventbase/model" + "github.com/apache/servicecomb-service-center/eventbase/service/tombstone" + "github.com/apache/servicecomb-service-center/eventbase/test" +) + +func init() { + err := datasource.Init(test.DbCfg) + if err != nil { + panic(err) + } +} + +func TestTombstoneService(t *testing.T) { + tombstoneOne := sync.NewTombstone("tombstone", "tombstone", "config", "111111") + tombstoneTwo := sync.NewTombstone("tombstone", "tombstone", "config", "222222") + tombstoneThree := sync.NewTombstone("tombstone", "tombstone", "config", "333333") + + t.Run("to create three tasks for next get delete and list operations, should pass", func(t *testing.T) { + _, err := datasource.GetDataSource().TombstoneDao().Create(context.Background(), tombstoneOne) + assert.Nil(t, err) + _, err = datasource.GetDataSource().TombstoneDao().Create(context.Background(), tombstoneTwo) + assert.Nil(t, err) + _, err = datasource.GetDataSource().TombstoneDao().Create(context.Background(), tombstoneThree) + assert.Nil(t, err) + }) + + t.Run("get tombstone service", func(t *testing.T) { + t.Run("get tombstoneOne should pass", func(t *testing.T) { + getReq := model.GetTombstoneRequest{ + Project: tombstoneOne.Project, + Domain: tombstoneOne.Domain, + ResourceType: tombstoneOne.ResourceType, + ResourceID: tombstoneOne.ResourceID, + } + tmpTombstone, err := tombstone.Get(context.Background(), &getReq) + assert.Nil(t, err) + assert.Equal(t, tmpTombstone.ResourceID, tmpTombstone.ResourceID) + assert.Equal(t, tmpTombstone.ResourceType, tmpTombstone.ResourceType) + assert.Equal(t, tmpTombstone.Domain, tmpTombstone.Domain) + assert.Equal(t, tmpTombstone.Project, tmpTombstone.Project) + }) + }) + + t.Run("list tombstone service", func(t *testing.T) { + t.Run("list all tombstones in default domain and default project should pass", func(t *testing.T) { + listReq := model.ListTombstoneRequest{ + Domain: "tombstone", + Project: "tombstone", + } + tombstones, err := tombstone.List(context.Background(), &listReq) + assert.Nil(t, err) + assert.Equal(t, 3, len(tombstones)) + }) + }) + + t.Run("delete tombstone service", func(t *testing.T) { + t.Run("delete all tombstones in default domain and default project should pass", func(t *testing.T) { + listReq := model.ListTombstoneRequest{ + Domain: "tombstone", + Project: "tombstone", + } + tombstones, err := tombstone.List(context.Background(), &listReq) + assert.Nil(t, err) + assert.Equal(t, 3, len(tombstones)) + err = tombstone.Delete(context.Background(), tombstones...) + assert.Nil(t, err) + dTombstones, err := tombstone.List(context.Background(), &listReq) + assert.Nil(t, err) + assert.Equal(t, 0, len(dTombstones)) + }) + }) + +} diff --git a/eventbase/test/test.go b/eventbase/test/test.go index c71d5ad..71f74a9 100644 --- a/eventbase/test/test.go +++ b/eventbase/test/test.go @@ -1,8 +1,56 @@ +/* + * 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 test +import ( + "time" + + "github.com/go-chassis/cari/db" + "github.com/go-chassis/go-archaius" + + _ "github.com/apache/servicecomb-service-center/eventbase/bootstrap" +) + var ( - Etcd = "etcd" - EtcdURI = "http://127.0.0.1:2379" - Mongo = "mongo" - MongoURI = "mongodb://127.0.0.1:27017" + Etcd = "etcd" + EtcdURI = "http://127.0.0.1:2379" + Mongo = "mongo" + MongoURI = "mongodb://127.0.0.1:27017" + DefaultTestDB = "etcd" + DefaultTestDBURI = "http://127.0.0.1:2379" ) + +var DbCfg = db.Config{} + +func init() { + err := archaius.Init(archaius.WithMemorySource(), archaius.WithENVSource()) + if err != nil { + panic(err) + } + mode, ok := archaius.Get("TEST_DB_MODE").(string) + if ok { + DefaultTestDB = mode + } + uri, ok := archaius.Get("TEST_DB_URI").(string) + if ok { + DefaultTestDBURI = uri + } + DbCfg.Kind = DefaultTestDB + DbCfg.URI = DefaultTestDBURI + DbCfg.Timeout = 10 * time.Second +} diff --git a/scripts/ut_test_in_docker.sh b/scripts/ut_test_in_docker.sh index cf3ef07..2337cb9 100644 --- a/scripts/ut_test_in_docker.sh +++ b/scripts/ut_test_in_docker.sh @@ -67,13 +67,11 @@ if [ ${db_name} == "etcd" ];then [ $? == 0 ] && ut_for_dir pkg [ $? == 0 ] && ut_for_dir server [ $? == 0 ] && ut_for_dir scctl - [ $? == 0 ] && ut_for_dir eventbase/datasource/etcd elif [ ${db_name} == "mongo" ];then export TEST_MODE=mongo [ $? == 0 ] && ut_for_file datasource [ $? == 0 ] && ut_for_dir datasource/mongo [ $? == 0 ] && ut_for_dir server - [ $? == 0 ] && ut_for_dir eventbase/datasource/mongo else echo "${db_name} non-existent" exit 1 @@ -92,4 +90,4 @@ fi echo "${green}Service-Center finished${reset}" echo "${green}Cleaning up the $db_name docker container${reset}" -docker rm -f $db_name \ No newline at end of file +docker rm -f $db_name