@@ -131,12 +131,9 @@ func newMockProvider(endpoints []*endpoint.Endpoint, changes *plan.Changes) prov
131
131
return dnsProvider
132
132
}
133
133
134
- // TestRunOnce tests that RunOnce correctly orchestrates the different components.
135
- func TestRunOnce (t * testing.T ) {
134
+ func getTestSource () * testutils.MockSource {
136
135
// Fake some desired endpoints coming from our source.
137
136
source := new (testutils.MockSource )
138
- cfg := externaldns .NewConfig ()
139
- cfg .ManagedDNSRecordTypes = []string {endpoint .RecordTypeA , endpoint .RecordTypeAAAA , endpoint .RecordTypeCNAME }
140
137
source .On ("Endpoints" ).Return ([]* endpoint.Endpoint {
141
138
{
142
139
DNSName : "create-record" ,
@@ -160,8 +157,18 @@ func TestRunOnce(t *testing.T) {
160
157
},
161
158
}, nil )
162
159
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 {
163
170
// Fake some existing records in our DNS provider and validate some desired changes.
164
- provider := newMockProvider (
171
+ return newMockProvider (
165
172
[]* endpoint.Endpoint {
166
173
{
167
174
DNSName : "update-record" ,
@@ -203,6 +210,13 @@ func TestRunOnce(t *testing.T) {
203
210
},
204
211
},
205
212
)
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 ()
206
220
207
221
r , err := registry .NewNoopRegistry (provider )
208
222
require .NoError (t , err )
@@ -224,6 +238,40 @@ func TestRunOnce(t *testing.T) {
224
238
assert .Equal (t , math .Float64bits (1 ), valueFromMetric (verifiedAAAARecords ))
225
239
}
226
240
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
+
227
275
func valueFromMetric (metric prometheus.Gauge ) uint64 {
228
276
ref := reflect .ValueOf (metric )
229
277
return reflect .Indirect (ref ).FieldByName ("valBits" ).Uint ()
0 commit comments