Files
Pulse/cmd/pulse-sensor-proxy/cleanup_test.go
rcourtman 9e339957c6 fix: Update runtime config when toggling Docker update actions setting
The DisableDockerUpdateActions setting was being saved to disk but not
updated in h.config, causing the UI toggle to appear to revert on page
refresh since the API returned the stale runtime value.

Related to #1023
2026-01-03 11:14:17 +00:00

172 lines
4.2 KiB
Go

package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/rs/zerolog"
)
func TestProxy_cleanupRequestPath_UsesConfiguredWorkDir(t *testing.T) {
p := &Proxy{workDir: "/tmp/pulse-sensor-proxy-test"}
got, err := p.cleanupRequestPath()
if err != nil {
t.Fatalf("cleanupRequestPath: %v", err)
}
want := filepath.Join(p.workDir, cleanupRequestFilename)
if got != want {
t.Fatalf("path = %q, want %q", got, want)
}
}
func TestProxy_handleRequestCleanup_WritesValidPayloadAndReplacesExisting(t *testing.T) {
workDir := t.TempDir()
p := &Proxy{workDir: workDir}
logger := zerolog.Nop()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
resp, err := p.handleRequestCleanup(ctx, &RPCRequest{
Method: RPCRequestCleanup,
Params: map[string]interface{}{
"host": "pve-1",
"reason": "testing",
},
}, logger)
if err != nil {
t.Fatalf("handleRequestCleanup: %v", err)
}
respMap, ok := resp.(map[string]any)
if !ok {
t.Fatalf("expected map response, got %#v", resp)
}
queued, ok := respMap["queued"].(bool)
if !ok || !queued {
t.Fatalf("expected queued=true response, got %#v", resp)
}
path, err := p.cleanupRequestPath()
if err != nil {
t.Fatalf("cleanupRequestPath: %v", err)
}
readPayload := func() map[string]any {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile(%s): %v", path, err)
}
var payload map[string]any
if err := json.Unmarshal(data, &payload); err != nil {
t.Fatalf("unmarshal payload: %v", err)
}
return payload
}
payload := readPayload()
if payload["host"] != "pve-1" {
t.Fatalf("payload host = %#v, want %q", payload["host"], "pve-1")
}
if payload["reason"] != "testing" {
t.Fatalf("payload reason = %#v, want %q", payload["reason"], "testing")
}
if _, ok := payload["requestedAt"].(string); !ok {
t.Fatalf("payload requestedAt missing or not string: %#v", payload["requestedAt"])
}
if ts, ok := payload["requestedAt"].(string); ok {
if _, err := time.Parse(time.RFC3339, ts); err != nil {
t.Fatalf("requestedAt not RFC3339: %q: %v", ts, err)
}
}
if runtime.GOOS != "windows" {
fi, err := os.Stat(path)
if err != nil {
t.Fatalf("Stat(%s): %v", path, err)
}
if fi.Mode().Perm() != 0o600 {
t.Fatalf("payload file mode = %v, want %v", fi.Mode().Perm(), os.FileMode(0o600))
}
}
resp, err = p.handleRequestCleanup(ctx, &RPCRequest{
Method: RPCRequestCleanup,
Params: map[string]interface{}{
"host": "pve-1",
"reason": "testing-2",
},
}, logger)
if err != nil {
t.Fatalf("handleRequestCleanup (2): %v", err)
}
respMap, ok = resp.(map[string]any)
if !ok {
t.Fatalf("expected map response (2), got %#v", resp)
}
queued, ok = respMap["queued"].(bool)
if !ok || !queued {
t.Fatalf("expected queued=true response (2), got %#v", resp)
}
payload2 := readPayload()
if payload2["reason"] != "testing-2" {
t.Fatalf("payload reason after replace = %#v, want %q", payload2["reason"], "testing-2")
}
}
func TestProxy_handleRequestCleanup_NilRequest(t *testing.T) {
p := &Proxy{workDir: t.TempDir()}
_, err := p.handleRequestCleanup(context.Background(), nil, zerolog.Nop())
if err != nil {
t.Fatalf("handleRequestCleanup: %v", err)
}
}
func TestProxy_handleRequestCleanup_MkdirFailure(t *testing.T) {
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "file")
if err := os.WriteFile(filePath, []byte("data"), 0644); err != nil {
t.Fatal(err)
}
p := &Proxy{workDir: filepath.Join(filePath, "subdir")}
_, err := p.handleRequestCleanup(context.Background(), nil, zerolog.Nop())
if err == nil {
t.Error("expected error due to MkdirAll failure")
}
}
func TestProxy_handleRequestCleanup_ParamsEdgeCases(t *testing.T) {
workDir := t.TempDir()
p := &Proxy{workDir: workDir}
logger := zerolog.Nop()
// Empty host/reason params
_, err := p.handleRequestCleanup(context.Background(), &RPCRequest{
Params: map[string]interface{}{
"host": "",
"reason": "",
},
}, logger)
if err != nil {
t.Fatal(err)
}
// Non-string params
_, err = p.handleRequestCleanup(context.Background(), &RPCRequest{
Params: map[string]interface{}{
"host": 123,
"reason": true,
},
}, logger)
if err != nil {
t.Fatal(err)
}
}