-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathwriter_grpc.go
More file actions
125 lines (110 loc) · 3.23 KB
/
writer_grpc.go
File metadata and controls
125 lines (110 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
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"
"io"
"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) WithAPICoverage(apiConverage apispec.APICoverage) ReportResultWriter {
return w
}
func (w *grpcResultWriter) WithResourceUsage([]ResourceUsage) ReportResultWriter {
return w
}
func (w *grpcResultWriter) GetWriter() io.Writer {
return nil
}