services -> domains
This commit is contained in:
@@ -36,78 +36,12 @@ func TestTrigger_NonBlocking(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_ExitsOnContextCancel(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
rn.Run(ctx, Config{}) // not enabled — returns immediately
|
||||
close(done)
|
||||
}()
|
||||
|
||||
cancel()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("Run did not exit after context cancel")
|
||||
func staticParams(token string, records []string) ParamsFunc {
|
||||
return func() Params {
|
||||
return Params{APIToken: token, Records: records, IntervalMinutes: 5}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_DisabledConfig_ExitsImmediately(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
ctx := context.Background()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
rn.Run(ctx, Config{Enabled: false})
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("Run should exit immediately when disabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_NoToken_ExitsImmediately(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
ctx := context.Background()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
rn.Run(ctx, Config{Enabled: true, APIToken: "", Records: []string{"cloud.example.com"}})
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("Run should exit immediately when no API token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_NoRecords_ExitsImmediately(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
ctx := context.Background()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
rn.Run(ctx, Config{Enabled: true, APIToken: "token", Records: nil})
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("Run should exit immediately when no records configured")
|
||||
}
|
||||
}
|
||||
|
||||
// TestStart_SetsEnabledStatus verifies Enabled=true after Start, false after Stop.
|
||||
// Uses Start (not Run directly) since Start owns the Enabled lifecycle.
|
||||
func TestStart_SetsEnabledStatus(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
started := make(chan struct{}, 1)
|
||||
@@ -119,8 +53,7 @@ func TestStart_SetsEnabledStatus(t *testing.T) {
|
||||
return "1.2.3.4", nil
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
rn.Start(ctx, Config{Enabled: true, APIToken: "tok", Records: []string{"a.example.com"}})
|
||||
rn.Start(context.Background(), staticParams("tok", []string{"a.example.com"}))
|
||||
|
||||
select {
|
||||
case <-started:
|
||||
@@ -129,7 +62,7 @@ func TestStart_SetsEnabledStatus(t *testing.T) {
|
||||
}
|
||||
|
||||
if !rn.GetStatus().Enabled {
|
||||
t.Error("expected Enabled=true after Start with valid config")
|
||||
t.Error("expected Enabled=true after Start with valid params")
|
||||
}
|
||||
|
||||
rn.Stop()
|
||||
@@ -139,23 +72,20 @@ func TestStart_SetsEnabledStatus(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestStart_InvalidConfig_SetsDisabled verifies Enabled=false when Start is called with bad config.
|
||||
func TestStart_InvalidConfig_SetsDisabled(t *testing.T) {
|
||||
for _, cfg := range []Config{
|
||||
{Enabled: false},
|
||||
{Enabled: true, APIToken: ""},
|
||||
{Enabled: true, APIToken: "tok", Records: nil},
|
||||
func TestStart_InvalidParams_SetsDisabled(t *testing.T) {
|
||||
for _, pfn := range []ParamsFunc{
|
||||
func() Params { return Params{} },
|
||||
func() Params { return Params{APIToken: ""} },
|
||||
func() Params { return Params{APIToken: "tok", Records: nil} },
|
||||
} {
|
||||
rn := NewRunner()
|
||||
rn.Start(context.Background(), cfg)
|
||||
rn.Start(context.Background(), pfn)
|
||||
if rn.GetStatus().Enabled {
|
||||
t.Errorf("expected Enabled=false for config %+v", cfg)
|
||||
t.Errorf("expected Enabled=false for empty params")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestStart_Restart_KeepsEnabled verifies that calling Start twice does not
|
||||
// clear Enabled — the race where the old goroutine's exit sets Enabled=false.
|
||||
func TestStart_Restart_KeepsEnabled(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
reached := make(chan struct{}, 2)
|
||||
@@ -167,11 +97,9 @@ func TestStart_Restart_KeepsEnabled(t *testing.T) {
|
||||
return "1.2.3.4", nil
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
cfg := Config{Enabled: true, APIToken: "tok", Records: []string{"a.example.com"}}
|
||||
rn.Start(ctx, cfg)
|
||||
pfn := staticParams("tok", []string{"a.example.com"})
|
||||
rn.Start(context.Background(), pfn)
|
||||
|
||||
// Wait for first goroutine to actually start
|
||||
select {
|
||||
case <-reached:
|
||||
case <-time.After(time.Second):
|
||||
@@ -179,9 +107,8 @@ func TestStart_Restart_KeepsEnabled(t *testing.T) {
|
||||
}
|
||||
|
||||
// Restart — old goroutine is cancelled, new one starts
|
||||
rn.Start(ctx, cfg)
|
||||
rn.Start(context.Background(), pfn)
|
||||
|
||||
// Enabled must still be true regardless of old goroutine's exit timing
|
||||
if !rn.GetStatus().Enabled {
|
||||
t.Error("expected Enabled=true after restart")
|
||||
}
|
||||
@@ -189,8 +116,6 @@ func TestStart_Restart_KeepsEnabled(t *testing.T) {
|
||||
rn.Stop()
|
||||
}
|
||||
|
||||
|
||||
// TestStop_ClearsEnabled verifies Stop sets Enabled=false.
|
||||
func TestStop_ClearsEnabled(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
rn.mu.Lock()
|
||||
@@ -204,7 +129,6 @@ func TestStop_ClearsEnabled(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestStart_CancelsAndRestarts verifies calling Start twice cancels the previous goroutine.
|
||||
func TestStart_CancelsAndRestarts(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
|
||||
@@ -219,10 +143,8 @@ func TestStart_CancelsAndRestarts(t *testing.T) {
|
||||
return "1.2.3.4", nil
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
cfg := Config{Enabled: true, APIToken: "tok", Records: []string{"a.example.com"}}
|
||||
|
||||
rn.Start(ctx, cfg)
|
||||
pfn := staticParams("tok", []string{"a.example.com"})
|
||||
rn.Start(context.Background(), pfn)
|
||||
|
||||
select {
|
||||
case <-firstReady:
|
||||
@@ -230,13 +152,12 @@ func TestStart_CancelsAndRestarts(t *testing.T) {
|
||||
t.Fatal("first goroutine did not start")
|
||||
}
|
||||
|
||||
// Unblock the first goroutine so the second Start can cancel it
|
||||
close(block)
|
||||
|
||||
// Second Start with disabled config must not deadlock
|
||||
// Second Start with disabled params must not deadlock
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
rn.Start(ctx, Config{Enabled: false})
|
||||
rn.Start(context.Background(), func() Params { return Params{} })
|
||||
close(done)
|
||||
}()
|
||||
|
||||
@@ -247,59 +168,45 @@ func TestStart_CancelsAndRestarts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckAndUpdate_UpdatesCurrentIP verifies CurrentIP is set after IP fetch.
|
||||
// Note: this calls updateCloudflareRecord which fails with a fake token,
|
||||
// but CurrentIP is still updated to reflect the fetched IP.
|
||||
func TestCheckAndUpdate_UpdatesCurrentIP(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
rn.ipFetcher = func() (string, error) { return "5.6.7.8", nil }
|
||||
rn.paramsFn = staticParams("fake", []string{"a.example.com"})
|
||||
|
||||
rn.checkAndUpdate(Config{APIToken: "fake", Records: []string{"a.example.com"}})
|
||||
rn.checkAndUpdate()
|
||||
|
||||
if rn.GetStatus().CurrentIP != "5.6.7.8" {
|
||||
t.Errorf("expected CurrentIP=5.6.7.8, got %q", rn.GetStatus().CurrentIP)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStart_ClearsCurrentIP verifies that Start resets CurrentIP so new records are
|
||||
// always synced on the first check, even when the public IP hasn't changed.
|
||||
// This catches the bug where adding a new record (e.g. dev.payne.io) would be
|
||||
// skipped because currentIP already matched the fetched IP.
|
||||
func TestStart_ClearsCurrentIP(t *testing.T) {
|
||||
func TestCheckAndUpdate_SkipsWhenNoParams(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
rn.mu.Lock()
|
||||
rn.status.CurrentIP = "1.2.3.4" // simulates a previously running goroutine
|
||||
rn.mu.Unlock()
|
||||
rn.ipFetcher = func() (string, error) { return "1.2.3.4", nil }
|
||||
rn.paramsFn = func() Params { return Params{} }
|
||||
|
||||
rn.Start(context.Background(), Config{Enabled: true, APIToken: "tok", Records: []string{"a.example.com"}})
|
||||
rn.Stop()
|
||||
rn.checkAndUpdate()
|
||||
|
||||
if rn.GetStatus().CurrentIP != "" {
|
||||
t.Error("Start should clear CurrentIP so the first check syncs all records")
|
||||
t.Error("expected empty CurrentIP when params have no token/records")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckAndUpdate_SkipsWhenIPUnchanged verifies no Cloudflare call when IP matches.
|
||||
func TestCheckAndUpdate_SkipsWhenIPUnchanged(t *testing.T) {
|
||||
func TestCheckAndUpdate_ParamsFuncCalledEachTime(t *testing.T) {
|
||||
rn := NewRunner()
|
||||
rn.mu.Lock()
|
||||
rn.status.CurrentIP = "9.9.9.9"
|
||||
rn.mu.Unlock()
|
||||
rn.ipFetcher = func() (string, error) { return "1.2.3.4", nil }
|
||||
|
||||
calls := 0
|
||||
rn.ipFetcher = func() (string, error) {
|
||||
rn.paramsFn = func() Params {
|
||||
calls++
|
||||
return "9.9.9.9", nil
|
||||
return Params{APIToken: "fake", Records: []string{"a.example.com"}}
|
||||
}
|
||||
|
||||
prevUpdated := rn.GetStatus().LastUpdated
|
||||
rn.checkAndUpdate(Config{APIToken: "tok", Records: []string{"a.example.com"}})
|
||||
rn.checkAndUpdate()
|
||||
rn.checkAndUpdate()
|
||||
|
||||
if rn.GetStatus().LastUpdated != prevUpdated {
|
||||
t.Error("LastUpdated should not change when IP is unchanged")
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Errorf("expected 1 ipFetcher call, got %d", calls)
|
||||
if calls != 2 {
|
||||
t.Errorf("expected paramsFn called 2 times, got %d", calls)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user