Skip to content

Commit 6ff7e9d

Browse files
committed
tests(grpc): use new EchoHeaders rpc in tests
Replace template-based mock grpc service with actual gRPC service to test for `:authority` headers.
1 parent 7925142 commit 6ff7e9d

File tree

5 files changed

+86
-155
lines changed

5 files changed

+86
-155
lines changed

kong/tools/grpc.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function _M.new()
7272
"/usr/local/opt/protobuf/include/", -- homebrew
7373
"/usr/include",
7474
"kong/include",
75-
"spec/fixtures/grpc",
75+
"spec/fixtures/grpc/proto",
7676
} do
7777
protoc_instance:addpath(v)
7878
end

spec/02-integration/05-proxy/19-grpc_proxy_spec.lua

+20-32
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ for _, strategy in helpers.each_strategy() do
3030
reload_router(flavor)
3131

3232
lazy_setup(function()
33+
assert(helpers.start_grpc_target())
34+
3335
local bp = helpers.get_db_utils(strategy, {
3436
"routes",
3537
"services",
@@ -47,12 +49,16 @@ for _, strategy in helpers.each_strategy() do
4749

4850
local mock_grpc_service = assert(bp.services:insert {
4951
name = "mock_grpc_service",
50-
url = "grpc://localhost:8765",
52+
protocol = "grpc",
53+
host = "127.0.0.1",
54+
port = helpers.get_grpc_target_port(),
5155
})
5256

5357
local mock_grpc_service_retry = assert(bp.services:insert {
5458
name = "mock_grpc_service_retry",
55-
url = "grpc://grpc_retry",
59+
protocol = "grpc",
60+
host = "127.0.0.1",
61+
port = helpers.get_grpc_target_port(),
5662
})
5763

5864
local upstream_retry = assert(bp.upstreams:insert {
@@ -111,30 +117,10 @@ for _, strategy in helpers.each_strategy() do
111117
},
112118
})
113119

114-
local fixtures = {
115-
http_mock = {}
116-
}
117-
118-
fixtures.http_mock.my_server_block = [[
119-
server {
120-
server_name myserver;
121-
listen 8765;
122-
http2 on;
123-
124-
location ~ / {
125-
content_by_lua_block {
126-
ngx.header.content_type = "application/grpc"
127-
ngx.header.received_host = ngx.req.get_headers()["Host"]
128-
}
129-
}
130-
}
131-
]]
132-
133120
assert(helpers.start_kong({
134121
router_flavor = flavor,
135122
database = strategy,
136-
nginx_conf = "spec/fixtures/custom_nginx.template",
137-
}, nil, nil, fixtures))
123+
}))
138124

139125
proxy_client_grpc = helpers.proxy_client_grpc()
140126
proxy_client_grpcs = helpers.proxy_client_grpcs()
@@ -150,6 +136,7 @@ for _, strategy in helpers.each_strategy() do
150136

151137
lazy_teardown(function()
152138
helpers.stop_kong()
139+
helpers.stop_grpc_target()
153140
end)
154141

155142
it("proxies grpc", function()
@@ -228,32 +215,33 @@ for _, strategy in helpers.each_strategy() do
228215

229216
it("proxies :authority header if `preserve_host` is set", function()
230217
local _, resp = proxy_client_grpc({
231-
service = "hello.HelloService.SayHello",
218+
service = "targetservice.Bouncer.EchoHeaders",
232219
body = {
233-
greeting = "world!"
234220
},
235221
opts = {
222+
["-proto"] = "./spec/fixtures/grpc/proto/targetservice.proto",
223+
["-import-path"] = "./spec/fixtures/grpc/proto",
236224
["-authority"] = "grpc_authority_1.example",
237-
["-v"] = true,
238225
}
239226
})
240-
241-
assert.matches("received%-host: grpc_authority_1.example", resp)
227+
local headers = cjson.decode(resp).headers
228+
assert.matches("grpc_authority_1.example", headers[":authority"])
242229
end)
243230

244231
it("sets default :authority header if `preserve_host` isn't set", function()
245232
local _, resp = proxy_client_grpc({
246-
service = "hello.HelloService.SayHello",
233+
service = "targetservice.Bouncer.EchoHeaders",
247234
body = {
248-
greeting = "world!"
249235
},
250236
opts = {
237+
["-proto"] = "./spec/fixtures/grpc/proto/targetservice.proto",
238+
["-import-path"] = "./spec/fixtures/grpc/proto",
251239
["-authority"] = "grpc_authority_2.example",
252-
["-v"] = true,
253240
}
254241
})
255242

256-
assert.matches("received%-host: localhost:8765", resp)
243+
local headers = cjson.decode(resp).headers
244+
assert.matches("127.0.0.1:15010", headers[":authority"])
257245
end)
258246

259247
it("proxies :authority header on balancer retry", function()

spec/fixtures/grpc/proto/targetservice.proto

+1-6
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,8 @@ message EchoMsg {
100100
string nullable = 2;
101101
}
102102

103-
message Header {
104-
string key = 1;
105-
string value = 2;
106-
}
107-
108103
message Headers {
109-
repeated Header headers = 1;
104+
map<string, string> headers = 1;
110105
}
111106

112107
message Void {}

spec/fixtures/grpc/target.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,11 @@ func (s *server) EchoHeaders(ctx context.Context, in *pb.Void) (*pb.Headers, err
6363
return nil, status.Errorf(codes.DataLoss, "UnaryEcho: failed to get metadata")
6464
}
6565
headers := &pb.Headers{}
66+
var headersMap = make(map[string]string, len(md))
6667
for k, v := range md {
67-
header := &pb.Header{
68-
Key: k,
69-
Value: strings.Join(v, ", "),
70-
}
71-
headers.Headers = append(headers.Headers, header)
68+
headersMap[k] = strings.Join(v, ", ")
7269
}
70+
headers.Headers = headersMap
7371
return headers, nil
7472
}
7573

0 commit comments

Comments
 (0)