initial commit
This commit is contained in:
commit
885e7d6e02
22 changed files with 3001 additions and 0 deletions
93
internal/parser/error_test.go
Normal file
93
internal/parser/error_test.go
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package parser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestParseErrorLine_WithConnID(t *testing.T) {
|
||||
line := `2024/02/20 16:45:01 [error] 1234#5678: *99 connect() failed (111: Connection refused) while connecting to upstream`
|
||||
entry, err := ParseErrorLine(line)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
want := time.Date(2024, 2, 20, 16, 45, 1, 0, time.UTC)
|
||||
if !entry.Time.Equal(want) {
|
||||
t.Errorf("Time = %v, want %v", entry.Time, want)
|
||||
}
|
||||
if entry.Level != "error" {
|
||||
t.Errorf("Level = %q, want %q", entry.Level, "error")
|
||||
}
|
||||
if entry.PID != 1234 {
|
||||
t.Errorf("PID = %d, want 1234", entry.PID)
|
||||
}
|
||||
if entry.TID != 5678 {
|
||||
t.Errorf("TID = %d, want 5678", entry.TID)
|
||||
}
|
||||
if entry.ConnID != 99 {
|
||||
t.Errorf("ConnID = %d, want 99", entry.ConnID)
|
||||
}
|
||||
if entry.Message != "connect() failed (111: Connection refused) while connecting to upstream" {
|
||||
t.Errorf("Message = %q", entry.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseErrorLine_WithoutConnID(t *testing.T) {
|
||||
line := `2024/02/20 17:00:00 [warn] 1234#5678: worker process 9999 exited on signal 15`
|
||||
entry, err := ParseErrorLine(line)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if entry.Level != "warn" {
|
||||
t.Errorf("Level = %q, want %q", entry.Level, "warn")
|
||||
}
|
||||
if entry.ConnID != 0 {
|
||||
t.Errorf("ConnID = %d, want 0", entry.ConnID)
|
||||
}
|
||||
if entry.Message != "worker process 9999 exited on signal 15" {
|
||||
t.Errorf("Message = %q", entry.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseErrorLine_AllLevels(t *testing.T) {
|
||||
levels := []string{"debug", "info", "notice", "warn", "error", "crit", "alert", "emerg"}
|
||||
for _, level := range levels {
|
||||
line := "2024/01/01 00:00:00 [" + level + "] 100#200: test message"
|
||||
entry, err := ParseErrorLine(line)
|
||||
if err != nil {
|
||||
t.Errorf("level %q: unexpected error: %v", level, err)
|
||||
continue
|
||||
}
|
||||
if entry.Level != level {
|
||||
t.Errorf("level %q: got Level = %q", level, entry.Level)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseErrorLine_Info(t *testing.T) {
|
||||
line := `2024/02/21 08:30:00 [info] 1234#5678: start worker process 10001`
|
||||
entry, err := ParseErrorLine(line)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if entry.Level != "info" {
|
||||
t.Errorf("Level = %q, want info", entry.Level)
|
||||
}
|
||||
if entry.PID != 1234 {
|
||||
t.Errorf("PID = %d, want 1234", entry.PID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseErrorLine_Malformed(t *testing.T) {
|
||||
cases := []string{
|
||||
"this is a malformed line",
|
||||
"",
|
||||
"2024/02/20 not-a-proper-format",
|
||||
}
|
||||
for _, c := range cases {
|
||||
_, err := ParseErrorLine(c)
|
||||
if err == nil {
|
||||
t.Errorf("expected error for malformed line %q, got nil", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue