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

feat: add trpc protocol support #254

Merged
merged 5 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix the trpc service parsing issue
  • Loading branch information
LinuxSuRen committed Oct 28, 2023
commit 928f881a75d0be7e3b4da8f08b4368a4ae0a3b33
31 changes: 24 additions & 7 deletions pkg/runner/trpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"time"

"github.com/linuxsuren/api-testing/pkg/testing"
Expand Down Expand Up @@ -83,8 +84,8 @@ func (r *tRPCTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext

r.log.Info("start to send request to %s\n", testcase.Request.API)

var fd *descriptor.FileDescriptor
fd, md, err := getTRPCMethodDescriptor(r.proto, testcase)
var service string
service, md, err := getTRPCMethodDescriptor(r.proto, testcase)
if err != nil {
if err == protoregistry.NotFound {
return nil, fmt.Errorf("API %q is not found", testcase.Request.API)
Expand All @@ -96,7 +97,7 @@ func (r *tRPCTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext
}

payload := testcase.Request.Body
resp, err := invokeTRPCRequest(ctx, r.cc, fd, md, payload, r.host)
resp, err := invokeTRPCRequest(ctx, r.cc, service, md, payload, r.host)
if err != nil {
return nil, err
}
Expand All @@ -120,7 +121,7 @@ func (r *tRPCTestCaseRunner) GetResponseRecord() SimpleResponse {
return r.response
}

func getTRPCMethodDescriptor(proto testing.RPCDesc, testcase *testing.TestCase) (fd *descriptor.FileDescriptor, d *descriptor.RPCDescriptor, err error) {
func getTRPCMethodDescriptor(proto testing.RPCDesc, testcase *testing.TestCase) (service string, d *descriptor.RPCDescriptor, err error) {
opts := []parser.Option{
parser.WithAliasOn(false),
parser.WithAPPName(""),
Expand All @@ -146,22 +147,38 @@ func getTRPCMethodDescriptor(proto testing.RPCDesc, testcase *testing.TestCase)
proto.ProtoFile = tempF.Name()
}

var fd *descriptor.FileDescriptor
var method string
service, method = splitServiceAndMethod(testcase.Request.API)
if fd, err = parser.Parse(
proto.ProtoFile,
[]string{},
0,
opts...,
); err == nil {
d = fd.Services[0].MethodRPC[testcase.Request.API]
for _, svc := range fd.Services {
if svc.Name == service {
d = svc.MethodRPC[method]
break
}
}
}
return
}

func splitServiceAndMethod(api string) (service, method string) {
parts := strings.Split(api, "/")
if len(parts) >= 2 {
service = parts[len(parts)-2]
method = parts[len(parts)-1]
}
return
}

func invokeTRPCRequest(ctx context.Context, cc client.Client, fd *descriptor.FileDescriptor, md *descriptor.RPCDescriptor, payload string, host string) (
func invokeTRPCRequest(ctx context.Context, cc client.Client, serviceName string, md *descriptor.RPCDescriptor, payload string, host string) (
resp map[string]string, err error) {
ctx, msg := codec.WithCloneMessage(ctx)
defer codec.PutBackMessage(msg)
serviceName := fd.Services[0].Name

msg.WithClientRPCName(fmt.Sprintf("/%s.%s/%s", md.RequestTypePkgDirective, serviceName, md.Name))
msg.WithCalleeServiceName(md.RequestTypePkgDirective + "." + serviceName)
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/trpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestTRPC(t *testing.T) {
testcase := &atest.TestCase{
Name: "Unary",
Request: atest.Request{
API: "Unary",
API: "/Main/Unary",
Body: "{}",
},
}
Expand All @@ -54,7 +54,7 @@ func TestTRPC(t *testing.T) {
_, err := tRPCRunner.RunTestCase(&atest.TestCase{
Name: "Fake",
Request: atest.Request{
API: "Fake",
API: "/Main/Fake",
Body: "{}",
},
}, nil, context.Background())
Expand Down