diff --git a/health.go b/health.go index 1e1f723..9d1a7b7 100644 --- a/health.go +++ b/health.go @@ -1,31 +1,21 @@ package main // File-based healthcheck for distroless containers, delegating to -// github.com/cplieger/health. +// github.com/cplieger/health. The library handles degraded mode (a +// read-only marker dir) internally, so this app keeps only thin wrappers. -import ( - "fmt" - "os" - "path/filepath" +import "github.com/cplieger/health" - "github.com/cplieger/health" -) - -// healthMarkerPath is the default marker location. +// healthMarkerPath is the marker location, sourced from the library. const healthMarkerPath = health.DefaultPath -// healthMarker wraps *health.Marker to preserve the internal API used +// healthMarker aliases *health.Marker to preserve the internal API used // by main.go and tests. -type healthMarker struct { - *health.Marker - degraded bool -} +type healthMarker = health.Marker -// newHealthMarker constructs a marker and detects degraded mode. +// newHealthMarker constructs a marker for path. func newHealthMarker(path string) *healthMarker { - m := health.NewMarker(path) - degraded := probeHealthDir(path) != nil - return &healthMarker{Marker: m, degraded: degraded} + return health.NewMarker(path) } // runProbe delegates to health.RunProbe (calls os.Exit). @@ -37,22 +27,3 @@ func runProbe(path string) { func probeCheck(path string) int { return health.ProbeCheck(path) } - -// probeHealthDir verifies the marker's parent directory is writable by -// creating and deleting a temp file. -func probeHealthDir(path string) error { - dir := filepath.Dir(path) - f, err := os.CreateTemp(dir, ".health-probe-*") - if err != nil { - return err - } - name := f.Name() - if closeErr := f.Close(); closeErr != nil { - _ = os.Remove(name) - return fmt.Errorf("close probe: %w", closeErr) - } - if rmErr := os.Remove(name); rmErr != nil { - return fmt.Errorf("remove probe: %w", rmErr) - } - return nil -} diff --git a/health_test.go b/health_test.go index 1348045..138f9c0 100644 --- a/health_test.go +++ b/health_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "testing" + "github.com/cplieger/health" "pgregory.net/rapid" ) @@ -52,15 +53,16 @@ func TestHealthMarker_DegradedMode(t *testing.T) { } path := filepath.Join(dir, ".healthy") - m := newHealthMarker(path) - if !m.degraded { - // Some environments (root, permissive filesystems like Windows - // or containers) allow writes through 0500; skip rather than - // fail in those cases. + // Some environments (root, permissive filesystems like Windows + // or containers) allow writes through 0500; skip rather than + // fail in those cases. + if health.ProbeDir(path) == nil { t.Skip("test environment bypasses directory mode; skipping") } + m := newHealthMarker(path) + m.Set(true) if _, err := os.Stat(path); !os.IsNotExist(err) { t.Fatalf("degraded marker should never create file: %v", err) @@ -119,12 +121,12 @@ func TestHealthMarker_Property(t *testing.T) { }) } -// TestProbeHealthDir_Writable confirms the probe succeeds on a normal +// TestProbeDir_Writable confirms the probe succeeds on a normal // writable temp dir and leaves no artifact behind. -func TestProbeHealthDir_Writable(t *testing.T) { +func TestProbeDir_Writable(t *testing.T) { dir := t.TempDir() - if err := probeHealthDir(filepath.Join(dir, ".healthy")); err != nil { - t.Fatalf("probeHealthDir on writable dir: %v", err) + if err := health.ProbeDir(filepath.Join(dir, ".healthy")); err != nil { + t.Fatalf("health.ProbeDir on writable dir: %v", err) } entries, err := os.ReadDir(dir) @@ -136,10 +138,10 @@ func TestProbeHealthDir_Writable(t *testing.T) { } } -// TestProbeHealthDir_NonExistent confirms a missing parent directory is +// TestProbeDir_NonExistent confirms a missing parent directory is // reported as an error rather than masked. -func TestProbeHealthDir_NonExistent(t *testing.T) { - err := probeHealthDir(filepath.Join(t.TempDir(), "nope", ".healthy")) +func TestProbeDir_NonExistent(t *testing.T) { + err := health.ProbeDir(filepath.Join(t.TempDir(), "nope", ".healthy")) if err == nil { t.Fatal("expected error for non-existent parent dir") } @@ -178,7 +180,7 @@ func TestProbeCheck_degraded_when_marker_absent_and_dir_not_writable(t *testing. path := filepath.Join(dir, ".healthy") // If the OS ignores directory mode (root, Windows), skip. - if probeHealthDir(path) == nil { + if health.ProbeDir(path) == nil { t.Skip("test environment bypasses directory mode; skipping") }