-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathharness.go
More file actions
265 lines (239 loc) · 8.19 KB
/
Copy pathharness.go
File metadata and controls
265 lines (239 loc) · 8.19 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package codegen
import (
"fmt"
"path/filepath"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/codegen/service"
"goa.design/goa/v3/expr"
)
type (
// harnessData contains the data used to generate test harness code.
// It embeds Goa's service.Data to leverage existing codegen structures.
harnessData struct {
// Embed the base service data from Goa's codegen
*service.Data
// Service expression for additional metadata
ServiceExpr *expr.ServiceExpr
// HasHTTP indicates if service has HTTP transport
HasHTTP bool
// HasGRPC indicates if service has gRPC transport
HasGRPC bool
// HasJSONRPC indicates if service has JSON-RPC transport
HasJSONRPC bool
// HasStreams indicates if service has streaming methods
HasStreams bool
// Methods with transport target information
Methods []*harnessMethodData
}
// harnessMethodData extends Goa's MethodData with transport targets.
// This allows us to leverage existing method data while adding test-specific info.
harnessMethodData struct {
// Embed the base method data from Goa's codegen
*service.MethodData
// Targets lists all supported transport combinations for this method
Targets []*suiteTarget
}
)
// generateHarness generates the test harness file for a service.
func generateHarness(genpkg string, svcData *service.Data, root *expr.RootExpr, svc *expr.ServiceExpr) *codegen.File {
path := filepath.Join(testingPath(genpkg, svc), "harness.go")
if svcData == nil {
return nil
}
data := buildHarnessData(svcData, root, svc)
specs := []*codegen.ImportSpec{
{Path: "context"},
{Path: "encoding/json"},
{Path: "fmt"},
{Path: "io"},
{Path: "net/http"},
{Path: "net/http/httptest"},
{Path: "strings"},
{Path: "testing"},
{Path: "time"},
{Path: filepath.Join(genpkg, codegen.SnakeCase(svc.Name)), Name: svcData.PkgName},
{Path: "goa.design/goa/v3/pkg"},
}
// Add transport-specific imports
if data.HasHTTP {
specs = append(specs,
&codegen.ImportSpec{Path: "bytes"},
&codegen.ImportSpec{Path: "bufio"},
&codegen.ImportSpec{Path: filepath.Join(genpkg, "http", codegen.SnakeCase(svc.Name), "server"), Name: "httpsvr"},
&codegen.ImportSpec{Path: filepath.Join(genpkg, "http", codegen.SnakeCase(svc.Name), "client"), Name: "httpcli"},
&codegen.ImportSpec{Path: "goa.design/goa/v3/http", Name: "goahttp"},
&codegen.ImportSpec{Path: "net/url"},
&codegen.ImportSpec{Path: "github.com/gorilla/websocket"},
)
}
if data.HasGRPC {
specs = append(specs,
&codegen.ImportSpec{Path: "net"},
&codegen.ImportSpec{Path: "google.golang.org/grpc"},
&codegen.ImportSpec{Path: "google.golang.org/grpc/test/bufconn"},
&codegen.ImportSpec{Path: "goa.design/goa/v3/grpc", Name: "goagrpc"},
&codegen.ImportSpec{Path: filepath.Join(genpkg, "grpc", codegen.SnakeCase(svc.Name), "server"), Name: "grpcsvr"},
&codegen.ImportSpec{Path: filepath.Join(genpkg, "grpc", codegen.SnakeCase(svc.Name), "client"), Name: "grpccli"},
&codegen.ImportSpec{Path: filepath.Join(genpkg, "grpc", codegen.SnakeCase(svc.Name), "pb"), Name: svcData.PkgName + "pb"},
)
}
if data.HasJSONRPC {
specs = append(specs,
&codegen.ImportSpec{Path: "net/url"},
&codegen.ImportSpec{Path: filepath.Join(genpkg, "jsonrpc", codegen.SnakeCase(svc.Name), "server"), Name: "jsonrpcsvr"},
&codegen.ImportSpec{Path: filepath.Join(genpkg, "jsonrpc", codegen.SnakeCase(svc.Name), "client"), Name: "jsonrpccli"},
&codegen.ImportSpec{Path: "goa.design/goa/v3/jsonrpc", Name: "jsonrpc"},
&codegen.ImportSpec{Path: "goa.design/goa/v3/http", Name: "goahttp"},
)
}
sections := []*codegen.SectionTemplate{
codegen.Header(fmt.Sprintf("Test harness for %s service", svc.Name), codegen.SnakeCase(svc.Name)+"test", specs),
{
Name: "harness-struct",
Source: testingTemplates.Read(harnessStructT),
Data: data,
},
{
Name: "harness-constructor",
Source: testingTemplates.Read(harnessConstructorT),
Data: data,
},
}
// Transport setup sections
if data.HasHTTP {
sections = append(sections, &codegen.SectionTemplate{Name: "http-harness", Source: testingTemplates.Read(httpHarnessT), Data: data})
}
if data.HasGRPC {
sections = append(sections, &codegen.SectionTemplate{Name: "grpc-harness", Source: testingTemplates.Read(grpcHarnessT), Data: data})
}
if data.HasJSONRPC {
sections = append(sections, &codegen.SectionTemplate{Name: "jsonrpc-harness", Source: testingTemplates.Read(jsonrpcHarnessT), Data: data})
}
return &codegen.File{
Path: path,
SectionTemplates: sections,
}
}
// buildHarnessData builds the template data for the harness.
func buildHarnessData(svcData *service.Data, root *expr.RootExpr, svc *expr.ServiceExpr) *harnessData {
// Create harness data using Goa's service.Data directly
data := &harnessData{
Data: svcData,
ServiceExpr: svc,
HasHTTP: hasHTTPTransport(root, svc),
HasGRPC: hasGRPCTransport(root, svc),
HasJSONRPC: hasJSONRPCTransport(root, svc),
HasStreams: hasStreams(svc),
Methods: make([]*harnessMethodData, 0, len(svcData.Methods)),
}
// Build method data with test-specific extensions
for i, m := range svc.Methods {
md := svcData.Methods[i]
// Build targets for this method using shared function
targets := buildMethodTargets(root, svc, m, md)
// Create harness method data
hmd := &harnessMethodData{
MethodData: md,
Targets: targets,
}
data.Methods = append(data.Methods, hmd)
}
return data
}
// hasHTTPTransport checks if the service has HTTP transport.
func hasHTTPTransport(root *expr.RootExpr, svc *expr.ServiceExpr) bool {
// Check if service is in HTTP services
if root != nil && root.API != nil && root.API.HTTP != nil {
for _, s := range root.API.HTTP.Services {
if s.Name() == svc.Name {
return true
}
}
}
return false
}
// hasGRPCTransport checks if the service has gRPC transport.
func hasGRPCTransport(root *expr.RootExpr, svc *expr.ServiceExpr) bool {
// Check if service is in gRPC services
if root != nil && root.API != nil && root.API.GRPC != nil {
for _, s := range root.API.GRPC.Services {
if s.Name() == svc.Name {
return true
}
}
}
return false
}
// hasJSONRPCTransport checks if the service has JSON-RPC transport.
func hasJSONRPCTransport(root *expr.RootExpr, svc *expr.ServiceExpr) bool {
if root != nil && root.API != nil && root.API.JSONRPC != nil {
for _, s := range root.API.JSONRPC.Services {
if s.Name() == svc.Name {
return true
}
}
}
return false
}
// testingPath returns the path to the testing package for the given service.
func testingPath(_ string, svc *expr.ServiceExpr) string {
// Use service-specific package name to avoid import collisions
return filepath.Join(codegen.Gendir, codegen.SnakeCase(svc.Name), codegen.SnakeCase(svc.Name)+"test")
}
// hasStreams checks if the service has streaming methods.
func hasStreams(svc *expr.ServiceExpr) bool {
for _, m := range svc.Methods {
if m.IsStreaming() {
return true
}
}
return false
}
// hasMethodHTTP checks whether the given method is bound to HTTP transport.
func hasMethodHTTP(root *expr.RootExpr, svc *expr.ServiceExpr, m *expr.MethodExpr) bool {
if root != nil && root.API != nil && root.API.HTTP != nil {
for _, hs := range root.API.HTTP.Services {
if hs.Name() != svc.Name {
continue
}
for _, hm := range hs.HTTPEndpoints {
if hm.MethodExpr == m {
return true
}
}
}
}
return false
}
// hasMethodGRPC checks whether the given method is bound to gRPC transport.
func hasMethodGRPC(root *expr.RootExpr, svc *expr.ServiceExpr, m *expr.MethodExpr) bool {
if root != nil && root.API != nil && root.API.GRPC != nil {
for _, gs := range root.API.GRPC.Services {
if gs.Name() != svc.Name {
continue
}
for _, gm := range gs.GRPCEndpoints {
if gm.MethodExpr == m {
return true
}
}
}
}
return false
}
// hasMethodJSONRPC checks whether the given method is bound to JSON-RPC transport.
func hasMethodJSONRPC(root *expr.RootExpr, svc *expr.ServiceExpr, m *expr.MethodExpr) bool {
if root != nil && root.API != nil && root.API.JSONRPC != nil {
for _, js := range root.API.JSONRPC.Services {
if js.Name() != svc.Name {
continue
}
for _, hm := range js.HTTPEndpoints {
if hm.MethodExpr == m {
return true
}
}
}
}
return false
}