Skip to content

Commit

Permalink
Add more tests (#513)
Browse files Browse the repository at this point in the history
* Add test for act/buitins.go
* Add pgx to allow list of tests
* Update test to cover more lines
* Add test for embed_swagger.go
* Add test for api/healthcheck.go
* Test Execute function
* Redirect stdout and stderr to buffer
* Create a directory before generating markdown files
* Add test for cmd/gen_docs.go
* Gofumpt
* Fix magic number
* Fix output message
* Test multi-tenant config
* Update config function signatures to return errors instead of log.Fatal that runs os.Exit
* Add test for config validation
* Fix linter errors
* Add test for missing config file
* Add more tests for config package
* Add tests for syslog and rsyslog
* Add tests for hclog log level
* Test hclog logging with different levels
* Add test for header bypass response writer
* Test removal of plugin from metrics merger and start/stop of the merger scheduler
* Add test for network/conn_wrapper.go
* Address linter issues
* Remove read/write test
* Test more plugin registry function
* Add test for OTLP tracer
  • Loading branch information
mostafa authored Apr 24, 2024
1 parent 5147137 commit 11164d0
Show file tree
Hide file tree
Showing 27 changed files with 856 additions and 92 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ libtensorflow*
# Test generated files
cmd/test_*.yaml.bak
cmd/test_*.yaml
cmd/docs/*

# docker-compose
gatewayd-files/
cmd/gatewayd-plugin-cache-linux-amd64-*

1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ linters-settings:
- "github.com/spf13/cobra"
- "github.com/knadh/koanf"
- "github.com/spf13/cast"
- "github.com/jackc/pgx/v5/pgproto3"
tagalign:
align: false
sort: false
Expand Down
131 changes: 131 additions & 0 deletions act/builtins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package act

import (
"testing"

sdkAct "github.com/gatewayd-io/gatewayd-plugin-sdk/act"
"github.com/gatewayd-io/gatewayd-plugin-sdk/databases/postgres"
gerr "github.com/gatewayd-io/gatewayd/errors"
"github.com/jackc/pgx/v5/pgproto3"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_Terminate_Action(t *testing.T) {
response, err := (&pgproto3.Terminate{}).Encode(
postgres.ErrorResponse(
"Request terminated",
"ERROR",
"42000",
"Policy terminated the request",
),
)
require.NoError(t, err)

tests := []struct {
params []sdkAct.Parameter
result any
err error
}{
{
params: []sdkAct.Parameter{},
result: nil,
err: gerr.ErrLoggerRequired,
},
{
params: []sdkAct.Parameter{
{
Key: LoggerKey,
Value: nil,
},
},
result: nil,
err: gerr.ErrLoggerRequired,
},
{
params: []sdkAct.Parameter{
{
Key: LoggerKey,
Value: zerolog.New(nil),
},
},
result: true,
},
{
params: []sdkAct.Parameter{
{
Key: LoggerKey,
Value: zerolog.New(nil),
},
{
Key: ResultKey,
Value: nil,
},
},
result: true,
},
{
params: []sdkAct.Parameter{
{
Key: LoggerKey,
Value: zerolog.New(nil),
},
{
Key: ResultKey,
Value: map[string]any{},
},
},
result: map[string]any{"response": response},
},
}

for _, test := range tests {
t.Run("Test_Terminate_Action", func(t *testing.T) {
result, err := Terminate(nil, test.params...)
assert.ErrorIs(t, err, test.err)
assert.Equal(t, result, test.result)
})
}
}

func Test_Log_Action(t *testing.T) {
tests := []struct {
params []sdkAct.Parameter
result any
err error
}{
{
params: []sdkAct.Parameter{},
result: nil,
err: gerr.ErrLoggerRequired,
},
{
params: []sdkAct.Parameter{
{
Key: LoggerKey,
Value: nil,
},
},
result: nil,
err: gerr.ErrLoggerRequired,
},
{
params: []sdkAct.Parameter{
{
Key: LoggerKey,
Value: zerolog.New(nil),
},
},
result: true,
},
}

for _, test := range tests {
t.Run("Test_Log_Action", func(t *testing.T) {
result, err := Log(nil, test.params...)
assert.ErrorIs(t, err, test.err)
assert.Equal(t, result, test.result)
})
}
}
37 changes: 31 additions & 6 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
pluginV1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
"github.com/gatewayd-io/gatewayd/act"
v1 "github.com/gatewayd-io/gatewayd/api/v1"
"github.com/gatewayd-io/gatewayd/config"
Expand All @@ -30,7 +31,8 @@ func TestGetGlobalConfig(t *testing.T) {
// Load config from the default config file.
conf := config.NewConfig(context.TODO(),
config.Config{GlobalConfigFile: "../gatewayd.yaml", PluginConfigFile: "../gatewayd_plugins.yaml"})
conf.InitConfig(context.TODO())
gerr := conf.InitConfig(context.TODO())
require.Nil(t, gerr)
assert.NotEmpty(t, conf.Global)

api := API{
Expand All @@ -53,13 +55,18 @@ func TestGetGlobalConfigWithGroupName(t *testing.T) {
// Load config from the default config file.
conf := config.NewConfig(context.TODO(),
config.Config{GlobalConfigFile: "../gatewayd.yaml", PluginConfigFile: "../gatewayd_plugins.yaml"})
conf.InitConfig(context.TODO())
gerr := conf.InitConfig(context.TODO())
require.Nil(t, gerr)
assert.NotEmpty(t, conf.Global)

api := API{
Config: conf,
}
globalConfig, err := api.GetGlobalConfig(context.Background(), &v1.Group{GroupName: nil})
defaultGroup := config.Default
globalConfig, err := api.GetGlobalConfig(
context.Background(),
&v1.Group{GroupName: &defaultGroup},
)
require.NoError(t, err)
globalconf := globalConfig.AsMap()
assert.NotEmpty(t, globalconf)
Expand All @@ -71,7 +78,7 @@ func TestGetGlobalConfigWithGroupName(t *testing.T) {
assert.NotEmpty(t, globalconf["servers"])
assert.NotEmpty(t, globalconf["metrics"])
assert.NotEmpty(t, globalconf["api"])
if _, ok := globalconf["loggers"].(map[string]interface{})["default"]; !ok {
if _, ok := globalconf["loggers"].(map[string]interface{})[config.Default]; !ok {
t.Errorf("loggers.default is not found")
}
}
Expand All @@ -80,7 +87,8 @@ func TestGetGlobalConfigWithNonExistingGroupName(t *testing.T) {
// Load config from the default config file.
conf := config.NewConfig(context.TODO(),
config.Config{GlobalConfigFile: "../gatewayd.yaml", PluginConfigFile: "../gatewayd_plugins.yaml"})
conf.InitConfig(context.TODO())
gerr := conf.InitConfig(context.TODO())
require.Nil(t, gerr)
assert.NotEmpty(t, conf.Global)

api := API{
Expand All @@ -96,7 +104,8 @@ func TestGetPluginConfig(t *testing.T) {
// Load config from the default config file.
conf := config.NewConfig(context.TODO(),
config.Config{GlobalConfigFile: "../gatewayd.yaml", PluginConfigFile: "../gatewayd_plugins.yaml"})
conf.InitConfig(context.TODO())
gerr := conf.InitConfig(context.TODO())
require.Nil(t, gerr)
assert.NotEmpty(t, conf.Global)

api := API{
Expand Down Expand Up @@ -136,6 +145,17 @@ func TestGetPlugins(t *testing.T) {
RemoteURL: "plugin-url",
Checksum: "plugin-checksum",
},
Requires: []sdkPlugin.Identifier{
{
Name: "plugin1-name",
Version: "plugin1-version",
RemoteURL: "plugin1-url",
Checksum: "plugin1-checksum",
},
},
Hooks: []pluginV1.HookName{
pluginV1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_CLIENT,
},
})

api := API{
Expand All @@ -145,6 +165,11 @@ func TestGetPlugins(t *testing.T) {
require.NoError(t, err)
assert.NotEmpty(t, plugins)
assert.NotEmpty(t, plugins.GetConfigs())
assert.NotEmpty(t, plugins.GetConfigs()[0].GetRequires())
assert.Equal(
t,
int32(pluginV1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_CLIENT),
plugins.GetConfigs()[0].GetHooks()[0])
}

func TestGetPluginsWithEmptyPluginRegistry(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions api/embed_swagger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package api

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_IsSwaggerEmbedded(t *testing.T) {
assert.False(t, IsSwaggerEmbedded())
}
94 changes: 94 additions & 0 deletions api/healthcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package api

import (
"context"
"testing"

"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"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/health/grpc_health_v1"
)

func Test_Healthchecker(t *testing.T) {
clientConfig := &config.Client{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
}
client := network.NewClient(context.TODO(), clientConfig, zerolog.Logger{}, nil)
newPool := pool.NewPool(context.TODO(), 1)
require.NotNil(t, newPool)
assert.Nil(t, newPool.Put(client.ID, client))

proxy := network.NewProxy(
context.TODO(),
network.Proxy{
AvailableConnections: newPool,
HealthCheckPeriod: config.DefaultHealthCheckPeriod,
ClientConfig: &config.Client{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
},
Logger: zerolog.Logger{},
PluginTimeout: config.DefaultPluginTimeout,
},
)

actRegistry := act.NewActRegistry(
act.Registry{
Signals: act.BuiltinSignals(),
Policies: act.BuiltinPolicies(),
Actions: act.BuiltinActions(),
DefaultPolicyName: config.DefaultPolicy,
PolicyTimeout: config.DefaultPolicyTimeout,
DefaultActionTimeout: config.DefaultActionTimeout,
Logger: zerolog.Logger{},
})

pluginRegistry := plugin.NewRegistry(
context.TODO(),
plugin.Registry{
ActRegistry: actRegistry,
Compatibility: config.Loose,
Logger: zerolog.Logger{},
DevMode: true,
},
)

server := network.NewServer(
context.TODO(),
network.Server{
Network: config.DefaultNetwork,
Address: config.DefaultAddress,
TickInterval: config.DefaultTickInterval,
Options: network.Option{
EnableTicker: false,
},
Proxy: proxy,
Logger: zerolog.Logger{},
PluginRegistry: pluginRegistry,
PluginTimeout: config.DefaultPluginTimeout,
HandshakeTimeout: config.DefaultHandshakeTimeout,
},
)

healthchecker := HealthChecker{
Servers: map[string]*network.Server{
config.Default: server,
},
}
assert.NotNil(t, healthchecker)
hcr, err := healthchecker.Check(context.TODO(), &grpc_health_v1.HealthCheckRequest{})
assert.NoError(t, err)
assert.NotNil(t, hcr)
assert.Equal(t, grpc_health_v1.HealthCheckResponse_NOT_SERVING, hcr.GetStatus())

err = healthchecker.Watch(&grpc_health_v1.HealthCheckRequest{}, nil)
assert.Error(t, err)
assert.Equal(t, "rpc error: code = Unimplemented desc = not implemented", err.Error())
}
Loading

0 comments on commit 11164d0

Please sign in to comment.