Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit test for function (#706) #739

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ type Server struct {
}

func New(tenantOp config.TenantOperator, serviceDiscovery base.Discovery) *Server {
srv := &myConfigService{
tenantOp: tenantOp,
srv := &MyConfigService{
TenantOp: tenantOp,
}
srvDiscSrv := &myServiceDiscovery{
serviceDiscovery: serviceDiscovery,
Expand Down
62 changes: 31 additions & 31 deletions pkg/admin/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,32 @@ import (
"github.com/arana-db/arana/pkg/util/misc"
)

var _ ConfigService = (*myConfigService)(nil)
var _ ConfigService = (*MyConfigService)(nil)

var (
errNoSuchTenant = errors.New("no such tenant")
errNoSuchCluster = errors.New("no such cluster")
errNoSuchGroup = errors.New("no such group")
)

type myConfigService struct {
tenantOp config.TenantOperator
type MyConfigService struct {
TenantOp config.TenantOperator
centers sync.Map // map[string]*lazyCenter
}

func (cs *myConfigService) RemoveUser(ctx context.Context, tenant string, username string) error {
return cs.tenantOp.RemoveTenantUser(tenant, username)
func (cs *MyConfigService) RemoveUser(ctx context.Context, tenant string, username string) error {
return cs.TenantOp.RemoveTenantUser(tenant, username)
}

func (cs *myConfigService) UpsertUser(ctx context.Context, tenant string, user *config.User, username string) error {
func (cs *MyConfigService) UpsertUser(ctx context.Context, tenant string, user *config.User, username string) error {
if username != "" && username != user.Username {
return cs.tenantOp.UpdateTenantUser(tenant, user.Username, user.Password, username)
return cs.TenantOp.UpdateTenantUser(tenant, user.Username, user.Password, username)
}
return cs.tenantOp.CreateTenantUser(tenant, user.Username, user.Password)
return cs.TenantOp.CreateTenantUser(tenant, user.Username, user.Password)
}

func (cs *myConfigService) ListTenants(ctx context.Context) ([]*TenantDTO, error) {
tenants := cs.tenantOp.ListTenants()
func (cs *MyConfigService) ListTenants(ctx context.Context) ([]*TenantDTO, error) {
tenants := cs.TenantOp.ListTenants()

ret := make([]*TenantDTO, 0, len(tenants))
for _, tenant := range tenants {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (cs *myConfigService) ListTenants(ctx context.Context) ([]*TenantDTO, error
return ret, nil
}

func (cs *myConfigService) ListNodes(ctx context.Context, tenant string) ([]*NodeDTO, error) {
func (cs *MyConfigService) ListNodes(ctx context.Context, tenant string) ([]*NodeDTO, error) {
ct, err := cs.getCenter(ctx, tenant)
if err != nil {
return nil, perrors.WithStack(err)
Expand Down Expand Up @@ -142,7 +142,7 @@ func (cs *myConfigService) ListNodes(ctx context.Context, tenant string) ([]*Nod
return ret, nil
}

func (cs *myConfigService) ListClusters(ctx context.Context, tenant string) ([]*ClusterDTO, error) {
func (cs *MyConfigService) ListClusters(ctx context.Context, tenant string) ([]*ClusterDTO, error) {
ct, err := cs.getCenter(ctx, tenant)
if err != nil {
return nil, perrors.WithStack(err)
Expand Down Expand Up @@ -181,7 +181,7 @@ func (cs *myConfigService) ListClusters(ctx context.Context, tenant string) ([]*
return ret, nil
}

func (cs *myConfigService) ListDBGroups(ctx context.Context, tenant, cluster string) ([]*GroupDTO, error) {
func (cs *MyConfigService) ListDBGroups(ctx context.Context, tenant, cluster string) ([]*GroupDTO, error) {
ct, err := cs.getCenter(ctx, tenant)
if err != nil {
return nil, perrors.WithStack(err)
Expand Down Expand Up @@ -224,7 +224,7 @@ func (cs *myConfigService) ListDBGroups(ctx context.Context, tenant, cluster str
return ret, nil
}

func (cs *myConfigService) ListTables(ctx context.Context, tenant, cluster string) ([]*TableDTO, error) {
func (cs *MyConfigService) ListTables(ctx context.Context, tenant, cluster string) ([]*TableDTO, error) {
ct, err := cs.getCenter(ctx, tenant)
if err != nil {
return nil, errNoSuchTenant
Expand Down Expand Up @@ -264,32 +264,32 @@ func (cs *myConfigService) ListTables(ctx context.Context, tenant, cluster strin
return ret, nil
}

func (cs *myConfigService) UpsertTenant(ctx context.Context, tenant string, body *TenantDTO) error {
func (cs *MyConfigService) UpsertTenant(ctx context.Context, tenant string, body *TenantDTO) error {
if tenant != body.Name {
cs.tenantOp.UpdateTenant(tenant, body.Name)
cs.TenantOp.UpdateTenant(tenant, body.Name)
return nil
}
if err := cs.tenantOp.CreateTenant(tenant); err != nil {
if err := cs.TenantOp.CreateTenant(tenant); err != nil {
return perrors.Wrapf(err, "failed to create tenant '%s'", tenant)
}

for _, next := range body.Users {
if err := cs.tenantOp.CreateTenantUser(tenant, next.Username, next.Password); err != nil {
if err := cs.TenantOp.CreateTenantUser(tenant, next.Username, next.Password); err != nil {
return perrors.WithStack(err)
}
}

return nil
}

func (cs *myConfigService) RemoveTenant(ctx context.Context, tenant string) error {
if err := cs.tenantOp.RemoveTenant(tenant); err != nil {
func (cs *MyConfigService) RemoveTenant(ctx context.Context, tenant string) error {
if err := cs.TenantOp.RemoveTenant(tenant); err != nil {
return perrors.Wrapf(err, "failed to remove tenant '%s'", tenant)
}
return nil
}

func (cs *myConfigService) UpsertCluster(ctx context.Context, tenant, cluster string, body *ClusterDTO) error {
func (cs *MyConfigService) UpsertCluster(ctx context.Context, tenant, cluster string, body *ClusterDTO) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand Down Expand Up @@ -336,7 +336,7 @@ func (cs *myConfigService) UpsertCluster(ctx context.Context, tenant, cluster st
return nil
}

func (cs *myConfigService) RemoveCluster(ctx context.Context, tenant, cluster string) error {
func (cs *MyConfigService) RemoveCluster(ctx context.Context, tenant, cluster string) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand All @@ -363,7 +363,7 @@ func (cs *myConfigService) RemoveCluster(ctx context.Context, tenant, cluster st
return nil
}

func (cs *myConfigService) UpsertNode(ctx context.Context, tenant, node string, body *NodeDTO) error {
func (cs *MyConfigService) UpsertNode(ctx context.Context, tenant, node string, body *NodeDTO) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand Down Expand Up @@ -409,7 +409,7 @@ func (cs *myConfigService) UpsertNode(ctx context.Context, tenant, node string,
return nil
}

func (cs *myConfigService) RemoveNode(ctx context.Context, tenant, node string) error {
func (cs *MyConfigService) RemoveNode(ctx context.Context, tenant, node string) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand All @@ -432,7 +432,7 @@ func (cs *myConfigService) RemoveNode(ctx context.Context, tenant, node string)
return nil
}

func (cs *myConfigService) UpsertGroup(ctx context.Context, tenant, cluster, group string, body *GroupDTO) error {
func (cs *MyConfigService) UpsertGroup(ctx context.Context, tenant, cluster, group string, body *GroupDTO) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand Down Expand Up @@ -481,7 +481,7 @@ func (cs *myConfigService) UpsertGroup(ctx context.Context, tenant, cluster, gro
return nil
}

func (cs *myConfigService) RemoveGroup(ctx context.Context, tenant, cluster, group string) error {
func (cs *MyConfigService) RemoveGroup(ctx context.Context, tenant, cluster, group string) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand Down Expand Up @@ -521,7 +521,7 @@ func (cs *myConfigService) RemoveGroup(ctx context.Context, tenant, cluster, gro
return nil
}

func (cs *myConfigService) BindNode(ctx context.Context, tenant, cluster, group, node string) error {
func (cs *MyConfigService) BindNode(ctx context.Context, tenant, cluster, group, node string) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand Down Expand Up @@ -559,7 +559,7 @@ func (cs *myConfigService) BindNode(ctx context.Context, tenant, cluster, group,
return nil
}

func (cs *myConfigService) UnbindNode(ctx context.Context, tenant, cluster, group, node string) error {
func (cs *MyConfigService) UnbindNode(ctx context.Context, tenant, cluster, group, node string) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand Down Expand Up @@ -600,7 +600,7 @@ func (cs *myConfigService) UnbindNode(ctx context.Context, tenant, cluster, grou
return nil
}

func (cs *myConfigService) UpsertTable(ctx context.Context, tenant, cluster, table string, body *TableDTO) error {
func (cs *MyConfigService) UpsertTable(ctx context.Context, tenant, cluster, table string, body *TableDTO) error {
if body.Name == "" {
body.Name = table
}
Expand Down Expand Up @@ -659,7 +659,7 @@ func (cs *myConfigService) UpsertTable(ctx context.Context, tenant, cluster, tab
return nil
}

func (cs *myConfigService) RemoveTable(ctx context.Context, tenant, cluster, table string) error {
func (cs *MyConfigService) RemoveTable(ctx context.Context, tenant, cluster, table string) error {
op, err := cs.getCenter(ctx, tenant)
if err != nil {
return perrors.WithStack(err)
Expand Down Expand Up @@ -697,7 +697,7 @@ func (cs *myConfigService) RemoveTable(ctx context.Context, tenant, cluster, tab
return nil
}

func (cs *myConfigService) getCenter(ctx context.Context, tenant string) (config.Center, error) {
func (cs *MyConfigService) getCenter(ctx context.Context, tenant string) (config.Center, error) {
if exist, ok := cs.centers.Load(tenant); ok {
return exist.(*lazyCenter).compute(ctx)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/admin/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type LoginPayload struct {
}

func NewAuthMiddleware(server *Server, realm, secretKey string) (*jwt.GinJWTMiddleware, error) {
conf := server.service.(*myConfigService)
for _, tenant := range conf.tenantOp.ListTenants() {
conf := server.service.(*MyConfigService)
for _, tenant := range conf.TenantOp.ListTenants() {
center, err := conf.getCenter(context.Background(), tenant)
if err != nil {
return nil, err
Expand Down
57 changes: 57 additions & 0 deletions pkg/boot/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ import (
"context"
"os"
"testing"
"time"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/arana-db/arana/pkg/admin"
"github.com/arana-db/arana/pkg/config"
"github.com/arana-db/arana/pkg/constants"
_ "github.com/arana-db/arana/pkg/runtime/builtin"
"github.com/arana-db/arana/testdata"
Expand Down Expand Up @@ -67,4 +70,58 @@ func TestFileProvider(t *testing.T) {
assert.NoError(t, err)
assert.True(t, table.AllowFullScan())
t.Logf("vtable: %v\n", table)

RunImport(testdata.Path("fake_bootstrap.yaml"), testdata.Path("fake_config.yaml"))

op, err := config.NewTenantOperator(config.GetStoreOperate())
assert.NoError(t, err)
srv := &admin.MyConfigService{
TenantOp: op,
}

var userBody config.User
userBody.Username = "arana"
userBody.Password = "654321"
err = srv.UpsertUser(context.Background(), "arana", &userBody, "arana")
assert.NoError(t, err)
err = srv.RemoveUser(context.Background(), "arana", "arana")
assert.NoError(t, err)

allGroups, err := srv.ListDBGroups(context.Background(), "arana", "employees")
assert.NoError(t, err)
groupBody := allGroups[0]
groupBody.Nodes = groupBody.Nodes[0:1]
err = srv.UpsertGroup(context.Background(), "arana", "employees", "employees_0000", groupBody)
assert.NoError(t, err)
var groupNew admin.GroupDTO
groupNew.ClusterName = "employees"
groupNew.Name = "employees_0100"
groupNew.Nodes = append(groupNew.Nodes, "node0_r_0")
err = srv.UpsertGroup(context.Background(), "arana", "employees", "employees_0100", &groupNew)
assert.NoError(t, err)
err = srv.RemoveGroup(context.Background(), "arana", "employees", "employees_0100")
assert.NoError(t, err)

allNodes, err := srv.ListNodes(context.Background(), "arana")
assert.NoError(t, err)
nodeBody := allNodes[0]
nodeBody.Weight = "r5w10"
err = srv.UpsertNode(context.Background(), "arana", nodeBody.Name, nodeBody)
assert.NoError(t, err)
err = srv.RemoveNode(context.Background(), "arana", "node0_r_0")
assert.NoError(t, err)

allTables, err := srv.ListTables(context.Background(), "arana", "employees")
assert.NoError(t, err)
tableBody := allTables[0]
tableBody.Attributes["allow_full_scan"] = "false"
err = srv.UpsertTable(context.Background(), "arana", "employees", tableBody.Name, tableBody)
assert.NoError(t, err)
err = srv.RemoveTable(context.Background(), "arana", "employees", "student")
assert.NoError(t, err)

err = srv.RemoveCluster(context.Background(), "arana", "employees")
assert.NoError(t, err)
//wait for watcher consumer
time.Sleep(3 * time.Second)
}
2 changes: 1 addition & 1 deletion pkg/boot/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func (d *watcher) onNodeAdd(ctx context.Context, node *config.Node) error {
func (d *watcher) onNodeUpdate(ctx context.Context, node *config.Node) error {
clusters := security.DefaultTenantManager().GetClusters(d.tenant)

var paramsMap map[string]config.ParametersMap
paramsMap := make(map[string]config.ParametersMap)

updateNode := func(rt runtime.Runtime, cluster, group string) {
clonedNode := *node
Expand Down
Loading