Skip to content
quarry Artifact [76989d922d]
Login

Artifact [76989d922d]

Artifact 76989d922d5b6f63563551f803d0a0048f30946290bf8706d6a7382ab9efcf23:


package obs

import (
	"errors"
	"testing"
)

type failingSink struct{ err error }

func (f failingSink) Render(Render)         {}
func (f failingSink) Subprocess(Subprocess) {}
func (f failingSink) Err() error            { return f.err }

// Switched off and broken must not look the same. One is a choice the
// operator made; the other is the forge failing to do what it was told,
// and the live panels look identical either way because they are fed by
// the rings.
func TestHealthDistinguishesOffFromBroken(t *testing.T) {
	r := NewRecorder(8, nil)
	if h := r.Health(); h.Configured || h.Err != "" {
		t.Fatalf("a fresh recorder reports %+v, want unconfigured and healthy", h)
	}
	r.SetHealth(Health{Configured: true, Path: "/tmp/ops.duckdb", Kept: "14d"})
	h := r.Health()
	if !h.Configured || h.Err != "" || h.Path != "/tmp/ops.duckdb" || h.Kept != "14d" {
		t.Fatalf("configured store reports %+v", h)
	}
	r.SetHealth(Health{Configured: true, Path: "/tmp/ops.duckdb", Err: "schema: at is reserved"})
	if h := r.Health(); h.Err == "" {
		t.Fatal("a store that failed to open reports healthy")
	}
}

// A store that opened and then started failing its writes is the case
// the open-time record cannot see, so Health has to ask the sink.
func TestHealthFoldsInTheSinksWriteFailure(t *testing.T) {
	r := NewRecorder(8, nil)
	r.SetHealth(Health{Configured: true, Path: "/tmp/ops.duckdb"})
	r.SetSink(failingSink{})
	if h := r.Health(); h.Err != "" {
		t.Fatalf("healthy sink reported %q", h.Err)
	}
	r.SetSink(failingSink{err: errors.New("disk full")})
	if h := r.Health(); h.Err != "disk full" {
		t.Fatalf("write failure reported as %q, want \"disk full\"", h.Err)
	}
}

// A sink that cannot fail must not be interrogated into looking broken:
// the default nop sink has no Err at all.
func TestHealthToleratesASinkWithoutErr(t *testing.T) {
	r := NewRecorder(8, nil)
	r.SetHealth(Health{Configured: true})
	r.SetSink(nil)
	if h := r.Health(); h.Err != "" {
		t.Fatalf("nop sink reported %q", h.Err)
	}
}