Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: controller run() and successfully shutdown #3639

Merged
merged 2 commits into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,9 @@ func newMockProvider(endpoints []*endpoint.Endpoint, changes *plan.Changes) prov
return dnsProvider
}

// TestRunOnce tests that RunOnce correctly orchestrates the different components.
func TestRunOnce(t *testing.T) {
func getTestSource() *testutils.MockSource {
// Fake some desired endpoints coming from our source.
source := new(testutils.MockSource)
cfg := externaldns.NewConfig()
cfg.ManagedDNSRecordTypes = []string{endpoint.RecordTypeA, endpoint.RecordTypeAAAA, endpoint.RecordTypeCNAME}
source.On("Endpoints").Return([]*endpoint.Endpoint{
{
DNSName: "create-record",
Expand All @@ -160,8 +157,18 @@ func TestRunOnce(t *testing.T) {
},
}, nil)

return source
}

func getTestConfig() *externaldns.Config {
cfg := externaldns.NewConfig()
cfg.ManagedDNSRecordTypes = []string{endpoint.RecordTypeA, endpoint.RecordTypeAAAA, endpoint.RecordTypeCNAME}
return cfg
}

func getTestProvider() provider.Provider {
// Fake some existing records in our DNS provider and validate some desired changes.
provider := newMockProvider(
return newMockProvider(
[]*endpoint.Endpoint{
{
DNSName: "update-record",
Expand Down Expand Up @@ -203,6 +210,13 @@ func TestRunOnce(t *testing.T) {
},
},
)
}

// TestRunOnce tests that RunOnce correctly orchestrates the different components.
func TestRunOnce(t *testing.T) {
source := getTestSource()
cfg := getTestConfig()
provider := getTestProvider()

r, err := registry.NewNoopRegistry(provider)
require.NoError(t, err)
Expand All @@ -224,6 +238,40 @@ func TestRunOnce(t *testing.T) {
assert.Equal(t, math.Float64bits(1), valueFromMetric(verifiedAAAARecords))
}

// TestRun tests that Run correctly starts and stops
func TestRun(t *testing.T) {
source := getTestSource()
cfg := getTestConfig()
provider := getTestProvider()

r, err := registry.NewNoopRegistry(provider)
require.NoError(t, err)

// Run our controller once to trigger the validation.
ctrl := &Controller{
Source: source,
Registry: r,
Policy: &plan.SyncPolicy{},
ManagedRecordTypes: cfg.ManagedDNSRecordTypes,
}
ctrl.nextRunAt = time.Now().Add(-time.Millisecond)
ctx, cancel := context.WithCancel(context.Background())
stopped := make(chan struct{})
go func() {
ctrl.Run(ctx)
close(stopped)
}()
time.Sleep(1500 * time.Millisecond)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's often a source of flakyness to introduce time.sleep in a test.
Do you think it's possible to use a readiness-like approach ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you are right, it's not really possible to do so.
cancel() starts shutdown (triggers being non-ready) and we want to wait for an iteration of external-dns.
We could of course also create a hook inside external-dns control loop, but that's a bit too much for only testing IMO.

cancel() // start shutdown
<-stopped

// Validate that the mock source was called.
source.AssertExpectations(t)
// check the verified records
assert.Equal(t, math.Float64bits(1), valueFromMetric(verifiedARecords))
assert.Equal(t, math.Float64bits(1), valueFromMetric(verifiedAAAARecords))
}

func valueFromMetric(metric prometheus.Gauge) uint64 {
ref := reflect.ValueOf(metric)
return reflect.Indirect(ref).FieldByName("valBits").Uint()
Expand Down
Loading