Skip to content

Commit fa17f9d

Browse files
authored
Merge pull request #3639 from kubernetes-sigs/test/controller-run
test: controller run() and successfully shutdown
2 parents b416018 + 2b50b14 commit fa17f9d

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

controller/controller_test.go

+53-5
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,9 @@ func newMockProvider(endpoints []*endpoint.Endpoint, changes *plan.Changes) prov
131131
return dnsProvider
132132
}
133133

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

160+
return source
161+
}
162+
163+
func getTestConfig() *externaldns.Config {
164+
cfg := externaldns.NewConfig()
165+
cfg.ManagedDNSRecordTypes = []string{endpoint.RecordTypeA, endpoint.RecordTypeAAAA, endpoint.RecordTypeCNAME}
166+
return cfg
167+
}
168+
169+
func getTestProvider() provider.Provider {
163170
// Fake some existing records in our DNS provider and validate some desired changes.
164-
provider := newMockProvider(
171+
return newMockProvider(
165172
[]*endpoint.Endpoint{
166173
{
167174
DNSName: "update-record",
@@ -203,6 +210,13 @@ func TestRunOnce(t *testing.T) {
203210
},
204211
},
205212
)
213+
}
214+
215+
// TestRunOnce tests that RunOnce correctly orchestrates the different components.
216+
func TestRunOnce(t *testing.T) {
217+
source := getTestSource()
218+
cfg := getTestConfig()
219+
provider := getTestProvider()
206220

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

241+
// TestRun tests that Run correctly starts and stops
242+
func TestRun(t *testing.T) {
243+
source := getTestSource()
244+
cfg := getTestConfig()
245+
provider := getTestProvider()
246+
247+
r, err := registry.NewNoopRegistry(provider)
248+
require.NoError(t, err)
249+
250+
// Run our controller once to trigger the validation.
251+
ctrl := &Controller{
252+
Source: source,
253+
Registry: r,
254+
Policy: &plan.SyncPolicy{},
255+
ManagedRecordTypes: cfg.ManagedDNSRecordTypes,
256+
}
257+
ctrl.nextRunAt = time.Now().Add(-time.Millisecond)
258+
ctx, cancel := context.WithCancel(context.Background())
259+
stopped := make(chan struct{})
260+
go func() {
261+
ctrl.Run(ctx)
262+
close(stopped)
263+
}()
264+
time.Sleep(1500 * time.Millisecond)
265+
cancel() // start shutdown
266+
<-stopped
267+
268+
// Validate that the mock source was called.
269+
source.AssertExpectations(t)
270+
// check the verified records
271+
assert.Equal(t, math.Float64bits(1), valueFromMetric(verifiedARecords))
272+
assert.Equal(t, math.Float64bits(1), valueFromMetric(verifiedAAAARecords))
273+
}
274+
227275
func valueFromMetric(metric prometheus.Gauge) uint64 {
228276
ref := reflect.ValueOf(metric)
229277
return reflect.Indirect(ref).FieldByName("valBits").Uint()

0 commit comments

Comments
 (0)