sigs.k8s.io/external-dns@v0.14.1/source/source_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  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"sigs.k8s.io/external-dns/endpoint"
    26  )
    27  
    28  func TestGetTTLFromAnnotations(t *testing.T) {
    29  	for _, tc := range []struct {
    30  		title       string
    31  		annotations map[string]string
    32  		expectedTTL endpoint.TTL
    33  	}{
    34  		{
    35  			title:       "TTL annotation not present",
    36  			annotations: map[string]string{"foo": "bar"},
    37  			expectedTTL: endpoint.TTL(0),
    38  		},
    39  		{
    40  			title:       "TTL annotation value is not a number",
    41  			annotations: map[string]string{ttlAnnotationKey: "foo"},
    42  			expectedTTL: endpoint.TTL(0),
    43  		},
    44  		{
    45  			title:       "TTL annotation value is empty",
    46  			annotations: map[string]string{ttlAnnotationKey: ""},
    47  			expectedTTL: endpoint.TTL(0),
    48  		},
    49  		{
    50  			title:       "TTL annotation value is negative number",
    51  			annotations: map[string]string{ttlAnnotationKey: "-1"},
    52  			expectedTTL: endpoint.TTL(0),
    53  		},
    54  		{
    55  			title:       "TTL annotation value is too high",
    56  			annotations: map[string]string{ttlAnnotationKey: fmt.Sprintf("%d", 1<<32)},
    57  			expectedTTL: endpoint.TTL(0),
    58  		},
    59  		{
    60  			title:       "TTL annotation value is set correctly using integer",
    61  			annotations: map[string]string{ttlAnnotationKey: "60"},
    62  			expectedTTL: endpoint.TTL(60),
    63  		},
    64  		{
    65  			title:       "TTL annotation value is set correctly using duration (whole)",
    66  			annotations: map[string]string{ttlAnnotationKey: "10m"},
    67  			expectedTTL: endpoint.TTL(600),
    68  		},
    69  		{
    70  			title:       "TTL annotation value is set correctly using duration (fractional)",
    71  			annotations: map[string]string{ttlAnnotationKey: "20.5s"},
    72  			expectedTTL: endpoint.TTL(20),
    73  		},
    74  	} {
    75  		t.Run(tc.title, func(t *testing.T) {
    76  			ttl := getTTLFromAnnotations(tc.annotations, "resource/test")
    77  			assert.Equal(t, tc.expectedTTL, ttl)
    78  		})
    79  	}
    80  }
    81  
    82  func TestSuitableType(t *testing.T) {
    83  	for _, tc := range []struct {
    84  		target, recordType, expected string
    85  	}{
    86  		{"8.8.8.8", "", "A"},
    87  		{"2001:db8::1", "", "AAAA"},
    88  		{"foo.example.org", "", "CNAME"},
    89  		{"bar.eu-central-1.elb.amazonaws.com", "", "CNAME"},
    90  	} {
    91  
    92  		recordType := suitableType(tc.target)
    93  
    94  		if recordType != tc.expected {
    95  			t.Errorf("expected %s, got %s", tc.expected, recordType)
    96  		}
    97  	}
    98  }