mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-10-22 19:56:01 +00:00
bd6f77e093
Tighten v5-to-v6 upgrade safety, release installability, provider MSP mode handling, AI cost accounting, metrics flushing, and frontend guardrails for the v6.0.0 GA candidate.
95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
package api
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func setupTempPulseBin(t *testing.T) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_BIN_DIR", dir)
|
|
return dir
|
|
}
|
|
|
|
func TestHandleDownloadUnifiedAgentSetsChecksumAndInvalidatesOnChange(t *testing.T) {
|
|
binDir := setupTempPulseBin(t)
|
|
filePath := filepath.Join(binDir, "pulse-agent-linux-amd64")
|
|
|
|
payload1 := validTestUnifiedAgentBinary("checksum-v1")
|
|
if err := os.WriteFile(filePath, payload1, 0o755); err != nil {
|
|
t.Fatalf("failed to write test binary: %v", err)
|
|
}
|
|
|
|
req1 := httptest.NewRequest(http.MethodGet, "/download/pulse-agent?arch=linux-amd64", nil)
|
|
rr1 := httptest.NewRecorder()
|
|
|
|
router := &Router{checksumCache: make(map[string]checksumCacheEntry)}
|
|
router.handleDownloadUnifiedAgent(rr1, req1)
|
|
|
|
if rr1.Code != http.StatusOK {
|
|
t.Fatalf("expected 200 OK, got %d", rr1.Code)
|
|
}
|
|
|
|
expected1 := fmt.Sprintf("%x", sha256.Sum256(payload1))
|
|
if got := rr1.Header().Get("X-Checksum-Sha256"); got != expected1 {
|
|
t.Fatalf("unexpected checksum header: got %q want %q", got, expected1)
|
|
}
|
|
|
|
// Ensure modtime changes for invalidation.
|
|
time.Sleep(10 * time.Millisecond)
|
|
payload2 := validTestUnifiedAgentBinary("checksum-v2")
|
|
if err := os.WriteFile(filePath, payload2, 0o755); err != nil {
|
|
t.Fatalf("failed to rewrite test binary: %v", err)
|
|
}
|
|
|
|
req2 := httptest.NewRequest(http.MethodGet, "/download/pulse-agent?arch=linux-amd64", nil)
|
|
rr2 := httptest.NewRecorder()
|
|
router.handleDownloadUnifiedAgent(rr2, req2)
|
|
|
|
expected2 := fmt.Sprintf("%x", sha256.Sum256(payload2))
|
|
if got := rr2.Header().Get("X-Checksum-Sha256"); got != expected2 {
|
|
t.Fatalf("checksum did not update after file change: got %q want %q", got, expected2)
|
|
}
|
|
|
|
if strings.TrimSpace(rr2.Body.String()) != string(payload2) {
|
|
t.Fatalf("unexpected response body after update")
|
|
}
|
|
}
|
|
|
|
func TestHandleDownloadUnifiedAgentAllowsVersionCacheKeyQuery(t *testing.T) {
|
|
binDir := setupTempPulseBin(t)
|
|
filePath := filepath.Join(binDir, "pulse-agent-linux-amd64")
|
|
|
|
payload := validTestUnifiedAgentBinary("agent-binary-v1")
|
|
if err := os.WriteFile(filePath, payload, 0o755); err != nil {
|
|
t.Fatalf("failed to write test binary: %v", err)
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/download/pulse-agent?arch=linux-amd64&serverVersion=v6.0.0-rc.6", nil)
|
|
rr := httptest.NewRecorder()
|
|
|
|
router := &Router{checksumCache: make(map[string]checksumCacheEntry)}
|
|
router.handleDownloadUnifiedAgent(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200 OK, got %d", rr.Code)
|
|
}
|
|
|
|
expected := fmt.Sprintf("%x", sha256.Sum256(payload))
|
|
if got := rr.Header().Get("X-Checksum-Sha256"); got != expected {
|
|
t.Fatalf("unexpected checksum header: got %q want %q", got, expected)
|
|
}
|
|
|
|
if strings.TrimSpace(rr.Body.String()) != string(payload) {
|
|
t.Fatalf("unexpected response body")
|
|
}
|
|
}
|