Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Reject incomplete Proxmox snapshot response inventories
pulse-triage[bot]4f0018789bbe
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.mdindex a4c84ffc76..7d3aa81132 100644--- a/docs/release-control/v6/internal/subsystems/monitoring.md+++ b/docs/release-control/v6/internal/subsystems/monitoring.md@@ -8368,3 +8368,24 @@ FQDN and IP dial URLs, and the dial URL itself are unchanged.`internal/monitoring/monitor_pve_cluster_refresh_test.go` registers theunpinned strict-member link, override precedence and the URL builder cases(custom port, IPv6, FQDN, IP and missing address).++### Snapshot HTTP success must be a complete inventory++Both QEMU and LXC snapshot readers admit only a fully read, bounded HTTP-200+response with one unambiguous JSON object and an explicit `data` array. Missing,+null, error-bearing, duplicate/case-overwritten or trailing envelopes and invalid+rows return no inventory, rather than an authoritative empty or partial list.+Named snapshots require a nonblank unique name and a positive creation time;+Proxmox's `current` marker is filtered without requiring a timestamp. Unique+provider metadata, valid snapshots, explicit empty arrays and current-only lists+remain supported. Response bodies do not become admission diagnostics. Existing+transport, auth, cluster failover, guest command and polling budgets are unchanged.++`TestSnapshotInventoryResponseAdmission` and the accepted/completed-response+controls exercise both actual HTTP readers. `TestPVESnapshotHTTPInvalidResponseRetainsHistory`+connects them to snapshot polling, warnings, asynchronous ingest and the public+recovery-store reader across warm/cold caches, failed responses, accepted peers,+complete recovery and confirmed scoped removal. `TestPVESnapshotHTTPBrowserProducer`+feeds the actual consuming proof. These are synthetic HTTP/provider fixtures,+not native guest safety, backup execution, installed recovery or a diagnosis of+#3248. The separate prior lifecycle-suite failure remains unexplained.diff --git a/internal/monitoring/snapshot_inventory_http_test.go b/internal/monitoring/snapshot_inventory_http_test.gonew file mode 100644index 0000000000..b132a42f04--- /dev/null+++ b/internal/monitoring/snapshot_inventory_http_test.go@@ -0,0 +1,243 @@+package monitoring++import (+ "context"+ "encoding/json"+ "fmt"+ "net/http"+ "net/http/httptest"+ "os"+ "reflect"+ "strings"+ "sync"+ "testing"+ "time"++ "github.com/rcourtman/pulse-go-rewrite/internal/models"+ "github.com/rcourtman/pulse-go-rewrite/internal/recovery"+ "github.com/rcourtman/pulse-go-rewrite/pkg/proxmox"+)++// The snapshot calls use the production HTTP client; unrelated storage calls+// stay empty. No QGA, backup job or external service is used by this fixture.+type snapshotInventoryHTTPClient struct {+ mockPVEClientExtra+ api *proxmox.Client+}++func (c *snapshotInventoryHTTPClient) GetVMSnapshots(ctx context.Context, node string, vmid int) ([]proxmox.Snapshot, error) {+ return c.api.GetVMSnapshots(ctx, node, vmid)+}++func (c *snapshotInventoryHTTPClient) GetContainerSnapshots(ctx context.Context, node string, vmid int) ([]proxmox.Snapshot, error) {+ return c.api.GetContainerSnapshots(ctx, node, vmid)+}++func (*snapshotInventoryHTTPClient) GetStorage(context.Context, string) ([]proxmox.Storage, error) {+ return nil, nil+}++type snapshotInventoryHTTPFixture struct {+ f *snapshotRecoveryFixture+ client *snapshotInventoryHTTPClient+ mu sync.Mutex+ bodies map[string]string+ requests int+}++func snapshotInventoryBody(name string, at time.Time) string {+ return fmt.Sprintf(`{"data":[{"name":"current"},{"name":%q,"snaptime":%d,"description":"accepted listing"}]}`, name, at.Unix())+}++func newSnapshotInventoryHTTPFixture(t *testing.T, kind string) *snapshotInventoryHTTPFixture {+ t.Helper()+ m := snapshotCollectionMonitor(t, kind, true)+ _, manager := recoveryIngestTestMonitor(t)+ m.recoveryManager = manager+ store, err := manager.StoreForOrg("default")+ if err != nil {+ t.Fatal(err)+ }+ f := &snapshotRecoveryFixture{m: m, store: store, kind: kind}+ t.Cleanup(func() { f.wait(t); _ = store.Close() })+ at := time.Now().Add(-16 * 24 * time.Hour)+ h := &snapshotInventoryHTTPFixture{f: f, bodies: map[string]string{+ "node-a": snapshotInventoryBody("weekly", at),+ "node-b": snapshotInventoryBody("weekly", at),+ }}+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ h.mu.Lock()+ defer h.mu.Unlock()+ h.requests+++ for node, body := range h.bodies {+ vmid := 101+ if node == "node-b" {+ vmid = 202+ }+ if r.URL.Path == fmt.Sprintf("/api2/json/nodes/%s/%s/%d/snapshot", node, kind, vmid) {+ w.Header().Set("Content-Type", "application/json")+ fmt.Fprint(w, body)+ return+ }+ }+ t.Errorf("unexpected fixture request: %s", r.URL.Path)+ http.NotFound(w, r)+ }))+ t.Cleanup(server.Close)+ api, err := proxmox.NewClient(proxmox.ClientConfig{+ Host: server.URL, TokenName: "fixture@pve!pulse", TokenValue: "fixture", Timeout: 5 * time.Second,+ })+ if err != nil {+ t.Fatal(err)+ }+ h.client = &snapshotInventoryHTTPClient{api: api}+ return h+}++func (h *snapshotInventoryHTTPFixture) set(node, body string) {+ h.mu.Lock()+ defer h.mu.Unlock()+ h.bodies[node] = body+}++func (h *snapshotInventoryHTTPFixture) poll(t *testing.T, instance string) {+ t.Helper()+ h.mu.Lock()+ before := h.requests+ h.mu.Unlock()+ h.f.m.pollGuestSnapshots(context.Background(), instance, h.client)+ h.f.wait(t)+ want := 2+ if instance == "other-site" {+ want = 1+ }+ h.mu.Lock()+ requests := h.requests - before+ h.mu.Unlock()+ if requests != want {+ t.Fatalf("poll made %d HTTP reads, want %d (no retries or other operations)", requests, want)+ }+}++func TestPVESnapshotHTTPInvalidResponseRetainsHistory(t *testing.T) {+ cases := map[string]string{+ "missing-data": `{}`,+ "null-data": `{"data":null}`,+ "error-envelope": `{"data":[],"errors":{"message":"fixture-private-detail"}}`,+ "duplicate-data": `{"data":[{"name":"unaccepted","snaptime":1700000000}],"data":[]}`,+ "trailing-value": `{"data":[]} {"data":[]}`,+ "null-row": `{"data":[null]}`,+ "invalid-time": `{"data":[{"name":"unaccepted"}]}`,+ "bad-row-after-good": `{"data":[{"name":"unaccepted","snaptime":1700000000},null]}`,+ }+ for _, kind := range []string{"qemu", "lxc"} {+ for _, cold := range []bool{false, true} {+ for name, body := range cases {+ t.Run(fmt.Sprintf("%s/cold=%t/%s", kind, cold, name), func(t *testing.T) {+ h := newSnapshotInventoryHTTPFixture(t, kind)+ h.poll(t, "site")+ h.poll(t, "other-site")+ before := h.f.points(t)+ if len(before) != 3 {+ t.Fatalf("control has %d points, want 3", len(before))+ }+ beforeRows := snapshotCollectionByID(h.f.m)+ warning := snapshotCollectionAlert(t, h.f.m, "site:node-a:101")+ if err := h.f.m.alertManager.AcknowledgeAlert(warning.ID, "operator"); err != nil {+ t.Fatal(err)+ }+ warning = snapshotCollectionAlert(t, h.f.m, "site:node-a:101")+ if cold {+ h.f.coldState(true)+ }+ h.set("node-a", body)+ h.set("node-b", snapshotInventoryBody("fresh-peer", time.Now().Add(-time.Hour)))+ for range 2 {+ h.poll(t, "site")+ }+ after := h.f.points(t)+ failedID, otherID := "pve-snapshot:site-node-a-101-weekly", "pve-snapshot:other-site-node-a-101-weekly"+ if !reflect.DeepEqual(before[failedID], after[failedID]) || !reflect.DeepEqual(before[otherID], after[otherID]) {+ t.Fatal("invalid HTTP success deleted or renewed failed/unrelated durable history")+ }+ if len(after) != 4 || after["pve-snapshot:site-node-b-202-fresh-peer"].ID == "" {+ t.Fatalf("unaccepted rows ingested or accepted peer lost: %+v", after)+ }+ if !cold {+ rows := snapshotCollectionByID(h.f.m)+ if rows["site-node-a-101-weekly"] != beforeRows["site-node-a-101-weekly"] || rows["other-site-node-a-101-weekly"] != beforeRows["other-site-node-a-101-weekly"] {+ t.Fatal("invalid success changed retained live membership")+ }+ if got := snapshotCollectionAlert(t, h.f.m, "site:node-a:101"); got.ID != warning.ID || !got.StartTime.Equal(warning.StartTime) || !got.Acknowledged || !reflect.DeepEqual(got.Metadata["snapshotCreatedAt"], warning.Metadata["snapshotCreatedAt"]) {+ t.Fatalf("invalid success changed warning occurrence/acknowledgement: before=%+v after=%+v", warning, got)+ }+ }+ h.set("node-a", snapshotInventoryBody("recovered", time.Now().Add(-2*time.Hour)))+ h.poll(t, "site")+ after = h.f.points(t)+ if len(after) != 3 || after["pve-snapshot:site-node-a-101-recovered"].ID == "" || !reflect.DeepEqual(before[otherID], after[otherID]) {+ t.Fatal("complete recovery failed to replace only its own snapshots")+ }+ h.set("node-a", `{"data":[]}`)+ h.set("node-b", `{"data":[{"name":"current"}]}`)+ h.poll(t, "site")+ after = h.f.points(t)+ if len(after) != 1 || !reflect.DeepEqual(before[otherID], after[otherID]) {+ t.Fatal("explicit empty/current-only listing failed to clear only its scope")+ }+ for _, alert := range h.f.m.GetState().ActiveAlerts {+ if strings.HasPrefix(alert.ResourceID, "site:") && alert.Type == "snapshot-age" {+ t.Fatal("confirmed empty inventory retained snapshot warning")+ }+ }+ })+ }+ }+ }+}++// Opt-in JSON uses the same HTTP reader, poller, queue and public store API as+// the regression. It is a producer for the production consuming/browser proof.+func TestPVESnapshotHTTPBrowserProducer(t *testing.T) {+ h := newSnapshotInventoryHTTPFixture(t, "qemu")+ h.poll(t, "other-site")+ type frame struct {+ State models.StateSnapshot `json:"state"`+ Points []recovery.RecoveryPoint `json:"points"`+ }+ frames := map[string]frame{}+ for _, phase := range []string{"observed", "missing-data", "invalid-row", "recovered", "confirmed-empty"} {+ switch phase {+ case "missing-data":+ h.set("node-a", `{}`)+ case "invalid-row":+ h.set("node-a", `{"data":[{"name":"unaccepted","snaptime":1700000000},null]}`)+ case "recovered":+ h.set("node-a", snapshotInventoryBody("recovered", time.Now().Add(-time.Hour)))+ case "confirmed-empty":+ h.set("node-a", `{"data":[]}`)+ h.set("node-b", `{"data":[{"name":"current"}]}`)+ }+ if phase == "missing-data" {+ h.set("node-b", snapshotInventoryBody("fresh-peer", time.Now().Add(-2*time.Hour)))+ }+ h.poll(t, "site")+ points, _, err := h.f.store.ListPoints(context.Background(), recovery.ListPointsOptions{Limit: 1000})+ if err != nil {+ t.Fatal(err)+ }+ state := h.f.m.GetState()+ wantPoints := map[string]int{"observed": 3, "missing-data": 4, "invalid-row": 4, "recovered": 3, "confirmed-empty": 1}[phase]+ if len(points) != wantPoints {+ t.Fatalf("phase %s: got %d points, want %d", phase, len(points), wantPoints)+ }+ frames[phase] = frame{State: state, Points: points}+ }+ if os.Getenv("PULSE_SNAPSHOT_HTTP_FRAMES") == "1" {+ data, err := json.Marshal(frames)+ if err != nil {+ t.Fatal(err)+ }+ fmt.Printf("SNAPSHOT_HTTP_FRAMES=%s\n", data)+ }+}diff --git a/pkg/proxmox/client.go b/pkg/proxmox/client.goindex 20bca9088b..eb342ba1a7 100644--- a/pkg/proxmox/client.go+++ b/pkg/proxmox/client.go@@ -1588,26 +1588,7 @@ func (c *Client) GetVMSnapshots(ctx context.Context, node string, vmid int) ([]Sif err != nil {return nil, err}- defer resp.Body.Close()-- var result struct {- Data []Snapshot `json:"data"`- }-- if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {- return nil, err- }-- // Filter out the 'current' snapshot which is not a real snapshot- var snapshots []Snapshot- for _, snap := range result.Data {- if snap.Name != "current" {- snap.VMID = vmid- snapshots = append(snapshots, snap)- }- }-- return snapshots, nil+ return readSnapshotInventory(resp, vmid)}// GetContainerSnapshots returns snapshots for a specific container@@ -1616,26 +1597,7 @@ func (c *Client) GetContainerSnapshots(ctx context.Context, node string, vmid inif err != nil {return nil, err}- defer resp.Body.Close()-- var result struct {- Data []Snapshot `json:"data"`- }-- if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {- return nil, err- }-- // Filter out the 'current' snapshot which is not a real snapshot- var snapshots []Snapshot- for _, snap := range result.Data {- if snap.Name != "current" {- snap.VMID = vmid- snapshots = append(snapshots, snap)- }- }-- return snapshots, nil+ return readSnapshotInventory(resp, vmid)}// ClusterStatus represents the cluster status responsediff --git a/pkg/proxmox/client_api_more_test.go b/pkg/proxmox/client_api_more_test.goindex bd72a5ee60..0dce4d55ba 100644--- a/pkg/proxmox/client_api_more_test.go+++ b/pkg/proxmox/client_api_more_test.go@@ -94,11 +94,11 @@ func TestClientSnapshotsAndContent(t *testing.T) {})case "/api2/json/nodes/node1/qemu/100/snapshot":writeJSON(t, w, map[string]interface{}{- "data": []Snapshot{{Name: "current"}, {Name: "snap1"}},+ "data": []Snapshot{{Name: "current"}, {Name: "snap1", SnapTime: 1700000000}},})case "/api2/json/nodes/node1/lxc/101/snapshot":writeJSON(t, w, map[string]interface{}{- "data": []Snapshot{{Name: "current"}, {Name: "snap2"}},+ "data": []Snapshot{{Name: "current"}, {Name: "snap2", SnapTime: 1700000001}},})default:http.NotFound(w, r)diff --git a/pkg/proxmox/cluster_client_api_test.go b/pkg/proxmox/cluster_client_api_test.goindex 723bce7ecc..4472d2cb1d 100644--- a/pkg/proxmox/cluster_client_api_test.go+++ b/pkg/proxmox/cluster_client_api_test.go@@ -85,7 +85,7 @@ func TestClusterClient_GetVMSnapshots(t *testing.T) {fmt.Fprint(w, `{"data":[{"node":"node1","status":"online"}]}`)return}- fmt.Fprint(w, `{"data":[{"name":"snap1","description":"first"}]}`)+ fmt.Fprint(w, `{"data":[{"name":"snap1","description":"first","snaptime":1700000000}]}`)}))defer server.Close()@@ -133,7 +133,7 @@ func TestClusterClient_ConfiguredAuthorityDoesNotWaitForMemberRecovery(t *testinswitch r.URL.Path {case "/api2/json/nodes/node1/qemu/100/snapshot":authorityDataCalls.Add(1)- fmt.Fprint(w, `{"data":[{"name":"snap-authority","description":"cluster snapshot"}]}`)+ fmt.Fprint(w, `{"data":[{"name":"snap-authority","description":"cluster snapshot","snaptime":1700000000}]}`)case "/api2/json/nodes/node1/storage/local/content":authorityDataCalls.Add(1)fmt.Fprint(w, `{"data":[{"volid":"local:backup/vzdump-qemu-100.vma.zst","format":"vma.zst","size":1000,"content":"backup"}]}`)@@ -495,7 +495,7 @@ func TestClusterClient_GetContainerSnapshots(t *testing.T) {fmt.Fprint(w, `{"data":[{"node":"node1","status":"online"}]}`)return}- fmt.Fprint(w, `{"data":[{"name":"snap1","description":"desc"}]}`)+ fmt.Fprint(w, `{"data":[{"name":"snap1","description":"desc","snaptime":1700000000}]}`)}))defer server.Close()diff --git a/pkg/proxmox/snapshot_inventory_response.go b/pkg/proxmox/snapshot_inventory_response.gonew file mode 100644index 0000000000..26f7f0c94d--- /dev/null+++ b/pkg/proxmox/snapshot_inventory_response.go@@ -0,0 +1,65 @@+package proxmox++import (+ "bytes"+ "encoding/json"+ "errors"+ "net/http"+ "strings"+ "unicode/utf8"+)++// An accepted snapshot inventory owns removal and recovery-point evidence.+// Never turn a malformed success into absence, or copy its body into diagnostics.+var errInvalidSnapshotInventoryResponse = errors.New("invalid Proxmox snapshot inventory response")++func readSnapshotInventory(resp *http.Response, vmid int) ([]Snapshot, error) {+ body, readErr := readResponseBodyLimited(resp.Body)+ closeErr := resp.Body.Close()+ if readErr != nil || closeErr != nil {+ return nil, errors.Join(readErr, closeErr)+ }+ if resp.StatusCode != http.StatusOK || !utf8.Valid(body) {+ return nil, errInvalidSnapshotInventoryResponse+ }+ envelope, valid := inventoryResponseObject(body, "data", "errors")+ if !valid {+ return nil, errInvalidSnapshotInventoryResponse+ }+ if failure, exists := envelope["errors"]; exists && !bytes.Equal(bytes.TrimSpace(failure), []byte("null")) {+ return nil, errInvalidSnapshotInventoryResponse+ }+ data := bytes.TrimSpace(envelope["data"])+ // Only an explicit array is an enumeration. Read and close the whole+ // bounded response before admitting even an otherwise valid JSON prefix.+ if len(data) == 0 || data[0] != '[' {+ return nil, errInvalidSnapshotInventoryResponse+ }+ var rows []json.RawMessage+ if json.Unmarshal(data, &rows) != nil {+ return nil, errInvalidSnapshotInventoryResponse+ }+ snapshots := make([]Snapshot, 0, len(rows))+ seen := make(map[string]bool, len(rows))+ for _, row := range rows {+ if _, valid := inventoryResponseObject(row, "name", "description", "snaptime", "parent", "vmid"); !valid {+ return nil, errInvalidSnapshotInventoryResponse+ }+ var snap Snapshot+ if json.Unmarshal(row, &snap) != nil || strings.TrimSpace(snap.Name) == "" || seen[snap.Name] {+ return nil, errInvalidSnapshotInventoryResponse+ }+ seen[snap.Name] = true+ // Proxmox's current-position marker is not a recovery artifact and+ // legitimately has no creation time. Named snapshots must have one.+ if snap.Name == "current" {+ continue+ }+ if snap.SnapTime <= 0 {+ return nil, errInvalidSnapshotInventoryResponse+ }+ snap.VMID = vmid+ snapshots = append(snapshots, snap)+ }+ return snapshots, nil+}diff --git a/pkg/proxmox/snapshot_inventory_response_test.go b/pkg/proxmox/snapshot_inventory_response_test.gonew file mode 100644index 0000000000..183fc74b5b--- /dev/null+++ b/pkg/proxmox/snapshot_inventory_response_test.go@@ -0,0 +1,147 @@+package proxmox++import (+ "context"+ "fmt"+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "sync/atomic"+ "testing"+)++func readTestSnapshotInventory(c *Client, kind string) ([]Snapshot, error) {+ if kind == "qemu" {+ return c.GetVMSnapshots(context.Background(), "member", 105)+ }+ return c.GetContainerSnapshots(context.Background(), "member", 105)+}++// Exercise both actual HTTP readers: invalid success must return no rows,+// never a partial/empty authoritative enumeration or response-body diagnostic.+func TestSnapshotInventoryResponseAdmission(t *testing.T) {+ const row = `{"name":"weekly","snaptime":1700000000,"description":"accepted"}`+ const inventory = `{"data":[` + row + `]}`+ cases := []struct {+ name, body string+ incomplete bool+ }{+ {"missing-data", `{}`, false},+ {"null-data", `{"data":null}`, false},+ {"null-envelope", `null`, false},+ {"non-array", `{"data":{}}`, false},+ {"error-as-success", `{"errors":{"message":"fixture-private-detail"}}`, false},+ {"conflicting-error", `{"data":[],"errors":{"message":"fixture-private-detail"}}`, false},+ {"case-data", `{"Data":[]}`, false},+ {"duplicate-data", `{"data":[` + row + `],"data":[]}`, false},+ {"escaped-duplicate-data", `{"data":[],"\u0064ata":[` + row + `]}`, false},+ {"case-overwrite", `{"data":[` + row + `],"Data":[]}`, false},+ {"case-error", `{"data":[],"Errors":null}`, false},+ {"trailing-value", inventory + ` {"data":[]}`, false},+ {"trailing-garbage", inventory + `fixture-private-detail`, false},+ {"truncated-after-prefix", inventory, true},+ {"oversized-after-prefix", inventory + strings.Repeat(" ", int(maxResponseBodyBytes)), false},+ {"invalid-utf8", `{"data":[` + row + `],"metadata":"` + string([]byte{0xff}) + `"}`, false},+ {"null-row", `{"data":[` + row + `,null]}`, false},+ {"empty-name", `{"data":[{"name":"","snaptime":1700000000}]}`, false},+ {"missing-name", `{"data":[{"snaptime":1700000000}]}`, false},+ {"null-name", `{"data":[{"name":null,"snaptime":1700000000}]}`, false},+ {"whitespace-name", `{"data":[{"name":" ","snaptime":1700000000}]}`, false},+ {"duplicate-name-field", `{"data":[{"name":"other","name":"weekly","snaptime":1700000000}]}`, false},+ {"case-name-field", `{"data":[{"name":"weekly","Name":"other","snaptime":1700000000}]}`, false},+ {"missing-time", `{"data":[{"name":"weekly"}]}`, false},+ {"null-time", `{"data":[{"name":"weekly","snaptime":null}]}`, false},+ {"zero-time", `{"data":[{"name":"weekly","snaptime":0}]}`, false},+ {"negative-time", `{"data":[{"name":"weekly","snaptime":-1}]}`, false},+ {"string-time", `{"data":[{"name":"weekly","snaptime":"1700000000"}]}`, false},+ {"case-time-field", `{"data":[{"name":"weekly","snaptime":1700000000,"SnapTime":0}]}`, false},+ {"duplicate-row", `{"data":[` + row + `,` + row + `]}`, false},+ {"bad-row-after-good", `{"data":[` + row + `,{"name":"second","snaptime":false}]}`, false},+ {"bad-marker-after-good", `{"data":[` + row + `,{"name":"current","description":42}]}`, false},+ }+ for _, kind := range []string{"qemu", "lxc"} {+ t.Run(kind, func(t *testing.T) {+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ var requests atomic.Int32+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ requests.Add(1)+ if r.URL.Path != "/api2/json/nodes/member/"+kind+"/105/snapshot" {+ t.Errorf("unexpected path: %s", r.URL.Path)+ }+ if tc.incomplete {+ w.Header().Set("Content-Length", fmt.Sprint(len(tc.body)+100))+ } else {+ w.(http.Flusher).Flush()+ }+ fmt.Fprint(w, tc.body)+ }))+ defer server.Close()+ snapshots, err := readTestSnapshotInventory(backupTestClient(t, server.URL), kind)+ if err == nil || snapshots != nil {+ t.Errorf("unverified inventory admitted: snapshots=%+v err=%v", snapshots, err)+ }+ if err != nil && strings.Contains(err.Error(), "fixture-private-detail") {+ t.Error("response body leaked into snapshot diagnostic")+ }+ if requests.Load() != 1 {+ t.Errorf("invalid success replayed: %d requests", requests.Load())+ }+ })+ }+ })+ }+}++func TestSnapshotInventoryAcceptedScopedAndExtendedResponses(t *testing.T) {+ cases := []struct {+ body string+ count int+ }{+ {`{"data":[]}`, 0},+ {`{"data":[],"errors":null,"total":0}`, 0},+ {`{"data":[{"name":"current","description":"You are here!","digest":"metadata"}]}`, 0},+ {`{"data":[{"name":"current"},{"name":"weekly","snaptime":1700000000,"description":"accepted","parent":"base","vmstate":1,"additional":{"version":1}}],"errors":null}`, 1},+ }+ for _, kind := range []string{"qemu", "lxc"} {+ for _, tc := range cases {+ t.Run(kind+"/"+tc.body, func(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, tc.body) }))+ defer server.Close()+ snapshots, err := readTestSnapshotInventory(backupTestClient(t, server.URL), kind)+ if err != nil || len(snapshots) != tc.count {+ t.Fatalf("accepted inventory refused: snapshots=%+v err=%v", snapshots, err)+ }+ if tc.count == 1 && (snapshots[0].Name != "weekly" || snapshots[0].SnapTime != 1700000000 || snapshots[0].Description != "accepted" || snapshots[0].Parent != "base" || snapshots[0].VMID != 105) {+ t.Fatalf("accepted snapshot fields changed: %+v", snapshots)+ }+ })+ }+ }+}++type snapshotCloseFailure struct {+ io.Reader+ closed bool+}++func (b *snapshotCloseFailure) Close() error {+ b.closed = true+ return io.ErrClosedPipe+}++func TestSnapshotInventoryRequiresCompletedResponse(t *testing.T) {+ for _, status := range []int{http.StatusNoContent, http.StatusPartialContent} {+ body := io.NopCloser(strings.NewReader(`{"data":[]}`))+ rows, err := readSnapshotInventory(&http.Response{StatusCode: status, Body: body}, 105)+ if rows != nil || err == nil {+ t.Errorf("status %d established absence: rows=%+v err=%v", status, rows, err)+ }+ }+ body := &snapshotCloseFailure{Reader: strings.NewReader(`{"data":[]}`)}+ rows, err := readSnapshotInventory(&http.Response{StatusCode: http.StatusOK, Body: body}, 105)+ if rows != nil || err == nil || !body.closed {+ t.Errorf("unfinished close established absence: rows=%+v err=%v closed=%t", rows, err, body.closed)+ }+}