Files
coredns/plugin/clouddns/setup_test.go
Aradhya Jain 538c3554bc plugin/clouddns: add refresh directive (#8616)
Brings clouddns to parity with route53, which already exposes a
refresh directive to control how often zones are re-synced. clouddns
currently hardcodes this to 1 minute (delay := 1 * time.Minute in Run)
with no way to change it.

Adds a refresh field to the CloudDNS struct, threads it through New(),
and has Run() use h.refresh instead of the hardcoded delay. Parses the
refresh directive in setup() mirroring route53's exact validation
(accepts a plain number as seconds or a duration string, must be
greater than 0).

Extends TestSetupCloudDNS with refresh coverage (valid duration, valid
plain number, missing arg, non-numeric, zero, negative). Verified it
fails without the setup.go change (unknown property error) and passes
with it.

Signed-off-by: stackedbyaradhya <aradhyaj736@gmail.com>
2026-10-08 18:47:32 -07:00

103 lines
3.0 KiB
Go

package clouddns
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/coredns/caddy"
"google.golang.org/api/option"
)
func TestSetupCloudDNS(t *testing.T) {
f = func(_ctx context.Context, _opt option.ClientOption) (gcpDNS, error) {
return fakeGCPClient{}, nil
}
validCreds := filepath.Join(t.TempDir(), "valid_creds.json")
if err := os.WriteFile(validCreds, []byte(`{"type": "service_account"}`), 0644); err != nil {
t.Fatalf("Failed to create valid creds: %v", err)
}
invalidTypeCreds := filepath.Join(t.TempDir(), "invalid_type_creds.json")
if err := os.WriteFile(invalidTypeCreds, []byte(`{"type": "bad_type"}`), 0644); err != nil {
t.Fatalf("Failed to create invalid creds: %v", err)
}
emptyCreds := filepath.Join(t.TempDir(), "empty_creds.json")
if err := os.WriteFile(emptyCreds, []byte(`{}`), 0644); err != nil {
t.Fatalf("Failed to create empty creds: %v", err)
}
invalidJSONCreds := filepath.Join(t.TempDir(), "invalid_json_creds.json")
if err := os.WriteFile(invalidJSONCreds, []byte(`{`), 0644); err != nil {
t.Fatalf("Failed to create invalid JSON creds: %v", err)
}
tests := []struct {
body string
expectedError bool
}{
{`clouddns`, false},
{`clouddns :`, true},
{`clouddns ::`, true},
{`clouddns example.org.:example-project:zone-name`, false},
{`clouddns example.org.:example-project:zone-name { }`, false},
{`clouddns example.org.:example-project: { }`, true},
{`clouddns example.org.:example-project:zone-name { }`, false},
{`clouddns example.org.:example-project:zone-name { wat
}`, true},
{`clouddns example.org.:example-project:zone-name {
fallthrough
}`, false},
{`clouddns example.org.:example-project:zone-name {
credentials
}`, true},
{`clouddns example.org.:example-project:zone-name {
refresh 3m
}`, false},
{`clouddns example.org.:example-project:zone-name {
refresh 30
}`, false},
{`clouddns example.org.:example-project:zone-name {
refresh
}`, true},
{`clouddns example.org.:example-project:zone-name {
refresh abc
}`, true},
{`clouddns example.org.:example-project:zone-name {
refresh 0
}`, true},
{`clouddns example.org.:example-project:zone-name {
refresh -5m
}`, true},
{`clouddns example.org.:example-project:zone-name example.org.:example-project:zone-name {
}`, true},
{`clouddns example.org {
}`, true},
{fmt.Sprintf(`clouddns example.org.:example-project:zone-name {
credentials %s
}`, validCreds), false},
{fmt.Sprintf(`clouddns example.org.:example-project:zone-name {
credentials %s
}`, invalidTypeCreds), true},
{fmt.Sprintf(`clouddns example.org.:example-project:zone-name {
credentials %s
}`, emptyCreds), true},
{fmt.Sprintf(`clouddns example.org.:example-project:zone-name {
credentials %s
}`, invalidJSONCreds), true},
}
for _, test := range tests {
c := caddy.NewTestController("dns", test.body)
if err := setup(c); (err == nil) == test.expectedError {
t.Errorf("Unexpected errors: %v", err)
}
}
}