Skip to content

Commit

Permalink
Check for error and the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Sep 16, 2023
1 parent f9ac4e5 commit bd29d47
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions network/server_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package network

import (
"bufio"
"context"
"errors"
"io"
"os"
"testing"

v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bd29d47

Please sign in to comment.