Skip to content

Commit

Permalink
Add test for api/healthcheck.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Apr 21, 2024
1 parent acfe75f commit be1c6d1
Showing 1 changed file with 94 additions and 0 deletions.
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.Status)

Check failure on line 89 in api/healthcheck_test.go

View workflow job for this annotation

GitHub Actions / Test GatewayD

avoid direct access to proto field hcr.Status, use hcr.GetStatus() instead (protogetter)

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

0 comments on commit be1c6d1

Please sign in to comment.