Files
Pulse/pkg/pbs/backup_inventory_envelope_test.go
pulse-triage[bot] 72e777d962 Require explicit PBS backup inventory arrays before reconciling absence
Missing or null data in namespace, group or snapshot responses is an unread inventory, not an authoritative empty array. Preserve the existing malformed-read retention, provider-evidence and ordinary recovery boundaries.

Contract-Neutral: Enforce the existing failed/malformed PBS enumeration and complete-success-only reconciliation contract; public payloads, endpoints, access, identity, retention policy and legacy namespace 404 compatibility are unchanged.
Change-source: pulse-maintainer
2026-10-29 07:29:10 +00:00

57 lines
1.8 KiB
Go

package pbs
import (
"context"
"net/http"
"testing"
)
// A successful HTTP status without an inventory array is an unread scope, not
// an accepted empty listing. In particular, decoding JSON null is not a parse
// error in Go and must not silently authorize backup-history reconciliation.
func TestBackupInventoryRequiresExplicitArray(t *testing.T) {
readers := []struct {
name string
read func(*Client) (int, error)
}{
{"namespaces", func(c *Client) (int, error) {
rows, err := c.ListNamespaces(context.Background(), "archive", "tenant", 2)
return len(rows), err
}},
{"groups", func(c *Client) (int, error) {
rows, err := c.ListBackupGroups(context.Background(), "archive", "tenant")
return len(rows), err
}},
{"snapshots", func(c *Client) (int, error) {
rows, err := c.ListBackupSnapshots(context.Background(), "archive", "tenant", "vm", "101")
return len(rows), err
}},
}
for _, reader := range readers {
for _, body := range []string{`null`, `{}`, `{"data":null}`, `{"unrelated":[]}`, `{"data":{}}`, `{"data":false}`, `{"data":`, `{"data":[]}`} {
t.Run(reader.name+"/"+body, func(t *testing.T) {
reads := 0
client, server := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
reads++
if r.Method != http.MethodGet {
t.Errorf("inventory read made a %s request", r.Method)
}
_, _ = w.Write([]byte(body))
})
defer server.Close()
count, err := reader.read(client)
if body == `{"data":[]}` {
if err != nil || count != 0 {
t.Fatalf("explicit empty array rejected: count=%d err=%v", count, err)
}
} else if err == nil || count != 0 {
t.Fatalf("unread scope accepted: count=%d err=%v", count, err)
}
if reads != 1 {
t.Fatalf("inventory admission caused %d requests, want one", reads)
}
})
}
}
}