87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package observability
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRotatingFileBoundsArchives_HP_OPS_02(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "nested", "rvbox.log")
|
|
writer, err := OpenRotatingFile(path, 5, 2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := writer.Write([]byte("first")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := writer.Write([]byte("two")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := writer.Write([]byte("three")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := writer.Write([]byte("four")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := writer.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
current, err := os.ReadFile(path)
|
|
if err != nil || string(current) != "four" {
|
|
t.Fatalf("current = %q, %v", current, err)
|
|
}
|
|
newest, err := os.ReadFile(path + ".1")
|
|
if err != nil || string(newest) != "three" {
|
|
t.Fatalf("newest archive = %q, %v", newest, err)
|
|
}
|
|
oldest, err := os.ReadFile(path + ".2")
|
|
if err != nil || string(oldest) != "two" {
|
|
t.Fatalf("oldest archive = %q, %v", oldest, err)
|
|
}
|
|
if _, err := os.Stat(path + ".3"); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("unexpected third archive: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRotatingFileRejectsPartialRotationConfig_BH_OPS_03(t *testing.T) {
|
|
t.Parallel()
|
|
for _, limits := range [][2]uint64{{1, 0}, {0, 1}} {
|
|
_, err := OpenRotatingFile(filepath.Join(t.TempDir(), "rvbox.log"), limits[0], uint32(limits[1]))
|
|
if err == nil || !strings.Contains(err.Error(), "both be zero") {
|
|
t.Fatalf("limits %v error = %v", limits, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFormatLogWritesStructuredBoundedRecords_HP_OPS_04(t *testing.T) {
|
|
t.Parallel()
|
|
var structured bytes.Buffer
|
|
if _, err := FormatLog(&structured, "json").Write([]byte("connected\nrecovered\n")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lines := bytes.Split(bytes.TrimSpace(structured.Bytes()), []byte{'\n'})
|
|
if len(lines) != 2 {
|
|
t.Fatalf("structured records = %q", structured.String())
|
|
}
|
|
for index, want := range []string{"connected", "recovered"} {
|
|
var record struct {
|
|
Time string `json:"time"`
|
|
Level string `json:"level"`
|
|
Message string `json:"message"`
|
|
}
|
|
if err := json.Unmarshal(lines[index], &record); err != nil || record.Time == "" || record.Level != "info" || record.Message != want {
|
|
t.Fatalf("record %d = %+v, %v", index, record, err)
|
|
}
|
|
}
|
|
var plain bytes.Buffer
|
|
if _, err := FormatLog(&plain, "text").Write([]byte("plain\n")); err != nil || plain.String() != "plain\n" {
|
|
t.Fatalf("text log = %q, %v", plain.String(), err)
|
|
}
|
|
}
|