Skip to content

armgit / Pulse

Public Imported from GitHub
0

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.

Isolate guest fixtures and safely observe pinned loser closure

pulse-triage[bot]d6e72532e3d6
diff --git a/pkg/proxmox/guest_agent_typed_response_test.go b/pkg/proxmox/guest_agent_typed_response_test.go
index 6b992a51d5..098a99d8c0 100644
--- a/pkg/proxmox/guest_agent_typed_response_test.go
+++ b/pkg/proxmox/guest_agent_typed_response_test.go
@@ -61,14 +61,18 @@ func TestGuestAgentTypedSuccessDefersEveryRead(t *testing.T) {
}
}))
defer server.Close()
- c := backupTestClient(t, server.URL)
+ // A recycled listener port must not inherit another synthetic
+ // guest's still-live uncertainty fence. Keep both clients in
+ // this case on the same namespaced endpoint.
+ host := guestAgentFixtureEndpoint(t, server.URL)
+ c := backupTestClient(t, host)
if err := first(context.Background(), c, 105); GuestAgentDeferredReason(err) != "agent-completion-unverified" {
t.Fatalf("typed reply released admission: %v", err)
}
if commands.Load() != 1 || configs.Load() != 1 {
t.Fatal("reply was treated as complete and received a postflight lock read")
}
- diagnostic := backupTestClient(t, server.URL)
+ diagnostic := backupTestClient(t, host)
for later, read := range backupAgentReads() {
if err := read(context.Background(), diagnostic, 105); GuestAgentDeferredReason(err) != "agent-cooldown" {
t.Errorf("next %s escaped uncertainty: %v", later, err)
@@ -87,6 +91,43 @@ func TestGuestAgentTypedSuccessDefersEveryRead(t *testing.T) {
}
}
+func TestGuestAgentTypedSuccessFixtureIsolation(t *testing.T) {
+ var commands, configs atomic.Int32
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if strings.HasSuffix(r.URL.Path, "/config") {
+ configs.Add(1)
+ fmt.Fprint(w, `{"data":{}}`)
+ return
+ }
+ commands.Add(1)
+ fmt.Fprint(w, invalidGuestAgentTypedReplies()["empty-text-result"])
+ }))
+ defer server.Close()
+ // Force listener reuse instead of waiting for the OS to recycle a port.
+ // Neither admitting the next fixture nor constructing a diagnostic client
+ // may erase, renew or bypass the first fixture's uncertainty.
+ var hosts []string
+ for _, name := range []string{"first", "second"} {
+ t.Run(name, func(t *testing.T) {
+ host := guestAgentFixtureEndpoint(t, server.URL)
+ hosts = append(hosts, host)
+ if err := backupAgentReads()["network"](context.Background(), backupTestClient(t, host), 105); GuestAgentDeferredReason(err) != "agent-completion-unverified" {
+ t.Fatalf("independent fixture did not admit its first typed reply: %v", err)
+ }
+ for _, previous := range hosts {
+ for reader, read := range backupAgentReads() {
+ if err := read(context.Background(), backupTestClient(t, previous), 105); GuestAgentDeferredReason(err) != "agent-cooldown" {
+ t.Errorf("retained fixture's %s lost its uncertainty fence: %v", reader, err)
+ }
+ }
+ }
+ })
+ }
+ if commands.Load() != 2 || configs.Load() != 2 {
+ t.Fatalf("independent fixture command/config counts=%d/%d, want 2/2", commands.Load(), configs.Load())
+ }
+}
+
func TestGuestAgentTypedSuccessSharesUnrenewedFenceAndResumes(t *testing.T) {
isolateGuestAgentCapacity(t)
var commands atomic.Int32
diff --git a/pkg/securityutil/pinned_dial_test.go b/pkg/securityutil/pinned_dial_test.go
index 1dfdc41bce..14a5157642 100644
--- a/pkg/securityutil/pinned_dial_test.go
+++ b/pkg/securityutil/pinned_dial_test.go
@@ -172,23 +172,67 @@ func TestDialPinnedIPsCancellationStopsBothWorkers(t *testing.T) {
}
}
+type pinnedCloseObservedConn struct {
+ net.Conn
+ closed chan struct{}
+}
+
+func (c *pinnedCloseObservedConn) Close() error {
+ err := c.Conn.Close()
+ close(c.closed)
+ return err
+}
+
func TestDialPinnedIPsClosesLateSuccessfulLoser(t *testing.T) {
- winner, _ := pinnedTestConnection(t)
- loser, peer := pinnedTestConnection(t)
- dial := func(ctx context.Context, network, address string) (net.Conn, error) {
- if address == "192.0.2.2:443" {
- return winner, nil
+ for _, alreadyClosed := range []bool{false, true} {
+ name := "read-immediately"
+ if alreadyClosed {
+ name = "already-closed"
}
- // Model a connect that completes just as its cancellation arrives.
- <-ctx.Done()
- return loser, nil
+ t.Run(name, func(t *testing.T) {
+ winner, winnerPeer := pinnedTestConnection(t)
+ loser, peer := pinnedTestConnection(t)
+ observed := &pinnedCloseObservedConn{Conn: loser, closed: make(chan struct{})}
+ // net.Pipe refuses deadline changes once either endpoint closes.
+ // Arm the bound before cancellation can correctly close the loser.
+ require.NoError(t, peer.SetReadDeadline(time.Now().Add(time.Second)))
+ started := make(chan struct{})
+ dial := func(ctx context.Context, network, address string) (net.Conn, error) {
+ if address == "192.0.2.2:443" {
+ <-started
+ return winner, nil
+ }
+ close(started)
+ // Model a connect that completes just as its cancellation arrives.
+ <-ctx.Done()
+ return observed, nil
+ }
+ connection, err := dialPinnedIPs(context.Background(), "tcp", "443", []net.IP{net.ParseIP("192.0.2.1"), net.ParseIP("192.0.2.2")}, time.Second, dial)
+ require.NoError(t, err)
+ require.Same(t, winner, connection)
+ if alreadyClosed {
+ // Make the formerly failing ordering deterministic; a closed
+ // loser is the desired result, not a failed observation setup.
+ select {
+ case <-observed.closed:
+ case <-time.After(time.Second):
+ t.Fatal("the unused successful connection did not close")
+ }
+ }
+ _, err = peer.Read(make([]byte, 1))
+ require.ErrorIs(t, err, io.EOF, "the unused successful connection must close")
+
+ // Cancellation owns only the unused connection, not the winner.
+ require.NoError(t, winner.SetDeadline(time.Now().Add(time.Second)))
+ written := make(chan error, 1)
+ go func() { _, err := winnerPeer.Write([]byte{42}); written <- err }()
+ buf := make([]byte, 1)
+ _, err = connection.Read(buf)
+ require.NoError(t, err)
+ require.Equal(t, byte(42), buf[0])
+ require.NoError(t, <-written)
+ })
}
- connection, err := dialPinnedIPs(context.Background(), "tcp", "443", []net.IP{net.ParseIP("192.0.2.1"), net.ParseIP("192.0.2.2")}, time.Second, dial)
- require.NoError(t, err)
- require.Same(t, winner, connection)
- require.NoError(t, peer.SetReadDeadline(time.Now().Add(time.Second)))
- _, err = peer.Read(make([]byte, 1))
- require.ErrorIs(t, err, io.EOF, "the unused successful connection must close")
}
func TestDialPinnedIPsValidatesAndNormalizesCandidates(t *testing.T) {