sigs.k8s.io/external-dns@v0.14.1/internal/testutils/mock_source.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testutils
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/mock"
    24  
    25  	"sigs.k8s.io/external-dns/endpoint"
    26  )
    27  
    28  // MockSource returns mock endpoints.
    29  type MockSource struct {
    30  	mock.Mock
    31  }
    32  
    33  // Endpoints returns the desired mock endpoints.
    34  func (m *MockSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) {
    35  	args := m.Called()
    36  
    37  	endpoints := args.Get(0)
    38  	if endpoints == nil {
    39  		return nil, args.Error(1)
    40  	}
    41  
    42  	return endpoints.([]*endpoint.Endpoint), args.Error(1)
    43  }
    44  
    45  // AddEventHandler adds an event handler that should be triggered if something in source changes
    46  func (m *MockSource) AddEventHandler(ctx context.Context, handler func()) {
    47  	go func() {
    48  		ticker := time.NewTicker(5 * time.Second)
    49  		defer ticker.Stop()
    50  
    51  		for {
    52  			select {
    53  			case <-ctx.Done():
    54  				return
    55  			case <-ticker.C:
    56  				handler()
    57  			}
    58  		}
    59  	}()
    60  }