Skip to content

Commit

Permalink
tests: adds more coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Nov 13, 2023
1 parent 79a317b commit 562c48c
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions internal/auditlog/concurrent_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package auditlog

import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
Expand Down Expand Up @@ -46,7 +47,51 @@ func TestConcurrentWriterFailsOnInit(t *testing.T) {
}
}

func TestConcurrentWriterWrites(t *testing.T) {
type mockFormatter struct {
plugintypes.AuditLogFormatter
formatted []byte
err error
}

func (ef mockFormatter) Format(plugintypes.AuditLog) ([]byte, error) {
return ef.formatted, ef.err
}

func TestConcurrentWriter(t *testing.T) {
t.Run("empty formatted", func(t *testing.T) {
config := plugintypes.AuditLogConfig{
Target: os.DevNull,
Formatter: mockFormatter{},
}

writer := &concurrentWriter{}
if err := writer.Init(config); err != nil {
t.Errorf("unexpected error: %v", err)
}

if err := writer.Write(nil); err != nil {
t.Errorf("unexpected error: %v", err)
}
})

t.Run("formatting error", func(t *testing.T) {
config := plugintypes.AuditLogConfig{
Target: os.DevNull,
Formatter: mockFormatter{err: errors.New("formatting error")},
}

writer := &concurrentWriter{}
if err := writer.Init(config); err != nil {
t.Errorf("unexpected error: %v", err)
}

if err := writer.Write(nil); err == nil {
t.Errorf("expected error: %v", err)
}
})
}

func TestConcurrentWriterSuccess(t *testing.T) {
dir := t.TempDir()
file, err := os.Create(filepath.Join(dir, "audit.log"))
if err != nil {
Expand All @@ -59,6 +104,12 @@ func TestConcurrentWriterWrites(t *testing.T) {
DirMode: fs.FileMode(0777),
Formatter: &jsonFormatter{},
}

writer := &concurrentWriter{}
if err := writer.Init(config); err != nil {
t.Error("failed to init concurrent logger", err)
}

ts := time.Now()
expectedLog := &Log{
Transaction_: Transaction{
Expand All @@ -74,10 +125,6 @@ func TestConcurrentWriterWrites(t *testing.T) {
},
},
}
writer := &concurrentWriter{}
if err := writer.Init(config); err != nil {
t.Error("failed to init concurrent logger", err)
}
if err := writer.Write(expectedLog); err != nil {
t.Error("failed to write to logger: ", err)
}
Expand Down

0 comments on commit 562c48c

Please sign in to comment.