-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support send report to a gRPC server (#431)
- Loading branch information
1 parent
8b2c8f2
commit 50798ff
Showing
8 changed files
with
698 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.