Skip to content

Commit

Permalink
feat: support send report to a gRPC server (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizzy-0323 authored May 17, 2024
1 parent 8b2c8f2 commit 50798ff
Show file tree
Hide file tree
Showing 8 changed files with 698 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, html, json, discard, std, prometheus, http")
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, html, json, discard, std, prometheus, http, grpc")
flags.StringVarP(&opt.reportFile, "report-file", "", "", "The file path of the report")
flags.BoolVarP(&opt.reportIgnore, "report-ignore", "", false, "Indicate if ignore the report output")
flags.StringVarP(&opt.reportTemplate, "report-template", "", "", "The template used to render the report")
Expand Down Expand Up @@ -166,6 +166,11 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
case "http":
templateOption := runner.NewTemplateOption(o.reportTemplate, "json")
o.reportWriter = runner.NewHTTPResultWriter(http.MethodPost, o.reportDest, nil, templateOption)
case "grpc":
if o.reportDest == "" {
err = fmt.Errorf("report gRPC server url is required for prometheus report")
}
o.reportWriter = runner.NewGRPCResultWriter(o.context, o.reportDest)
default:
err = fmt.Errorf("not supported report type: '%s'", o.report)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/runner/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (s *gRPCTestCaseRunner) WithSuite(suite *testing.TestSuite) {
// not need this parameter
}

func invokeRequest(ctx context.Context, md protoreflect.MethodDescriptor, payload string, conn *grpc.ClientConn) (respones []string, err error) {
func invokeRequest(ctx context.Context, md protoreflect.MethodDescriptor, payload string, conn *grpc.ClientConn) (response []string, err error) {
resps := make([]*dynamicpb.Message, 0)
if md.IsStreamingClient() || md.IsStreamingServer() {
reqs, err := getStreamMessagepb(md.Input(), payload)
Expand Down Expand Up @@ -483,7 +483,6 @@ func getByReflect(ctx context.Context, r *gRPCTestCaseRunner, fullName protorefl
if err != nil {
return nil, err
}

req := &grpc_reflection_v1.ServerReflectionRequest{
Host: "",
MessageRequest: &grpc_reflection_v1.ServerReflectionRequest_FileContainingSymbol{
Expand Down
120 changes: 120 additions & 0 deletions pkg/runner/writer_grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright 2024 API Testing Authors.
Licensed 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 runner

import (
"context"
"encoding/json"
"errors"
"fmt"
"log"

"github.com/linuxsuren/api-testing/pkg/apispec"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)

type grpcResultWriter struct {
context context.Context
targetUrl string
}

// NewGRPCResultWriter creates a new grpcResultWriter
func NewGRPCResultWriter(ctx context.Context, url string) ReportResultWriter {
return &grpcResultWriter{
context: ctx,
targetUrl: url,
}
}

// Output writes the JSON base report to target writer
func (w *grpcResultWriter) Output(result []ReportResult) (err error) {
server, err := w.getHost()
if err != nil {
log.Fatalln(err)
}
log.Println("will send report to:" + server)
conn, err := getConnection(server)
if err != nil {
log.Println("Error when connecting to grpc server", err)
return err
}
defer conn.Close()
md, err := w.getMethodDescriptor(conn)
if err != nil {
if err == protoregistry.NotFound {
return fmt.Errorf("api %q is not found on grpc server", w.targetUrl)
}
return err
}
jsonPayload, _ := json.Marshal(
map[string][]ReportResult{
"data": result,
})
payload := string(jsonPayload)
resp, err := invokeRequest(w.context, md, payload, conn)
if err != nil {
log.Fatalln(err)
}
log.Println("getting response back:", resp)
return
}

// use server reflection to get the method descriptor
func (w *grpcResultWriter) getMethodDescriptor(conn *grpc.ClientConn) (protoreflect.MethodDescriptor, error) {
fullName, err := splitFullQualifiedName(w.targetUrl)
if err != nil {
return nil, err
}
var dp protoreflect.Descriptor

dp, err = getByReflect(w.context, nil, fullName, conn)
if err != nil {
return nil, err
}
if dp.IsPlaceholder() {
return nil, protoregistry.NotFound
}
if md, ok := dp.(protoreflect.MethodDescriptor); ok {
return md, nil
}
return nil, protoregistry.NotFound
}
func (w *grpcResultWriter) getHost() (host string, err error) {
qn := regexFullQualifiedName.FindStringSubmatch(w.targetUrl)
if len(qn) == 0 {
return "", errors.New("can not get host from url")
}
return qn[1], nil
}

// get connection with gRPC server
func getConnection(host string) (conn *grpc.ClientConn, err error) {
conn, err = grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
return
}

// WithAPIConverage sets the api coverage
func (w *grpcResultWriter) WithAPIConverage(apiConverage apispec.APIConverage) ReportResultWriter {
return w
}

func (w *grpcResultWriter) WithResourceUsage([]ResourceUsage) ReportResultWriter {
return w
}
68 changes: 68 additions & 0 deletions pkg/runner/writer_grpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2024 API Testing Authors.
Licensed 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 runner

import (
"context"
"testing"

testWriter "github.com/linuxsuren/api-testing/pkg/runner/writer_templates"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

func TestGRPCResultWriter(t *testing.T) {
t.Run("test request", func(t *testing.T) {
s := grpc.NewServer()
testServer := &testWriter.ReportServer{}
testWriter.RegisterReportWriterServer(s, testServer)
reflection.RegisterV1(s)
l := runServer(t, s)
url := l.Addr().String() + "/writer_templates.ReportWriter/SendReportResult"
ctx := context.Background()
writer := NewGRPCResultWriter(ctx, url)
err := writer.Output([]ReportResult{{
Name: "test",
API: "/api",
Max: 1,
Average: 2,
Error: 3,
Count: 1,
}})
assert.NoError(t, err)
s.Stop()
})
t.Run("test reflect unsupported on server", func(t *testing.T) {
s := grpc.NewServer()
testServer := &testWriter.ReportServer{}
testWriter.RegisterReportWriterServer(s, testServer)
l := runServer(t, s)
url := l.Addr().String() + "/writer_templates.ReportWriter/SendReportResult"
ctx := context.Background()
writer := NewGRPCResultWriter(ctx, url)
err := writer.Output([]ReportResult{{
Name: "test",
API: "/api",
Max: 1,
Average: 2,
Error: 3,
Count: 1,
}})
assert.NotNil(t, err)
})
}
18 changes: 18 additions & 0 deletions pkg/runner/writer_templates/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package writer_templates

import (
"context"
"log"
)

type ReportServer struct {
UnimplementedReportWriterServer
}

func (s *ReportServer) SendReportResult(ctx context.Context, req *ReportResultRepeated) (*Empty, error) {
// print received data
for _, result := range req.Data {
log.Printf("Received report: %+v", result)
}
return &Empty{}, nil
}
Loading

0 comments on commit 50798ff

Please sign in to comment.