Skip to content

Commit

Permalink
add/update logging tests
Browse files Browse the repository at this point in the history
Signed-off-by: James Ranson <james@ranson.org>
  • Loading branch information
jranson committed Aug 16, 2024
1 parent c98f5fd commit 82e77ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
14 changes: 14 additions & 0 deletions pkg/observability/logging/level/level_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package level

import "testing"

func TestGetLevelID(t *testing.T) {
id := GetLevelID("invalid")
if id != 0 {
t.Errorf("expected %d got %d", 0, id)
}
id = GetLevelID(Info)
if id != InfoID {
t.Errorf("expected %d got %d", InfoID, id)
}
}
21 changes: 11 additions & 10 deletions pkg/observability/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestNewLogger_LogFile(t *testing.T) {
logger.SetLogAsynchronous(false)
logger.Info("testEntry ", Pairs{"testKey": "test Val", "testKey2": "testValue2"})
if _, err := os.Stat(instanceFileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}
Expand All @@ -83,7 +83,7 @@ func TestNewLoggerDebug_LogFile(t *testing.T) {
logger.SetLogAsynchronous(false)
logger.Debug("test entry", Pairs{"testKey": "testVal"})
if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}
Expand All @@ -98,7 +98,7 @@ func TestNewLoggerWarn_LogFile(t *testing.T) {
logger.SetLogAsynchronous(false)
logger.Warn("test entry", Pairs{"testKey": "testVal"})
if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}
Expand All @@ -108,7 +108,7 @@ func TestNewLoggerWarnOnce_LogFile(t *testing.T) {
// it should create a logger that outputs to a log file ("out.test.log")
conf := config.NewConfig()
conf.Main = &config.MainConfig{InstanceID: 0}
conf.Logging = &options.Options{LogFile: fileName, LogLevel: "x"}
conf.Logging = &options.Options{LogFile: fileName, LogLevel: "info"}
logger := New(conf)
logger.SetLogAsynchronous(false)
key := "warnonce-test-key"
Expand Down Expand Up @@ -136,9 +136,10 @@ func TestNewLoggerWarnOnce_LogFile(t *testing.T) {
}

if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
os.Remove(fileName)
}

func TestNewLoggerError_LogFile(t *testing.T) {
Expand All @@ -151,7 +152,7 @@ func TestNewLoggerError_LogFile(t *testing.T) {
logger.SetLogAsynchronous(false)
logger.Error("test entry", Pairs{"testKey": "testVal"})
if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}
Expand All @@ -176,7 +177,7 @@ func TestNewLoggerErrorOnce_LogFile(t *testing.T) {
}

if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}
Expand All @@ -191,7 +192,7 @@ func TestNewLoggerDefault_LogFile(t *testing.T) {
logger.SetLogAsynchronous(false)
logger.Info("test entry", Pairs{"testKey": "testVal"})
if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}
Expand All @@ -215,7 +216,7 @@ func TestNewLoggerInfoOnce_LogFile(t *testing.T) {
}

if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}

logger.Close()
Expand All @@ -231,7 +232,7 @@ func TestNewLoggerFatal_LogFile(t *testing.T) {
logger.SetLogAsynchronous(false)
logger.Fatal(-1, "test entry", Pairs{"testKey": "testVal"})
if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/engines/access_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestLogUpstreamRequest(t *testing.T) {
logUpstreamRequest(logger, "testBackend", "testType", "testHandler", "testMethod",
"testPath", "testUserAgent", 200, 0, 1.0)
if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}
Expand All @@ -58,7 +58,7 @@ func TestLogDownstreamRequest(t *testing.T) {
logDownstreamRequest(logger, r)

if _, err := os.Stat(fileName); err != nil {
t.Errorf(err.Error())
t.Error(err)
}
logger.Close()
}

0 comments on commit 82e77ea

Please sign in to comment.