sigs.k8s.io/external-dns@v0.14.1/endpoint/endpoint_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 endpoint
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestNewEndpoint(t *testing.T) {
    25  	e := NewEndpoint("example.org", "CNAME", "foo.com")
    26  	if e.DNSName != "example.org" || e.Targets[0] != "foo.com" || e.RecordType != "CNAME" {
    27  		t.Error("endpoint is not initialized correctly")
    28  	}
    29  	if e.Labels == nil {
    30  		t.Error("Labels is not initialized")
    31  	}
    32  
    33  	w := NewEndpoint("example.org.", "", "load-balancer.com.")
    34  	if w.DNSName != "example.org" || w.Targets[0] != "load-balancer.com" || w.RecordType != "" {
    35  		t.Error("endpoint is not initialized correctly")
    36  	}
    37  }
    38  
    39  func TestTargetsSame(t *testing.T) {
    40  	tests := []Targets{
    41  		{""},
    42  		{"1.2.3.4"},
    43  		{"8.8.8.8", "8.8.4.4"},
    44  		{"example.org", "EXAMPLE.ORG"},
    45  	}
    46  
    47  	for _, d := range tests {
    48  		if d.Same(d) != true {
    49  			t.Errorf("%#v should equal %#v", d, d)
    50  		}
    51  	}
    52  }
    53  
    54  func TestSameFailures(t *testing.T) {
    55  	tests := []struct {
    56  		a Targets
    57  		b Targets
    58  	}{
    59  		{
    60  			[]string{"1.2.3.4"},
    61  			[]string{"4.3.2.1"},
    62  		}, {
    63  			[]string{"1.2.3.4"},
    64  			[]string{"1.2.3.4", "4.3.2.1"},
    65  		}, {
    66  			[]string{"1.2.3.4", "4.3.2.1"},
    67  			[]string{"1.2.3.4"},
    68  		}, {
    69  			[]string{"1.2.3.4", "4.3.2.1"},
    70  			[]string{"8.8.8.8", "8.8.4.4"},
    71  		},
    72  	}
    73  
    74  	for _, d := range tests {
    75  		if d.a.Same(d.b) == true {
    76  			t.Errorf("%#v should not equal %#v", d.a, d.b)
    77  		}
    78  	}
    79  }
    80  
    81  func TestIsLess(t *testing.T) {
    82  	testsA := []Targets{
    83  		{""},
    84  		{"1.2.3.4"},
    85  		{"1.2.3.4"},
    86  		{"example.org", "example.com"},
    87  		{"8.8.8.8", "8.8.4.4"},
    88  		{"1-2-3-4.example.org", "EXAMPLE.ORG"},
    89  		{"1-2-3-4.example.org", "EXAMPLE.ORG", "1.2.3.4"},
    90  		{"example.com", "example.org"},
    91  	}
    92  	testsB := []Targets{
    93  		{"", ""},
    94  		{"1-2-3-4.example.org"},
    95  		{"1.2.3.5"},
    96  		{"example.com", "examplea.org"},
    97  		{"8.8.8.8"},
    98  		{"1.2.3.4", "EXAMPLE.ORG"},
    99  		{"1-2-3-4.example.org", "EXAMPLE.ORG"},
   100  		{"example.com", "example.org"},
   101  	}
   102  	expected := []bool{
   103  		true,
   104  		true,
   105  		true,
   106  		true,
   107  		false,
   108  		false,
   109  		false,
   110  		false,
   111  	}
   112  
   113  	for i, d := range testsA {
   114  		if d.IsLess(testsB[i]) != expected[i] {
   115  			t.Errorf("%v < %v is expected to be %v", d, testsB[i], expected[i])
   116  		}
   117  	}
   118  }
   119  
   120  func TestFilterEndpointsByOwnerID(t *testing.T) {
   121  	foo1 := &Endpoint{
   122  		DNSName:    "foo.com",
   123  		RecordType: RecordTypeA,
   124  		Labels: Labels{
   125  			OwnerLabelKey: "foo",
   126  		},
   127  	}
   128  	foo2 := &Endpoint{
   129  		DNSName:    "foo.com",
   130  		RecordType: RecordTypeCNAME,
   131  		Labels: Labels{
   132  			OwnerLabelKey: "foo",
   133  		},
   134  	}
   135  	bar := &Endpoint{
   136  		DNSName:    "foo.com",
   137  		RecordType: RecordTypeA,
   138  		Labels: Labels{
   139  			OwnerLabelKey: "bar",
   140  		},
   141  	}
   142  	type args struct {
   143  		ownerID string
   144  		eps     []*Endpoint
   145  	}
   146  	tests := []struct {
   147  		name string
   148  		args args
   149  		want []*Endpoint
   150  	}{
   151  		{
   152  			name: "filter values",
   153  			args: args{
   154  				ownerID: "foo",
   155  				eps: []*Endpoint{
   156  					foo1,
   157  					foo2,
   158  					bar,
   159  				},
   160  			},
   161  			want: []*Endpoint{
   162  				foo1,
   163  				foo2,
   164  			},
   165  		},
   166  	}
   167  	for _, tt := range tests {
   168  		t.Run(tt.name, func(t *testing.T) {
   169  			if got := FilterEndpointsByOwnerID(tt.args.ownerID, tt.args.eps); !reflect.DeepEqual(got, tt.want) {
   170  				t.Errorf("ApplyEndpointFilter() = %v, want %v", got, tt.want)
   171  			}
   172  		})
   173  	}
   174  }
   175  
   176  func TestIsOwnedBy(t *testing.T) {
   177  	type fields struct {
   178  		Labels Labels
   179  	}
   180  	type args struct {
   181  		ownerID string
   182  	}
   183  	tests := []struct {
   184  		name   string
   185  		fields fields
   186  		args   args
   187  		want   bool
   188  	}{
   189  		{
   190  			name:   "empty labels",
   191  			fields: fields{Labels: Labels{}},
   192  			args:   args{ownerID: "foo"},
   193  			want:   false,
   194  		},
   195  		{
   196  			name:   "owner label not match",
   197  			fields: fields{Labels: Labels{OwnerLabelKey: "bar"}},
   198  			args:   args{ownerID: "foo"},
   199  			want:   false,
   200  		},
   201  		{
   202  			name:   "owner label match",
   203  			fields: fields{Labels: Labels{OwnerLabelKey: "foo"}},
   204  			args:   args{ownerID: "foo"},
   205  			want:   true,
   206  		},
   207  	}
   208  	for _, tt := range tests {
   209  		t.Run(tt.name, func(t *testing.T) {
   210  			e := &Endpoint{
   211  				Labels: tt.fields.Labels,
   212  			}
   213  			if got := e.IsOwnedBy(tt.args.ownerID); got != tt.want {
   214  				t.Errorf("Endpoint.IsOwnedBy() = %v, want %v", got, tt.want)
   215  			}
   216  		})
   217  	}
   218  }