MSP-1069 update tests and benchmark
This commit is contained in:
1 parent
2b329d5270
commit
01027e6f29
2 files changed
+65
-26
No files matched your search
+38
-11
@@ -8,6 +8,11 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"isp-gate-service/assembly"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/routes"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/google/uuid"
|
||||
@@ -20,10 +25,7 @@ import (
|
||||
"github.com/integration-system/isp-kit/test/grpct"
|
||||
"github.com/integration-system/isp-kit/test/httpt"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"isp-gate-service/assembly"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/routes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
type request struct {
|
||||
@@ -40,7 +42,7 @@ type HappyPathTestSuite struct {
|
||||
|
||||
func (s *HappyPathTestSuite) TestGrpcProxy() {
|
||||
test, require := test.New(s.T())
|
||||
config, redisCli, systemCli := s.commonDependencies(test)
|
||||
config, redisCli, systemCli, adminCli := s.commonDependencies(test)
|
||||
|
||||
requestId := requestid.Next()
|
||||
targetService, targetCli := grpct.NewMock(test)
|
||||
@@ -58,12 +60,18 @@ func (s *HappyPathTestSuite) TestGrpcProxy() {
|
||||
require.EqualValues(2, domainId)
|
||||
require.EqualValues(3, serviceId)
|
||||
require.EqualValues(4, applicationId)
|
||||
|
||||
adminId, err := grpc.StringFromMd("x-admin-id", metadata.MD(authData))
|
||||
require.NoError(err)
|
||||
require.EqualValues("1", adminId)
|
||||
|
||||
return response{Id: req.Id} //nolint:gosimple
|
||||
})
|
||||
targetClients := map[string]*client.Client{"target": targetCli}
|
||||
logger, err := log.New(log.WithLevel(log.DebugLevel))
|
||||
require.NoError(err)
|
||||
locator := assembly.NewLocator(logger, targetClients, nil, routes.NewRoutes(), systemCli)
|
||||
locator := assembly.NewLocator(logger, targetClients, nil, routes.NewRoutes(), systemCli, adminCli)
|
||||
|
||||
locations := []conf.Location{{
|
||||
SkipAuth: false,
|
||||
PathPrefix: "/api",
|
||||
@@ -80,6 +88,7 @@ func (s *HappyPathTestSuite) TestGrpcProxy() {
|
||||
_, err = cli.R().
|
||||
SetHeader("x-application-token", "token").
|
||||
SetHeader("x-request-id", requestId).
|
||||
SetHeader("x-auth-admin", "mock-token").
|
||||
SetBody(req).
|
||||
SetResult(&resp).
|
||||
Post("/api/endpoint")
|
||||
@@ -89,7 +98,7 @@ func (s *HappyPathTestSuite) TestGrpcProxy() {
|
||||
|
||||
func (s *HappyPathTestSuite) TestHttpProxy() {
|
||||
test, require := test.New(s.T())
|
||||
config, redisCli, systemCli := s.commonDependencies(test)
|
||||
config, redisCli, systemCli, adminCli := s.commonDependencies(test)
|
||||
|
||||
requestId := requestid.Next()
|
||||
targetService := httpt.NewMock(test)
|
||||
@@ -107,13 +116,18 @@ func (s *HappyPathTestSuite) TestHttpProxy() {
|
||||
require.EqualValues(2, domainId)
|
||||
require.EqualValues(3, serviceId)
|
||||
require.EqualValues(4, applicationId)
|
||||
|
||||
adminId, err := strconv.Atoi(httpReq.Header.Get("x-admin-id"))
|
||||
require.NoError(err)
|
||||
require.EqualValues(1, adminId)
|
||||
|
||||
return response{Id: req.Id} //nolint:gosimple
|
||||
})
|
||||
targetUrl, err := url.Parse(targetService.BaseURL())
|
||||
require.NoError(err)
|
||||
rr := lb.NewRoundRobin([]string{targetUrl.Host})
|
||||
targetClients := map[string]*lb.RoundRobin{"target": rr}
|
||||
locator := assembly.NewLocator(test.Logger(), nil, targetClients, routes.NewRoutes(), systemCli)
|
||||
locator := assembly.NewLocator(test.Logger(), nil, targetClients, routes.NewRoutes(), systemCli, adminCli)
|
||||
locations := []conf.Location{{
|
||||
SkipAuth: false,
|
||||
PathPrefix: "/api",
|
||||
@@ -130,6 +144,7 @@ func (s *HappyPathTestSuite) TestHttpProxy() {
|
||||
_, err = cli.R().
|
||||
SetHeader("x-application-token", "token").
|
||||
SetHeader("x-request-id", requestId).
|
||||
SetHeader("x-auth-admin", "mock-token").
|
||||
SetBody(req).
|
||||
SetResult(&resp).
|
||||
Post("/api/endpoint")
|
||||
@@ -137,10 +152,11 @@ func (s *HappyPathTestSuite) TestHttpProxy() {
|
||||
require.EqualValues(req.Id, resp.Id)
|
||||
}
|
||||
|
||||
func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, redis.UniversalClient, *client.Client) {
|
||||
func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, redis.UniversalClient, *client.Client, *client.Client) {
|
||||
require := test.Assert()
|
||||
redisCli := NewRedis(test)
|
||||
ctx := context.Background()
|
||||
|
||||
s.T().Cleanup(func() {
|
||||
_, err := redisCli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, 0)
|
||||
@@ -153,15 +169,16 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, r
|
||||
})
|
||||
require.NoError(err)
|
||||
})
|
||||
|
||||
config := conf.Remote{
|
||||
Redis: &conf.Redis{Address: redisCli.address},
|
||||
Secrets: conf.Secrets{AdminTokenSecret: "secret"},
|
||||
Http: conf.Http{MaxRequestBodySizeInMb: 1, ProxyTimeoutInSec: 15},
|
||||
Logging: conf.Logging{LogLevel: log.DebugLevel, RequestLogEnable: true, BodyLogEnable: true},
|
||||
Caching: conf.Caching{AuthorizationDataInSec: 1, AuthenticationDataInSec: 1},
|
||||
DailyLimits: []conf.DailyLimit{{ApplicationId: 1, RequestsPerDay: 100}},
|
||||
Throttling: []conf.Throttling{{ApplicationId: 1, RequestsPerSeconds: 100}},
|
||||
}
|
||||
|
||||
systemService, systemCli := grpct.NewMock(test)
|
||||
systemService.Mock("system/secure/authenticate", func() domain.AuthenticateResponse {
|
||||
return domain.AuthenticateResponse{
|
||||
@@ -177,7 +194,17 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, r
|
||||
}).Mock("system/secure/authorize", func() domain.AuthorizeResponse {
|
||||
return domain.AuthorizeResponse{Authorized: true}
|
||||
})
|
||||
return config, redisCli, systemCli
|
||||
|
||||
adminService, adminCli := grpct.NewMock(test)
|
||||
adminService.Mock("admin/secure/authenticate", func() domain.AdminAuthenticateResponse {
|
||||
return domain.AdminAuthenticateResponse{
|
||||
Authenticated: true,
|
||||
ErrorReason: "",
|
||||
AdminId: 1,
|
||||
}
|
||||
})
|
||||
|
||||
return config, redisCli, systemCli, adminCli
|
||||
}
|
||||
|
||||
func TestHappyPathTestSuite(t *testing.T) {
|
||||
|
||||
+27
-15
@@ -5,16 +5,17 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"isp-gate-service/assembly"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/routes"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/integration-system/isp-kit/grpc"
|
||||
"github.com/integration-system/isp-kit/grpc/client"
|
||||
endpoint2 "github.com/integration-system/isp-kit/grpc/endpoint"
|
||||
"github.com/integration-system/isp-kit/grpc/isp"
|
||||
"github.com/integration-system/isp-kit/log"
|
||||
"isp-gate-service/assembly"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/routes"
|
||||
)
|
||||
|
||||
type request struct {
|
||||
@@ -46,14 +47,14 @@ func main() {
|
||||
}()
|
||||
config := conf.Remote{
|
||||
Redis: &conf.Redis{Address: "localhost:6379"},
|
||||
Secrets: conf.Secrets{AdminTokenSecret: "secret"},
|
||||
Http: conf.Http{MaxRequestBodySizeInMb: 1, ProxyTimeoutInSec: 15},
|
||||
Logging: conf.Logging{LogLevel: log.DebugLevel, RequestLogEnable: true, BodyLogEnable: true},
|
||||
Caching: conf.Caching{AuthorizationDataInSec: 15, AuthenticationDataInSec: 15},
|
||||
DailyLimits: []conf.DailyLimit{{ApplicationId: 1, RequestsPerDay: 100000000}},
|
||||
Throttling: []conf.Throttling{{ApplicationId: 1, RequestsPerSeconds: 5000}},
|
||||
}
|
||||
systemService, systemCli := NewMock(logger)
|
||||
|
||||
systemService, systemCli, _ := NewMock(logger)
|
||||
systemService.Mock("system/secure/authenticate", func() domain.AuthenticateResponse {
|
||||
return domain.AuthenticateResponse{
|
||||
Authenticated: true,
|
||||
@@ -69,13 +70,22 @@ func main() {
|
||||
return domain.AuthorizeResponse{Authorized: true}
|
||||
})
|
||||
|
||||
targetService, targetCli := NewMock(logger)
|
||||
adminService, _, adminCli := NewMock(logger)
|
||||
adminService.Mock("admin/secure/authenticate", func() domain.AdminAuthenticateResponse {
|
||||
return domain.AdminAuthenticateResponse{
|
||||
Authenticated: true,
|
||||
ErrorReason: "",
|
||||
AdminId: 1,
|
||||
}
|
||||
})
|
||||
|
||||
targetService, targetCli, _ := NewMock(logger)
|
||||
targetService.Mock("endpoint", func(req request) response {
|
||||
return response{Id: req.Id}
|
||||
})
|
||||
targetClients := map[string]*client.Client{"target": targetCli}
|
||||
logger, _ = log.New(log.WithLevel(log.DebugLevel))
|
||||
locator := assembly.NewLocator(logger, targetClients, nil, routes.NewRoutes(), systemCli)
|
||||
locator := assembly.NewLocator(logger, targetClients, nil, routes.NewRoutes(), systemCli, adminCli)
|
||||
locations := []conf.Location{{
|
||||
SkipAuth: false,
|
||||
PathPrefix: "/api",
|
||||
@@ -91,17 +101,19 @@ func main() {
|
||||
srv.ListenAndServe()
|
||||
}
|
||||
|
||||
func TestServer(service isp.BackendServiceServer) (*grpc.Server, *client.Client) {
|
||||
func TestServer(service isp.BackendServiceServer) (*grpc.Server, *client.Client, *client.Client) {
|
||||
listener, _ := net.Listen("tcp", "127.0.0.1:")
|
||||
srv := grpc.NewServer()
|
||||
cli, _ := client.Default()
|
||||
sysCli, _ := client.Default()
|
||||
adminCli, _ := client.Default()
|
||||
srv.Upgrade(service)
|
||||
go func() {
|
||||
_ = srv.Serve(listener)
|
||||
}()
|
||||
|
||||
cli.Upgrade([]string{listener.Addr().String()})
|
||||
return srv, cli
|
||||
sysCli.Upgrade([]string{listener.Addr().String()})
|
||||
adminCli.Upgrade([]string{listener.Addr().String()})
|
||||
return srv, sysCli, adminCli
|
||||
}
|
||||
|
||||
type MockServer struct {
|
||||
@@ -110,13 +122,13 @@ type MockServer struct {
|
||||
mockEndpoints map[string]interface{}
|
||||
}
|
||||
|
||||
func NewMock(logger log.Logger) (*MockServer, *client.Client) {
|
||||
srv, cli := TestServer(grpc.NewMux())
|
||||
func NewMock(logger log.Logger) (*MockServer, *client.Client, *client.Client) {
|
||||
srv, syscli, admincli := TestServer(grpc.NewMux())
|
||||
return &MockServer{
|
||||
srv: srv,
|
||||
logger: logger,
|
||||
mockEndpoints: make(map[string]interface{}),
|
||||
}, cli
|
||||
}, syscli, admincli
|
||||
}
|
||||
|
||||
func (m *MockServer) Mock(endpoint string, handler interface{}) *MockServer {
|
||||
|
||||
Reference in new issue
Block a user