Skip to content

Commit

Permalink
Add more tests (#515)
Browse files Browse the repository at this point in the history
* Add tests for gRPC server and HTTP gateway
* Add docstrings
* Fix linter errors
* Add tests for healthz and version endpoints
  • Loading branch information
mostafa authored Apr 25, 2024
1 parent 11164d0 commit 2666650
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
84 changes: 84 additions & 0 deletions api/api_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package api

import (
"context"

"github.com/gatewayd-io/gatewayd/act"
"github.com/gatewayd-io/gatewayd/config"
"github.com/gatewayd-io/gatewayd/network"
"github.com/gatewayd-io/gatewayd/plugin"
"github.com/gatewayd-io/gatewayd/pool"
"github.com/rs/zerolog"
)

// getAPIConfig returns a new API configuration with all the necessary components.
func getAPIConfig() *API {
logger := zerolog.New(nil)
defaultPool := pool.NewPool(context.Background(), config.DefaultPoolSize)
pluginReg := plugin.NewRegistry(
context.Background(),
plugin.Registry{
ActRegistry: act.NewActRegistry(act.Registry{
Logger: logger,
PolicyTimeout: config.DefaultPolicyTimeout,
DefaultActionTimeout: config.DefaultActionTimeout,
Signals: act.BuiltinSignals(),
Policies: act.BuiltinPolicies(),
Actions: act.BuiltinActions(),
DefaultPolicyName: config.DefaultPolicy,
}),
DevMode: true,
Logger: logger,
Compatibility: config.DefaultCompatibilityPolicy,
StartTimeout: config.DefaultPluginStartTimeout,
},
)
defaultProxy := network.NewProxy(
context.Background(),
network.Proxy{
AvailableConnections: defaultPool,
Logger: logger,
PluginRegistry: pluginReg,
PluginTimeout: config.DefaultPluginTimeout,
HealthCheckPeriod: config.DefaultHealthCheckPeriod,
ClientConfig: &config.Client{},
},
)
servers := map[string]*network.Server{
config.Default: network.NewServer(
context.Background(),
network.Server{
Logger: logger,
Proxy: defaultProxy,
PluginRegistry: pluginReg,
PluginTimeout: config.DefaultPluginTimeout,
Network: "tcp",
Address: "localhost:15432",
},
),
}
return &API{
Options: &Options{
GRPCNetwork: "tcp",
GRPCAddress: "localhost:19090",
HTTPAddress: "localhost:18080",
Logger: logger,
Servers: servers,
},
Config: config.NewConfig(
context.Background(),
config.Config{
GlobalConfigFile: "gatewayd.yaml",
PluginConfigFile: "gatewayd_plugins.yaml",
},
),
PluginRegistry: pluginReg,
Pools: map[string]*pool.Pool{
config.Default: defaultPool,
},
Proxies: map[string]*network.Proxy{
config.Default: defaultProxy,
},
Servers: servers,
}
}
39 changes: 39 additions & 0 deletions api/grpc_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package api

import (
"context"
"testing"

v1 "github.com/gatewayd-io/gatewayd/api/v1"
"github.com/gatewayd-io/gatewayd/config"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/emptypb"
)

// Test_GRPC_Server tests the gRPC server.
func Test_GRPC_Server(t *testing.T) {
api := getAPIConfig()
healthchecker := &HealthChecker{Servers: api.Servers}
grpcServer := NewGRPCServer(GRPCServer{API: api, HealthChecker: healthchecker})
assert.NotNil(t, grpcServer)

go func(grpcServer *GRPCServer) {
grpcServer.Start()
}(grpcServer)

grpcClient, err := grpc.NewClient(
"localhost:19090", grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err)
defer grpcClient.Close()

client := v1.NewGatewayDAdminAPIServiceClient(grpcClient)
resp, err := client.Version(context.Background(), &emptypb.Empty{})
assert.Nil(t, err)
assert.NotNil(t, resp)
assert.Equal(t, config.Version, resp.GetVersion())
assert.Equal(t, config.VersionInfo(), resp.GetVersionInfo())

grpcServer.Shutdown(context.Background())
}
88 changes: 88 additions & 0 deletions api/http_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package api

import (
"context"
"encoding/json"
"io"
"net/http"
"testing"

"github.com/gatewayd-io/gatewayd/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// Test_HTTP_Server tests the HTTP to gRPC gateway.
func Test_HTTP_Server(t *testing.T) {
api := getAPIConfig()
healthchecker := &HealthChecker{Servers: api.Servers}
grpcServer := NewGRPCServer(GRPCServer{API: api, HealthChecker: healthchecker})
assert.NotNil(t, grpcServer)
httpServer := NewHTTPServer(api.Options)
assert.NotNil(t, httpServer)

go func(grpcServer *GRPCServer) {
grpcServer.Start()
}(grpcServer)

go func(httpServer *HTTPServer) {
httpServer.Start()
}(httpServer)

// Check version via the gRPC server.
req, err := http.NewRequestWithContext(
context.Background(),
http.MethodGet,
"http://localhost:18080/v1/GatewayDPluginService/Version",
nil,
)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
var respBody map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&respBody)
require.NoError(t, err)
assert.Equal(t, config.Version, respBody["version"])
assert.Equal(t, config.VersionInfo(), respBody["versionInfo"])

// Check health via the gRPC gateway.
req, err = http.NewRequestWithContext(
context.Background(),
http.MethodGet,
"http://localhost:18080/healthz",
nil,
)
require.NoError(t, err)
resp, err = http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
err = json.NewDecoder(resp.Body).Decode(&respBody)
require.NoError(t, err)
assert.Equal(t, "NOT_SERVING", respBody["status"])

// Check version via the gRPC gateway.
req, err = http.NewRequestWithContext(
context.Background(),
http.MethodGet,
"http://localhost:18080/version",
nil,
)
require.NoError(t, err)
resp, err = http.DefaultClient.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
respBodyBytes, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, len(config.Version), len(respBodyBytes))
assert.Equal(t, config.Version, string(respBodyBytes))

grpcServer.Shutdown(context.Background())
httpServer.Shutdown(context.Background())
}

0 comments on commit 2666650

Please sign in to comment.