Skip to content

Commit

Permalink
fix response
Browse files Browse the repository at this point in the history
  • Loading branch information
humingcheng committed Aug 12, 2024
1 parent f469d7b commit 96bd532
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
18 changes: 9 additions & 9 deletions syncer/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *Server) Sync(ctx context.Context, events *v1sync.EventList) (*v1sync.Re
err := auth(ctx)
if err != nil {
log.Error("auth failed", err)
return generateAuthFailResults(err), nil
return generatorFailedResults(events, err)
}

log.Info(fmt.Sprintf("start sync: %s", events.Flag()))
Expand All @@ -65,15 +65,15 @@ func (s *Server) Sync(ctx context.Context, events *v1sync.EventList) (*v1sync.Re
return s.toResults(res), nil
}

func generateAuthFailResults(err error) *v1sync.Results {
return &v1sync.Results{
Results: map[string]*v1sync.Result{
"auth": &v1sync.Result{
Code: resource.AuthFail,
Message: err.Error(),
},
},
func generatorFailedResults(events *v1sync.EventList, err error) (*v1sync.Results, error) {
rsts := make(map[string]*v1sync.Result, len(events.Events))
for _, evt := range events.Events {
rsts[evt.Id] = &v1sync.Result{
Code: resource.Fail,
Message: err.Error(),
}
}
return &v1sync.Results{Results: rsts}, nil
}

func (s *Server) toResults(results []*resource.Result) *v1sync.Results {
Expand Down
81 changes: 81 additions & 0 deletions syncer/rpc/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package rpc

import (
"context"
"reflect"
"testing"

"github.com/go-chassis/cari/rbac"
"github.com/stretchr/testify/assert"

v1sync "github.com/apache/servicecomb-service-center/syncer/api/v1"
"github.com/apache/servicecomb-service-center/syncer/config"
"github.com/apache/servicecomb-service-center/syncer/service/replicator/resource"
)

type testReplicator struct{}

func (testReplicator) Replicate(ctx context.Context, el *v1sync.EventList) (*v1sync.Results, error) {
return &v1sync.Results{Results: map[string]*v1sync.Result{
"constant_id": &v1sync.Result{
Code: resource.Success,
},
}}, nil
}

func (testReplicator) Persist(ctx context.Context, el *v1sync.EventList) []*resource.Result {
return []*resource.Result{
&resource.Result{
EventID: "constant_id",
Status: resource.Success,
},
}
}

func TestServer_Sync(t *testing.T) {
s := NewServer()

// rbac enabled, should sync failed and return auth failed message
config.SetConfig(config.Config{
Sync: &config.Sync{
RbacEnabled: true,
}})
events := &v1sync.EventList{Events: []*v1sync.Event{
{
Id: "evt1",
},
{
Id: "evt2",
},
}}

expectedRst := map[string]*v1sync.Result{
"evt1": &v1sync.Result{
Code: resource.Fail,
Message: rbac.NewError(rbac.ErrNoAuthHeader, "").Error(),
},

"evt2": &v1sync.Result{
Code: resource.Fail,
Message: rbac.NewError(rbac.ErrNoAuthHeader, "").Error(),
},
}
rst, err := s.Sync(context.Background(), events)
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(expectedRst, rst.Results))

// rbac disabled, should sync success(with the mock replicator)
config.SetConfig(config.Config{
Sync: &config.Sync{
RbacEnabled: false,
}})
expectedRst = map[string]*v1sync.Result{
"constant_id": &v1sync.Result{
Code: resource.Success,
},
}
s.replicator = &testReplicator{}
rst, err = s.Sync(context.Background(), events)
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(expectedRst, rst.Results))
}
3 changes: 0 additions & 3 deletions syncer/service/replicator/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const (
MicroNonExist
InstNonExist
NonImplement
AuthFail
)

const (
Expand All @@ -50,7 +49,6 @@ const (
ResultStatusMicroNonExist = "microNonExist"
ResultStatusInstNonExist = "instNonExist"
ResultStatusNonImplement = "nonImplement"
ResultStatusAuthFail = "authFail"
)

var codeDescriber = map[int32]string{
Expand All @@ -60,7 +58,6 @@ var codeDescriber = map[int32]string{
MicroNonExist: ResultStatusMicroNonExist,
InstNonExist: ResultStatusInstNonExist,
NonImplement: ResultStatusNonImplement,
AuthFail: ResultStatusAuthFail,
}

type NewResource func(event *v1sync.Event) Resource
Expand Down

0 comments on commit 96bd532

Please sign in to comment.