diff --git a/network/server_test.go b/network/server_test.go index 79da4e57..c413167a 100644 --- a/network/server_test.go +++ b/network/server_test.go @@ -1,8 +1,11 @@ package network import ( + "bufio" "context" "errors" + "io" + "os" "testing" v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" @@ -21,11 +24,15 @@ func TestRunServer(t *testing.T) { errs := make(chan error) logger := logging.NewLogger(context.Background(), logging.LoggerConfig{ - Output: []config.LogOutput{config.Console}, + Output: []config.LogOutput{ + config.Console, + config.File, + }, TimeFormat: zerolog.TimeFormatUnix, ConsoleTimeFormat: config.DefaultConsoleTimeFormat, - Level: zerolog.ErrorLevel, + Level: zerolog.DebugLevel, NoColor: true, + FileName: "server_test.log", }) pluginRegistry := plugin.NewRegistry( @@ -54,7 +61,12 @@ func TestRunServer(t *testing.T) { } else { errs <- errors.New("request is not a []byte") //nolint:goerr113 } - assert.Empty(t, paramsMap["error"]) + + assert.Empty(t, paramsMap["error"], "The error MUST be empty.") + + // Add an error to the params to test if the error is logged. + params.Fields["error"] = v1.NewStringValue("test_error") + return params, nil } pluginRegistry.AddHook(v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_CLIENT, 1, onTrafficFromClient) @@ -184,6 +196,29 @@ func TestRunServer(t *testing.T) { errs <- err } close(errs) + + // Read the log file and check if the log file contains the expected log messages. + if _, err := os.Stat("server_test.log"); err == nil { + logFile, err := os.Open("server_test.log") + assert.NoError(t, err) + defer logFile.Close() + + reader := bufio.NewReader(logFile) + assert.NotNil(t, reader) + + buffer, err := io.ReadAll(reader) + assert.NoError(t, err) + assert.Greater(t, len(buffer), 0) // The log file should not be empty. + + logLines := string(buffer) + assert.Contains(t, logLines, "GatewayD is running", "GatewayD should be running") + assert.Contains(t, logLines, "GatewayD is ticking...", "GatewayD should be ticking") + assert.Contains(t, logLines, "\"error\":\"test_error\"", "The error MUST be logged") + assert.Contains(t, logLines, "Error in hook", "The error MUST be logged") + assert.Contains(t, logLines, "Ingress traffic", "Ingress traffic should be logged") + assert.Contains(t, logLines, "Egress traffic", "Egress traffic should be logged") + assert.Contains(t, logLines, "GatewayD is shutting down...", "GatewayD should be shutting down") + } }(server, errs) //nolint:thelper