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 abed953 Fix: Can not delete unused role (#1152)
abed953 is described below
commit abed95307df6cb1ef61b5d123ff8d308c337e7ee
Author: little-cui <[email protected]>
AuthorDate: Mon Sep 13 09:52:21 2021 +0800
Fix: Can not delete unused role (#1152)
---
datasource/etcd/etcd.go | 2 +-
datasource/etcd/path/key_generator.go | 1 +
datasource/role_test.go | 26 +++++++++++++-----------
integration/instances_test.go | 38 +++++++++++++++++------------------
4 files changed, 35 insertions(+), 32 deletions(-)
diff --git a/datasource/etcd/etcd.go b/datasource/etcd/etcd.go
index c1a8797..42e5e5a 100644
--- a/datasource/etcd/etcd.go
+++ b/datasource/etcd/etcd.go
@@ -154,7 +154,7 @@ func (ds *DataSource) initPlugins() {
ds.initClustersIndex()
// discovery
- kind := config.GetString("discovery.kind", "",
config.WithStandby("discovery_plugin"))
+ kind := config.GetString("discovery.kind", "etcd",
config.WithStandby("discovery_plugin"))
err = state.Init(state.Config{
Kind: kind,
ClusterName: ds.Options.ClusterName,
diff --git a/datasource/etcd/path/key_generator.go
b/datasource/etcd/path/key_generator.go
index 4e63818..0481d4e 100644
--- a/datasource/etcd/path/key_generator.go
+++ b/datasource/etcd/path/key_generator.go
@@ -103,6 +103,7 @@ func GenRoleAccountPrefixIdxKey(role string) string {
GetRootKey(),
"idx-role-account",
role,
+ "",
}, SPLIT)
}
diff --git a/datasource/role_test.go b/datasource/role_test.go
index 9eaafc2..5ed6259 100644
--- a/datasource/role_test.go
+++ b/datasource/role_test.go
@@ -33,20 +33,20 @@ import (
var (
r1 = rbac.Role{
ID: "11111-22222-33333",
- Name: "test-role1",
+ Name: "test-role",
Perms: nil,
}
r2 = rbac.Role{
ID: "11111-22222-33333-44444",
- Name: "test-role2",
+ Name: "test-role-ex",
Perms: nil,
}
a = rbac.Account{
Name: "account-role-test",
Password: "abc",
- Roles: []string{"test-role1"},
+ Roles: []string{"test-role"},
}
)
@@ -54,7 +54,7 @@ func TestRole(t *testing.T) {
t.Run("create role should success", func(t *testing.T) {
err :=
datasource.GetRoleManager().CreateRole(context.Background(), &r1)
assert.NoError(t, err)
- r, err :=
datasource.GetRoleManager().GetRole(context.Background(), "test-role1")
+ r, err :=
datasource.GetRoleManager().GetRole(context.Background(), "test-role")
assert.NoError(t, err)
assert.Equal(t, r1, *r)
dt, _ := strconv.Atoi(r.CreateTime)
@@ -62,7 +62,7 @@ func TestRole(t *testing.T) {
assert.Equal(t, r.CreateTime, r.UpdateTime)
})
t.Run("role should exist", func(t *testing.T) {
- exist, err :=
datasource.GetRoleManager().RoleExist(context.Background(), "test-role1")
+ exist, err :=
datasource.GetRoleManager().RoleExist(context.Background(), "test-role")
assert.NoError(t, err)
assert.True(t, exist)
})
@@ -73,21 +73,22 @@ func TestRole(t *testing.T) {
})
t.Run("update role should success", func(t *testing.T) {
- r, err :=
datasource.GetRoleManager().GetRole(context.Background(), "test-role1")
+ r, err :=
datasource.GetRoleManager().GetRole(context.Background(), "test-role")
assert.NoError(t, err)
old, _ := strconv.Atoi(r.UpdateTime)
time.Sleep(time.Second)
r1.ID = "11111-22222-33333-4"
- err =
datasource.GetRoleManager().UpdateRole(context.Background(), "test-role1", &r1)
+ err =
datasource.GetRoleManager().UpdateRole(context.Background(), "test-role", &r1)
assert.NoError(t, err)
- r, err =
datasource.GetRoleManager().GetRole(context.Background(), "test-role1")
+ r, err =
datasource.GetRoleManager().GetRole(context.Background(), "test-role")
assert.NoError(t, err)
last, _ := strconv.Atoi(r.UpdateTime)
assert.Less(t, old, last)
assert.NotEqual(t, r.CreateTime, r.UpdateTime)
})
+
t.Run("add new role should success", func(t *testing.T) {
err :=
datasource.GetRoleManager().CreateRole(context.Background(), &r2)
assert.NoError(t, err)
@@ -100,16 +101,16 @@ func TestRole(t *testing.T) {
err :=
datasource.GetAccountManager().CreateAccount(context.Background(), &a)
assert.NoError(t, err)
- _, err =
datasource.GetRoleManager().DeleteRole(context.Background(), "test-role1")
+ _, err =
datasource.GetRoleManager().DeleteRole(context.Background(), "test-role")
assert.Error(t, err)
})
t.Run("update account role and delete old role should success", func(t
*testing.T) {
- a.Roles = []string{"test-role2"}
+ a.Roles = []string{"test-role-ex"}
err :=
datasource.GetAccountManager().UpdateAccount(context.Background(),
"account-role-test", &a)
assert.NoError(t, err)
- _, err =
datasource.GetRoleManager().DeleteRole(context.Background(), "test-role1")
+ _, err =
datasource.GetRoleManager().DeleteRole(context.Background(), "test-role")
assert.NoError(t, err)
})
@@ -118,8 +119,9 @@ func TestRole(t *testing.T) {
assert.True(t, b)
assert.NoError(t, err)
- _, err =
datasource.GetRoleManager().DeleteRole(context.Background(), "test-role2")
+ _, err =
datasource.GetRoleManager().DeleteRole(context.Background(), "test-role-ex")
assert.NoError(t, err)
+
_, n, err :=
datasource.GetRoleManager().ListRole(context.Background())
assert.NoError(t, err)
assert.Equal(t, int64(0), n)
diff --git a/integration/instances_test.go b/integration/instances_test.go
index 3b7a641..8e39140 100644
--- a/integration/instances_test.go
+++ b/integration/instances_test.go
@@ -340,25 +340,6 @@ var _ = Describe("MicroService Api Test", func() {
Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
})
- It("Find Micro-service Info by alias", func() {
- req, _ := http.NewRequest(GET,
SCURL+FINDINSTANCE+"?appId="+serviceAppId+"&serviceName="+alias+"&version="+serviceVersion,
nil)
- req.Header.Set("X-Domain-Name", "default")
- req.Header.Set("X-ConsumerId", consumerID)
- resp, _ := scclient.Do(req)
- respbody, _ := ioutil.ReadAll(resp.Body)
- Expect(resp.StatusCode).To(Equal(http.StatusOK))
- servicesStruct :=
map[string][]map[string]interface{}{}
- json.Unmarshal(respbody, &servicesStruct)
- foundMicroServiceInstance := false
- for _, services := range
servicesStruct["instances"] {
- if services["instanceId"] ==
serviceInstanceID {
- foundMicroServiceInstance = true
- break
- }
- }
-
Expect(foundMicroServiceInstance).To(Equal(true))
- })
-
It("Find Micro-Service Instance by ServiceID", func() {
url := strings.Replace(GETINSTANCE,
":serviceId", providerID, 1)
req, _ := http.NewRequest(GET, SCURL+url, nil)
@@ -413,6 +394,25 @@ var _ = Describe("MicroService Api Test", func() {
Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
})
+ It("Find Micro-service Info by alias", func() {
+ req, _ := http.NewRequest(GET,
SCURL+FINDINSTANCE+"?appId="+serviceAppId+"&serviceName="+alias+"&version="+serviceVersion,
nil)
+ req.Header.Set("X-Domain-Name", "default")
+ req.Header.Set("X-ConsumerId", consumerID)
+ resp, _ := scclient.Do(req)
+ respbody, _ := ioutil.ReadAll(resp.Body)
+ Expect(resp.StatusCode).To(Equal(http.StatusOK))
+ servicesStruct :=
map[string][]map[string]interface{}{}
+ json.Unmarshal(respbody, &servicesStruct)
+ foundMicroServiceInstance := false
+ for _, services := range
servicesStruct["instances"] {
+ if services["instanceId"] ==
serviceInstanceID {
+ foundMicroServiceInstance = true
+ break
+ }
+ }
+
Expect(foundMicroServiceInstance).To(Equal(true))
+ })
+
It("Find Micro-Service Instance with rev", func() {
req, _ := http.NewRequest(GET,
SCURL+FINDINSTANCE+"?appId="+serviceAppId+"&serviceName="+serviceName+"&version="+serviceVersion,
nil)
req.Header.Set("X-Domain-Name", "default")