sigs.k8s.io/external-dns@v0.14.1/source/shared_test.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 source
    18  
    19  import (
    20  	"reflect"
    21  	"sort"
    22  	"testing"
    23  
    24  	"sigs.k8s.io/external-dns/endpoint"
    25  )
    26  
    27  func sortEndpoints(endpoints []*endpoint.Endpoint) {
    28  	for _, ep := range endpoints {
    29  		sort.Strings([]string(ep.Targets))
    30  	}
    31  	sort.Slice(endpoints, func(i, k int) bool {
    32  		// Sort by DNSName, RecordType, and Targets
    33  		ei, ek := endpoints[i], endpoints[k]
    34  		if ei.DNSName != ek.DNSName {
    35  			return ei.DNSName < ek.DNSName
    36  		}
    37  		if ei.RecordType != ek.RecordType {
    38  			return ei.RecordType < ek.RecordType
    39  		}
    40  		// Targets are sorted ahead of time.
    41  		for j, ti := range ei.Targets {
    42  			if j >= len(ek.Targets) {
    43  				return true
    44  			}
    45  			if tk := ek.Targets[j]; ti != tk {
    46  				return ti < tk
    47  			}
    48  		}
    49  		return false
    50  	})
    51  }
    52  
    53  func validateEndpoints(t *testing.T, endpoints, expected []*endpoint.Endpoint) {
    54  	t.Helper()
    55  
    56  	if len(endpoints) != len(expected) {
    57  		t.Fatalf("expected %d endpoints, got %d", len(expected), len(endpoints))
    58  	}
    59  
    60  	// Make sure endpoints are sorted - validateEndpoint() depends on it.
    61  	sortEndpoints(endpoints)
    62  	sortEndpoints(expected)
    63  
    64  	for i := range endpoints {
    65  		validateEndpoint(t, endpoints[i], expected[i])
    66  	}
    67  }
    68  
    69  func validateEndpoint(t *testing.T, endpoint, expected *endpoint.Endpoint) {
    70  	t.Helper()
    71  
    72  	if endpoint.DNSName != expected.DNSName {
    73  		t.Errorf("DNSName expected %q, got %q", expected.DNSName, endpoint.DNSName)
    74  	}
    75  
    76  	if !endpoint.Targets.Same(expected.Targets) {
    77  		t.Errorf("Targets expected %q, got %q", expected.Targets, endpoint.Targets)
    78  	}
    79  
    80  	if endpoint.RecordTTL != expected.RecordTTL {
    81  		t.Errorf("RecordTTL expected %v, got %v", expected.RecordTTL, endpoint.RecordTTL)
    82  	}
    83  
    84  	// if non-empty record type is expected, check that it matches.
    85  	if endpoint.RecordType != expected.RecordType {
    86  		t.Errorf("RecordType expected %q, got %q", expected.RecordType, endpoint.RecordType)
    87  	}
    88  
    89  	// if non-empty labels are expected, check that they matches.
    90  	if expected.Labels != nil && !reflect.DeepEqual(endpoint.Labels, expected.Labels) {
    91  		t.Errorf("Labels expected %s, got %s", expected.Labels, endpoint.Labels)
    92  	}
    93  
    94  	if (len(expected.ProviderSpecific) != 0 || len(endpoint.ProviderSpecific) != 0) &&
    95  		!reflect.DeepEqual(endpoint.ProviderSpecific, expected.ProviderSpecific) {
    96  		t.Errorf("ProviderSpecific expected %s, got %s", expected.ProviderSpecific, endpoint.ProviderSpecific)
    97  	}
    98  
    99  	if endpoint.SetIdentifier != expected.SetIdentifier {
   100  		t.Errorf("SetIdentifier expected %q, got %q", expected.SetIdentifier, endpoint.SetIdentifier)
   101  	}
   102  }