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: support send report to a gRPC server #431

Merged
merged 9 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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: 3 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,8 @@ 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":
o.reportWriter = runner.NewGRPCResultWriter(o.reportDest)
LinuxSuRen marked this conversation as resolved.
Show resolved Hide resolved
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
108 changes: 108 additions & 0 deletions pkg/runner/writer_grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
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"
"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 {
targetUrl string
}

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

// Output writes the JSON base report to target writer
func (w *grpcResultWriter) Output(result []ReportResult) (err error) {
server := getHost(w.targetUrl, "127.0.0.1")
LinuxSuRen marked this conversation as resolved.
Show resolved Hide resolved
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()
ctx := context.Background()
md, err := w.getMethodDescriptor(ctx, 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(ctx, 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(ctx context.Context, conn *grpc.ClientConn) (protoreflect.MethodDescriptor, error) {
fullName, err := splitFullQualifiedName(w.targetUrl)
if err != nil {
return nil, err
}
var dp protoreflect.Descriptor

dp, err = getByReflect(ctx, 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
}

// 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
}
69 changes: 69 additions & 0 deletions pkg/runner/writer_grpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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 (
"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)
api := "/writer_templates.ReportWriter/SendReportResult"
host := l.Addr().String()
url := host + api
writer := NewGRPCResultWriter(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)
api := "/writer_templates.ReportWriter/SendReportResult"
host := l.Addr().String()
url := host + api
writer := NewGRPCResultWriter(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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this server?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this server is used for testing, when receiving the report, it will print it.

// print received data
for _, result := range req.Data {
log.Printf("Received report: %+v", result)
}
return &Empty{}, nil
}
Loading
Loading