fix: harden DynDNS recovery and publish v1.5.1

This commit is contained in:
admin committed 2026-10-10 02:26:47 +02:00
1 parent 3383b91e49
commit 81def77e3c
37 files changed
+2415 -250

No files matched your search

+101 -2
View File
@@ -3,11 +3,107 @@ package hetzner
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/root-kdw12/hetzner-api-dyndns-records-webinterface/internal/diagnostics"
)
func TestDNSWritesWaitForActionCompletion(t *testing.T) {
operations := map[string]func(*Client) error{
"create": func(c *Client) error {
return c.CreateRRSet(context.Background(), "token", "123", "home", "A", 60, nil)
},
"records": func(c *Client) error {
return c.SetRRSetRecords(context.Background(), "token", "123", "home", "A", nil)
},
"ttl": func(c *Client) error { return c.ChangeRRSetTTL(context.Background(), "token", "123", "home", "A", 60) },
"comments": func(c *Client) error {
return c.UpdateRecordComments(context.Background(), "token", "123", "home", "A", nil)
},
"delete": func(c *Client) error { return c.DeleteRRSet(context.Background(), "token", "123", "home", "A") },
}
for name, operation := range operations {
t.Run(name, func(t *testing.T) {
for _, status := range []string{"success", "error"} {
t.Run(status, func(t *testing.T) {
var writes, polls int
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writes++
_, _ = w.Write([]byte(`{"action":{"id":42,"status":"running"}}`))
return
}
if r.URL.Path != "/zones/actions/42" || r.Header.Get("Authorization") != "Bearer token" {
t.Errorf("incorrect action polling request: %s", r.URL.Path)
}
polls++
if polls == 1 {
_, _ = w.Write([]byte(`{"action":{"id":42,"status":"running"}}`))
} else {
_, _ = w.Write([]byte(`{"action":{"id":42,"status":"` + status + `","error":{"code":"action_failed","message":"failed later"}}}`))
}
}))
defer api.Close()
client := New(api.URL)
client.actionPollInterval = time.Millisecond
err := operation(client)
if status == "success" && err != nil {
t.Fatal(err)
}
var actionErr *ActionError
if status == "error" && (!errors.As(err, &actionErr) || actionErr.Code != "action_failed") {
t.Fatalf("lost action failure: %v", err)
}
if status == "error" && (diagnostics.Failure(err) != "provider_action" || diagnostics.ActionID(err) != 42) {
t.Fatal("action failure missing from safe diagnostics")
}
if writes != 1 || polls != 2 {
t.Fatalf("returned too early or repeated write: writes=%d polls=%d", writes, polls)
}
})
}
})
}
}
func TestActionWaitIsBoundedAndRejectsMalformedResponses(t *testing.T) {
for _, body := range []string{"", `{}`, `{"action":{"id":42,"status":"unknown"}}`, `{"action":{"status":"running"}}`, `{"action":{"id":42,"status":"running"}}`} {
t.Run(body, func(t *testing.T) {
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(body)) }))
defer api.Close()
client := New(api.URL)
client.actionPollInterval = time.Millisecond
client.actionTimeout = 30 * time.Millisecond
if err := client.DeleteRRSet(context.Background(), "token", "123", "home", "A"); err == nil {
t.Fatal("unconfirmed operation reported success")
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := client.DeleteRRSet(ctx, "token", "123", "home", "A"); !errors.Is(err, context.Canceled) {
t.Fatalf("cancellation lost: %v", err)
}
})
}
}
func TestPaginationRejectsRepeatedPage(t *testing.T) {
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"zones":[],"rrsets":[],"meta":{"pagination":{"next_page":1}}}`))
}))
defer api.Close()
client := New(api.URL)
if _, err := client.ListZones(context.Background(), "token"); err == nil {
t.Fatal("repeated zones page accepted")
}
if _, err := client.ListRRSets(context.Background(), "token", "123"); err == nil {
t.Fatal("repeated RRsets page accepted")
}
}
func TestSetRRSetRecordsUsesSetRecordsAction(t *testing.T) {
t.Parallel()
type capturedRequest struct {
@@ -33,7 +129,7 @@ func TestSetRRSetRecordsUsesSetRecordsAction(t *testing.T) {
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(`{"action":{"status":"running"}}`))
_, _ = w.Write([]byte(`{"action":{"id":42,"status":"success"}}`))
}))
defer api.Close()
@@ -77,6 +173,7 @@ func TestUpdateRecordCommentsAlwaysSendsComment(t *testing.T) {
body["path"] = r.URL.Path
captured <- body
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(`{"action":{"id":42,"status":"success"}}`))
}))
defer api.Close()
@@ -115,6 +212,7 @@ func TestChangeRRSetTTLUsesDedicatedAction(t *testing.T) {
body["path"] = r.URL.Path
captured <- body
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(`{"action":{"id":42,"status":"success"}}`))
}))
defer api.Close()
@@ -133,7 +231,8 @@ func TestDeleteRRSetUsesRRSetEndpoint(t *testing.T) {
captured := make(chan *http.Request, 1)
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
captured <- r
w.WriteHeader(http.StatusNoContent)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"action":{"id":42,"status":"success"}}`))
}))
defer api.Close()