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

tests(helpers): reorganize grpc target service #14045

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion kong/tools/grpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function _M.new()
"/usr/local/opt/protobuf/include/", -- homebrew
"/usr/include",
"kong/include",
"spec/fixtures/grpc",
"spec/fixtures/grpc/proto",
} do
protoc_instance:addpath(v)
end
Expand Down
52 changes: 20 additions & 32 deletions spec/02-integration/05-proxy/19-grpc_proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ for _, strategy in helpers.each_strategy() do
reload_router(flavor)

lazy_setup(function()
assert(helpers.start_grpc_target())

local bp = helpers.get_db_utils(strategy, {
"routes",
"services",
Expand All @@ -47,12 +49,16 @@ for _, strategy in helpers.each_strategy() do

local mock_grpc_service = assert(bp.services:insert {
name = "mock_grpc_service",
url = "grpc://localhost:8765",
protocol = "grpc",
host = "127.0.0.1",
port = helpers.get_grpc_target_port(),
})

local mock_grpc_service_retry = assert(bp.services:insert {
name = "mock_grpc_service_retry",
url = "grpc://grpc_retry",
protocol = "grpc",
host = "127.0.0.1",
port = helpers.get_grpc_target_port(),
})

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

local fixtures = {
http_mock = {}
}

fixtures.http_mock.my_server_block = [[
server {
server_name myserver;
listen 8765;
http2 on;

location ~ / {
content_by_lua_block {
ngx.header.content_type = "application/grpc"
ngx.header.received_host = ngx.req.get_headers()["Host"]
}
}
}
]]

assert(helpers.start_kong({
router_flavor = flavor,
database = strategy,
nginx_conf = "spec/fixtures/custom_nginx.template",
}, nil, nil, fixtures))
}))

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

lazy_teardown(function()
helpers.stop_kong()
helpers.stop_grpc_target()
end)

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

it("proxies :authority header if `preserve_host` is set", function()
local _, resp = proxy_client_grpc({
service = "hello.HelloService.SayHello",
service = "targetservice.Bouncer.EchoHeaders",
body = {
greeting = "world!"
},
opts = {
["-proto"] = "./spec/fixtures/grpc/proto/targetservice.proto",
["-import-path"] = "./spec/fixtures/grpc/proto",
["-authority"] = "grpc_authority_1.example",
["-v"] = true,
}
})

assert.matches("received%-host: grpc_authority_1.example", resp)
local headers = cjson.decode(resp).headers
assert.matches("grpc_authority_1.example", headers[":authority"])
end)

it("sets default :authority header if `preserve_host` isn't set", function()
local _, resp = proxy_client_grpc({
service = "hello.HelloService.SayHello",
service = "targetservice.Bouncer.EchoHeaders",
body = {
greeting = "world!"
},
opts = {
["-proto"] = "./spec/fixtures/grpc/proto/targetservice.proto",
["-import-path"] = "./spec/fixtures/grpc/proto",
["-authority"] = "grpc_authority_2.example",
["-v"] = true,
}
})

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

it("proxies :authority header on balancer retry", function()
Expand Down
4 changes: 2 additions & 2 deletions spec/03-plugins/28-grpc-gateway/01-proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ for _, strategy in helpers.each_strategy() do
route = route1,
name = "grpc-gateway",
config = {
proto = "./spec/fixtures/grpc/targetservice.proto",
proto = "./spec/fixtures/grpc/proto/targetservice.proto",
},
})

Expand All @@ -64,7 +64,7 @@ for _, strategy in helpers.each_strategy() do
route = mock_grpc_route,
name = "grpc-gateway",
config = {
proto = "./spec/fixtures/grpc/targetservice.proto",
proto = "./spec/fixtures/grpc/proto/targetservice.proto",
},
})

Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/grpc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
20 changes: 20 additions & 0 deletions spec/fixtures/grpc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ALL= targetservice/targetservice.pb.go targetservice/targetservice_grpc.pb.go target

all: $(ALL)

.PHONY: go

clean:
rm -rf $(ALL)

go:
go mod tidy && go mod download all

target: go targetservice/targetservice.pb.go targetservice/targetservice_grpc.pb.go target.go
go build -o $@

targetservice/targetservice.pb.go: proto/targetservice.proto
protoc -I proto/ --go_out=. --go-grpc_out=. proto/targetservice.proto

targetservice/targetservice_grpc.pb.go: proto/targetservice.proto
protoc -I proto/ --go_out=. --go-grpc_out=. proto/targetservice.proto
18 changes: 18 additions & 0 deletions spec/fixtures/grpc/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module grpc

go 1.21

toolchain go1.22.10

require (
github.com/golang/protobuf v1.5.4
google.golang.org/genproto v0.0.0-20210716133855-ce7ef5c701ea
google.golang.org/grpc v1.65.1
google.golang.org/protobuf v1.34.1
)

require (
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
)
Loading
Loading