From be1c6d1a7c5470a361297b019b0ac2c28179d70a Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sun, 21 Apr 2024 17:43:30 +0200 Subject: [PATCH] Add test for api/healthcheck.go --- api/healthcheck_test.go | 94 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 api/healthcheck_test.go diff --git a/api/healthcheck_test.go b/api/healthcheck_test.go new file mode 100644 index 00000000..300f6ee9 --- /dev/null +++ b/api/healthcheck_test.go @@ -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) + + err = healthchecker.Watch(&grpc_health_v1.HealthCheckRequest{}, nil) + assert.Error(t, err) + assert.Equal(t, "rpc error: code = Unimplemented desc = not implemented", err.Error()) +}