feat(go): advance validation sidecar toward production
This commit is contained in:
parent
6a06eba255
commit
f85c2e831a
92 changed files with 8881 additions and 91 deletions
71
go-agent/internal/health/heartbeat_test.go
Normal file
71
go-agent/internal/health/heartbeat_test.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package health
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHeartbeatRoundTripAndPermissions(t *testing.T) {
|
||||
directory := filepath.Join(t.TempDir(), "state")
|
||||
want := time.Unix(1_753_012_345, 987_654_321)
|
||||
if err := WriteHeartbeat(directory, want); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := ReadHeartbeat(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !got.Equal(time.Unix(want.Unix(), 0)) {
|
||||
t.Fatalf("heartbeat=%s want=%s", got, want)
|
||||
}
|
||||
info, err := os.Stat(filepath.Join(directory, Filename))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := info.Mode().Perm(); got != 0o600 {
|
||||
t.Fatalf("mode=%#o", got)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(directory, Filename+".tmp")); !os.IsNotExist(err) {
|
||||
t.Fatalf("temporary heartbeat remains: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeartbeatRejectsMissingDirectory(t *testing.T) {
|
||||
if err := WriteHeartbeat("", time.Now()); err == nil {
|
||||
t.Fatal("empty directory accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadHeartbeatRejectsInvalidContent(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(directory, Filename), []byte("invalid\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ReadHeartbeat(directory); err == nil {
|
||||
t.Fatal("invalid heartbeat accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckReportsFreshStaleAndMissing(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
now := time.Unix(2_000, 0)
|
||||
if err := WriteHeartbeat(directory, now.Add(-30*time.Second)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fresh := Check(directory, now, time.Minute)
|
||||
if !fresh.Healthy || fresh.AgeSeconds != 30 || fresh.Detail != "heartbeat is fresh" {
|
||||
t.Fatalf("fresh=%#v", fresh)
|
||||
}
|
||||
stale := Check(directory, now, 10*time.Second)
|
||||
if stale.Healthy || stale.Detail != "heartbeat is stale" || stale.MaxAgeSeconds != 10 {
|
||||
t.Fatalf("stale=%#v", stale)
|
||||
}
|
||||
missing := Check(filepath.Join(directory, "missing"), now, time.Minute)
|
||||
if missing.Healthy || missing.Detail == "" {
|
||||
t.Fatalf("missing=%#v", missing)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue