38 lines
771 B
Go
38 lines
771 B
Go
package audit
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestRecorderRecord(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "audit.jsonl")
|
|
rec := NewRecorder(path)
|
|
entry := Entry{TimeMS: 123, Action: "config.update", Success: true, RemoteIP: "1.2.3.4"}
|
|
if err := rec.Record(entry); err != nil {
|
|
t.Fatalf("record: %v", err)
|
|
}
|
|
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
scan := bufio.NewScanner(f)
|
|
if !scan.Scan() {
|
|
t.Fatalf("no log line")
|
|
}
|
|
var got Entry
|
|
if err := json.Unmarshal(scan.Bytes(), &got); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if got.Action != entry.Action || got.Success != entry.Success {
|
|
t.Fatalf("entry mismatch: %+v", got)
|
|
}
|
|
}
|